aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-03-10 12:19:27 +0000
committerChristian Grothoff <christian@grothoff.org>2010-03-10 12:19:27 +0000
commita747d35a8bb6593e3418c5bf2c5704c29dc56d73 (patch)
tree0ccf716ec1677b00dd1b6c7c0d7185985c70c6e5 /src/testcurl
parent2fb1e810a3f5bca2b32f946b3a33b6faebb54f6d (diff)
downloadlibmicrohttpd-a747d35a8bb6593e3418c5bf2c5704c29dc56d73.tar.gz
libmicrohttpd-a747d35a8bb6593e3418c5bf2c5704c29dc56d73.zip
bugfix
Diffstat (limited to 'src/testcurl')
-rw-r--r--src/testcurl/daemontest_post.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_post.c b/src/testcurl/daemontest_post.c
index 715047fe..c6233925 100644
--- a/src/testcurl/daemontest_post.c
+++ b/src/testcurl/daemontest_post.c
@@ -409,7 +409,181 @@ testExternalPost ()
409 return 0; 409 return 0;
410} 410}
411 411
412static int
413ahc_cancel (void *cls,
414 struct MHD_Connection *connection,
415 const char *url,
416 const char *method,
417 const char *version,
418 const char *upload_data, size_t *upload_data_size,
419 void **unused)
420{
421 struct MHD_Response *response;
422 int ret;
423
424 if (0 != strcmp ("POST", method))
425 {
426 fprintf (stderr,
427 "Unexpected method `%s'\n", method);
428 return MHD_NO;
429 }
430
431 if (*unused == NULL)
432 {
433 *unused = "wibble";
434 /* We don't want the body. Send a 500. */
435 response = MHD_create_response_from_data(0, NULL, 0, 0);
436 ret = MHD_queue_response(connection, 500, response);
437 if (ret != MHD_YES)
438 fprintf(stderr, "Failed to queue response\n");
439 MHD_destroy_response(response);
440 return ret;
441 }
442 else
443 {
444 fprintf(stderr,
445 "In ahc_cancel again. This should not happen.\n");
446 return MHD_NO;
447 }
448}
449
450struct CRBC
451{
452 const char *buffer;
453 size_t size;
454 size_t pos;
455};
456
457static size_t
458readBuffer(void *p, size_t size, size_t nmemb, void *opaque)
459{
460 struct CRBC *data = opaque;
461 size_t required = size * nmemb;
462 size_t left = data->size - data->pos;
463
464 if (required > left)
465 required = left;
466
467 memcpy(p, data->buffer + data->pos, required);
468 data->pos += required;
469
470 return required/size;
471}
472
473static size_t
474slowReadBuffer(void *p, size_t size, size_t nmemb, void *opaque)
475{
476 sleep(1);
477 return readBuffer(p, size, nmemb, opaque);
478}
479
480#define FLAG_EXPECT_CONTINUE 1
481#define FLAG_CHUNKED 2
482#define FLAG_FORM_DATA 4
483#define FLAG_SLOW_READ 8
484#define FLAG_COUNT 16
485
486static int
487testMultithreadedPostCancelPart(int flags)
488{
489 struct MHD_Daemon *d;
490 CURL *c;
491 char buf[2048];
492 struct CBC cbc;
493 CURLcode errornum;
494 struct curl_slist *headers = NULL;
495 long response_code;
496 CURLcode cc;
497 int result = 0;
498 struct CRBC crbc;
499
500 /* Don't test features that aren't available with HTTP/1.0 in
501 * HTTP/1.0 mode. */
502 if (!oneone && (flags & (FLAG_EXPECT_CONTINUE | FLAG_CHUNKED)))
503 return 0;
504
505 cbc.buf = buf;
506 cbc.size = 2048;
507 cbc.pos = 0;
508 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
509 1081, NULL, NULL, &ahc_cancel, NULL, MHD_OPTION_END);
510 if (d == NULL)
511 return 32768;
412 512
513 crbc.buffer = "Test content";
514 crbc.size = strlen(crbc.buffer);
515 crbc.pos = 0;
516
517 c = curl_easy_init ();
518 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
519 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
520 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
521 curl_easy_setopt (c, CURLOPT_READFUNCTION, (flags & FLAG_SLOW_READ) ? &slowReadBuffer : &readBuffer);
522 curl_easy_setopt (c, CURLOPT_READDATA, &crbc);
523 curl_easy_setopt (c, CURLOPT_POSTFIELDS, NULL);
524 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, crbc.size);
525 curl_easy_setopt (c, CURLOPT_POST, 1L);
526 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
527 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
528 if (oneone)
529 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
530 else
531 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
532 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
533 // NOTE: use of CONNECTTIMEOUT without also
534 // setting NOSIGNAL results in really weird
535 // crashes on my system!
536 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
537
538 if (flags & FLAG_CHUNKED)
539 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
540 if (!(flags & FLAG_FORM_DATA))
541 headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
542 if (flags & FLAG_EXPECT_CONTINUE)
543 headers = curl_slist_append(headers, "Expect: 100-Continue");
544 curl_easy_setopt(c, CURLOPT_HTTPHEADER, headers);
545
546 if (CURLE_HTTP_RETURNED_ERROR != (errornum = curl_easy_perform (c)))
547 {
548 fprintf (stderr,
549 "flibbet curl_easy_perform didn't fail as expected: `%s' %d\n",
550 curl_easy_strerror (errornum), errornum);
551 curl_easy_cleanup (c);
552 MHD_stop_daemon (d);
553 curl_slist_free_all(headers);
554 return 65536;
555 }
556
557 if (CURLE_OK != (cc = curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &response_code)))
558 {
559 fprintf(stderr, "curl_easy_getinfo failed: '%s'\n", curl_easy_strerror(errornum));
560 result = 65536;
561 }
562
563 if (!result && (response_code != 500))
564 {
565 fprintf(stderr, "Unexpected response code: %ld\n", response_code);
566 result = 131072;
567 }
568
569 if (!result && (cbc.pos != 0))
570 result = 262144;
571
572 curl_easy_cleanup (c);
573 MHD_stop_daemon (d);
574 curl_slist_free_all(headers);
575 return result;
576}
577
578static int
579testMultithreadedPostCancel()
580{
581 int result = 0;
582 int flags;
583 for(flags = 0; flags < FLAG_COUNT; ++flags)
584 result |= testMultithreadedPostCancelPart(flags);
585 return result;
586}
413 587
414int 588int
415main (int argc, char *const *argv) 589main (int argc, char *const *argv)
@@ -419,6 +593,7 @@ main (int argc, char *const *argv)
419 oneone = NULL != strstr (argv[0], "11"); 593 oneone = NULL != strstr (argv[0], "11");
420 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 594 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
421 return 2; 595 return 2;
596 errorCount += testMultithreadedPostCancel ();
422 errorCount += testInternalPost (); 597 errorCount += testInternalPost ();
423 errorCount += testMultithreadedPost (); 598 errorCount += testMultithreadedPost ();
424 errorCount += testMultithreadedPoolPost (); 599 errorCount += testMultithreadedPoolPost ();