[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [libmicrohttpd] Question regarding HTTP basic auth example
From: |
Christian Grothoff |
Subject: |
Re: [libmicrohttpd] Question regarding HTTP basic auth example |
Date: |
Mon, 17 Feb 2014 13:58:50 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 |
I think simply adding a
if (*upload_data_size > 0)
{
*upload_data_size = 0; // consume
return MHD_YES;
}
just before
const char *page = "Not Authorized!\n\n";
will do the trick (untested). This will cause MHD to process (and throw
away) all upload data before you queue a response. If you do not do this,
ret = MHD_queue_basic_auth_fail_response
will set 'ret' to MHD_NO (response queued too early, before
processing all upload data) and returning MHD_NO from the
callback will then cause MHD to close the connection.
Happy hacking!
Christian
On 02/17/2014 01:47 PM, Sebastian Hammerl wrote:
> 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
>
>
0x48426C7E.asc
Description: application/pgp-keys
signature.asc
Description: OpenPGP digital signature