diff options
Diffstat (limited to 'src/examples/post_example.c')
-rw-r--r-- | src/examples/post_example.c | 111 |
1 files changed, 55 insertions, 56 deletions
diff --git a/src/examples/post_example.c b/src/examples/post_example.c index 173a1353..9f104080 100644 --- a/src/examples/post_example.c +++ b/src/examples/post_example.c | |||
@@ -76,7 +76,7 @@ struct Session | |||
76 | struct Session *next; | 76 | struct Session *next; |
77 | 77 | ||
78 | /** | 78 | /** |
79 | * Unique ID for this session. | 79 | * Unique ID for this session. |
80 | */ | 80 | */ |
81 | char sid[33]; | 81 | char sid[33]; |
82 | 82 | ||
@@ -122,7 +122,7 @@ struct Request | |||
122 | struct MHD_PostProcessor *pp; | 122 | struct MHD_PostProcessor *pp; |
123 | 123 | ||
124 | /** | 124 | /** |
125 | * URL to serve in response to this POST (if this request | 125 | * URL to serve in response to this POST (if this request |
126 | * was a 'POST') | 126 | * was a 'POST') |
127 | */ | 127 | */ |
128 | const char *post_url; | 128 | const char *post_url; |
@@ -140,7 +140,7 @@ static struct Session *sessions; | |||
140 | 140 | ||
141 | 141 | ||
142 | /** | 142 | /** |
143 | * Return the session handle for this connection, or | 143 | * Return the session handle for this connection, or |
144 | * create one if this is a new user. | 144 | * create one if this is a new user. |
145 | */ | 145 | */ |
146 | static struct Session * | 146 | static struct Session * |
@@ -171,9 +171,9 @@ get_session (struct MHD_Connection *connection) | |||
171 | /* create fresh session */ | 171 | /* create fresh session */ |
172 | ret = calloc (1, sizeof (struct Session)); | 172 | ret = calloc (1, sizeof (struct Session)); |
173 | if (NULL == ret) | 173 | if (NULL == ret) |
174 | { | 174 | { |
175 | fprintf (stderr, "calloc error: %s\n", strerror (errno)); | 175 | fprintf (stderr, "calloc error: %s\n", strerror (errno)); |
176 | return NULL; | 176 | return NULL; |
177 | } | 177 | } |
178 | /* not a super-secure way to generate a random session ID, | 178 | /* not a super-secure way to generate a random session ID, |
179 | but should do for a simple example... */ | 179 | but should do for a simple example... */ |
@@ -184,7 +184,7 @@ get_session (struct MHD_Connection *connection) | |||
184 | (unsigned int) rand (), | 184 | (unsigned int) rand (), |
185 | (unsigned int) rand (), | 185 | (unsigned int) rand (), |
186 | (unsigned int) rand ()); | 186 | (unsigned int) rand ()); |
187 | ret->rc++; | 187 | ret->rc++; |
188 | ret->start = time (NULL); | 188 | ret->start = time (NULL); |
189 | ret->next = sessions; | 189 | ret->next = sessions; |
190 | sessions = ret; | 190 | sessions = ret; |
@@ -209,7 +209,7 @@ typedef int (*PageHandler)(const void *cls, | |||
209 | 209 | ||
210 | /** | 210 | /** |
211 | * Entry we generate for each page served. | 211 | * Entry we generate for each page served. |
212 | */ | 212 | */ |
213 | struct Page | 213 | struct Page |
214 | { | 214 | { |
215 | /** | 215 | /** |
@@ -229,7 +229,7 @@ struct Page | |||
229 | 229 | ||
230 | /** | 230 | /** |
231 | * Extra argument to handler. | 231 | * Extra argument to handler. |
232 | */ | 232 | */ |
233 | const void *handler_cls; | 233 | const void *handler_cls; |
234 | }; | 234 | }; |
235 | 235 | ||
@@ -239,7 +239,7 @@ struct Page | |||
239 | * | 239 | * |
240 | * @param session session to use | 240 | * @param session session to use |
241 | * @param response response to modify | 241 | * @param response response to modify |
242 | */ | 242 | */ |
243 | static void | 243 | static void |
244 | add_session_cookie (struct Session *session, | 244 | add_session_cookie (struct Session *session, |
245 | struct MHD_Response *response) | 245 | struct MHD_Response *response) |
@@ -250,12 +250,12 @@ add_session_cookie (struct Session *session, | |||
250 | "%s=%s", | 250 | "%s=%s", |
251 | COOKIE_NAME, | 251 | COOKIE_NAME, |
252 | session->sid); | 252 | session->sid); |
253 | if (MHD_NO == | 253 | if (MHD_NO == |
254 | MHD_add_response_header (response, | 254 | MHD_add_response_header (response, |
255 | MHD_HTTP_HEADER_SET_COOKIE, | 255 | MHD_HTTP_HEADER_SET_COOKIE, |
256 | cstr)) | 256 | cstr)) |
257 | { | 257 | { |
258 | fprintf (stderr, | 258 | fprintf (stderr, |
259 | "Failed to set session cookie header!\n"); | 259 | "Failed to set session cookie header!\n"); |
260 | } | 260 | } |
261 | } | 261 | } |
@@ -267,7 +267,7 @@ add_session_cookie (struct Session *session, | |||
267 | * | 267 | * |
268 | * @param cls a 'const char *' with the HTML webpage to return | 268 | * @param cls a 'const char *' with the HTML webpage to return |
269 | * @param mime mime type to use | 269 | * @param mime mime type to use |
270 | * @param session session handle | 270 | * @param session session handle |
271 | * @param connection connection to use | 271 | * @param connection connection to use |
272 | */ | 272 | */ |
273 | static int | 273 | static int |
@@ -290,8 +290,8 @@ serve_simple_form (const void *cls, | |||
290 | MHD_add_response_header (response, | 290 | MHD_add_response_header (response, |
291 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 291 | MHD_HTTP_HEADER_CONTENT_ENCODING, |
292 | mime); | 292 | mime); |
293 | ret = MHD_queue_response (connection, | 293 | ret = MHD_queue_response (connection, |
294 | MHD_HTTP_OK, | 294 | MHD_HTTP_OK, |
295 | response); | 295 | response); |
296 | MHD_destroy_response (response); | 296 | MHD_destroy_response (response); |
297 | return ret; | 297 | return ret; |
@@ -301,9 +301,9 @@ serve_simple_form (const void *cls, | |||
301 | /** | 301 | /** |
302 | * Handler that adds the 'v1' value to the given HTML code. | 302 | * Handler that adds the 'v1' value to the given HTML code. |
303 | * | 303 | * |
304 | * @param cls a 'const char *' with the HTML webpage to return | 304 | * @param cls unused |
305 | * @param mime mime type to use | 305 | * @param mime mime type to use |
306 | * @param session session handle | 306 | * @param session session handle |
307 | * @param connection connection to use | 307 | * @param connection connection to use |
308 | */ | 308 | */ |
309 | static int | 309 | static int |
@@ -313,16 +313,15 @@ fill_v1_form (const void *cls, | |||
313 | struct MHD_Connection *connection) | 313 | struct MHD_Connection *connection) |
314 | { | 314 | { |
315 | int ret; | 315 | int ret; |
316 | const char *form = cls; | ||
317 | char *reply; | 316 | char *reply; |
318 | struct MHD_Response *response; | 317 | struct MHD_Response *response; |
319 | 318 | ||
320 | reply = malloc (strlen (form) + strlen (session->value_1) + 1); | 319 | reply = malloc (strlen (MAIN_PAGE) + strlen (session->value_1) + 1); |
321 | if (NULL == reply) | 320 | if (NULL == reply) |
322 | return MHD_NO; | 321 | return MHD_NO; |
323 | snprintf (reply, | 322 | snprintf (reply, |
324 | strlen (form) + strlen (session->value_1) + 1, | 323 | strlen (MAIN_PAGE) + strlen (session->value_1) + 1, |
325 | form, | 324 | MAIN_PAGE, |
326 | session->value_1); | 325 | session->value_1); |
327 | /* return static form */ | 326 | /* return static form */ |
328 | response = MHD_create_response_from_buffer (strlen (reply), | 327 | response = MHD_create_response_from_buffer (strlen (reply), |
@@ -334,8 +333,8 @@ fill_v1_form (const void *cls, | |||
334 | MHD_add_response_header (response, | 333 | MHD_add_response_header (response, |
335 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 334 | MHD_HTTP_HEADER_CONTENT_ENCODING, |
336 | mime); | 335 | mime); |
337 | ret = MHD_queue_response (connection, | 336 | ret = MHD_queue_response (connection, |
338 | MHD_HTTP_OK, | 337 | MHD_HTTP_OK, |
339 | response); | 338 | response); |
340 | MHD_destroy_response (response); | 339 | MHD_destroy_response (response); |
341 | return ret; | 340 | return ret; |
@@ -345,9 +344,9 @@ fill_v1_form (const void *cls, | |||
345 | /** | 344 | /** |
346 | * Handler that adds the 'v1' and 'v2' values to the given HTML code. | 345 | * Handler that adds the 'v1' and 'v2' values to the given HTML code. |
347 | * | 346 | * |
348 | * @param cls a 'const char *' with the HTML webpage to return | 347 | * @param cls unused |
349 | * @param mime mime type to use | 348 | * @param mime mime type to use |
350 | * @param session session handle | 349 | * @param session session handle |
351 | * @param connection connection to use | 350 | * @param connection connection to use |
352 | */ | 351 | */ |
353 | static int | 352 | static int |
@@ -357,17 +356,17 @@ fill_v1_v2_form (const void *cls, | |||
357 | struct MHD_Connection *connection) | 356 | struct MHD_Connection *connection) |
358 | { | 357 | { |
359 | int ret; | 358 | int ret; |
360 | const char *form = cls; | ||
361 | char *reply; | 359 | char *reply; |
362 | struct MHD_Response *response; | 360 | struct MHD_Response *response; |
363 | 361 | ||
364 | reply = malloc (strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1); | 362 | reply = malloc (strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1); |
365 | if (NULL == reply) | 363 | if (NULL == reply) |
366 | return MHD_NO; | 364 | return MHD_NO; |
367 | snprintf (reply, | 365 | snprintf (reply, |
368 | strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1, | 366 | strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1, |
369 | form, | 367 | SECOND_PAGE, |
370 | session->value_1); | 368 | session->value_1, |
369 | session->value_2); | ||
371 | /* return static form */ | 370 | /* return static form */ |
372 | response = MHD_create_response_from_buffer (strlen (reply), | 371 | response = MHD_create_response_from_buffer (strlen (reply), |
373 | (void *) reply, | 372 | (void *) reply, |
@@ -378,8 +377,8 @@ fill_v1_v2_form (const void *cls, | |||
378 | MHD_add_response_header (response, | 377 | MHD_add_response_header (response, |
379 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 378 | MHD_HTTP_HEADER_CONTENT_ENCODING, |
380 | mime); | 379 | mime); |
381 | ret = MHD_queue_response (connection, | 380 | ret = MHD_queue_response (connection, |
382 | MHD_HTTP_OK, | 381 | MHD_HTTP_OK, |
383 | response); | 382 | response); |
384 | MHD_destroy_response (response); | 383 | MHD_destroy_response (response); |
385 | return ret; | 384 | return ret; |
@@ -391,7 +390,7 @@ fill_v1_v2_form (const void *cls, | |||
391 | * | 390 | * |
392 | * @param cls a 'const char *' with the HTML webpage to return | 391 | * @param cls a 'const char *' with the HTML webpage to return |
393 | * @param mime mime type to use | 392 | * @param mime mime type to use |
394 | * @param session session handle | 393 | * @param session session handle |
395 | * @param connection connection to use | 394 | * @param connection connection to use |
396 | */ | 395 | */ |
397 | static int | 396 | static int |
@@ -409,8 +408,8 @@ not_found_page (const void *cls, | |||
409 | MHD_RESPMEM_PERSISTENT); | 408 | MHD_RESPMEM_PERSISTENT); |
410 | if (NULL == response) | 409 | if (NULL == response) |
411 | return MHD_NO; | 410 | return MHD_NO; |
412 | ret = MHD_queue_response (connection, | 411 | ret = MHD_queue_response (connection, |
413 | MHD_HTTP_NOT_FOUND, | 412 | MHD_HTTP_NOT_FOUND, |
414 | response); | 413 | response); |
415 | MHD_add_response_header (response, | 414 | MHD_add_response_header (response, |
416 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 415 | MHD_HTTP_HEADER_CONTENT_ENCODING, |
@@ -423,10 +422,10 @@ not_found_page (const void *cls, | |||
423 | /** | 422 | /** |
424 | * List of all pages served by this HTTP server. | 423 | * List of all pages served by this HTTP server. |
425 | */ | 424 | */ |
426 | static struct Page pages[] = | 425 | static struct Page pages[] = |
427 | { | 426 | { |
428 | { "/", "text/html", &fill_v1_form, MAIN_PAGE }, | 427 | { "/", "text/html", &fill_v1_form, NULL }, |
429 | { "/2", "text/html", &fill_v1_v2_form, SECOND_PAGE }, | 428 | { "/2", "text/html", &fill_v1_v2_form, NULL }, |
430 | { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE }, | 429 | { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE }, |
431 | { "/F", "text/html", &serve_simple_form, LAST_PAGE }, | 430 | { "/F", "text/html", &serve_simple_form, LAST_PAGE }, |
432 | { NULL, NULL, ¬_found_page, NULL } /* 404 */ | 431 | { NULL, NULL, ¬_found_page, NULL } /* 404 */ |
@@ -476,27 +475,27 @@ post_iterator (void *cls, | |||
476 | } | 475 | } |
477 | if (0 == strcmp ("v1", key)) | 476 | if (0 == strcmp ("v1", key)) |
478 | { | 477 | { |
479 | if (size + off > sizeof(session->value_1)) | 478 | if (size + off >= sizeof(session->value_1)) |
480 | size = sizeof (session->value_1) - off; | 479 | size = sizeof (session->value_1) - off - 1; |
481 | memcpy (&session->value_1[off], | 480 | memcpy (&session->value_1[off], |
482 | data, | 481 | data, |
483 | size); | 482 | size); |
484 | if (size + off < sizeof (session->value_1)) | 483 | session->value_1[size+off] = '\0'; |
485 | session->value_1[size+off] = '\0'; | ||
486 | return MHD_YES; | 484 | return MHD_YES; |
487 | } | 485 | } |
488 | if (0 == strcmp ("v2", key)) | 486 | if (0 == strcmp ("v2", key)) |
489 | { | 487 | { |
490 | if (size + off > sizeof(session->value_2)) | 488 | if (size + off >= sizeof(session->value_2)) |
491 | size = sizeof (session->value_2) - off; | 489 | size = sizeof (session->value_2) - off - 1; |
492 | memcpy (&session->value_2[off], | 490 | memcpy (&session->value_2[off], |
493 | data, | 491 | data, |
494 | size); | 492 | size); |
495 | if (size + off < sizeof (session->value_2)) | 493 | session->value_2[size+off] = '\0'; |
496 | session->value_2[size+off] = '\0'; | ||
497 | return MHD_YES; | 494 | return MHD_YES; |
498 | } | 495 | } |
499 | fprintf (stderr, "Unsupported form value `%s'\n", key); | 496 | fprintf (stderr, |
497 | "Unsupported form value `%s'\n", | ||
498 | key); | ||
500 | return MHD_YES; | 499 | return MHD_YES; |
501 | } | 500 | } |
502 | 501 | ||
@@ -540,7 +539,7 @@ create_response (void *cls, | |||
540 | const char *url, | 539 | const char *url, |
541 | const char *method, | 540 | const char *method, |
542 | const char *version, | 541 | const char *version, |
543 | const char *upload_data, | 542 | const char *upload_data, |
544 | size_t *upload_data_size, | 543 | size_t *upload_data_size, |
545 | void **ptr) | 544 | void **ptr) |
546 | { | 545 | { |
@@ -586,7 +585,7 @@ create_response (void *cls, | |||
586 | session = request->session; | 585 | session = request->session; |
587 | session->start = time (NULL); | 586 | session->start = time (NULL); |
588 | if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) | 587 | if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) |
589 | { | 588 | { |
590 | /* evaluate POST data */ | 589 | /* evaluate POST data */ |
591 | MHD_post_process (request->pp, | 590 | MHD_post_process (request->pp, |
592 | upload_data, | 591 | upload_data, |
@@ -612,7 +611,7 @@ create_response (void *cls, | |||
612 | while ( (pages[i].url != NULL) && | 611 | while ( (pages[i].url != NULL) && |
613 | (0 != strcmp (pages[i].url, url)) ) | 612 | (0 != strcmp (pages[i].url, url)) ) |
614 | i++; | 613 | i++; |
615 | ret = pages[i].handler (pages[i].handler_cls, | 614 | ret = pages[i].handler (pages[i].handler_cls, |
616 | pages[i].mime, | 615 | pages[i].mime, |
617 | session, connection); | 616 | session, connection); |
618 | if (ret != MHD_YES) | 617 | if (ret != MHD_YES) |
@@ -624,8 +623,8 @@ create_response (void *cls, | |||
624 | response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), | 623 | response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), |
625 | (void *) METHOD_ERROR, | 624 | (void *) METHOD_ERROR, |
626 | MHD_RESPMEM_PERSISTENT); | 625 | MHD_RESPMEM_PERSISTENT); |
627 | ret = MHD_queue_response (connection, | 626 | ret = MHD_queue_response (connection, |
628 | MHD_HTTP_METHOD_NOT_ACCEPTABLE, | 627 | MHD_HTTP_METHOD_NOT_ACCEPTABLE, |
629 | response); | 628 | response); |
630 | MHD_destroy_response (response); | 629 | MHD_destroy_response (response); |
631 | return ret; | 630 | return ret; |
@@ -689,7 +688,7 @@ expire_sessions () | |||
689 | else | 688 | else |
690 | prev = pos; | 689 | prev = pos; |
691 | pos = next; | 690 | pos = next; |
692 | } | 691 | } |
693 | } | 692 | } |
694 | 693 | ||
695 | 694 | ||
@@ -718,8 +717,8 @@ main (int argc, char *const *argv) | |||
718 | srand ((unsigned int) time (NULL)); | 717 | srand ((unsigned int) time (NULL)); |
719 | d = MHD_start_daemon (MHD_USE_DEBUG, | 718 | d = MHD_start_daemon (MHD_USE_DEBUG, |
720 | atoi (argv[1]), | 719 | atoi (argv[1]), |
721 | NULL, NULL, | 720 | NULL, NULL, |
722 | &create_response, NULL, | 721 | &create_response, NULL, |
723 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, | 722 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, |
724 | MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, | 723 | MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, |
725 | MHD_OPTION_END); | 724 | MHD_OPTION_END); |
@@ -734,11 +733,11 @@ main (int argc, char *const *argv) | |||
734 | FD_ZERO (&es); | 733 | FD_ZERO (&es); |
735 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | 734 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) |
736 | break; /* fatal internal error */ | 735 | break; /* fatal internal error */ |
737 | if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) | 736 | if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) |
738 | { | 737 | { |
739 | tv.tv_sec = mhd_timeout / 1000; | 738 | tv.tv_sec = mhd_timeout / 1000; |
740 | tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000; | 739 | tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000; |
741 | tvp = &tv; | 740 | tvp = &tv; |
742 | } | 741 | } |
743 | else | 742 | else |
744 | tvp = NULL; | 743 | tvp = NULL; |