return responses from HTTPDHandler::requestHandler instead of constant ones v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 08 Apr 2022 22:38:45 +0200
branchv_0
changeset 3 1184f3de5533
parent 2 4b05b16b97e6
child 4 37a86904145c
return responses from HTTPDHandler::requestHandler instead of constant ones
src/HTTPDHandler.h
src/HTTPServer.cpp
src/HTTPServer.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 = "<h1>greetings and salutations</h1>";
+
+			return response;
+		}
+
+	};
+
+	std::shared_ptr<RequestHandler> requestHandler = std::make_shared<RequestHandler>();
+
 public:
 
 	HttpdHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration, std::shared_ptr<HTTPServer> httpServer) : relationalWriter(relationalWriter), configuration(configuration), httpServer(httpServer) {
-
+		httpServer->setRequestHandler(requestHandler);
 	}
 
 	virtual ~HttpdHandler() {
--- 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<HTTPServer::HTTPServerImpl*> (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[] = "<p>Hello, <b>relational</b> world!</p>";
-		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<HTTPServer::HTTPServerImpl*> (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[] = "<h1>HTTP 404: Not found</h1>";
+			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;
+		}
 	};
 
 
--- 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 {