aboutsummaryrefslogtreecommitdiff
path: root/doc/examples
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-04-08 23:36:41 +0200
committerChristian Grothoff <christian@grothoff.org>2020-04-08 23:36:41 +0200
commitde872dea2d88f183c3f434c694acdcdb373f639c (patch)
treefb9084eb931c9789060e65ee3be3dc5adcb30e73 /doc/examples
parent6347f514aa2388e774d5bf356df8046864e5f73c (diff)
downloadlibmicrohttpd-de872dea2d88f183c3f434c694acdcdb373f639c.tar.gz
libmicrohttpd-de872dea2d88f183c3f434c694acdcdb373f639c.zip
define and use 'enum MHD_Result'
Diffstat (limited to 'doc/examples')
-rw-r--r--doc/examples/basicauthentication.c62
-rw-r--r--doc/examples/hellobrowser.c25
-rw-r--r--doc/examples/largepost.c302
-rw-r--r--doc/examples/logging.c18
-rw-r--r--doc/examples/responseheaders.c56
-rw-r--r--doc/examples/sessions.c561
-rw-r--r--doc/examples/simplepost.c156
-rw-r--r--doc/examples/tlsauthentication.c150
8 files changed, 672 insertions, 658 deletions
diff --git a/doc/examples/basicauthentication.c b/doc/examples/basicauthentication.c
index 88cb79b7..5d156f04 100644
--- a/doc/examples/basicauthentication.c
+++ b/doc/examples/basicauthentication.c
@@ -17,7 +17,7 @@
17#define PORT 8888 17#define PORT 8888
18 18
19 19
20static int 20static enum MHD_Result
21answer_to_connection (void *cls, struct MHD_Connection *connection, 21answer_to_connection (void *cls, struct MHD_Connection *connection,
22 const char *url, const char *method, 22 const char *url, const char *method,
23 const char *version, const char *upload_data, 23 const char *version, const char *upload_data,
@@ -28,45 +28,47 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
28 int fail; 28 int fail;
29 int ret; 29 int ret;
30 struct MHD_Response *response; 30 struct MHD_Response *response;
31 (void)cls; /* Unused. Silent compiler warning. */ 31 (void) cls; /* Unused. Silent compiler warning. */
32 (void)url; /* Unused. Silent compiler warning. */ 32 (void) url; /* Unused. Silent compiler warning. */
33 (void)version; /* Unused. Silent compiler warning. */ 33 (void) version; /* Unused. Silent compiler warning. */
34 (void)upload_data; /* Unused. Silent compiler warning. */ 34 (void) upload_data; /* Unused. Silent compiler warning. */
35 (void)upload_data_size; /* Unused. Silent compiler warning. */ 35 (void) upload_data_size; /* Unused. Silent compiler warning. */
36 36
37 if (0 != strcmp (method, "GET")) 37 if (0 != strcmp (method, "GET"))
38 return MHD_NO; 38 return MHD_NO;
39 if (NULL == *con_cls) 39 if (NULL == *con_cls)
40 { 40 {
41 *con_cls = connection; 41 *con_cls = connection;
42 return MHD_YES; 42 return MHD_YES;
43 } 43 }
44 pass = NULL; 44 pass = NULL;
45 user = MHD_basic_auth_get_username_password (connection, 45 user = MHD_basic_auth_get_username_password (connection,
46 &pass); 46 &pass);
47 fail = ( (NULL == user) || 47 fail = ( (NULL == user) ||
48 (0 != strcmp (user, "root")) || 48 (0 != strcmp (user, "root")) ||
49 (0 != strcmp (pass, "pa$$w0rd") ) ); 49 (0 != strcmp (pass, "pa$$w0rd") ) );
50 if (NULL != user) MHD_free (user); 50 if (NULL != user)
51 if (NULL != pass) MHD_free (pass); 51 MHD_free (user);
52 if (NULL != pass)
53 MHD_free (pass);
52 if (fail) 54 if (fail)
53 { 55 {
54 const char *page = "<html><body>Go away.</body></html>"; 56 const char *page = "<html><body>Go away.</body></html>";
55 response = 57 response =
56 MHD_create_response_from_buffer (strlen (page), (void *) page, 58 MHD_create_response_from_buffer (strlen (page), (void *) page,
57 MHD_RESPMEM_PERSISTENT); 59 MHD_RESPMEM_PERSISTENT);
58 ret = MHD_queue_basic_auth_fail_response (connection, 60 ret = MHD_queue_basic_auth_fail_response (connection,
59 "my realm", 61 "my realm",
60 response); 62 response);
61 } 63 }
62 else 64 else
63 { 65 {
64 const char *page = "<html><body>A secret.</body></html>"; 66 const char *page = "<html><body>A secret.</body></html>";
65 response = 67 response =
66 MHD_create_response_from_buffer (strlen (page), (void *) page, 68 MHD_create_response_from_buffer (strlen (page), (void *) page,
67 MHD_RESPMEM_PERSISTENT); 69 MHD_RESPMEM_PERSISTENT);
68 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 70 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
69 } 71 }
70 MHD_destroy_response (response); 72 MHD_destroy_response (response);
71 return ret; 73 return ret;
72} 74}
diff --git a/doc/examples/hellobrowser.c b/doc/examples/hellobrowser.c
index dce4ee6d..0c13c24d 100644
--- a/doc/examples/hellobrowser.c
+++ b/doc/examples/hellobrowser.c
@@ -14,7 +14,7 @@
14 14
15#define PORT 8888 15#define PORT 8888
16 16
17static int 17static enum MHD_Result
18answer_to_connection (void *cls, struct MHD_Connection *connection, 18answer_to_connection (void *cls, struct MHD_Connection *connection,
19 const char *url, const char *method, 19 const char *url, const char *method,
20 const char *version, const char *upload_data, 20 const char *version, const char *upload_data,
@@ -22,18 +22,18 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
22{ 22{
23 const char *page = "<html><body>Hello, browser!</body></html>"; 23 const char *page = "<html><body>Hello, browser!</body></html>";
24 struct MHD_Response *response; 24 struct MHD_Response *response;
25 int ret; 25 enum MHD_Result ret;
26 (void)cls; /* Unused. Silent compiler warning. */ 26 (void) cls; /* Unused. Silent compiler warning. */
27 (void)url; /* Unused. Silent compiler warning. */ 27 (void) url; /* Unused. Silent compiler warning. */
28 (void)method; /* Unused. Silent compiler warning. */ 28 (void) method; /* Unused. Silent compiler warning. */
29 (void)version; /* Unused. Silent compiler warning. */ 29 (void) version; /* Unused. Silent compiler warning. */
30 (void)upload_data; /* Unused. Silent compiler warning. */ 30 (void) upload_data; /* Unused. Silent compiler warning. */
31 (void)upload_data_size; /* Unused. Silent compiler warning. */ 31 (void) upload_data_size; /* Unused. Silent compiler warning. */
32 (void)con_cls; /* Unused. Silent compiler warning. */ 32 (void) con_cls; /* Unused. Silent compiler warning. */
33 33
34 response = 34 response =
35 MHD_create_response_from_buffer (strlen (page), (void *) page, 35 MHD_create_response_from_buffer (strlen (page), (void *) page,
36 MHD_RESPMEM_PERSISTENT); 36 MHD_RESPMEM_PERSISTENT);
37 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 37 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
38 MHD_destroy_response (response); 38 MHD_destroy_response (response);
39 39
@@ -46,7 +46,8 @@ main (void)
46{ 46{
47 struct MHD_Daemon *daemon; 47 struct MHD_Daemon *daemon;
48 48
49 daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL, 49 daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
50 PORT, NULL, NULL,
50 &answer_to_connection, NULL, MHD_OPTION_END); 51 &answer_to_connection, NULL, MHD_OPTION_END);
51 if (NULL == daemon) 52 if (NULL == daemon)
52 return 1; 53 return 1;
diff --git a/doc/examples/largepost.c b/doc/examples/largepost.c
index 8ef7c9fe..8618209f 100644
--- a/doc/examples/largepost.c
+++ b/doc/examples/largepost.c
@@ -15,11 +15,11 @@
15 15
16#ifdef _MSC_VER 16#ifdef _MSC_VER
17#ifndef strcasecmp 17#ifndef strcasecmp
18#define strcasecmp(a,b) _stricmp((a),(b)) 18#define strcasecmp(a,b) _stricmp ((a),(b))
19#endif /* !strcasecmp */ 19#endif /* !strcasecmp */
20#endif /* _MSC_VER */ 20#endif /* _MSC_VER */
21 21
22#if defined(_MSC_VER) && _MSC_VER+0 <= 1800 22#if defined(_MSC_VER) && _MSC_VER + 0 <= 1800
23/* Substitution is OK while return value is not used */ 23/* Substitution is OK while return value is not used */
24#define snprintf _snprintf 24#define snprintf _snprintf
25#endif 25#endif
@@ -29,10 +29,10 @@
29#define MAXCLIENTS 2 29#define MAXCLIENTS 2
30 30
31enum ConnectionType 31enum ConnectionType
32 { 32{
33 GET = 0, 33 GET = 0,
34 POST = 1 34 POST = 1
35 }; 35};
36 36
37static unsigned int nr_of_uploading_clients = 0; 37static unsigned int nr_of_uploading_clients = 0;
38 38
@@ -66,7 +66,8 @@ struct connection_info_struct
66}; 66};
67 67
68 68
69const char *askpage = "<html><body>\n\ 69const char *askpage =
70 "<html><body>\n\
70 Upload a file, please!<br>\n\ 71 Upload a file, please!<br>\n\
71 There are %u clients uploading at the moment.<br>\n\ 72 There are %u clients uploading at the moment.<br>\n\
72 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\ 73 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
@@ -85,23 +86,23 @@ const char *fileexistspage =
85 "<html><body>This file already exists.</body></html>"; 86 "<html><body>This file already exists.</body></html>";
86const char *fileioerror = 87const char *fileioerror =
87 "<html><body>IO error writing to disk.</body></html>"; 88 "<html><body>IO error writing to disk.</body></html>";
88const char* const postprocerror = 89const char*const postprocerror =
89 "<html><head><title>Error</title></head><body>Error processing POST data</body></html>"; 90 "<html><head><title>Error</title></head><body>Error processing POST data</body></html>";
90 91
91 92
92static int 93static enum MHD_Result
93send_page (struct MHD_Connection *connection, 94send_page (struct MHD_Connection *connection,
94 const char *page, 95 const char *page,
95 int status_code) 96 int status_code)
96{ 97{
97 int ret; 98 enum MHD_Result ret;
98 struct MHD_Response *response; 99 struct MHD_Response *response;
99 100
100 response = 101 response =
101 MHD_create_response_from_buffer (strlen (page), 102 MHD_create_response_from_buffer (strlen (page),
102 (void *) page, 103 (void *) page,
103 MHD_RESPMEM_MUST_COPY); 104 MHD_RESPMEM_MUST_COPY);
104 if (!response) 105 if (! response)
105 return MHD_NO; 106 return MHD_NO;
106 MHD_add_response_header (response, 107 MHD_add_response_header (response,
107 MHD_HTTP_HEADER_CONTENT_TYPE, 108 MHD_HTTP_HEADER_CONTENT_TYPE,
@@ -115,7 +116,7 @@ send_page (struct MHD_Connection *connection,
115} 116}
116 117
117 118
118static int 119static enum MHD_Result
119iterate_post (void *coninfo_cls, 120iterate_post (void *coninfo_cls,
120 enum MHD_ValueKind kind, 121 enum MHD_ValueKind kind,
121 const char *key, 122 const char *key,
@@ -128,50 +129,50 @@ iterate_post (void *coninfo_cls,
128{ 129{
129 struct connection_info_struct *con_info = coninfo_cls; 130 struct connection_info_struct *con_info = coninfo_cls;
130 FILE *fp; 131 FILE *fp;
131 (void)kind; /* Unused. Silent compiler warning. */ 132 (void) kind; /* Unused. Silent compiler warning. */
132 (void)content_type; /* Unused. Silent compiler warning. */ 133 (void) content_type; /* Unused. Silent compiler warning. */
133 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 134 (void) transfer_encoding; /* Unused. Silent compiler warning. */
134 (void)off; /* Unused. Silent compiler warning. */ 135 (void) off; /* Unused. Silent compiler warning. */
135 136
136 if (0 != strcmp (key, "file")) 137 if (0 != strcmp (key, "file"))
138 {
139 con_info->answerstring = servererrorpage;
140 con_info->answercode = MHD_HTTP_BAD_REQUEST;
141 return MHD_YES;
142 }
143
144 if (! con_info->fp)
145 {
146 if (0 != con_info->answercode) /* something went wrong */
147 return MHD_YES;
148 if (NULL != (fp = fopen (filename, "rb")))
137 { 149 {
138 con_info->answerstring = servererrorpage; 150 fclose (fp);
139 con_info->answercode = MHD_HTTP_BAD_REQUEST; 151 con_info->answerstring = fileexistspage;
152 con_info->answercode = MHD_HTTP_FORBIDDEN;
140 return MHD_YES; 153 return MHD_YES;
141 } 154 }
142 155 /* NOTE: This is technically a race with the 'fopen()' above,
143 if (! con_info->fp) 156 but there is no easy fix, short of moving to open(O_EXCL)
157 instead of using fopen(). For the example, we do not care. */
158 con_info->fp = fopen (filename, "ab");
159 if (! con_info->fp)
144 { 160 {
145 if (0 != con_info->answercode) /* something went wrong */ 161 con_info->answerstring = fileioerror;
146 return MHD_YES; 162 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
147 if (NULL != (fp = fopen (filename, "rb"))) 163 return MHD_YES;
148 {
149 fclose (fp);
150 con_info->answerstring = fileexistspage;
151 con_info->answercode = MHD_HTTP_FORBIDDEN;
152 return MHD_YES;
153 }
154 /* NOTE: This is technically a race with the 'fopen()' above,
155 but there is no easy fix, short of moving to open(O_EXCL)
156 instead of using fopen(). For the example, we do not care. */
157 con_info->fp = fopen (filename, "ab");
158 if (!con_info->fp)
159 {
160 con_info->answerstring = fileioerror;
161 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
162 return MHD_YES;
163 }
164 } 164 }
165 }
165 166
166 if (size > 0) 167 if (size > 0)
168 {
169 if (! fwrite (data, sizeof (char), size, con_info->fp))
167 { 170 {
168 if (! fwrite (data, sizeof (char), size, con_info->fp)) 171 con_info->answerstring = fileioerror;
169 { 172 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
170 con_info->answerstring = fileioerror; 173 return MHD_YES;
171 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
172 return MHD_YES;
173 }
174 } 174 }
175 }
175 176
176 return MHD_YES; 177 return MHD_YES;
177} 178}
@@ -184,31 +185,31 @@ request_completed (void *cls,
184 enum MHD_RequestTerminationCode toe) 185 enum MHD_RequestTerminationCode toe)
185{ 186{
186 struct connection_info_struct *con_info = *con_cls; 187 struct connection_info_struct *con_info = *con_cls;
187 (void)cls; /* Unused. Silent compiler warning. */ 188 (void) cls; /* Unused. Silent compiler warning. */
188 (void)connection; /* Unused. Silent compiler warning. */ 189 (void) connection; /* Unused. Silent compiler warning. */
189 (void)toe; /* Unused. Silent compiler warning. */ 190 (void) toe; /* Unused. Silent compiler warning. */
190 191
191 if (NULL == con_info) 192 if (NULL == con_info)
192 return; 193 return;
193 194
194 if (con_info->connectiontype == POST) 195 if (con_info->connectiontype == POST)
196 {
197 if (NULL != con_info->postprocessor)
195 { 198 {
196 if (NULL != con_info->postprocessor) 199 MHD_destroy_post_processor (con_info->postprocessor);
197 { 200 nr_of_uploading_clients--;
198 MHD_destroy_post_processor (con_info->postprocessor);
199 nr_of_uploading_clients--;
200 }
201
202 if (con_info->fp)
203 fclose (con_info->fp);
204 } 201 }
205 202
203 if (con_info->fp)
204 fclose (con_info->fp);
205 }
206
206 free (con_info); 207 free (con_info);
207 *con_cls = NULL; 208 *con_cls = NULL;
208} 209}
209 210
210 211
211static int 212static enum MHD_Result
212answer_to_connection (void *cls, 213answer_to_connection (void *cls,
213 struct MHD_Connection *connection, 214 struct MHD_Connection *connection,
214 const char *url, 215 const char *url,
@@ -218,109 +219,109 @@ answer_to_connection (void *cls,
218 size_t *upload_data_size, 219 size_t *upload_data_size,
219 void **con_cls) 220 void **con_cls)
220{ 221{
221 (void)cls; /* Unused. Silent compiler warning. */ 222 (void) cls; /* Unused. Silent compiler warning. */
222 (void)url; /* Unused. Silent compiler warning. */ 223 (void) url; /* Unused. Silent compiler warning. */
223 (void)version; /* Unused. Silent compiler warning. */ 224 (void) version; /* Unused. Silent compiler warning. */
224 225
225 if (NULL == *con_cls) 226 if (NULL == *con_cls)
226 { 227 {
227 /* First call, setup data structures */ 228 /* First call, setup data structures */
228 struct connection_info_struct *con_info; 229 struct connection_info_struct *con_info;
230
231 if (nr_of_uploading_clients >= MAXCLIENTS)
232 return send_page (connection,
233 busypage,
234 MHD_HTTP_SERVICE_UNAVAILABLE);
229 235
230 if (nr_of_uploading_clients >= MAXCLIENTS) 236 con_info = malloc (sizeof (struct connection_info_struct));
231 return send_page (connection, 237 if (NULL == con_info)
232 busypage, 238 return MHD_NO;
233 MHD_HTTP_SERVICE_UNAVAILABLE); 239 con_info->answercode = 0; /* none yet */
240 con_info->fp = NULL;
234 241
235 con_info = malloc (sizeof (struct connection_info_struct)); 242 if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
236 if (NULL == con_info) 243 {
244 con_info->postprocessor =
245 MHD_create_post_processor (connection,
246 POSTBUFFERSIZE,
247 &iterate_post,
248 (void *) con_info);
249
250 if (NULL == con_info->postprocessor)
251 {
252 free (con_info);
237 return MHD_NO; 253 return MHD_NO;
238 con_info->answercode = 0; /* none yet */ 254 }
239 con_info->fp = NULL;
240 255
241 if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST)) 256 nr_of_uploading_clients++;
242 {
243 con_info->postprocessor =
244 MHD_create_post_processor (connection,
245 POSTBUFFERSIZE,
246 &iterate_post,
247 (void *) con_info);
248 257
249 if (NULL == con_info->postprocessor) 258 con_info->connectiontype = POST;
250 { 259 }
251 free (con_info); 260 else
252 return MHD_NO; 261 {
253 } 262 con_info->connectiontype = GET;
263 }
254 264
255 nr_of_uploading_clients++; 265 *con_cls = (void *) con_info;
256 266
257 con_info->connectiontype = POST; 267 return MHD_YES;
258 } 268 }
259 else
260 {
261 con_info->connectiontype = GET;
262 }
263 269
264 *con_cls = (void *) con_info; 270 if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET))
271 {
272 /* We just return the standard form for uploads on all GET requests */
273 char buffer[1024];
274
275 snprintf (buffer,
276 sizeof (buffer),
277 askpage,
278 nr_of_uploading_clients);
279 return send_page (connection,
280 buffer,
281 MHD_HTTP_OK);
282 }
265 283
266 return MHD_YES; 284 if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
267 } 285 {
286 struct connection_info_struct *con_info = *con_cls;
268 287
269 if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET)) 288 if (0 != *upload_data_size)
270 { 289 {
271 /* We just return the standard form for uploads on all GET requests */ 290 /* Upload not yet done */
272 char buffer[1024]; 291 if (0 != con_info->answercode)
292 {
293 /* we already know the answer, skip rest of upload */
294 *upload_data_size = 0;
295 return MHD_YES;
296 }
297 if (MHD_YES !=
298 MHD_post_process (con_info->postprocessor,
299 upload_data,
300 *upload_data_size))
301 {
302 con_info->answerstring = postprocerror;
303 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
304 }
305 *upload_data_size = 0;
273 306
274 snprintf (buffer, 307 return MHD_YES;
275 sizeof (buffer),
276 askpage,
277 nr_of_uploading_clients);
278 return send_page (connection,
279 buffer,
280 MHD_HTTP_OK);
281 } 308 }
282 309 /* Upload finished */
283 if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST)) 310 if (NULL != con_info->fp)
284 { 311 {
285 struct connection_info_struct *con_info = *con_cls; 312 fclose (con_info->fp);
286 313 con_info->fp = NULL;
287 if (0 != *upload_data_size)
288 {
289 /* Upload not yet done */
290 if (0 != con_info->answercode)
291 {
292 /* we already know the answer, skip rest of upload */
293 *upload_data_size = 0;
294 return MHD_YES;
295 }
296 if (MHD_YES !=
297 MHD_post_process (con_info->postprocessor,
298 upload_data,
299 *upload_data_size))
300 {
301 con_info->answerstring = postprocerror;
302 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
303 }
304 *upload_data_size = 0;
305
306 return MHD_YES;
307 }
308 /* Upload finished */
309 if (NULL != con_info->fp)
310 {
311 fclose (con_info->fp);
312 con_info->fp = NULL;
313 }
314 if (0 == con_info->answercode)
315 {
316 /* No errors encountered, declare success */
317 con_info->answerstring = completepage;
318 con_info->answercode = MHD_HTTP_OK;
319 }
320 return send_page (connection,
321 con_info->answerstring,
322 con_info->answercode);
323 } 314 }
315 if (0 == con_info->answercode)
316 {
317 /* No errors encountered, declare success */
318 con_info->answerstring = completepage;
319 con_info->answercode = MHD_HTTP_OK;
320 }
321 return send_page (connection,
322 con_info->answerstring,
323 con_info->answercode);
324 }
324 325
325 /* Note a GET or a POST, generate error */ 326 /* Note a GET or a POST, generate error */
326 return send_page (connection, 327 return send_page (connection,
@@ -337,14 +338,15 @@ main ()
337 daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD, 338 daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD,
338 PORT, NULL, NULL, 339 PORT, NULL, NULL,
339 &answer_to_connection, NULL, 340 &answer_to_connection, NULL,
340 MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL, 341 MHD_OPTION_NOTIFY_COMPLETED, &request_completed,
342 NULL,
341 MHD_OPTION_END); 343 MHD_OPTION_END);
342 if (NULL == daemon) 344 if (NULL == daemon)
343 { 345 {
344 fprintf (stderr, 346 fprintf (stderr,
345 "Failed to start daemon\n"); 347 "Failed to start daemon\n");
346 return 1; 348 return 1;
347 } 349 }
348 (void) getchar (); 350 (void) getchar ();
349 MHD_stop_daemon (daemon); 351 MHD_stop_daemon (daemon);
350 return 0; 352 return 0;
diff --git a/doc/examples/logging.c b/doc/examples/logging.c
index 239fbe7d..22ff7e62 100644
--- a/doc/examples/logging.c
+++ b/doc/examples/logging.c
@@ -14,28 +14,28 @@
14#define PORT 8888 14#define PORT 8888
15 15
16 16
17static int 17static enum MHD_Result
18print_out_key (void *cls, enum MHD_ValueKind kind, const char *key, 18print_out_key (void *cls, enum MHD_ValueKind kind, const char *key,
19 const char *value) 19 const char *value)
20{ 20{
21 (void)cls; /* Unused. Silent compiler warning. */ 21 (void) cls; /* Unused. Silent compiler warning. */
22 (void)kind; /* Unused. Silent compiler warning. */ 22 (void) kind; /* Unused. Silent compiler warning. */
23 printf ("%s: %s\n", key, value); 23 printf ("%s: %s\n", key, value);
24 return MHD_YES; 24 return MHD_YES;
25} 25}
26 26
27 27
28static int 28static enum MHD_Result
29answer_to_connection (void *cls, struct MHD_Connection *connection, 29answer_to_connection (void *cls, struct MHD_Connection *connection,
30 const char *url, const char *method, 30 const char *url, const char *method,
31 const char *version, const char *upload_data, 31 const char *version, const char *upload_data,
32 size_t *upload_data_size, void **con_cls) 32 size_t *upload_data_size, void **con_cls)
33{ 33{
34 (void)cls; /* Unused. Silent compiler warning. */ 34 (void) cls; /* Unused. Silent compiler warning. */
35 (void)version; /* Unused. Silent compiler warning. */ 35 (void) version; /* Unused. Silent compiler warning. */
36 (void)upload_data; /* Unused. Silent compiler warning. */ 36 (void) upload_data; /* Unused. Silent compiler warning. */
37 (void)upload_data_size; /* Unused. Silent compiler warning. */ 37 (void) upload_data_size; /* Unused. Silent compiler warning. */
38 (void)con_cls; /* Unused. Silent compiler warning. */ 38 (void) con_cls; /* Unused. Silent compiler warning. */
39 printf ("New %s request for %s using version %s\n", method, url, version); 39 printf ("New %s request for %s using version %s\n", method, url, version);
40 40
41 MHD_get_connection_values (connection, MHD_HEADER_KIND, print_out_key, 41 MHD_get_connection_values (connection, MHD_HEADER_KIND, print_out_key,
diff --git a/doc/examples/responseheaders.c b/doc/examples/responseheaders.c
index 0f459c2e..5c0580b6 100644
--- a/doc/examples/responseheaders.c
+++ b/doc/examples/responseheaders.c
@@ -19,7 +19,7 @@
19#define FILENAME "picture.png" 19#define FILENAME "picture.png"
20#define MIMETYPE "image/png" 20#define MIMETYPE "image/png"
21 21
22static int 22static enum MHD_Result
23answer_to_connection (void *cls, struct MHD_Connection *connection, 23answer_to_connection (void *cls, struct MHD_Connection *connection,
24 const char *url, const char *method, 24 const char *url, const char *method,
25 const char *version, const char *upload_data, 25 const char *version, const char *upload_data,
@@ -27,43 +27,43 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
27{ 27{
28 struct MHD_Response *response; 28 struct MHD_Response *response;
29 int fd; 29 int fd;
30 int ret; 30 enum MHD_Result ret;
31 struct stat sbuf; 31 struct stat sbuf;
32 (void)cls; /* Unused. Silent compiler warning. */ 32 (void) cls; /* Unused. Silent compiler warning. */
33 (void)url; /* Unused. Silent compiler warning. */ 33 (void) url; /* Unused. Silent compiler warning. */
34 (void)version; /* Unused. Silent compiler warning. */ 34 (void) version; /* Unused. Silent compiler warning. */
35 (void)upload_data; /* Unused. Silent compiler warning. */ 35 (void) upload_data; /* Unused. Silent compiler warning. */
36 (void)upload_data_size; /* Unused. Silent compiler warning. */ 36 (void) upload_data_size; /* Unused. Silent compiler warning. */
37 (void)con_cls; /* Unused. Silent compiler warning. */ 37 (void) con_cls; /* Unused. Silent compiler warning. */
38 38
39 if (0 != strcmp (method, "GET")) 39 if (0 != strcmp (method, "GET"))
40 return MHD_NO; 40 return MHD_NO;
41 41
42 if ( (-1 == (fd = open (FILENAME, O_RDONLY))) || 42 if ( (-1 == (fd = open (FILENAME, O_RDONLY))) ||
43 (0 != fstat (fd, &sbuf)) ) 43 (0 != fstat (fd, &sbuf)) )
44 { 44 {
45 const char *errorstr = 45 const char *errorstr =
46 "<html><body>An internal server error has occured!\ 46 "<html><body>An internal server error has occured!\
47 </body></html>"; 47 </body></html>";
48 /* error accessing file */ 48 /* error accessing file */
49 if (fd != -1) 49 if (fd != -1)
50 (void) close (fd); 50 (void) close (fd);
51 response = 51 response =
52 MHD_create_response_from_buffer (strlen (errorstr), 52 MHD_create_response_from_buffer (strlen (errorstr),
53 (void *) errorstr, 53 (void *) errorstr,
54 MHD_RESPMEM_PERSISTENT); 54 MHD_RESPMEM_PERSISTENT);
55 if (NULL != response) 55 if (NULL != response)
56 { 56 {
57 ret = 57 ret =
58 MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, 58 MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
59 response); 59 response);
60 MHD_destroy_response (response); 60 MHD_destroy_response (response);
61 61
62 return ret; 62 return ret;
63 }
64 else
65 return MHD_NO;
66 } 63 }
64 else
65 return MHD_NO;
66 }
67 response = 67 response =
68 MHD_create_response_from_fd_at_offset64 (sbuf.st_size, fd, 0); 68 MHD_create_response_from_fd_at_offset64 (sbuf.st_size, fd, 0);
69 MHD_add_response_header (response, "Content-Type", MIMETYPE); 69 MHD_add_response_header (response, "Content-Type", MIMETYPE);
diff --git a/doc/examples/sessions.c b/doc/examples/sessions.c
index b5b25440..adabc610 100644
--- a/doc/examples/sessions.c
+++ b/doc/examples/sessions.c
@@ -11,7 +11,7 @@
11#include <time.h> 11#include <time.h>
12#include <microhttpd.h> 12#include <microhttpd.h>
13 13
14#if defined _WIN32 && !defined(__MINGW64_VERSION_MAJOR) 14#if defined _WIN32 && ! defined(__MINGW64_VERSION_MAJOR)
15static int 15static int
16asprintf (char **resultp, const char *format, ...) 16asprintf (char **resultp, const char *format, ...)
17{ 17{
@@ -32,7 +32,7 @@ asprintf (char **resultp, const char *format, ...)
32 if (result != NULL) 32 if (result != NULL)
33 { 33 {
34 int len2 = _vscprintf ((char *) format, argptr); 34 int len2 = _vscprintf ((char *) format, argptr);
35 if (len2 != len - 1 || len2 <= 0) 35 if ((len2 != len - 1) || (len2 <= 0))
36 { 36 {
37 free (result); 37 free (result);
38 result = NULL; 38 result = NULL;
@@ -49,37 +49,45 @@ asprintf (char **resultp, const char *format, ...)
49 va_end (argptr); 49 va_end (argptr);
50 return len; 50 return len;
51} 51}
52
53
52#endif 54#endif
53 55
54/** 56/**
55 * Invalid method page. 57 * Invalid method page.
56 */ 58 */
57#define METHOD_ERROR "<html><head><title>Illegal request</title></head><body>Go away.</body></html>" 59#define METHOD_ERROR \
60 "<html><head><title>Illegal request</title></head><body>Go away.</body></html>"
58 61
59/** 62/**
60 * Invalid URL page. 63 * Invalid URL page.
61 */ 64 */
62#define NOT_FOUND_ERROR "<html><head><title>Not found</title></head><body>Go away.</body></html>" 65#define NOT_FOUND_ERROR \
66 "<html><head><title>Not found</title></head><body>Go away.</body></html>"
63 67
64/** 68/**
65 * Front page. (/) 69 * Front page. (/)
66 */ 70 */
67#define MAIN_PAGE "<html><head><title>Welcome</title></head><body><form action=\"/2\" method=\"post\">What is your name? <input type=\"text\" name=\"v1\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>" 71#define MAIN_PAGE \
72 "<html><head><title>Welcome</title></head><body><form action=\"/2\" method=\"post\">What is your name? <input type=\"text\" name=\"v1\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
68 73
69/** 74/**
70 * Second page. (/2) 75 * Second page. (/2)
71 */ 76 */
72#define SECOND_PAGE "<html><head><title>Tell me more</title></head><body><a href=\"/\">previous</a> <form action=\"/S\" method=\"post\">%s, what is your job? <input type=\"text\" name=\"v2\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>" 77#define SECOND_PAGE \
78 "<html><head><title>Tell me more</title></head><body><a href=\"/\">previous</a> <form action=\"/S\" method=\"post\">%s, what is your job? <input type=\"text\" name=\"v2\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
73 79
74/** 80/**
75 * Second page (/S) 81 * Second page (/S)
76 */ 82 */
77#define SUBMIT_PAGE "<html><head><title>Ready to submit?</title></head><body><form action=\"/F\" method=\"post\"><a href=\"/2\">previous </a> <input type=\"hidden\" name=\"DONE\" value=\"yes\" /><input type=\"submit\" value=\"Submit\" /></body></html>" 83#define SUBMIT_PAGE \
84 "<html><head><title>Ready to submit?</title></head><body><form action=\"/F\" method=\"post\"><a href=\"/2\">previous </a> <input type=\"hidden\" name=\"DONE\" value=\"yes\" /><input type=\"submit\" value=\"Submit\" /></body></html>"
78 85
79/** 86/**
80 * Last page. 87 * Last page.
81 */ 88 */
82#define LAST_PAGE "<html><head><title>Thank you</title></head><body>Thank you.</body></html>" 89#define LAST_PAGE \
90 "<html><head><title>Thank you</title></head><body>Thank you.</body></html>"
83 91
84/** 92/**
85 * Name of our cookie. 93 * Name of our cookie.
@@ -159,8 +167,6 @@ struct Request
159static struct Session *sessions; 167static struct Session *sessions;
160 168
161 169
162
163
164/** 170/**
165 * Return the session handle for this connection, or 171 * Return the session handle for this connection, or
166 * create one if this is a new user. 172 * create one if this is a new user.
@@ -172,40 +178,40 @@ get_session (struct MHD_Connection *connection)
172 const char *cookie; 178 const char *cookie;
173 179
174 cookie = MHD_lookup_connection_value (connection, 180 cookie = MHD_lookup_connection_value (connection,
175 MHD_COOKIE_KIND, 181 MHD_COOKIE_KIND,
176 COOKIE_NAME); 182 COOKIE_NAME);
177 if (cookie != NULL) 183 if (cookie != NULL)
184 {
185 /* find existing session */
186 ret = sessions;
187 while (NULL != ret)
178 { 188 {
179 /* find existing session */ 189 if (0 == strcmp (cookie, ret->sid))
180 ret = sessions; 190 break;
181 while (NULL != ret) 191 ret = ret->next;
182 {
183 if (0 == strcmp (cookie, ret->sid))
184 break;
185 ret = ret->next;
186 }
187 if (NULL != ret)
188 {
189 ret->rc++;
190 return ret;
191 }
192 } 192 }
193 if (NULL != ret)
194 {
195 ret->rc++;
196 return ret;
197 }
198 }
193 /* create fresh session */ 199 /* create fresh session */
194 ret = calloc (1, sizeof (struct Session)); 200 ret = calloc (1, sizeof (struct Session));
195 if (NULL == ret) 201 if (NULL == ret)
196 { 202 {
197 fprintf (stderr, "calloc error: %s\n", strerror (errno)); 203 fprintf (stderr, "calloc error: %s\n", strerror (errno));
198 return NULL; 204 return NULL;
199 } 205 }
200 /* not a super-secure way to generate a random session ID, 206 /* not a super-secure way to generate a random session ID,
201 but should do for a simple example... */ 207 but should do for a simple example... */
202 snprintf (ret->sid, 208 snprintf (ret->sid,
203 sizeof (ret->sid), 209 sizeof (ret->sid),
204 "%X%X%X%X", 210 "%X%X%X%X",
205 (unsigned int) rand (), 211 (unsigned int) rand (),
206 (unsigned int) rand (), 212 (unsigned int) rand (),
207 (unsigned int) rand (), 213 (unsigned int) rand (),
208 (unsigned int) rand ()); 214 (unsigned int) rand ());
209 ret->rc++; 215 ret->rc++;
210 ret->start = time (NULL); 216 ret->start = time (NULL);
211 ret->next = sessions; 217 ret->next = sessions;
@@ -221,12 +227,12 @@ get_session (struct MHD_Connection *connection)
221 * @param mime mime type to use 227 * @param mime mime type to use
222 * @param session session information 228 * @param session session information
223 * @param connection connection to process 229 * @param connection connection to process
224 * @param MHD_YES on success, MHD_NO on failure 230 * @param #MHD_YES on success, #MHD_NO on failure
225 */ 231 */
226typedef int (*PageHandler)(const void *cls, 232typedef enum MHD_Result (*PageHandler)(const void *cls,
227 const char *mime, 233 const char *mime,
228 struct Session *session, 234 struct Session *session,
229 struct MHD_Connection *connection); 235 struct MHD_Connection *connection);
230 236
231 237
232/** 238/**
@@ -264,22 +270,22 @@ struct Page
264 */ 270 */
265static void 271static void
266add_session_cookie (struct Session *session, 272add_session_cookie (struct Session *session,
267 struct MHD_Response *response) 273 struct MHD_Response *response)
268{ 274{
269 char cstr[256]; 275 char cstr[256];
270 snprintf (cstr, 276 snprintf (cstr,
271 sizeof (cstr), 277 sizeof (cstr),
272 "%s=%s", 278 "%s=%s",
273 COOKIE_NAME, 279 COOKIE_NAME,
274 session->sid); 280 session->sid);
275 if (MHD_NO == 281 if (MHD_NO ==
276 MHD_add_response_header (response, 282 MHD_add_response_header (response,
277 MHD_HTTP_HEADER_SET_COOKIE, 283 MHD_HTTP_HEADER_SET_COOKIE,
278 cstr)) 284 cstr))
279 { 285 {
280 fprintf (stderr, 286 fprintf (stderr,
281 "Failed to set session cookie header!\n"); 287 "Failed to set session cookie header!\n");
282 } 288 }
283} 289}
284 290
285 291
@@ -292,27 +298,27 @@ add_session_cookie (struct Session *session,
292 * @param session session handle 298 * @param session session handle
293 * @param connection connection to use 299 * @param connection connection to use
294 */ 300 */
295static int 301static enum MHD_Result
296serve_simple_form (const void *cls, 302serve_simple_form (const void *cls,
297 const char *mime, 303 const char *mime,
298 struct Session *session, 304 struct Session *session,
299 struct MHD_Connection *connection) 305 struct MHD_Connection *connection)
300{ 306{
301 int ret; 307 enum MHD_Result ret;
302 const char *form = cls; 308 const char *form = cls;
303 struct MHD_Response *response; 309 struct MHD_Response *response;
304 310
305 /* return static form */ 311 /* return static form */
306 response = MHD_create_response_from_buffer (strlen (form), 312 response = MHD_create_response_from_buffer (strlen (form),
307 (void *) form, 313 (void *) form,
308 MHD_RESPMEM_PERSISTENT); 314 MHD_RESPMEM_PERSISTENT);
309 add_session_cookie (session, response); 315 add_session_cookie (session, response);
310 MHD_add_response_header (response, 316 MHD_add_response_header (response,
311 MHD_HTTP_HEADER_CONTENT_ENCODING, 317 MHD_HTTP_HEADER_CONTENT_ENCODING,
312 mime); 318 mime);
313 ret = MHD_queue_response (connection, 319 ret = MHD_queue_response (connection,
314 MHD_HTTP_OK, 320 MHD_HTTP_OK,
315 response); 321 response);
316 MHD_destroy_response (response); 322 MHD_destroy_response (response);
317 return ret; 323 return ret;
318} 324}
@@ -326,35 +332,35 @@ serve_simple_form (const void *cls,
326 * @param session session handle 332 * @param session session handle
327 * @param connection connection to use 333 * @param connection connection to use
328 */ 334 */
329static int 335static enum MHD_Result
330fill_v1_form (const void *cls, 336fill_v1_form (const void *cls,
331 const char *mime, 337 const char *mime,
332 struct Session *session, 338 struct Session *session,
333 struct MHD_Connection *connection) 339 struct MHD_Connection *connection)
334{ 340{
335 int ret; 341 enum MHD_Result ret;
336 const char *form = cls; 342 const char *form = cls;
337 char *reply; 343 char *reply;
338 struct MHD_Response *response; 344 struct MHD_Response *response;
339 345
340 if (-1 == asprintf (&reply, 346 if (-1 == asprintf (&reply,
341 form, 347 form,
342 session->value_1)) 348 session->value_1))
343 { 349 {
344 /* oops */ 350 /* oops */
345 return MHD_NO; 351 return MHD_NO;
346 } 352 }
347 /* return static form */ 353 /* return static form */
348 response = MHD_create_response_from_buffer (strlen (reply), 354 response = MHD_create_response_from_buffer (strlen (reply),
349 (void *) reply, 355 (void *) reply,
350 MHD_RESPMEM_MUST_FREE); 356 MHD_RESPMEM_MUST_FREE);
351 add_session_cookie (session, response); 357 add_session_cookie (session, response);
352 MHD_add_response_header (response, 358 MHD_add_response_header (response,
353 MHD_HTTP_HEADER_CONTENT_ENCODING, 359 MHD_HTTP_HEADER_CONTENT_ENCODING,
354 mime); 360 mime);
355 ret = MHD_queue_response (connection, 361 ret = MHD_queue_response (connection,
356 MHD_HTTP_OK, 362 MHD_HTTP_OK,
357 response); 363 response);
358 MHD_destroy_response (response); 364 MHD_destroy_response (response);
359 return ret; 365 return ret;
360} 366}
@@ -368,36 +374,36 @@ fill_v1_form (const void *cls,
368 * @param session session handle 374 * @param session session handle
369 * @param connection connection to use 375 * @param connection connection to use
370 */ 376 */
371static int 377static enum MHD_Result
372fill_v1_v2_form (const void *cls, 378fill_v1_v2_form (const void *cls,
373 const char *mime, 379 const char *mime,
374 struct Session *session, 380 struct Session *session,
375 struct MHD_Connection *connection) 381 struct MHD_Connection *connection)
376{ 382{
377 int ret; 383 enum MHD_Result ret;
378 const char *form = cls; 384 const char *form = cls;
379 char *reply; 385 char *reply;
380 struct MHD_Response *response; 386 struct MHD_Response *response;
381 387
382 if (-1 == asprintf (&reply, 388 if (-1 == asprintf (&reply,
383 form, 389 form,
384 session->value_1, 390 session->value_1,
385 session->value_2)) 391 session->value_2))
386 { 392 {
387 /* oops */ 393 /* oops */
388 return MHD_NO; 394 return MHD_NO;
389 } 395 }
390 /* return static form */ 396 /* return static form */
391 response = MHD_create_response_from_buffer (strlen (reply), 397 response = MHD_create_response_from_buffer (strlen (reply),
392 (void *) reply, 398 (void *) reply,
393 MHD_RESPMEM_MUST_FREE); 399 MHD_RESPMEM_MUST_FREE);
394 add_session_cookie (session, response); 400 add_session_cookie (session, response);
395 MHD_add_response_header (response, 401 MHD_add_response_header (response,
396 MHD_HTTP_HEADER_CONTENT_ENCODING, 402 MHD_HTTP_HEADER_CONTENT_ENCODING,
397 mime); 403 mime);
398 ret = MHD_queue_response (connection, 404 ret = MHD_queue_response (connection,
399 MHD_HTTP_OK, 405 MHD_HTTP_OK,
400 response); 406 response);
401 MHD_destroy_response (response); 407 MHD_destroy_response (response);
402 return ret; 408 return ret;
403} 409}
@@ -411,27 +417,27 @@ fill_v1_v2_form (const void *cls,
411 * @param session session handle 417 * @param session session handle
412 * @param connection connection to use 418 * @param connection connection to use
413 */ 419 */
414static int 420static enum MHD_Result
415not_found_page (const void *cls, 421not_found_page (const void *cls,
416 const char *mime, 422 const char *mime,
417 struct Session *session, 423 struct Session *session,
418 struct MHD_Connection *connection) 424 struct MHD_Connection *connection)
419{ 425{
420 int ret; 426 enum MHD_Result ret;
421 struct MHD_Response *response; 427 struct MHD_Response *response;
422 (void)cls; /* Unused. Silent compiler warning. */ 428 (void) cls; /* Unused. Silent compiler warning. */
423 (void)session; /* Unused. Silent compiler warning. */ 429 (void) session; /* Unused. Silent compiler warning. */
424 430
425 /* unsupported HTTP method */ 431 /* unsupported HTTP method */
426 response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR), 432 response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR),
427 (void *) NOT_FOUND_ERROR, 433 (void *) NOT_FOUND_ERROR,
428 MHD_RESPMEM_PERSISTENT); 434 MHD_RESPMEM_PERSISTENT);
429 ret = MHD_queue_response (connection, 435 ret = MHD_queue_response (connection,
430 MHD_HTTP_NOT_FOUND, 436 MHD_HTTP_NOT_FOUND,
431 response); 437 response);
432 MHD_add_response_header (response, 438 MHD_add_response_header (response,
433 MHD_HTTP_HEADER_CONTENT_ENCODING, 439 MHD_HTTP_HEADER_CONTENT_ENCODING,
434 mime); 440 mime);
435 MHD_destroy_response (response); 441 MHD_destroy_response (response);
436 return ret; 442 return ret;
437} 443}
@@ -440,15 +446,13 @@ not_found_page (const void *cls,
440/** 446/**
441 * List of all pages served by this HTTP server. 447 * List of all pages served by this HTTP server.
442 */ 448 */
443static struct Page pages[] = 449static struct Page pages[] = {
444 { 450 { "/", "text/html", &fill_v1_form, MAIN_PAGE },
445 { "/", "text/html", &fill_v1_form, MAIN_PAGE }, 451 { "/2", "text/html", &fill_v1_v2_form, SECOND_PAGE },
446 { "/2", "text/html", &fill_v1_v2_form, SECOND_PAGE }, 452 { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE },
447 { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE }, 453 { "/F", "text/html", &serve_simple_form, LAST_PAGE },
448 { "/F", "text/html", &serve_simple_form, LAST_PAGE }, 454 { NULL, NULL, &not_found_page, NULL } /* 404 */
449 { NULL, NULL, &not_found_page, NULL } /* 404 */ 455};
450 };
451
452 456
453 457
454/** 458/**
@@ -470,53 +474,53 @@ static struct Page pages[] =
470 * @return MHD_YES to continue iterating, 474 * @return MHD_YES to continue iterating,
471 * MHD_NO to abort the iteration 475 * MHD_NO to abort the iteration
472 */ 476 */
473static int 477static enum MHD_Result
474post_iterator (void *cls, 478post_iterator (void *cls,
475 enum MHD_ValueKind kind, 479 enum MHD_ValueKind kind,
476 const char *key, 480 const char *key,
477 const char *filename, 481 const char *filename,
478 const char *content_type, 482 const char *content_type,
479 const char *transfer_encoding, 483 const char *transfer_encoding,
480 const char *data, uint64_t off, size_t size) 484 const char *data, uint64_t off, size_t size)
481{ 485{
482 struct Request *request = cls; 486 struct Request *request = cls;
483 struct Session *session = request->session; 487 struct Session *session = request->session;
484 (void)kind; /* Unused. Silent compiler warning. */ 488 (void) kind; /* Unused. Silent compiler warning. */
485 (void)filename; /* Unused. Silent compiler warning. */ 489 (void) filename; /* Unused. Silent compiler warning. */
486 (void)content_type; /* Unused. Silent compiler warning. */ 490 (void) content_type; /* Unused. Silent compiler warning. */
487 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 491 (void) transfer_encoding; /* Unused. Silent compiler warning. */
488 492
489 if (0 == strcmp ("DONE", key)) 493 if (0 == strcmp ("DONE", key))
490 { 494 {
491 fprintf (stdout, 495 fprintf (stdout,
492 "Session `%s' submitted `%s', `%s'\n", 496 "Session `%s' submitted `%s', `%s'\n",
493 session->sid, 497 session->sid,
494 session->value_1, 498 session->value_1,
495 session->value_2); 499 session->value_2);
496 return MHD_YES; 500 return MHD_YES;
497 } 501 }
498 if (0 == strcmp ("v1", key)) 502 if (0 == strcmp ("v1", key))
499 { 503 {
500 if (size + off > sizeof(session->value_1)) 504 if (size + off > sizeof(session->value_1))
501 size = sizeof (session->value_1) - off; 505 size = sizeof (session->value_1) - off;
502 memcpy (&session->value_1[off], 506 memcpy (&session->value_1[off],
503 data, 507 data,
504 size); 508 size);
505 if (size + off < sizeof (session->value_1)) 509 if (size + off < sizeof (session->value_1))
506 session->value_1[size+off] = '\0'; 510 session->value_1[size + off] = '\0';
507 return MHD_YES; 511 return MHD_YES;
508 } 512 }
509 if (0 == strcmp ("v2", key)) 513 if (0 == strcmp ("v2", key))
510 { 514 {
511 if (size + off > sizeof(session->value_2)) 515 if (size + off > sizeof(session->value_2))
512 size = sizeof (session->value_2) - off; 516 size = sizeof (session->value_2) - off;
513 memcpy (&session->value_2[off], 517 memcpy (&session->value_2[off],
514 data, 518 data,
515 size); 519 size);
516 if (size + off < sizeof (session->value_2)) 520 if (size + off < sizeof (session->value_2))
517 session->value_2[size+off] = '\0'; 521 session->value_2[size + off] = '\0';
518 return MHD_YES; 522 return MHD_YES;
519 } 523 }
520 fprintf (stderr, "Unsupported form value `%s'\n", key); 524 fprintf (stderr, "Unsupported form value `%s'\n", key);
521 return MHD_YES; 525 return MHD_YES;
522} 526}
@@ -556,101 +560,101 @@ post_iterator (void *cls,
556 * MHS_NO if the socket must be closed due to a serios 560 * MHS_NO if the socket must be closed due to a serios
557 * error while handling the request 561 * error while handling the request
558 */ 562 */
559static int 563static enum MHD_Result
560create_response (void *cls, 564create_response (void *cls,
561 struct MHD_Connection *connection, 565 struct MHD_Connection *connection,
562 const char *url, 566 const char *url,
563 const char *method, 567 const char *method,
564 const char *version, 568 const char *version,
565 const char *upload_data, 569 const char *upload_data,
566 size_t *upload_data_size, 570 size_t *upload_data_size,
567 void **ptr) 571 void **ptr)
568{ 572{
569 struct MHD_Response *response; 573 struct MHD_Response *response;
570 struct Request *request; 574 struct Request *request;
571 struct Session *session; 575 struct Session *session;
572 int ret; 576 enum MHD_Result ret;
573 unsigned int i; 577 unsigned int i;
574 (void)cls; /* Unused. Silent compiler warning. */ 578 (void) cls; /* Unused. Silent compiler warning. */
575 (void)version; /* Unused. Silent compiler warning. */ 579 (void) version; /* Unused. Silent compiler warning. */
576 580
577 request = *ptr; 581 request = *ptr;
578 if (NULL == request) 582 if (NULL == request)
583 {
584 request = calloc (1, sizeof (struct Request));
585 if (NULL == request)
579 { 586 {
580 request = calloc (1, sizeof (struct Request)); 587 fprintf (stderr, "calloc error: %s\n", strerror (errno));
581 if (NULL == request) 588 return MHD_NO;
582 { 589 }
583 fprintf (stderr, "calloc error: %s\n", strerror (errno)); 590 *ptr = request;
584 return MHD_NO; 591 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
585 } 592 {
586 *ptr = request; 593 request->pp = MHD_create_post_processor (connection, 1024,
587 if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) 594 &post_iterator, request);
588 { 595 if (NULL == request->pp)
589 request->pp = MHD_create_post_processor (connection, 1024, 596 {
590 &post_iterator, request); 597 fprintf (stderr, "Failed to setup post processor for `%s'\n",
591 if (NULL == request->pp) 598 url);
592 { 599 return MHD_NO; /* internal error */
593 fprintf (stderr, "Failed to setup post processor for `%s'\n", 600 }
594 url);
595 return MHD_NO; /* internal error */
596 }
597 }
598 return MHD_YES;
599 } 601 }
602 return MHD_YES;
603 }
600 if (NULL == request->session) 604 if (NULL == request->session)
605 {
606 request->session = get_session (connection);
607 if (NULL == request->session)
601 { 608 {
602 request->session = get_session (connection); 609 fprintf (stderr, "Failed to setup session for `%s'\n",
603 if (NULL == request->session) 610 url);
604 { 611 return MHD_NO; /* internal error */
605 fprintf (stderr, "Failed to setup session for `%s'\n",
606 url);
607 return MHD_NO; /* internal error */
608 }
609 } 612 }
613 }
610 session = request->session; 614 session = request->session;
611 session->start = time (NULL); 615 session->start = time (NULL);
612 if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) 616 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
617 {
618 /* evaluate POST data */
619 MHD_post_process (request->pp,
620 upload_data,
621 *upload_data_size);
622 if (0 != *upload_data_size)
613 { 623 {
614 /* evaluate POST data */ 624 *upload_data_size = 0;
615 MHD_post_process (request->pp, 625 return MHD_YES;
616 upload_data,
617 *upload_data_size);
618 if (0 != *upload_data_size)
619 {
620 *upload_data_size = 0;
621 return MHD_YES;
622 }
623 /* done with POST data, serve response */
624 MHD_destroy_post_processor (request->pp);
625 request->pp = NULL;
626 method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
627 if (NULL != request->post_url)
628 url = request->post_url;
629 } 626 }
627 /* done with POST data, serve response */
628 MHD_destroy_post_processor (request->pp);
629 request->pp = NULL;
630 method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
631 if (NULL != request->post_url)
632 url = request->post_url;
633 }
630 634
631 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) || 635 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
632 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) ) 636 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
633 { 637 {
634 /* find out which page to serve */ 638 /* find out which page to serve */
635 i=0; 639 i = 0;
636 while ( (pages[i].url != NULL) && 640 while ( (pages[i].url != NULL) &&
637 (0 != strcmp (pages[i].url, url)) ) 641 (0 != strcmp (pages[i].url, url)) )
638 i++; 642 i++;
639 ret = pages[i].handler (pages[i].handler_cls, 643 ret = pages[i].handler (pages[i].handler_cls,
640 pages[i].mime, 644 pages[i].mime,
641 session, connection); 645 session, connection);
642 if (ret != MHD_YES) 646 if (ret != MHD_YES)
643 fprintf (stderr, "Failed to create page for `%s'\n", 647 fprintf (stderr, "Failed to create page for `%s'\n",
644 url); 648 url);
645 return ret; 649 return ret;
646 } 650 }
647 /* unsupported HTTP method */ 651 /* unsupported HTTP method */
648 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), 652 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
649 (void *) METHOD_ERROR, 653 (void *) METHOD_ERROR,
650 MHD_RESPMEM_PERSISTENT); 654 MHD_RESPMEM_PERSISTENT);
651 ret = MHD_queue_response (connection, 655 ret = MHD_queue_response (connection,
652 MHD_HTTP_NOT_ACCEPTABLE, 656 MHD_HTTP_NOT_ACCEPTABLE,
653 response); 657 response);
654 MHD_destroy_response (response); 658 MHD_destroy_response (response);
655 return ret; 659 return ret;
656} 660}
@@ -667,14 +671,14 @@ create_response (void *cls,
667 */ 671 */
668static void 672static void
669request_completed_callback (void *cls, 673request_completed_callback (void *cls,
670 struct MHD_Connection *connection, 674 struct MHD_Connection *connection,
671 void **con_cls, 675 void **con_cls,
672 enum MHD_RequestTerminationCode toe) 676 enum MHD_RequestTerminationCode toe)
673{ 677{
674 struct Request *request = *con_cls; 678 struct Request *request = *con_cls;
675 (void)cls; /* Unused. Silent compiler warning. */ 679 (void) cls; /* Unused. Silent compiler warning. */
676 (void)connection; /* Unused. Silent compiler warning. */ 680 (void) connection; /* Unused. Silent compiler warning. */
677 (void)toe; /* Unused. Silent compiler warning. */ 681 (void) toe; /* Unused. Silent compiler warning. */
678 682
679 if (NULL == request) 683 if (NULL == request)
680 return; 684 return;
@@ -702,21 +706,21 @@ expire_sessions ()
702 prev = NULL; 706 prev = NULL;
703 pos = sessions; 707 pos = sessions;
704 while (NULL != pos) 708 while (NULL != pos)
709 {
710 next = pos->next;
711 if (now - pos->start > 60 * 60)
705 { 712 {
706 next = pos->next; 713 /* expire sessions after 1h */
707 if (now - pos->start > 60 * 60) 714 if (NULL == prev)
708 { 715 sessions = pos->next;
709 /* expire sessions after 1h */
710 if (NULL == prev)
711 sessions = pos->next;
712 else
713 prev->next = next;
714 free (pos);
715 }
716 else 716 else
717 prev = pos; 717 prev->next = next;
718 pos = next; 718 free (pos);
719 } 719 }
720 else
721 prev = pos;
722 pos = next;
723 }
720} 724}
721 725
722 726
@@ -737,48 +741,49 @@ main (int argc, char *const *argv)
737 MHD_UNSIGNED_LONG_LONG mhd_timeout; 741 MHD_UNSIGNED_LONG_LONG mhd_timeout;
738 742
739 if (argc != 2) 743 if (argc != 2)
740 { 744 {
741 printf ("%s PORT\n", argv[0]); 745 printf ("%s PORT\n", argv[0]);
742 return 1; 746 return 1;
743 } 747 }
744 /* initialize PRNG */ 748 /* initialize PRNG */
745 srand ((unsigned int) time (NULL)); 749 srand ((unsigned int) time (NULL));
746 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 750 d = MHD_start_daemon (MHD_USE_ERROR_LOG,
747 atoi (argv[1]), 751 atoi (argv[1]),
748 NULL, NULL, 752 NULL, NULL,
749 &create_response, NULL, 753 &create_response, NULL,
750 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, 754 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
751 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, 755 MHD_OPTION_NOTIFY_COMPLETED,
752 MHD_OPTION_END); 756 &request_completed_callback, NULL,
757 MHD_OPTION_END);
753 if (NULL == d) 758 if (NULL == d)
754 return 1; 759 return 1;
755 while (1) 760 while (1)
761 {
762 expire_sessions ();
763 max = 0;
764 FD_ZERO (&rs);
765 FD_ZERO (&ws);
766 FD_ZERO (&es);
767 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
768 break; /* fatal internal error */
769 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
756 { 770 {
757 expire_sessions (); 771 tv.tv_sec = mhd_timeout / 1000;
758 max = 0; 772 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
759 FD_ZERO (&rs); 773 tvp = &tv;
760 FD_ZERO (&ws);
761 FD_ZERO (&es);
762 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
763 break; /* fatal internal error */
764 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
765 {
766 tv.tv_sec = mhd_timeout / 1000;
767 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
768 tvp = &tv;
769 }
770 else
771 tvp = NULL;
772 if (-1 == select (max + 1, &rs, &ws, &es, tvp))
773 {
774 if (EINTR != errno)
775 fprintf (stderr,
776 "Aborting due to error during select: %s\n",
777 strerror (errno));
778 break;
779 }
780 MHD_run (d);
781 } 774 }
775 else
776 tvp = NULL;
777 if (-1 == select (max + 1, &rs, &ws, &es, tvp))
778 {
779 if (EINTR != errno)
780 fprintf (stderr,
781 "Aborting due to error during select: %s\n",
782 strerror (errno));
783 break;
784 }
785 MHD_run (d);
786 }
782 MHD_stop_daemon (d); 787 MHD_stop_daemon (d);
783 return 0; 788 return 0;
784} 789}
diff --git a/doc/examples/simplepost.c b/doc/examples/simplepost.c
index a3bba94a..1e52e5dd 100644
--- a/doc/examples/simplepost.c
+++ b/doc/examples/simplepost.c
@@ -13,7 +13,7 @@
13#include <string.h> 13#include <string.h>
14#include <stdlib.h> 14#include <stdlib.h>
15 15
16#if defined(_MSC_VER) && _MSC_VER+0 <= 1800 16#if defined(_MSC_VER) && _MSC_VER + 0 <= 1800
17/* Substitution is OK while return value is not used */ 17/* Substitution is OK while return value is not used */
18#define snprintf _snprintf 18#define snprintf _snprintf
19#endif 19#endif
@@ -33,7 +33,8 @@ struct connection_info_struct
33 struct MHD_PostProcessor *postprocessor; 33 struct MHD_PostProcessor *postprocessor;
34}; 34};
35 35
36const char *askpage = "<html><body>\ 36const char *askpage =
37 "<html><body>\
37 What's your name, Sir?<br>\ 38 What's your name, Sir?<br>\
38 <form action=\"/namepost\" method=\"post\">\ 39 <form action=\"/namepost\" method=\"post\">\
39 <input name=\"name\" type=\"text\">\ 40 <input name=\"name\" type=\"text\">\
@@ -47,17 +48,17 @@ const char *errorpage =
47 "<html><body>This doesn't seem to be right.</body></html>"; 48 "<html><body>This doesn't seem to be right.</body></html>";
48 49
49 50
50static int 51static enum MHD_Result
51send_page (struct MHD_Connection *connection, const char *page) 52send_page (struct MHD_Connection *connection, const char *page)
52{ 53{
53 int ret; 54 enum MHD_Result ret;
54 struct MHD_Response *response; 55 struct MHD_Response *response;
55 56
56 57
57 response = 58 response =
58 MHD_create_response_from_buffer (strlen (page), (void *) page, 59 MHD_create_response_from_buffer (strlen (page), (void *) page,
59 MHD_RESPMEM_PERSISTENT); 60 MHD_RESPMEM_PERSISTENT);
60 if (!response) 61 if (! response)
61 return MHD_NO; 62 return MHD_NO;
62 63
63 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 64 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
@@ -67,135 +68,138 @@ send_page (struct MHD_Connection *connection, const char *page)
67} 68}
68 69
69 70
70static int 71static enum MHD_Result
71iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, 72iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
72 const char *filename, const char *content_type, 73 const char *filename, const char *content_type,
73 const char *transfer_encoding, const char *data, uint64_t off, 74 const char *transfer_encoding, const char *data, uint64_t off,
74 size_t size) 75 size_t size)
75{ 76{
76 struct connection_info_struct *con_info = coninfo_cls; 77 struct connection_info_struct *con_info = coninfo_cls;
77 (void)kind; /* Unused. Silent compiler warning. */ 78 (void) kind; /* Unused. Silent compiler warning. */
78 (void)filename; /* Unused. Silent compiler warning. */ 79 (void) filename; /* Unused. Silent compiler warning. */
79 (void)content_type; /* Unused. Silent compiler warning. */ 80 (void) content_type; /* Unused. Silent compiler warning. */
80 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 81 (void) transfer_encoding; /* Unused. Silent compiler warning. */
81 (void)off; /* Unused. Silent compiler warning. */ 82 (void) off; /* Unused. Silent compiler warning. */
82 83
83 if (0 == strcmp (key, "name")) 84 if (0 == strcmp (key, "name"))
85 {
86 if ((size > 0) && (size <= MAXNAMESIZE))
84 { 87 {
85 if ((size > 0) && (size <= MAXNAMESIZE)) 88 char *answerstring;
86 { 89 answerstring = malloc (MAXANSWERSIZE);
87 char *answerstring; 90 if (! answerstring)
88 answerstring = malloc (MAXANSWERSIZE); 91 return MHD_NO;
89 if (!answerstring)
90 return MHD_NO;
91
92 snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);
93 con_info->answerstring = answerstring;
94 }
95 else
96 con_info->answerstring = NULL;
97 92
98 return MHD_NO; 93 snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);
94 con_info->answerstring = answerstring;
99 } 95 }
96 else
97 con_info->answerstring = NULL;
98
99 return MHD_NO;
100 }
100 101
101 return MHD_YES; 102 return MHD_YES;
102} 103}
103 104
105
104static void 106static void
105request_completed (void *cls, struct MHD_Connection *connection, 107request_completed (void *cls, struct MHD_Connection *connection,
106 void **con_cls, enum MHD_RequestTerminationCode toe) 108 void **con_cls, enum MHD_RequestTerminationCode toe)
107{ 109{
108 struct connection_info_struct *con_info = *con_cls; 110 struct connection_info_struct *con_info = *con_cls;
109 (void)cls; /* Unused. Silent compiler warning. */ 111 (void) cls; /* Unused. Silent compiler warning. */
110 (void)connection; /* Unused. Silent compiler warning. */ 112 (void) connection; /* Unused. Silent compiler warning. */
111 (void)toe; /* Unused. Silent compiler warning. */ 113 (void) toe; /* Unused. Silent compiler warning. */
112 114
113 if (NULL == con_info) 115 if (NULL == con_info)
114 return; 116 return;
115 117
116 if (con_info->connectiontype == POST) 118 if (con_info->connectiontype == POST)
117 { 119 {
118 MHD_destroy_post_processor (con_info->postprocessor); 120 MHD_destroy_post_processor (con_info->postprocessor);
119 if (con_info->answerstring) 121 if (con_info->answerstring)
120 free (con_info->answerstring); 122 free (con_info->answerstring);
121 } 123 }
122 124
123 free (con_info); 125 free (con_info);
124 *con_cls = NULL; 126 *con_cls = NULL;
125} 127}
126 128
127 129
128static int 130static enum MHD_Result
129answer_to_connection (void *cls, struct MHD_Connection *connection, 131answer_to_connection (void *cls, struct MHD_Connection *connection,
130 const char *url, const char *method, 132 const char *url, const char *method,
131 const char *version, const char *upload_data, 133 const char *version, const char *upload_data,
132 size_t *upload_data_size, void **con_cls) 134 size_t *upload_data_size, void **con_cls)
133{ 135{
134 (void)cls; /* Unused. Silent compiler warning. */ 136 (void) cls; /* Unused. Silent compiler warning. */
135 (void)url; /* Unused. Silent compiler warning. */ 137 (void) url; /* Unused. Silent compiler warning. */
136 (void)version; /* Unused. Silent compiler warning. */ 138 (void) version; /* Unused. Silent compiler warning. */
137 139
138 if (NULL == *con_cls) 140 if (NULL == *con_cls)
139 { 141 {
140 struct connection_info_struct *con_info; 142 struct connection_info_struct *con_info;
141 143
142 con_info = malloc (sizeof (struct connection_info_struct)); 144 con_info = malloc (sizeof (struct connection_info_struct));
143 if (NULL == con_info) 145 if (NULL == con_info)
144 return MHD_NO; 146 return MHD_NO;
145 con_info->answerstring = NULL; 147 con_info->answerstring = NULL;
146 148
147 if (0 == strcmp (method, "POST")) 149 if (0 == strcmp (method, "POST"))
148 { 150 {
149 con_info->postprocessor = 151 con_info->postprocessor =
150 MHD_create_post_processor (connection, POSTBUFFERSIZE, 152 MHD_create_post_processor (connection, POSTBUFFERSIZE,
151 iterate_post, (void *) con_info); 153 iterate_post, (void *) con_info);
152 154
153 if (NULL == con_info->postprocessor) 155 if (NULL == con_info->postprocessor)
154 { 156 {
155 free (con_info); 157 free (con_info);
156 return MHD_NO; 158 return MHD_NO;
157 } 159 }
158 160
159 con_info->connectiontype = POST; 161 con_info->connectiontype = POST;
160 } 162 }
161 else 163 else
162 con_info->connectiontype = GET; 164 con_info->connectiontype = GET;
163 165
164 *con_cls = (void *) con_info; 166 *con_cls = (void *) con_info;
165 167
166 return MHD_YES; 168 return MHD_YES;
167 } 169 }
168 170
169 if (0 == strcmp (method, "GET")) 171 if (0 == strcmp (method, "GET"))
170 { 172 {
171 return send_page (connection, askpage); 173 return send_page (connection, askpage);
172 } 174 }
173 175
174 if (0 == strcmp (method, "POST")) 176 if (0 == strcmp (method, "POST"))
177 {
178 struct connection_info_struct *con_info = *con_cls;
179
180 if (*upload_data_size != 0)
175 { 181 {
176 struct connection_info_struct *con_info = *con_cls; 182 MHD_post_process (con_info->postprocessor, upload_data,
177 183 *upload_data_size);
178 if (*upload_data_size != 0) 184 *upload_data_size = 0;
179 { 185
180 MHD_post_process (con_info->postprocessor, upload_data, 186 return MHD_YES;
181 *upload_data_size);
182 *upload_data_size = 0;
183
184 return MHD_YES;
185 }
186 else if (NULL != con_info->answerstring)
187 return send_page (connection, con_info->answerstring);
188 } 187 }
188 else if (NULL != con_info->answerstring)
189 return send_page (connection, con_info->answerstring);
190 }
189 191
190 return send_page (connection, errorpage); 192 return send_page (connection, errorpage);
191} 193}
192 194
195
193int 196int
194main () 197main ()
195{ 198{
196 struct MHD_Daemon *daemon; 199 struct MHD_Daemon *daemon;
197 200
198 daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL, 201 daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
202 PORT, NULL, NULL,
199 &answer_to_connection, NULL, 203 &answer_to_connection, NULL,
200 MHD_OPTION_NOTIFY_COMPLETED, request_completed, 204 MHD_OPTION_NOTIFY_COMPLETED, request_completed,
201 NULL, MHD_OPTION_END); 205 NULL, MHD_OPTION_END);
diff --git a/doc/examples/tlsauthentication.c b/doc/examples/tlsauthentication.c
index 43b41bf1..ca8187bc 100644
--- a/doc/examples/tlsauthentication.c
+++ b/doc/examples/tlsauthentication.c
@@ -38,20 +38,20 @@ string_to_base64 (const char *message)
38 return NULL; 38 return NULL;
39 tmp[0] = 0; 39 tmp[0] = 0;
40 for (i = 0; i < length; i += 3) 40 for (i = 0; i < length; i += 3)
41 { 41 {
42 l = (((unsigned long) message[i]) << 16) 42 l = (((unsigned long) message[i]) << 16)
43 | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0) 43 | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0)
44 | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0); 44 | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0);
45 45
46 46
47 strncat (tmp, &lookup[(l >> 18) & 0x3F], 1); 47 strncat (tmp, &lookup[(l >> 18) & 0x3F], 1);
48 strncat (tmp, &lookup[(l >> 12) & 0x3F], 1); 48 strncat (tmp, &lookup[(l >> 12) & 0x3F], 1);
49 49
50 if (i + 1 < length) 50 if (i + 1 < length)
51 strncat (tmp, &lookup[(l >> 6) & 0x3F], 1); 51 strncat (tmp, &lookup[(l >> 6) & 0x3F], 1);
52 if (i + 2 < length) 52 if (i + 2 < length)
53 strncat (tmp, &lookup[l & 0x3F], 1); 53 strncat (tmp, &lookup[l & 0x3F], 1);
54 } 54 }
55 55
56 if (length % 3) 56 if (length % 3)
57 strncat (tmp, "===", 3 - length % 3); 57 strncat (tmp, "===", 3 - length % 3);
@@ -67,16 +67,16 @@ get_file_size (const char *filename)
67 67
68 fp = fopen (filename, "rb"); 68 fp = fopen (filename, "rb");
69 if (fp) 69 if (fp)
70 { 70 {
71 long size; 71 long size;
72 72
73 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp)))) 73 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
74 size = 0; 74 size = 0;
75 75
76 fclose (fp); 76 fclose (fp);
77 77
78 return size; 78 return size;
79 } 79 }
80 else 80 else
81 return 0; 81 return 0;
82} 82}
@@ -99,58 +99,58 @@ load_file (const char *filename)
99 99
100 buffer = malloc (size + 1); 100 buffer = malloc (size + 1);
101 if (! buffer) 101 if (! buffer)
102 { 102 {
103 fclose (fp); 103 fclose (fp);
104 return NULL; 104 return NULL;
105 } 105 }
106 buffer[size] = '\0'; 106 buffer[size] = '\0';
107 107
108 if (size != (long)fread (buffer, 1, size, fp)) 108 if (size != (long) fread (buffer, 1, size, fp))
109 { 109 {
110 free (buffer); 110 free (buffer);
111 buffer = NULL; 111 buffer = NULL;
112 } 112 }
113 113
114 fclose (fp); 114 fclose (fp);
115 return buffer; 115 return buffer;
116} 116}
117 117
118 118
119static int 119static enum MHD_Result
120ask_for_authentication (struct MHD_Connection *connection, const char *realm) 120ask_for_authentication (struct MHD_Connection *connection, const char *realm)
121{ 121{
122 int ret; 122 enum MHD_Result ret;
123 struct MHD_Response *response; 123 struct MHD_Response *response;
124 char *headervalue; 124 char *headervalue;
125 size_t slen; 125 size_t slen;
126 const char *strbase = "Basic realm="; 126 const char *strbase = "Basic realm=";
127 127
128 response = MHD_create_response_from_buffer (0, NULL, 128 response = MHD_create_response_from_buffer (0, NULL,
129 MHD_RESPMEM_PERSISTENT); 129 MHD_RESPMEM_PERSISTENT);
130 if (!response) 130 if (! response)
131 return MHD_NO; 131 return MHD_NO;
132 132
133 slen = strlen (strbase) + strlen (realm) + 1; 133 slen = strlen (strbase) + strlen (realm) + 1;
134 if (NULL == (headervalue = malloc (slen))) 134 if (NULL == (headervalue = malloc (slen)))
135 return MHD_NO; 135 return MHD_NO;
136 snprintf (headervalue, 136 snprintf (headervalue,
137 slen, 137 slen,
138 "%s%s", 138 "%s%s",
139 strbase, 139 strbase,
140 realm); 140 realm);
141 ret = MHD_add_response_header (response, 141 ret = MHD_add_response_header (response,
142 "WWW-Authenticate", 142 "WWW-Authenticate",
143 headervalue); 143 headervalue);
144 free (headervalue); 144 free (headervalue);
145 if (! ret) 145 if (! ret)
146 { 146 {
147 MHD_destroy_response (response); 147 MHD_destroy_response (response);
148 return MHD_NO; 148 return MHD_NO;
149 } 149 }
150 150
151 ret = MHD_queue_response (connection, 151 ret = MHD_queue_response (connection,
152 MHD_HTTP_UNAUTHORIZED, 152 MHD_HTTP_UNAUTHORIZED,
153 response); 153 response);
154 MHD_destroy_response (response); 154 MHD_destroy_response (response);
155 return ret; 155 return ret;
156} 156}
@@ -159,7 +159,7 @@ ask_for_authentication (struct MHD_Connection *connection, const char *realm)
159static int 159static int
160is_authenticated (struct MHD_Connection *connection, 160is_authenticated (struct MHD_Connection *connection,
161 const char *username, 161 const char *username,
162 const char *password) 162 const char *password)
163{ 163{
164 const char *headervalue; 164 const char *headervalue;
165 char *expected_b64; 165 char *expected_b64;
@@ -180,10 +180,10 @@ is_authenticated (struct MHD_Connection *connection,
180 if (NULL == (expected = malloc (slen))) 180 if (NULL == (expected = malloc (slen)))
181 return 0; 181 return 0;
182 snprintf (expected, 182 snprintf (expected,
183 slen, 183 slen,
184 "%s:%s", 184 "%s:%s",
185 username, 185 username,
186 password); 186 password);
187 expected_b64 = string_to_base64 (expected); 187 expected_b64 = string_to_base64 (expected);
188 free (expected); 188 free (expected);
189 if (NULL == expected_b64) 189 if (NULL == expected_b64)
@@ -196,17 +196,17 @@ is_authenticated (struct MHD_Connection *connection,
196} 196}
197 197
198 198
199static int 199static enum MHD_Result
200secret_page (struct MHD_Connection *connection) 200secret_page (struct MHD_Connection *connection)
201{ 201{
202 int ret; 202 enum MHD_Result ret;
203 struct MHD_Response *response; 203 struct MHD_Response *response;
204 const char *page = "<html><body>A secret.</body></html>"; 204 const char *page = "<html><body>A secret.</body></html>";
205 205
206 response = 206 response =
207 MHD_create_response_from_buffer (strlen (page), (void *) page, 207 MHD_create_response_from_buffer (strlen (page), (void *) page,
208 MHD_RESPMEM_PERSISTENT); 208 MHD_RESPMEM_PERSISTENT);
209 if (!response) 209 if (! response)
210 return MHD_NO; 210 return MHD_NO;
211 211
212 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 212 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
@@ -216,27 +216,27 @@ secret_page (struct MHD_Connection *connection)
216} 216}
217 217
218 218
219static int 219static enum MHD_Result
220answer_to_connection (void *cls, struct MHD_Connection *connection, 220answer_to_connection (void *cls, struct MHD_Connection *connection,
221 const char *url, const char *method, 221 const char *url, const char *method,
222 const char *version, const char *upload_data, 222 const char *version, const char *upload_data,
223 size_t *upload_data_size, void **con_cls) 223 size_t *upload_data_size, void **con_cls)
224{ 224{
225 (void)cls; /* Unused. Silent compiler warning. */ 225 (void) cls; /* Unused. Silent compiler warning. */
226 (void)url; /* Unused. Silent compiler warning. */ 226 (void) url; /* Unused. Silent compiler warning. */
227 (void)version; /* Unused. Silent compiler warning. */ 227 (void) version; /* Unused. Silent compiler warning. */
228 (void)upload_data; /* Unused. Silent compiler warning. */ 228 (void) upload_data; /* Unused. Silent compiler warning. */
229 (void)upload_data_size; /* Unused. Silent compiler warning. */ 229 (void) upload_data_size; /* Unused. Silent compiler warning. */
230 230
231 if (0 != strcmp (method, "GET")) 231 if (0 != strcmp (method, "GET"))
232 return MHD_NO; 232 return MHD_NO;
233 if (NULL == *con_cls) 233 if (NULL == *con_cls)
234 { 234 {
235 *con_cls = connection; 235 *con_cls = connection;
236 return MHD_YES; 236 return MHD_YES;
237 } 237 }
238 238
239 if (!is_authenticated (connection, USER, PASSWORD)) 239 if (! is_authenticated (connection, USER, PASSWORD))
240 return ask_for_authentication (connection, REALM); 240 return ask_for_authentication (connection, REALM);
241 241
242 return secret_page (connection); 242 return secret_page (connection);
@@ -254,14 +254,14 @@ main ()
254 cert_pem = load_file (SERVERCERTFILE); 254 cert_pem = load_file (SERVERCERTFILE);
255 255
256 if ((key_pem == NULL) || (cert_pem == NULL)) 256 if ((key_pem == NULL) || (cert_pem == NULL))
257 { 257 {
258 printf ("The key/certificate files could not be read.\n"); 258 printf ("The key/certificate files could not be read.\n");
259 if (NULL != key_pem) 259 if (NULL != key_pem)
260 free (key_pem); 260 free (key_pem);
261 if (NULL != cert_pem) 261 if (NULL != cert_pem)
262 free (cert_pem); 262 free (cert_pem);
263 return 1; 263 return 1;
264 } 264 }
265 265
266 daemon = 266 daemon =
267 MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS, PORT, NULL, 267 MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS, PORT, NULL,
@@ -269,14 +269,14 @@ main ()
269 MHD_OPTION_HTTPS_MEM_KEY, key_pem, 269 MHD_OPTION_HTTPS_MEM_KEY, key_pem,
270 MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END); 270 MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END);
271 if (NULL == daemon) 271 if (NULL == daemon)
272 { 272 {
273 printf ("%s\n", cert_pem); 273 printf ("%s\n", cert_pem);
274 274
275 free (key_pem); 275 free (key_pem);
276 free (cert_pem); 276 free (cert_pem);
277 277
278 return 1; 278 return 1;
279 } 279 }
280 280
281 (void) getchar (); 281 (void) getchar ();
282 282