libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] Keep-alive and POST method


From: silvioprog
Subject: [libmicrohttpd] Keep-alive and POST method
Date: Thu, 5 Nov 2015 19:43:53 -0300

Hello,

Is the keep-alive feature compatible only with GET method? See this log below, it compares the headers from MHD and NodeJS:

=== NodeJS ===

...  GET ...

$ curl -I http://127.0.0.1:8080
HTTP/1.1 200 OK
Content-Type: text/html
Date: Thu, 05 Nov 2015 22:34:34 GMT
Connection: keep-alive

... POST ...

$ curl -X POST -I http://127.0.0.1:8080
HTTP/1.1 200 OK
Content-Type: text/html
Date: Thu, 05 Nov 2015 22:36:45 GMT
Connection: keep-alive
Transfer-Encoding: chunked

=== NodeJS ===

=== MHD ===

... GET ...

$ curl -I http://127.0.0.1:8080
HTTP/1.1 200 OK
Content-Length: 76
Content-Type: text/html
Connection: keep-alive
Date: Thu, 05 Nov 2015 22:39:37 GMT

... POST ...

$ curl -X POST -I http://127.0.0.1:8080
HTTP/1.1 200 OK
Connection: close
Content-Length: 76
Content-Type: text/html
Date: Thu, 05 Nov 2015 22:41:20 GMT

=== MHD ===

Source codes:

helloworld_nodejs.js:
-------
const PORT = 8080;
var http = require('http');
var body = '<html><head><title>Hello world</title></head><body>Hello world</body></html>';
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(body);
}).listen(PORT);
console.log('Server is running on port ' + PORT);
-------

helloworld_mhd.c:
-------
#include <microhttpd.h>
#include <string.h>
#include <stdio.h>

#define PORT 8080
#define PAGE "<html><head><title>Hello world</title></head><body>Hello world</body></html>"

static struct MHD_Response *response;

static int ahc_echo(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 **ptr) {
    return MHD_queue_response(connection, MHD_HTTP_OK, response);
}

int main(int argc, char *const *argv) {
    struct MHD_Daemon *d;
    response = MHD_create_response_from_buffer(strlen(PAGE), (void *) PAGE, MHD_RESPMEM_PERSISTENT);
    (void) MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, "keep-alive");
    (void) MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html");
    d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY,
                         PORT, NULL, NULL, &ahc_echo, NULL,
                         MHD_OPTION_END);
    if (d == NULL)
        return 1;
    printf("Server is running on port %d", PORT);
    getchar();
    MHD_stop_daemon(d);
    MHD_destroy_response(response);
    return 0;
}
-------

Thank you!

--
Silvio Clécio

reply via email to

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