libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] Question regarding HTTP basic auth example


From: Sebastian Hammerl
Subject: [libmicrohttpd] Question regarding HTTP basic auth example
Date: Mon, 17 Feb 2014 13:47:51 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0

Hi,

I just started experimenting with libmicrohttpd and followed the
tutorial with all the examples. When I implemented HTTP basic auth,
I experienced the following problem:

When I send a request with invalid auth credentials and no additional
(e.g. POST) data everything works well and I get the "Not Authorized!"
response correctly. But when I additionally attach data to my request
I get an empty reply from the server.

I figured out that "MHD_queue_basic_auth_fail_response" returns 0 in
the case that additional data is attached to the request which causes
the ending of the connection.

However I could not figure out how to solve this problem. My goal is to
get a valid response from the server when sending invalid credentials
with extra data.

I stripped down the example code to the minimum, this is what I am
testing with, compile with "g++ basicauth.c -lmicrohttpd":


basicauth.cpp:

#include <microhttpd.h>
#include <string.h>
#include <stdio.h>

static int 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) {
    int ret;
    struct MHD_Response *response;

    if (NULL == *con_cls) {
        *con_cls = connection;
        return MHD_YES;
    }

    const char *page = "Not Authorized!\n\n";
    response = MHD_create_response_from_buffer(strlen (page),
                                               (void *) page,
MHD_RESPMEM_PERSISTENT);
    ret = MHD_queue_basic_auth_fail_response(connection,
                                             "my realm",
                                             response);

    MHD_destroy_response (response);
    return ret;
}


int main () {
    struct MHD_Daemon *d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY,
                                           8888, NULL, NULL,
&answer_to_connection, NULL,
                                           MHD_OPTION_END);
    if (NULL == d)
        return 1;
    getchar ();
    MHD_stop_daemon (d);
    return 0;
}


Here are the two request examples I am using with curl:

:~> curl -X POST localhost:8888
Not Authorized!
:~> curl -X POST -d a=b localhost:8888
curl: (52) Empty reply from server


Can anyone help me with this problem?

Thank you very much,
    Sebastian




reply via email to

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