libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] MHD_add_connection Problems


From: Damon Earp
Subject: [libmicrohttpd] MHD_add_connection Problems
Date: Fri, 18 Sep 2020 16:00:35 -0500

I cannot get `MHD_add_connection` to work at all, regardless of the flags I pass to `MHD_start_daemon`.

Environment: Debian 10, libmicrohttpd 0.9.62

I've attached a test that I tried to get workin to no avail. I never get an error but microhttpd never starts up the epoll thread either, in fact if I start_daemon then stop_daemon with NO_LISTEN_SOCKET I get an error from mhd because it cannot join the non-existent thread.

I've tried using USE_EPOLL + MHD_run() which didn't do anything.

I've tried creating a second listen socket, which I don't use, and passing it to start_daemon without the no listen socket. This does force the epoll thread to be created but add_connection still doesn't work.

I'm obviously missing something basic. Examples are few and far between.
Thanks for help in advance!
Damon

Sample Output of test.c below
$ ./test
daemon 0x55c2bb065ea0
MHD_add_conntection() returned 1


// test.c
#include <microhttpd.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

int access_handler(void *cls, struct MHD_Connection *con,
    const char *url, const char *method, const char *version,
    const char *upload_data, size_t *upload_data_size, void **con_cls)
{
    printf("access_handler(%p, %p, '%s', '%s', '%s', %p, %zu, %p)\n", cls, con,
            url, method, version, upload_data, *upload_data_size, con_cls);
    struct MHD_Response *resp = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
    MHD_queue_response(con, 400, resp);
    MHD_destroy_response(resp);
    return MHD_YES;
}

int create_listen_socket(short port)
{
    int on = 1;
    int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    bind(fd, (struct sockaddr *) &sin, sizeof(sin));
    listen(fd, 128);
    return fd;
}

int main(int argc, const char *argv[])
{
    int flags = MHD_USE_NO_LISTEN_SOCKET | MHD_USE_EPOLL_INTERNAL_THREAD | MHD_USE_ITC;

    struct MHD_Daemon *d = MHD_start_daemon(flags, 8080, NULL, NULL, &access_handler, NULL, MHD_OPTION_END);
    printf("daemon %p\n", d);

    int sock = create_listen_socket(8080);
    while (1)
    {
        struct sockaddr addr;
        socklen_t addrlen;
        int con = accept(sock, &addr, &addrlen);
        if (con >= 0)
        {
            int ret = MHD_add_connection(d, con, &addr, addrlen);
            printf("MHD_add_conntection() returned %d\n", ret);
        }
    }
    return 0;
}

reply via email to

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