headers, cookies, GET parameters v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 30 Apr 2022 02:49:50 +0200
branchv_0
changeset 8 90990c8b6aef
parent 7 315d985f8424
child 9 36479a47faa8
headers, cookies, GET parameters
src/HTTPDHandler.h
src/HTTPServer.cpp
src/HTTPServer.h
--- a/src/HTTPDHandler.h	Thu Apr 21 00:43:54 2022 +0200
+++ b/src/HTTPDHandler.h	Sat Apr 30 02:49:50 2022 +0200
@@ -163,7 +163,32 @@
 				{L"value", relpipe::writer::TypeId::STRING},
 			}, true);
 
-			for (const HTTPServer::Header& h : request.header) {
+			for (const HTTPServer::AVP& h : request.header) {
+				relationalWriter->writeAttribute(exchangeId);
+				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
+				relationalWriter->writeAttribute(L"request");
+				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
+			}
+
+			for (const HTTPServer::AVP& h : response.header) {
+				relationalWriter->writeAttribute(exchangeId);
+				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
+				relationalWriter->writeAttribute(L"response");
+				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
+			}
+			
+			relationalWriter->startRelation(L"cookie",{
+				// TODO: ordinal number?
+				{L"exchange", relpipe::writer::TypeId::STRING},
+				{L"url", relpipe::writer::TypeId::STRING},
+				{L"direction", relpipe::writer::TypeId::STRING},
+				{L"name", relpipe::writer::TypeId::STRING},
+				{L"value", relpipe::writer::TypeId::STRING},
+			}, true);
+			
+			for (const HTTPServer::AVP& h : request.cookie) {
 				relationalWriter->writeAttribute(exchangeId);
 				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
 				relationalWriter->writeAttribute(L"request");
@@ -171,13 +196,43 @@
 				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
 			}
 
-			for (const HTTPServer::Header& h : response.header) {
+			for (const HTTPServer::AVP& h : response.cookie) {
 				relationalWriter->writeAttribute(exchangeId);
 				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
 				relationalWriter->writeAttribute(L"response");
 				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
 				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
 			}
+			
+			relationalWriter->startRelation(L"get_parameter",{
+				// TODO: ordinal number?
+				{L"exchange", relpipe::writer::TypeId::STRING},
+				{L"url", relpipe::writer::TypeId::STRING},
+				{L"name", relpipe::writer::TypeId::STRING},
+				{L"value", relpipe::writer::TypeId::STRING},
+			}, true);
+			
+			for (const HTTPServer::AVP& h : request.getParameter) {
+				relationalWriter->writeAttribute(exchangeId);
+				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
+			}
+			
+			relationalWriter->startRelation(L"post_parameter",{
+				// TODO: ordinal number?
+				{L"exchange", relpipe::writer::TypeId::STRING},
+				{L"url", relpipe::writer::TypeId::STRING},
+				{L"name", relpipe::writer::TypeId::STRING},
+				{L"value", relpipe::writer::TypeId::STRING},
+			}, true);
+			
+			for (const HTTPServer::AVP& h : request.postParameter) {
+				relationalWriter->writeAttribute(exchangeId);
+				relationalWriter->writeAttribute(convertor.from_bytes(request.url));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.name));
+				relationalWriter->writeAttribute(convertor.from_bytes(h.value));
+			}
 
 
 
@@ -212,8 +267,8 @@
 					response.code = t.code;
 					response.body = t.body;
 					// TODO: replace global header values with request-specific ones instead of appending?
-					for (const GlobalHeaderTemplate& h : *headerTemplates) if (h.matches(request.method, request.url)) response.header.push_back(HTTPServer::Header(h.name, h.value));
-					for (const HeaderTemplate& h : t.headers) response.header.push_back(HTTPServer::Header(h.name, h.value));
+					for (const GlobalHeaderTemplate& h : *headerTemplates) if (h.matches(request.method, request.url)) response.header.push_back({h.name, h.value});
+					for (const HeaderTemplate& h : t.headers) response.header.push_back({h.name, h.value});
 					break;
 				}
 			}
--- a/src/HTTPServer.cpp	Thu Apr 21 00:43:54 2022 +0200
+++ b/src/HTTPServer.cpp	Sat Apr 30 02:49:50 2022 +0200
@@ -59,12 +59,33 @@
 
 			// TODO: return also client IP address etc.
 			// const MHD_ConnectionInfo* connetionInfo = MHD_get_connection_info(connection, MHD_ConnectionInfoType::MHD_CONNECTION_INFO_CLIENT_ADDRESS);
-
+			
 			// FIXME: request.body = ...
 			// FIXME: multiple calls for one request
+			// FIXME: POST parameters
+			
+			// HTTP headers:
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_HEADER_KIND, [](void* cls, MHD_ValueKind kind, const char* key, const char* value) {
+				(static_cast<std::vector<AVP>*> (cls))->push_back({key, value});
+				return MHD_YES;
+			}, &request.header);
+
+			// GET parameters:
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_GET_ARGUMENT_KIND, [](void* cls, MHD_ValueKind kind, const char* key, const char* value) {
+				(static_cast<std::vector<AVP>*> (cls))->push_back({key, value});
+				return MHD_YES;
+			}, &request.getParameter);
+
+			// COOKIE parameters:
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_COOKIE_KIND, [](void* cls, MHD_ValueKind kind, const char* key, const char* value) {
+				(static_cast<std::vector<AVP>*> (cls))->push_back({key, value});
+				return MHD_YES;
+			}, &request.cookie);
+
 			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);
-			for (Header h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str());
+			for (AVP h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str());
+			// FIXME: cookies: for (AVP c : response.cookie) ...
 			MHD_queue_response(connection, response.code, mhdResponse);
 			MHD_destroy_response(mhdResponse);
 			return MHD_YES;
--- a/src/HTTPServer.h	Thu Apr 21 00:43:54 2022 +0200
+++ b/src/HTTPServer.h	Sat Apr 30 02:49:50 2022 +0200
@@ -37,21 +37,25 @@
 
 public:
 
-	class Header {
+	/** Attribute-value pair – header, cookie, GET or POST parameter */
+	class AVP {
 	public:
 		std::string name;
 		std::string value;
 
-		Header(std::string name, std::string value) : name(name), value(value) {
+		AVP(std::string name, std::string value) : name(name), value(value) {
 		}
 
-		virtual ~Header() {
+		virtual ~AVP() {
 		}
 	};
 
 	class Request {
 	public:
-		std::vector<Header> header;
+		std::vector<AVP> header;
+		std::vector<AVP> cookie;
+		std::vector<AVP> getParameter;
+		std::vector<AVP> postParameter;
 		std::string host;
 		std::string method;
 		std::string url;
@@ -60,7 +64,8 @@
 
 	class Response {
 	public:
-		std::vector<Header> header;
+		std::vector<AVP> header;
+		std::vector<AVP> cookie;
 		uint16_t code;
 		std::string body;
 	};