HTTP server demo v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 07 Apr 2022 21:06:37 +0200
branchv_0
changeset 1 23c516259cc5
parent 0 7b70918c30af
child 2 4b05b16b97e6
HTTP server demo
src/HTTPDHandler.h
src/HTTPServer.cpp
src/relpipe-tr-httpd.cpp
--- a/src/HTTPDHandler.h	Tue Apr 05 23:46:34 2022 +0200
+++ b/src/HTTPDHandler.h	Thu Apr 07 21:06:37 2022 +0200
@@ -44,6 +44,7 @@
 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8
 	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
 	Configuration configuration;
+	std::shared_ptr<HTTPServer> httpServer;
 	RelationConfiguration* currentRelationConfiguration = nullptr;
 	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
 	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
@@ -52,19 +53,19 @@
 
 public:
 
-	HttpdHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
-		
+	HttpdHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration, std::shared_ptr<HTTPServer> httpServer) : relationalWriter(relationalWriter), configuration(configuration), httpServer(httpServer) {
+
 	}
 
 	virtual ~HttpdHandler() {
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
-		
+
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
-		
+
 	}
 
 	void endOfPipe() {
--- a/src/HTTPServer.cpp	Tue Apr 05 23:46:34 2022 +0200
+++ b/src/HTTPServer.cpp	Thu Apr 07 21:06:37 2022 +0200
@@ -16,6 +16,7 @@
  */
 
 #include <microhttpd.h>
+#include <stdexcept>
 
 #include "HTTPServer.h"
 
@@ -24,17 +25,44 @@
 namespace httpd {
 
 class HTTPServer::HTTPServerImpl {
+public:
+	MHD_Daemon* mhd = nullptr;
 };
 
 HTTPServer* HTTPServer::create() {
 	HTTPServer::HTTPServerImpl* impl = new HTTPServer::HTTPServerImpl();
 
+	void* acceptCallbackData = nullptr;
+	MHD_AcceptPolicyCallback acceptCallback = [](void* cls, const struct sockaddr* addr, socklen_t addrlen) {
+		return MHD_YES;
+	};
 
-	return new HTTPServer(impl);
+	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;
+	};
+
+
+	impl->mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD,
+			8080,
+			acceptCallback, acceptCallbackData,
+			accessCallback, accessCallbackData,
+			MHD_OPTION_THREAD_POOL_SIZE, 10,
+			MHD_OPTION_CONNECTION_TIMEOUT, 60,
+			MHD_OPTION_END);
+
+
+	if (impl->mhd) return new HTTPServer(impl);
+	else throw std::logic_error("Unable to start MHD.");
 }
 
 HTTPServer::~HTTPServer() {
-
+	MHD_stop_daemon(impl->mhd);
+	delete impl;
 }
 
 
--- a/src/relpipe-tr-httpd.cpp	Tue Apr 05 23:46:34 2022 +0200
+++ b/src/relpipe-tr-httpd.cpp	Thu Apr 07 21:06:37 2022 +0200
@@ -51,7 +51,8 @@
 		Configuration configuration = cliParser.parse(cli.arguments());
 		std::shared_ptr<RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
 		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
-		HttpdHandler handler(writer, configuration);
+		std::shared_ptr<HTTPServer> httpServer(HTTPServer::create());
+		HttpdHandler handler(writer, configuration, httpServer);
 		reader->addHandler(&handler);
 		reader->process();