# HG changeset patch # User František Kučera # Date 1651279790 -7200 # Node ID 90990c8b6aef9593f4e712213745e627540691b3 # Parent 315d985f8424f6622d07a96ca5cdd79a30b68379 headers, cookies, GET parameters diff -r 315d985f8424 -r 90990c8b6aef src/HTTPDHandler.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; } } diff -r 315d985f8424 -r 90990c8b6aef src/HTTPServer.cpp --- 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*> (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*> (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*> (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; diff -r 315d985f8424 -r 90990c8b6aef src/HTTPServer.h --- 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; + std::vector header; + std::vector cookie; + std::vector getParameter; + std::vector postParameter; std::string host; std::string method; std::string url; @@ -60,7 +64,8 @@ class Response { public: - std::vector
header; + std::vector header; + std::vector cookie; uint16_t code; std::string body; };