]> git.vomp.tv Git - jsonserver.git/commitdiff
Allow empty post fields & bug fix
authorChris Tallon <chris@vomp.tv>
Tue, 10 May 2022 14:34:25 +0000 (15:34 +0100)
committerChris Tallon <chris@vomp.tv>
Tue, 10 May 2022 14:34:25 +0000 (15:34 +0100)
Allow post fields where the value is empty string
Bug fix: Clear the post/get fields maps after requests

httpdclient.c
vdrclient.c
vdrclient.h

index 00083c1a932347246db0c30922d4d6adc2be3f15..87727143bc6c64e78889d9fb01f575a871ed7712 100644 (file)
@@ -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;
 }
index 47bdf66f93e3d7863b4c5831df696da0245975cd..f0592e6cc596ee2e16ffdc0bb7cf01ca0d82d2f7 100644 (file)
@@ -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;
 }
 
index b49c8b85433b6420a86ce45f8d565468cbfdc1ad..bf7e3fdb4c6ab151398cc7467fe45afa189128c2 100644 (file)
@@ -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);