# HG changeset patch # User František Kučera # Date 1649450325 -7200 # Node ID 1184f3de553383a4ffc2e53a3065aad6c7bb6353 # Parent 4b05b16b97e6541dadbe72e81ab84b44fc7812e0 return responses from HTTPDHandler::requestHandler instead of constant ones diff -r 4b05b16b97e6 -r 1184f3de5533 src/HTTPDHandler.h --- a/src/HTTPDHandler.h Thu Apr 07 23:04:12 2022 +0200 +++ b/src/HTTPDHandler.h Fri Apr 08 22:38:45 2022 +0200 @@ -50,10 +50,27 @@ size_t currentAttributeIndex = 0; size_t currentRecordNumber = 1; + class RequestHandler : public HTTPServer::RequestHandler { + public: + + const HTTPServer::Response handle(const HTTPServer::Request& request) override { + HTTPServer::Response response; + + // TODO: return real responses + response.code = 200; + response.body = "

greetings and salutations

"; + + return response; + } + + }; + + std::shared_ptr requestHandler = std::make_shared(); + public: HttpdHandler(shared_ptr relationalWriter, Configuration configuration, std::shared_ptr httpServer) : relationalWriter(relationalWriter), configuration(configuration), httpServer(httpServer) { - + httpServer->setRequestHandler(requestHandler); } virtual ~HttpdHandler() { diff -r 4b05b16b97e6 -r 1184f3de5533 src/HTTPServer.cpp --- a/src/HTTPServer.cpp Thu Apr 07 23:04:12 2022 +0200 +++ b/src/HTTPServer.cpp Fri Apr 08 22:38:45 2022 +0200 @@ -37,18 +37,37 @@ HTTPServer* HTTPServer::create(HTTPServer::Options options) { HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl(); - void* acceptCallbackData = nullptr; - MHD_AcceptPolicyCallback acceptCallback = [](void* cls, const struct sockaddr* addr, socklen_t addrlen) { - return MHD_YES; + void* acceptCallbackData = impl; + MHD_AcceptPolicyCallback acceptCallback = [](void* rawImpl, const struct sockaddr* addr, socklen_t addrlen) { + if (rawImpl) { + HTTPServer::HTTPServerImpl* impl = static_cast (rawImpl); + // TODO: call impl if present and has such method + return MHD_YES; + } else { + return MHD_YES; + } }; - void* accessCallbackData = nullptr; - MHD_AccessHandlerCallback accessCallback = [](void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { - static const char html[] = "

Hello, relational world!

"; - struct MHD_Response* response = MHD_create_response_from_buffer(sizeof (html), (void*) html, MHD_RESPMEM_PERSISTENT); - MHD_queue_response(connection, 200, response); - MHD_destroy_response(response); - return MHD_YES; + void* accessCallbackData = impl; + MHD_AccessHandlerCallback accessCallback = [](void* rawImpl, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { + HTTPServer::HTTPServerImpl* impl = static_cast (rawImpl); + // TODO: return also HTTP headers + if (impl->requestHandler) { + HTTPServer::Request request; + const HTTPServer::Response response = impl->requestHandler->handle(request); + struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY); + MHD_queue_response(connection, response.code, mhdResponse); + MHD_destroy_response(mhdResponse); + return MHD_YES; + } else { + // there might be a point in time when the HTTP server is started and HTTP handler is not set + // TODO: just return MHD_NO? + static const char body[] = "

HTTP 404: Not found

"; + struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT); + MHD_queue_response(connection, 404, mhdResponse); + MHD_destroy_response(mhdResponse); + return MHD_YES; + } }; diff -r 4b05b16b97e6 -r 1184f3de5533 src/HTTPServer.h --- a/src/HTTPServer.h Thu Apr 07 23:04:12 2022 +0200 +++ b/src/HTTPServer.h Fri Apr 08 22:38:45 2022 +0200 @@ -62,7 +62,10 @@ class RequestHandler { public: - virtual const Response handle(const Request& request); + virtual const Response handle(const Request& request) = 0; + + virtual ~RequestHandler() = default; + }; class Options {