Hi tz, You're answering too early. Try (for minimalistic test, not for good style):
static enum MHD_Result answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { if (NULL == *con_cls) { *con_cls = "foo"; return MHD_YES; } // continue as before That should do the trick. And yes, the API is counter-intuitive here, especially for simple GET calls. The reason is that the first request is for responding to 'Expect: 100 continue' and error replies _only_. Which I guess for GET is quite odd. But once you handle POSTs and start to keep state in *con_cls, it'll be more natural. Happy hacking! Christian On 6/28/21 5:49 PM, t...@objectiveapps.net wrote: > Hello, > > I've compiled one of the first MHD examples and I'm struggeling how to > get a keep-alive session. From my understanding, MHD should always > respond with Connection: keep-alive, as long as not getting a > Connection: close in the request. Here is my code: > > -------------------------- > #include <sys/types.h> > #ifndef _WIN32 > #include <sys/select.h> > #include <sys/socket.h> > #else > #include <winsock2.h> > #endif > #include <string.h> > #include <microhttpd.h> > #include <stdio.h> > > #define PORT 8888 > > static enum MHD_Result > answer_to_connection (void *cls, struct MHD_Connection *connection, > const char *url, const char *method, > const char *version, const char *upload_data, > size_t *upload_data_size, void **con_cls) > { > const char *page = "<html><body>Hello, browser!</body></html>"; > struct MHD_Response *response; > enum MHD_Result ret; > (void) cls; /* Unused. Silent compiler warning. */ > (void) url; /* Unused. Silent compiler warning. */ > (void) method; /* Unused. Silent compiler warning. */ > (void) version; /* Unused. Silent compiler warning. */ > (void) upload_data; /* Unused. Silent compiler warning. */ > (void) upload_data_size; /* Unused. Silent compiler warning. */ > (void) con_cls; /* Unused. Silent compiler warning. */ > > response = > MHD_create_response_from_buffer (strlen (page), (void *) page, > MHD_RESPMEM_PERSISTENT); > MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, > "keep-alive"); > ret = MHD_queue_response (connection, MHD_HTTP_OK, response); > MHD_destroy_response (response); > return ret; > } > > int main (void) > { > struct MHD_Daemon *daemon; > daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION, > PORT, NULL, NULL, > &answer_to_connection, NULL, > MHD_OPTION_END); > if (NULL == daemon) > return 1; > (void) getchar (); > MHD_stop_daemon (daemon); > return 0; > } > -------------------------- > > > I'm calling the server from curl with the following command: > >> curl -v 192.168.178.70:8888 -H "Connection: keep-alive" > > Curl output is as follows: > > >> GET / HTTP/1.1 >> Host: 192.168.178.70:8888 >> User-Agent: curl/7.66.0 >> Accept: */* >> Connection: keep-alive >> > > < HTTP/1.1 200 OK > < Connection: close > < Content-Length: 41 > < Date: Mon, 28 Jun 2021 15:44:19 GMT > < > * Closing connection 0 > > > > As you can see, Curl asks for a keep-alive connection, MHD answers with > Connection: close. Im getting the same result when using a browser > (chrome) and inspecting the packets. > > Obviously, I'm doing something wrong. But what? > > Thanks in advance, > Thomas > > > > > > > > > > > > > >
signature.asc
Description: OpenPGP digital signature