libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] not getting a keep-alive session from MHD


From: tz
Subject: [libmicrohttpd] not getting a keep-alive session from MHD
Date: Mon, 28 Jun 2021 17:49:53 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

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
















reply via email to

[Prev in Thread] Current Thread [Next in Thread]