libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit de35faa11a8bcfb8a771a924f183694be7ba0ddb
parent 5f6d6b2ccc8dfaeef9c961a7c91d62597fa406d3
Author: Christian Grothoff <christian@grothoff.org>
Date:   Thu,  1 Dec 2011 14:24:24 +0000

Hi there,

am I right in the assumption, that the documentation at
http://www.gnu.org/s/libmicrohttpd/tutorial.html#Supporting-basic-authentication
is newer than authorization_example.c in the src/examples/ subdirectory?

If so here is a patch to make authorization_example.c use the functions from
the tutorial instead of raw base64 strings.

Regards

Sven
-


Diffstat:
MAUTHORS | 1+
MChangeLog | 4++++
Msrc/examples/authorization_example.c | 27+++++++++++++--------------
3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/AUTHORS b/AUTHORS @@ -35,6 +35,7 @@ Zhimin Huang <zhimin.huang@bqvision.com> Jan Seeger <jan.seeger@thenybble.de> Will Bryant <will.bryant@gmail.com> LRN <lrn1986@gmail.com> +Sven Geggus <sts@fuchsschwanzdomain.de> Documentation contributions also came from: Marco Maggi <marco.maggi-ipsu@poste.it> diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,7 @@ +Thu Dec 1 15:22:57 CET 2011 + Updated authorization_example.c to actually demonstrate the current + MHD API. -SG + Mon Nov 21 18:51:30 CET 2011 Added option to suppress generation of the 'Date:' header to be used on embedded systems without RTC. Documented the new option diff --git a/src/examples/authorization_example.c b/src/examples/authorization_example.c @@ -44,8 +44,9 @@ ahc_echo (void *cls, const char *me = cls; struct MHD_Response *response; int ret; - int code; - const char *auth; + char *user; + char *pass; + int fail; if (0 != strcmp (method, "GET")) return MHD_NO; /* unexpected method */ @@ -56,28 +57,26 @@ ahc_echo (void *cls, return MHD_YES; } *ptr = NULL; /* reset when done */ - auth = MHD_lookup_connection_value (connection, - MHD_HEADER_KIND, - MHD_HTTP_HEADER_AUTHORIZATION); - if ((auth == NULL) || - (0 != strcmp (auth, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="))) - { - /* require: "Aladdin" with password "open sesame" */ + + /* require: "Aladdin" with password "open sesame" */ + pass = NULL; + user = MHD_basic_auth_get_username_password (connection, &pass); + fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp (pass, "open sesame") ) ); + if (fail) + { response = MHD_create_response_from_buffer (strlen (DENIED), (void *) DENIED, MHD_RESPMEM_PERSISTENT); - MHD_add_response_header (response, MHD_HTTP_HEADER_WWW_AUTHENTICATE, - "Basic realm=\"TestRealm\""); - code = MHD_HTTP_UNAUTHORIZED; + ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); } else { response = MHD_create_response_from_buffer (strlen (me), (void *) me, MHD_RESPMEM_PERSISTENT); - code = MHD_HTTP_OK; + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } - ret = MHD_queue_response (connection, code, response); + MHD_destroy_response (response); return ret; }