1
\$\begingroup\$

The Pynt class defined the interface between the NisseServer object and code that handles input on a stream. This proivdes an implementation of Pynt for the HTTP protocol.

This proves that handling of the incoming HTTP message and the outgoing HTTP response. It then provides the virtual void processRequest(Request const& request, Response& response) to be implemented by the user that will handle the parsing of the HTTP body incoming request and the generating the HTTP body for the outgoing response.

PyntHTTP.h

#ifndef THORSANVIL_NISSE_NISSEHTTP_PYNTHTTP_H
#define THORSANVIL_NISSE_NISSEHTTP_PYNTHTTP_H
/*
 * An HTTP implementation of Pynt
 * Version 1:
 */
#include "NisseHTTPConfig.h"
#include "NisseServer/Pynt.h"
#include <ThorsSocket/SocketStream.h>
#include <string_view>
namespace TAS = ThorsAnvil::ThorsSocket;
namespace ThorsAnvil::Nisse::NisseHTTP
{
class Request;
class Response;
class PyntHTTP: public Pynt
{
 public:
 virtual PyntResult handleRequest(TAS::SocketStream& stream);
 virtual void processRequest(Request const& request, Response& response) = 0;
};
}
#endif

PyntHTTP.cpp

#include "PyntHTTP.h"
#include "Response.h"
#include "Request.h"
using namespace ThorsAnvil::Nisse::NisseHTTP;
ThorsAnvil::Nisse::PyntResult PyntHTTP::handleRequest(TAS::SocketStream& stream)
{
 Request request(stream.getSocket().protocol(), stream);
 if (!request.isValidRequest())
 {
 Response clientError(stream, 400);
 clientError.addHeaders(request.failHeader(), 0);
 return PyntResult::More;
 }
 Response response(stream);
 this->processRequest(request, response);
 return PyntResult::More;
}

The Hello World implementation.

HTTPHelloWorld.h

#ifndef THORSANVIL_NISSE_SIMPLE_SERVER_HTTP_HELLO_WORLD_H
#define THORSANVIL_NISSE_SIMPLE_SERVER_HTTP_HELLO_WORLD_H
#include "NisseHTTP/PyntHTTP.h"
class HTTPHelloWorld: public ThorsAnvil::Nisse::NisseHTTP::PyntHTTP
{
 public:
 virtual void processRequest(ThorsAnvil::Nisse::NisseHTTP::Request const& request, ThorsAnvil::Nisse::NisseHTTP::Response& response) override;
};
#endif

HTPPHelloWorld.cpp

#include "HTTPHelloWorld.h"
#include "NisseHTTP/Response.h"
#include <string_view>
void HTTPHelloWorld::processRequest(ThorsAnvil::Nisse::NisseHTTP::Request const& request, ThorsAnvil::Nisse::NisseHTTP::Response& response)
{
 std::string_view page = R"(
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>Nisse server 1.1</title></head>
<body>Hello world</body>
</html>
)";
 ThorsAnvil::Nisse::NisseHTTP::HeaderResponse header;
 response.addHeaders(header, page.size()) << page;
}
asked Oct 20, 2024 at 21:13
\$\endgroup\$
2
  • \$\begingroup\$ When I saw your 8 very similar question titles, I thought I should close them as duplicates of each other. Luckily, I had a look at your reputation. \$\endgroup\$ Commented Oct 20, 2024 at 22:18
  • \$\begingroup\$ For the include guard I meanwhile prefer #pragma once, since it's widely supported and there are less mistakes to make and it's simpler to read. \$\endgroup\$ Commented Oct 20, 2024 at 22:20

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.