headers, cookies, GET parameters: single function v_0 tip
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 01 May 2022 17:58:11 +0200
branchv_0
changeset 9 36479a47faa8
parent 8 90990c8b6aef
headers, cookies, GET parameters: single function
src/HTTPServer.cpp
--- a/src/HTTPServer.cpp	Sat Apr 30 02:49:50 2022 +0200
+++ b/src/HTTPServer.cpp	Sun May 01 17:58:11 2022 +0200
@@ -59,28 +59,23 @@
 
 			// 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) {
+
+			// MHD_create_post_processor()
+			// https://git.gnunet.org/libmicrohttpd.git/tree/src/examples/post_example.c
+
+			MHD_KeyValueIterator avpVectorAppender = [](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);
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_HEADER_KIND, avpVectorAppender, &request.header);
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_COOKIE_KIND, avpVectorAppender, &request.cookie);
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_GET_ARGUMENT_KIND, avpVectorAppender, &request.getParameter);
+			MHD_get_connection_values(connection, MHD_ValueKind::MHD_POSTDATA_KIND, avpVectorAppender, &request.postParameter);
 
 			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);