diff options
Diffstat (limited to 'doc/examples/basicauthentication.c')
-rw-r--r-- | doc/examples/basicauthentication.c | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/doc/examples/basicauthentication.c b/doc/examples/basicauthentication.c index d75ba636..6e1493a3 100644 --- a/doc/examples/basicauthentication.c +++ b/doc/examples/basicauthentication.c | |||
@@ -23,9 +23,7 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
23 | const char *version, const char *upload_data, | 23 | const char *version, const char *upload_data, |
24 | size_t *upload_data_size, void **req_cls) | 24 | size_t *upload_data_size, void **req_cls) |
25 | { | 25 | { |
26 | char *user; | 26 | struct MHD_BasicAuthInfo *auth_info; |
27 | char *pass; | ||
28 | int fail; | ||
29 | enum MHD_Result ret; | 27 | enum MHD_Result ret; |
30 | struct MHD_Response *response; | 28 | struct MHD_Response *response; |
31 | (void) cls; /* Unused. Silent compiler warning. */ | 29 | (void) cls; /* Unused. Silent compiler warning. */ |
@@ -41,30 +39,43 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
41 | *req_cls = connection; | 39 | *req_cls = connection; |
42 | return MHD_YES; | 40 | return MHD_YES; |
43 | } | 41 | } |
44 | pass = NULL; | 42 | auth_info = MHD_basic_auth_get_username_password3 (connection); |
45 | user = MHD_basic_auth_get_username_password (connection, | 43 | if (NULL == auth_info) |
46 | &pass); | ||
47 | fail = ( (NULL == user) || | ||
48 | (0 != strcmp (user, "root")) || | ||
49 | (0 != strcmp (pass, "pa$$w0rd") ) ); | ||
50 | if (NULL != user) | ||
51 | MHD_free (user); | ||
52 | if (NULL != pass) | ||
53 | MHD_free (pass); | ||
54 | if (fail) | ||
55 | { | 44 | { |
56 | const char *page = "<html><body>Go away.</body></html>"; | 45 | static const char *page = |
46 | "<html><body>Authorization required</body></html>"; | ||
57 | response = MHD_create_response_from_buffer_static (strlen (page), page); | 47 | response = MHD_create_response_from_buffer_static (strlen (page), page); |
58 | ret = MHD_queue_basic_auth_fail_response (connection, | 48 | ret = MHD_queue_basic_auth_fail_response3 (connection, |
59 | "my realm", | 49 | "admins", |
60 | response); | 50 | MHD_YES, |
51 | response); | ||
52 | } | ||
53 | else if ((strlen ("root") != auth_info->username_len) || | ||
54 | (0 != memcmp (auth_info->username, "root", | ||
55 | auth_info->username_len)) || | ||
56 | /* The next check against NULL is optional, | ||
57 | * if 'password' is NULL then 'password_len' is always zero. */ | ||
58 | (NULL == auth_info->password) || | ||
59 | (strlen ("pa$$w0rd") != auth_info->password_len) || | ||
60 | (0 != memcmp (auth_info->password, "pa$$w0rd", | ||
61 | auth_info->password_len))) | ||
62 | { | ||
63 | static const char *page = | ||
64 | "<html><body>Wrong username or password</body></html>"; | ||
65 | response = MHD_create_response_from_buffer_static (strlen (page), page); | ||
66 | ret = MHD_queue_basic_auth_fail_response3 (connection, | ||
67 | "admins", | ||
68 | MHD_YES, | ||
69 | response); | ||
61 | } | 70 | } |
62 | else | 71 | else |
63 | { | 72 | { |
64 | const char *page = "<html><body>A secret.</body></html>"; | 73 | static const char *page = "<html><body>A secret.</body></html>"; |
65 | response = MHD_create_response_from_buffer_static (strlen (page), page); | 74 | response = MHD_create_response_from_buffer_static (strlen (page), page); |
66 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 75 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
67 | } | 76 | } |
77 | if (NULL != auth_info) | ||
78 | MHD_free (auth_info); | ||
68 | MHD_destroy_response (response); | 79 | MHD_destroy_response (response); |
69 | return ret; | 80 | return ret; |
70 | } | 81 | } |