logger = spd::get("jsonserver_spdlog");
- httpdserver = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
+ httpdserver = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv6, port, NULL, NULL,
&HTTPDClient::handle_connection, NULL,
MHD_OPTION_NOTIFY_CONNECTION, &HTTPDClient::connection_notify, NULL,
MHD_OPTION_NOTIFY_COMPLETED, &HTTPDClient::request_completed, NULL,
const char* version, const char* upload_data,
size_t* upload_data_size, void** 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);
-
HTTPDClient* httpdclient = (HTTPDClient*)mhdc->socket_context;
+ return httpdclient->handleConnection(url, method, version, upload_data, upload_data_size, userData);
+}
+
+// End of static callbacks, now for the client object itself
+
+HTTPDClient::HTTPDClient(struct MHD_Connection* _mhd_connection, const std::string& _configDir)
+: postprocessor(NULL), url(NULL), mhd_connection(_mhd_connection), vdrclient(_configDir)
+{
+// printf("HTTPDClient created %p\n", this);
+}
+
+HTTPDClient::~HTTPDClient()
+{
+// printf("%p HTTPDClient destructor\n", this);
+ if (url) free(url);
+}
+
+int HTTPDClient::handleConnection(const char* turl, const char* method,
+ const char* version, const char* upload_data,
+ size_t* upload_data_size, void** 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);
+ */
// Now we are going to use userData as a flag to say first run or not
if (!*userData) // new request
{
*userData = (void*)1;
- httpdclient->setUrl(url);
+ int size __attribute__((unused)) = asprintf(&url, "%s", turl);
if (!strcmp(method, "POST"))
{
- MHD_get_connection_values(mhd_connection, MHD_GET_ARGUMENT_KIND, HTTPDClient::uri_key_value, (void*)httpdclient);
+ MHD_get_connection_values(mhd_connection, MHD_GET_ARGUMENT_KIND, HTTPDClient::uri_key_value, (void*)this);
// The following returns NULL if there are no POST fields to come
- httpdclient->postprocessor = MHD_create_post_processor(mhd_connection, POSTBUFFERSIZE,
- HTTPDClient::iterate_post, (void*)httpdclient);
+ postprocessor = MHD_create_post_processor(mhd_connection, POSTBUFFERSIZE,
+ HTTPDClient::iterate_post, (void*)this);
}
else if (!strcmp(method, "GET"))
{
if (!strcmp(method, "GET"))
{
- return httpdclient->processGET();
+ return processGET();
}
else if (!strcmp(method, "POST"))
{
if (*upload_data_size != 0) // There is more to process, and signal run again
{
- MHD_post_process(httpdclient->postprocessor, upload_data, *upload_data_size);
+ MHD_post_process(postprocessor, upload_data, *upload_data_size);
*upload_data_size = 0;
return MHD_YES;
}
else
{
// printf("hc: zero post provided, end of upload\n");
- return httpdclient->processPOST();
+
+ MHD_destroy_post_processor(postprocessor);
+ postprocessor = NULL;
+
+ return processPOST();
}
}
else
{
- return httpdclient->sendStockResponse(405);
+ return sendStockResponse(405);
}
}
-// End of static callbacks, now for the client object itself
-
-HTTPDClient::HTTPDClient(struct MHD_Connection* _mhd_connection, const std::string& _configDir)
-: postprocessor(NULL), url(NULL), mhd_connection(_mhd_connection), vdrclient(_configDir)
-{
-// printf("HTTPDClient created %p\n", this);
-}
-
-HTTPDClient::~HTTPDClient()
-{
-// printf("%p HTTPDClient destructor\n", this);
- if (url) free(url);
-}
-
void HTTPDClient::requestComplete()
{
// printf("%p HTTPDClient request complete\n", this);
}
}
-void HTTPDClient::setUrl(const char* _url)
-{
- int size __attribute__((unused)) = asprintf(&url, "%s", _url);
-}
-
void HTTPDClient::addGetVar(const char* key, const char* value)
{
if (strlen(key) > 50) return;