From 187dfe5d7bd45903591d049bde90b19d0a7ff5e4 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 10 May 2022 15:34:25 +0100 Subject: [PATCH] Allow empty post fields & bug fix Allow post fields where the value is empty string Bug fix: Clear the post/get fields maps after requests --- httpdclient.c | 40 +++++++++++++++++++++++----------------- vdrclient.c | 4 ++-- vdrclient.h | 2 +- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/httpdclient.c b/httpdclient.c index 00083c1..8772714 100644 --- a/httpdclient.c +++ b/httpdclient.c @@ -57,13 +57,10 @@ int HTTPDClient::iterate_post(void *clientIdentifier, enum MHD_ValueKind kind, c const char *filename, const char* content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) { - if (size == 0) return MHD_NO; - + // logger->info("HTTPDClient: add post field: {} {} {}", key, data, size); HTTPDClient* httpdclient = (HTTPDClient*)clientIdentifier; httpdclient->addPostField(key, data, size); - return MHD_YES; - - //Return MHD_YES to continue iterating, MHD_NO to abort the iteration. + return MHD_YES; //Return MHD_YES to continue iterating, MHD_NO to abort the iteration. } void HTTPDClient::request_completed(void *cls, struct MHD_Connection *mhd_connection, @@ -97,18 +94,18 @@ int HTTPDClient::handle_connection(void* cls, struct MHD_Connection* mhd_connect size_t* upload_data_size, void** userData) { /* - printf("handle_connection called\n"); - printf("hc: cls %p\n", cls); - printf("hc: mhd_connection %p\n", mhd_connection); - printf("hc: url %p\n", url); - if (url) printf("hc: url: %s\n", url); - printf("hc: method %p\n", method); - if (url) printf("hc: method: %s\n", method); - printf("hc: version %p\n", version); - if (url) printf("hc: version: %s\n", version); - printf("hc: upload_data %p\n", upload_data); - printf("hc: upload_data_size %lu\n", *upload_data_size); - printf("hc: userData %p\n", *userData); + logger->debug("handle_connection called"); + logger->debug("hc: cls {}", (void*)cls); + logger->debug("hc: mhd_connection {}", (void*)mhd_connection); + logger->debug("hc: url {}", (void*)url); + if (url) logger->debug("hc: url: {}", url); + logger->debug("hc: method {}", (void*)method); + if (url) logger->debug("hc: method: {}", method); + logger->debug("hc: version {}", (void*)version); + if (url) logger->debug("hc: version: {}", version); + logger->debug("hc: upload_data {}", (void*)upload_data); + logger->debug("hc: upload_data_size {}", *upload_data_size); + logger->debug("hc: userData {}", (void*)*userData); */ const MHD_ConnectionInfo* mhdc = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_SOCKET_CONTEXT); @@ -264,6 +261,9 @@ int HTTPDClient::processGET() struct MHD_Response* response = MHD_create_response_from_fd(sbuf.st_size, fd); int ret = MHD_queue_response(mhd_connection, MHD_HTTP_OK, response); + + getVars.clear(); + postFields.clear(); MHD_destroy_response(response); return ret; @@ -285,6 +285,9 @@ int HTTPDClient::processPOST() struct MHD_Response* response = MHD_create_response_from_buffer(strlen(returnData.c_str()), (void *)returnData.c_str(), MHD_RESPMEM_MUST_COPY); MHD_add_response_header(response, "Content-Type", "application/json"); int ret = MHD_queue_response(mhd_connection, MHD_HTTP_OK, response); + + getVars.clear(); + postFields.clear(); MHD_destroy_response(response); return ret; } @@ -304,6 +307,9 @@ int HTTPDClient::sendStockResponse(int code) struct MHD_Response* response = MHD_create_response_from_buffer(strlen(page), (void *)page, MHD_RESPMEM_PERSISTENT); int ret = MHD_queue_response(mhd_connection, code, response); + + getVars.clear(); + postFields.clear(); MHD_destroy_response(response); return ret; } diff --git a/vdrclient.c b/vdrclient.c index 47bdf66..f0592e6 100644 --- a/vdrclient.c +++ b/vdrclient.c @@ -1725,11 +1725,11 @@ int VDRClient::getVarInt(PFMap& postFields, const char* paramName) // THROWS return r; } -std::string VDRClient::getVarString(PFMap& postFields, const char* paramName) // THROWS +std::string VDRClient::getVarString(PFMap& postFields, const char* paramName, bool emptyOk) // THROWS { auto i = postFields.find(paramName); if (i == postFields.end()) throw BadParamException(paramName); - if (i->second.empty()) throw BadParamException(paramName); + if (!emptyOk && i->second.empty()) throw BadParamException(paramName); return i->second; } diff --git a/vdrclient.h b/vdrclient.h index b49c8b8..bf7e3fd 100644 --- a/vdrclient.h +++ b/vdrclient.h @@ -77,7 +77,7 @@ class VDRClient std::string& dirNameFullPathDate); // throws int int getVarInt(PFMap& postFields, const char* paramName); // THROWS - std::string getVarString(PFMap& postFields, const char* paramName); // THROWS + std::string getVarString(PFMap& postFields, const char* paramName, bool emptyOk = false); // THROWS bool loadEpgFilter(libconfig::Config& epgFilter); bool saveEpgFilter(libconfig::Config& epgFilter); -- 2.39.2