aboutsummaryrefslogtreecommitdiff
path: root/src/examples/authorization_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/authorization_example.c')
-rw-r--r--src/examples/authorization_example.c27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/examples/authorization_example.c b/src/examples/authorization_example.c
index 60512183..bb480267 100644
--- a/src/examples/authorization_example.c
+++ b/src/examples/authorization_example.c
@@ -44,8 +44,9 @@ ahc_echo (void *cls,
44 const char *me = cls; 44 const char *me = cls;
45 struct MHD_Response *response; 45 struct MHD_Response *response;
46 int ret; 46 int ret;
47 int code; 47 char *user;
48 const char *auth; 48 char *pass;
49 int fail;
49 50
50 if (0 != strcmp (method, "GET")) 51 if (0 != strcmp (method, "GET"))
51 return MHD_NO; /* unexpected method */ 52 return MHD_NO; /* unexpected method */
@@ -56,28 +57,26 @@ ahc_echo (void *cls,
56 return MHD_YES; 57 return MHD_YES;
57 } 58 }
58 *ptr = NULL; /* reset when done */ 59 *ptr = NULL; /* reset when done */
59 auth = MHD_lookup_connection_value (connection, 60
60 MHD_HEADER_KIND, 61 /* require: "Aladdin" with password "open sesame" */
61 MHD_HTTP_HEADER_AUTHORIZATION); 62 pass = NULL;
62 if ((auth == NULL) || 63 user = MHD_basic_auth_get_username_password (connection, &pass);
63 (0 != strcmp (auth, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="))) 64 fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp (pass, "open sesame") ) );
64 { 65 if (fail)
65 /* require: "Aladdin" with password "open sesame" */ 66 {
66 response = MHD_create_response_from_buffer (strlen (DENIED), 67 response = MHD_create_response_from_buffer (strlen (DENIED),
67 (void *) DENIED, 68 (void *) DENIED,
68 MHD_RESPMEM_PERSISTENT); 69 MHD_RESPMEM_PERSISTENT);
69 MHD_add_response_header (response, MHD_HTTP_HEADER_WWW_AUTHENTICATE, 70 ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response);
70 "Basic realm=\"TestRealm\"");
71 code = MHD_HTTP_UNAUTHORIZED;
72 } 71 }
73 else 72 else
74 { 73 {
75 response = MHD_create_response_from_buffer (strlen (me), 74 response = MHD_create_response_from_buffer (strlen (me),
76 (void *) me, 75 (void *) me,
77 MHD_RESPMEM_PERSISTENT); 76 MHD_RESPMEM_PERSISTENT);
78 code = MHD_HTTP_OK; 77 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
79 } 78 }
80 ret = MHD_queue_response (connection, code, response); 79
81 MHD_destroy_response (response); 80 MHD_destroy_response (response);
82 return ret; 81 return ret;
83} 82}