aboutsummaryrefslogtreecommitdiff
path: root/src/examples/digest_auth_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/digest_auth_example.c')
-rw-r--r--src/examples/digest_auth_example.c142
1 files changed, 73 insertions, 69 deletions
diff --git a/src/examples/digest_auth_example.c b/src/examples/digest_auth_example.c
index 889967fb..720576cd 100644
--- a/src/examples/digest_auth_example.c
+++ b/src/examples/digest_auth_example.c
@@ -26,9 +26,11 @@
26#include <microhttpd.h> 26#include <microhttpd.h>
27#include <stdlib.h> 27#include <stdlib.h>
28 28
29#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" 29#define PAGE \
30 "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
30 31
31#define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" 32#define DENIED \
33 "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
32 34
33#define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4" 35#define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4"
34 36
@@ -45,51 +47,52 @@ ahc_echo (void *cls,
45 const char *password = "testpass"; 47 const char *password = "testpass";
46 const char *realm = "test@example.com"; 48 const char *realm = "test@example.com";
47 int ret; 49 int ret;
48 (void)cls; /* Unused. Silent compiler warning. */ 50 (void) cls; /* Unused. Silent compiler warning. */
49 (void)url; /* Unused. Silent compiler warning. */ 51 (void) url; /* Unused. Silent compiler warning. */
50 (void)method; /* Unused. Silent compiler warning. */ 52 (void) method; /* Unused. Silent compiler warning. */
51 (void)version; /* Unused. Silent compiler warning. */ 53 (void) version; /* Unused. Silent compiler warning. */
52 (void)upload_data; /* Unused. Silent compiler warning. */ 54 (void) upload_data; /* Unused. Silent compiler warning. */
53 (void)upload_data_size; /* Unused. Silent compiler warning. */ 55 (void) upload_data_size; /* Unused. Silent compiler warning. */
54 (void)ptr; /* Unused. Silent compiler warning. */ 56 (void) ptr; /* Unused. Silent compiler warning. */
55 57
56 username = MHD_digest_auth_get_username(connection); 58 username = MHD_digest_auth_get_username (connection);
57 if (NULL == username) 59 if (NULL == username)
58 { 60 {
59 response = MHD_create_response_from_buffer(strlen (DENIED), 61 response = MHD_create_response_from_buffer (strlen (DENIED),
60 DENIED, 62 DENIED,
61 MHD_RESPMEM_PERSISTENT); 63 MHD_RESPMEM_PERSISTENT);
62 ret = MHD_queue_auth_fail_response(connection, realm, 64 ret = MHD_queue_auth_fail_response (connection, realm,
63 MY_OPAQUE_STR, 65 MY_OPAQUE_STR,
64 response, 66 response,
65 MHD_NO); 67 MHD_NO);
66 MHD_destroy_response(response); 68 MHD_destroy_response (response);
67 return ret; 69 return ret;
68 } 70 }
69 ret = MHD_digest_auth_check(connection, realm, 71 ret = MHD_digest_auth_check (connection, realm,
70 username, 72 username,
71 password, 73 password,
72 300); 74 300);
73 MHD_free (username); 75 MHD_free (username);
74 if ( (ret == MHD_INVALID_NONCE) || 76 if ( (ret == MHD_INVALID_NONCE) ||
75 (ret == MHD_NO) ) 77 (ret == MHD_NO) )
76 { 78 {
77 response = MHD_create_response_from_buffer(strlen (DENIED), 79 response = MHD_create_response_from_buffer (strlen (DENIED),
78 DENIED, 80 DENIED,
79 MHD_RESPMEM_PERSISTENT); 81 MHD_RESPMEM_PERSISTENT);
80 if (NULL == response) 82 if (NULL == response)
81 return MHD_NO; 83 return MHD_NO;
82 ret = MHD_queue_auth_fail_response(connection, realm, 84 ret = MHD_queue_auth_fail_response (connection, realm,
83 MY_OPAQUE_STR, 85 MY_OPAQUE_STR,
84 response, 86 response,
85 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); 87 (ret == MHD_INVALID_NONCE) ? MHD_YES :
86 MHD_destroy_response(response); 88 MHD_NO);
87 return ret; 89 MHD_destroy_response (response);
88 } 90 return ret;
89 response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, 91 }
90 MHD_RESPMEM_PERSISTENT); 92 response = MHD_create_response_from_buffer (strlen (PAGE), PAGE,
91 ret = MHD_queue_response(connection, MHD_HTTP_OK, response); 93 MHD_RESPMEM_PERSISTENT);
92 MHD_destroy_response(response); 94 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
95 MHD_destroy_response (response);
93 return ret; 96 return ret;
94} 97}
95 98
@@ -104,40 +107,41 @@ main (int argc, char *const *argv)
104 struct MHD_Daemon *d; 107 struct MHD_Daemon *d;
105 108
106 if (argc != 2) 109 if (argc != 2)
107 { 110 {
108 printf ("%s PORT\n", argv[0]); 111 printf ("%s PORT\n", argv[0]);
109 return 1; 112 return 1;
110 } 113 }
111 fd = open("/dev/urandom", O_RDONLY); 114 fd = open ("/dev/urandom", O_RDONLY);
112 if (-1 == fd) 115 if (-1 == fd)
113 { 116 {
114 fprintf (stderr, "Failed to open `%s': %s\n", 117 fprintf (stderr, "Failed to open `%s': %s\n",
115 "/dev/urandom", 118 "/dev/urandom",
116 strerror (errno)); 119 strerror (errno));
117 return 1; 120 return 1;
118 } 121 }
119 off = 0; 122 off = 0;
120 while (off < 8) 123 while (off < 8)
124 {
125 len = read (fd, rnd, 8);
126 if (len == -1)
121 { 127 {
122 len = read(fd, rnd, 8); 128 fprintf (stderr, "Failed to read `%s': %s\n",
123 if (len == -1) 129 "/dev/urandom",
124 { 130 strerror (errno));
125 fprintf (stderr, "Failed to read `%s': %s\n", 131 (void) close (fd);
126 "/dev/urandom", 132 return 1;
127 strerror (errno));
128 (void) close (fd);
129 return 1;
130 }
131 off += len;
132 } 133 }
133 (void) close(fd); 134 off += len;
134 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 135 }
136 (void) close (fd);
137 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
138 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
135 atoi (argv[1]), 139 atoi (argv[1]),
136 NULL, NULL, &ahc_echo, PAGE, 140 NULL, NULL, &ahc_echo, PAGE,
137 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd, 141 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd,
138 MHD_OPTION_NONCE_NC_SIZE, 300, 142 MHD_OPTION_NONCE_NC_SIZE, 300,
139 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 143 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
140 MHD_OPTION_END); 144 MHD_OPTION_END);
141 if (d == NULL) 145 if (d == NULL)
142 return 1; 146 return 1;
143 (void) getc (stdin); 147 (void) getc (stdin);