test_post.c (23961B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007 Christian Grothoff 4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) 5 6 libmicrohttpd is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published 8 by the Free Software Foundation; either version 2, or (at your 9 option) any later version. 10 11 libmicrohttpd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with libmicrohttpd; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 Boston, MA 02110-1301, USA. 20 */ 21 22 /** 23 * @file test_post.c 24 * @brief Testcase for libmicrohttpd POST operations using URL-encoding 25 * @author Christian Grothoff 26 * @author Karlson2k (Evgeny Grin) 27 */ 28 29 #include "MHD_config.h" 30 #include "platform.h" 31 #include <curl/curl.h> 32 #include <microhttpd.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <time.h> 36 #include <errno.h> 37 38 #ifndef WINDOWS 39 #include <unistd.h> 40 #endif 41 42 #ifdef _WIN32 43 #ifndef WIN32_LEAN_AND_MEAN 44 #define WIN32_LEAN_AND_MEAN 1 45 #endif /* !WIN32_LEAN_AND_MEAN */ 46 #include <windows.h> 47 #endif 48 49 #include "mhd_has_in_name.h" 50 51 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 52 test into a marked, classifiable test error (TESTING.md, P5). */ 53 #include "mhd_panic_tripwire.h" 54 55 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 56 #undef MHD_CPU_COUNT 57 #endif 58 #if ! defined(MHD_CPU_COUNT) 59 #define MHD_CPU_COUNT 2 60 #endif 61 62 #define POST_DATA "name=daniel&project=curl" 63 64 static int oneone; 65 66 struct CBC 67 { 68 char *buf; 69 size_t pos; 70 size_t size; 71 }; 72 73 74 static void 75 completed_cb (void *cls, 76 struct MHD_Connection *connection, 77 void **req_cls, 78 enum MHD_RequestTerminationCode toe) 79 { 80 struct MHD_PostProcessor *pp = *req_cls; 81 (void) cls; (void) connection; (void) toe; /* Unused. Silent compiler warning. */ 82 83 if (NULL != pp) 84 MHD_destroy_post_processor (pp); 85 *req_cls = NULL; 86 } 87 88 89 static size_t 90 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 91 { 92 struct CBC *cbc = ctx; 93 94 if (cbc->pos + size * nmemb > cbc->size) 95 return 0; /* overflow */ 96 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 97 cbc->pos += size * nmemb; 98 return size * nmemb; 99 } 100 101 102 /** 103 * Note that this post_iterator is not perfect 104 * in that it fails to support incremental processing. 105 * (to be fixed in the future) 106 */ 107 static enum MHD_Result 108 post_iterator (void *cls, 109 enum MHD_ValueKind kind, 110 const char *key, 111 const char *filename, 112 const char *content_type, 113 const char *transfer_encoding, 114 const char *value, uint64_t off, size_t size) 115 { 116 int *eok = cls; 117 (void) kind; (void) filename; (void) content_type; /* Unused. Silent compiler warning. */ 118 (void) transfer_encoding; (void) off; /* Unused. Silent compiler warning. */ 119 120 if ((0 == strcmp (key, "name")) && 121 (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size))) 122 (*eok) |= 1; 123 if ((0 == strcmp (key, "project")) && 124 (size == strlen ("curl")) && (0 == strncmp (value, "curl", size))) 125 (*eok) |= 2; 126 return MHD_YES; 127 } 128 129 130 static enum MHD_Result 131 ahc_echo (void *cls, 132 struct MHD_Connection *connection, 133 const char *url, 134 const char *method, 135 const char *version, 136 const char *upload_data, size_t *upload_data_size, 137 void **req_cls) 138 { 139 static int eok; 140 struct MHD_Response *response; 141 struct MHD_PostProcessor *pp; 142 enum MHD_Result ret; 143 (void) cls; (void) version; /* Unused. Silent compiler warning. */ 144 145 if (0 != strcmp (MHD_HTTP_METHOD_POST, method)) 146 { 147 fprintf (stderr, "METHOD: %s\n", method); 148 return MHD_NO; /* unexpected method */ 149 } 150 pp = *req_cls; 151 if (pp == NULL) 152 { 153 eok = 0; 154 pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok); 155 *req_cls = pp; 156 } 157 MHD_post_process (pp, upload_data, *upload_data_size); 158 if ((eok == 3) && (0 == *upload_data_size)) 159 { 160 response = MHD_create_response_from_buffer_copy (strlen (url), 161 (const void *) url); 162 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 163 MHD_destroy_response (response); 164 MHD_destroy_post_processor (pp); 165 *req_cls = NULL; 166 return ret; 167 } 168 *upload_data_size = 0; 169 return MHD_YES; 170 } 171 172 173 static unsigned int 174 testInternalPost (void) 175 { 176 struct MHD_Daemon *d; 177 CURL *c; 178 char buf[2048]; 179 struct CBC cbc; 180 CURLcode errornum; 181 uint16_t port; 182 183 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 184 port = 0; 185 else 186 { 187 port = 1370; 188 if (oneone) 189 port += 10; 190 } 191 192 cbc.buf = buf; 193 cbc.size = 2048; 194 cbc.pos = 0; 195 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 196 port, NULL, NULL, &ahc_echo, NULL, 197 MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, 198 MHD_OPTION_END); 199 if (d == NULL) 200 return 1; 201 if (0 == port) 202 { 203 const union MHD_DaemonInfo *dinfo; 204 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 205 if ((NULL == dinfo) || (0 == dinfo->port) ) 206 { 207 MHD_stop_daemon (d); return 32; 208 } 209 port = dinfo->port; 210 } 211 c = curl_easy_init (); 212 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 213 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 214 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 215 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 216 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); 217 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); 218 curl_easy_setopt (c, CURLOPT_POST, 1L); 219 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 220 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 221 if (oneone) 222 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 223 else 224 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 225 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 226 /* NOTE: use of CONNECTTIMEOUT without also 227 * setting NOSIGNAL results in really weird 228 * crashes on my system! */ 229 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 230 curl_easy_setopt (c, CURLOPT_PROXY, ""); 231 if (CURLE_OK != (errornum = curl_easy_perform (c))) 232 { 233 fprintf (stderr, 234 "curl_easy_perform failed: `%s'\n", 235 curl_easy_strerror (errornum)); 236 curl_easy_cleanup (c); 237 MHD_stop_daemon (d); 238 return 2; 239 } 240 curl_easy_cleanup (c); 241 MHD_stop_daemon (d); 242 if (cbc.pos != strlen ("/hello_world")) 243 return 4; 244 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 245 return 8; 246 return 0; 247 } 248 249 250 static unsigned int 251 testMultithreadedPost (void) 252 { 253 struct MHD_Daemon *d; 254 CURL *c; 255 char buf[2048]; 256 struct CBC cbc; 257 CURLcode errornum; 258 uint16_t port; 259 260 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 261 port = 0; 262 else 263 { 264 port = 1371; 265 if (oneone) 266 port += 10; 267 } 268 269 cbc.buf = buf; 270 cbc.size = 2048; 271 cbc.pos = 0; 272 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 273 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 274 port, NULL, NULL, &ahc_echo, NULL, 275 MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, 276 MHD_OPTION_END); 277 if (d == NULL) 278 return 16; 279 if (0 == port) 280 { 281 const union MHD_DaemonInfo *dinfo; 282 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 283 if ((NULL == dinfo) || (0 == dinfo->port) ) 284 { 285 MHD_stop_daemon (d); return 32; 286 } 287 port = dinfo->port; 288 } 289 c = curl_easy_init (); 290 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 291 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 292 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 293 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 294 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); 295 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); 296 curl_easy_setopt (c, CURLOPT_POST, 1L); 297 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 298 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 299 if (oneone) 300 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 301 else 302 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 303 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 304 /* NOTE: use of CONNECTTIMEOUT without also 305 * setting NOSIGNAL results in really weird 306 * crashes on my system! */ 307 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 308 curl_easy_setopt (c, CURLOPT_PROXY, ""); 309 if (CURLE_OK != (errornum = curl_easy_perform (c))) 310 { 311 fprintf (stderr, 312 "curl_easy_perform failed: `%s'\n", 313 curl_easy_strerror (errornum)); 314 curl_easy_cleanup (c); 315 MHD_stop_daemon (d); 316 return 32; 317 } 318 curl_easy_cleanup (c); 319 MHD_stop_daemon (d); 320 if (cbc.pos != strlen ("/hello_world")) 321 return 64; 322 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 323 return 128; 324 return 0; 325 } 326 327 328 static unsigned int 329 testMultithreadedPoolPost (void) 330 { 331 struct MHD_Daemon *d; 332 CURL *c; 333 char buf[2048]; 334 struct CBC cbc; 335 CURLcode errornum; 336 uint16_t port; 337 338 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 339 port = 0; 340 else 341 { 342 port = 1372; 343 if (oneone) 344 port += 10; 345 } 346 347 cbc.buf = buf; 348 cbc.size = 2048; 349 cbc.pos = 0; 350 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 351 port, NULL, NULL, &ahc_echo, NULL, 352 MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT, 353 MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, 354 MHD_OPTION_END); 355 if (d == NULL) 356 return 16; 357 if (0 == port) 358 { 359 const union MHD_DaemonInfo *dinfo; 360 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 361 if ((NULL == dinfo) || (0 == dinfo->port) ) 362 { 363 MHD_stop_daemon (d); return 32; 364 } 365 port = dinfo->port; 366 } 367 c = curl_easy_init (); 368 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 369 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 370 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 371 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 372 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); 373 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); 374 curl_easy_setopt (c, CURLOPT_POST, 1L); 375 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 376 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 377 if (oneone) 378 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 379 else 380 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 381 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 382 /* NOTE: use of CONNECTTIMEOUT without also 383 * setting NOSIGNAL results in really weird 384 * crashes on my system! */ 385 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 386 curl_easy_setopt (c, CURLOPT_PROXY, ""); 387 if (CURLE_OK != (errornum = curl_easy_perform (c))) 388 { 389 fprintf (stderr, 390 "curl_easy_perform failed: `%s'\n", 391 curl_easy_strerror (errornum)); 392 curl_easy_cleanup (c); 393 MHD_stop_daemon (d); 394 return 32; 395 } 396 curl_easy_cleanup (c); 397 MHD_stop_daemon (d); 398 if (cbc.pos != strlen ("/hello_world")) 399 return 64; 400 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 401 return 128; 402 return 0; 403 } 404 405 406 static unsigned int 407 testExternalPost (void) 408 { 409 struct MHD_Daemon *d; 410 CURL *c; 411 char buf[2048]; 412 struct CBC cbc; 413 CURLM *multi; 414 CURLMcode mret; 415 fd_set rs; 416 fd_set ws; 417 fd_set es; 418 MHD_socket maxsock; 419 #ifdef MHD_WINSOCK_SOCKETS 420 int maxposixs; /* Max socket number unused on W32 */ 421 #else /* MHD_POSIX_SOCKETS */ 422 #define maxposixs maxsock 423 #endif /* MHD_POSIX_SOCKETS */ 424 int running; 425 struct CURLMsg *msg; 426 time_t start; 427 struct timeval tv; 428 uint16_t port; 429 430 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 431 port = 0; 432 else 433 { 434 port = 1373; 435 if (oneone) 436 port += 10; 437 } 438 439 multi = NULL; 440 cbc.buf = buf; 441 cbc.size = 2048; 442 cbc.pos = 0; 443 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 444 port, NULL, NULL, &ahc_echo, NULL, 445 MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, 446 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 447 MHD_OPTION_END); 448 if (d == NULL) 449 return 256; 450 if (0 == port) 451 { 452 const union MHD_DaemonInfo *dinfo; 453 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 454 if ((NULL == dinfo) || (0 == dinfo->port) ) 455 { 456 MHD_stop_daemon (d); return 32; 457 } 458 port = dinfo->port; 459 } 460 c = curl_easy_init (); 461 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 462 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 463 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 464 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 465 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); 466 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); 467 curl_easy_setopt (c, CURLOPT_POST, 1L); 468 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 469 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 470 if (oneone) 471 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 472 else 473 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 474 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 475 /* NOTE: use of CONNECTTIMEOUT without also 476 * setting NOSIGNAL results in really weird 477 * crashes on my system! */ 478 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 479 curl_easy_setopt (c, CURLOPT_PROXY, ""); 480 481 482 multi = curl_multi_init (); 483 if (multi == NULL) 484 { 485 curl_easy_cleanup (c); 486 MHD_stop_daemon (d); 487 return 512; 488 } 489 mret = curl_multi_add_handle (multi, c); 490 if (mret != CURLM_OK) 491 { 492 curl_multi_cleanup (multi); 493 curl_easy_cleanup (c); 494 MHD_stop_daemon (d); 495 return 1024; 496 } 497 start = time (NULL); 498 while ((time (NULL) - start < 5) && (multi != NULL)) 499 { 500 maxsock = MHD_INVALID_SOCKET; 501 maxposixs = -1; 502 FD_ZERO (&rs); 503 FD_ZERO (&ws); 504 FD_ZERO (&es); 505 curl_multi_perform (multi, &running); 506 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); 507 if (mret != CURLM_OK) 508 { 509 curl_multi_remove_handle (multi, c); 510 curl_multi_cleanup (multi); 511 curl_easy_cleanup (c); 512 MHD_stop_daemon (d); 513 return 2048; 514 } 515 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) 516 { 517 curl_multi_remove_handle (multi, c); 518 curl_multi_cleanup (multi); 519 curl_easy_cleanup (c); 520 MHD_stop_daemon (d); 521 return 4096; 522 } 523 tv.tv_sec = 0; 524 tv.tv_usec = 1000; 525 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) 526 { 527 #ifdef MHD_POSIX_SOCKETS 528 if (EINTR != errno) 529 { 530 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 531 (int) errno, __LINE__); 532 fflush (stderr); 533 exit (99); 534 } 535 #else 536 if ((WSAEINVAL != WSAGetLastError ()) || 537 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 538 { 539 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 540 (int) WSAGetLastError (), __LINE__); 541 fflush (stderr); 542 exit (99); 543 } 544 Sleep (1); 545 #endif 546 } 547 curl_multi_perform (multi, &running); 548 if (0 == running) 549 { 550 int pending; 551 int curl_fine = 0; 552 while (NULL != (msg = curl_multi_info_read (multi, &pending))) 553 { 554 if (msg->msg == CURLMSG_DONE) 555 { 556 if (msg->data.result == CURLE_OK) 557 curl_fine = 1; 558 else 559 { 560 fprintf (stderr, 561 "%s failed at %s:%d: `%s'\n", 562 "curl_multi_perform", 563 __FILE__, 564 __LINE__, curl_easy_strerror (msg->data.result)); 565 abort (); 566 } 567 } 568 } 569 if (! curl_fine) 570 { 571 fprintf (stderr, "libcurl haven't returned OK code\n"); 572 abort (); 573 } 574 curl_multi_remove_handle (multi, c); 575 curl_multi_cleanup (multi); 576 curl_easy_cleanup (c); 577 c = NULL; 578 multi = NULL; 579 } 580 MHD_run (d); 581 } 582 if (multi != NULL) 583 { 584 curl_multi_remove_handle (multi, c); 585 curl_easy_cleanup (c); 586 curl_multi_cleanup (multi); 587 } 588 MHD_stop_daemon (d); 589 if (cbc.pos != strlen ("/hello_world")) 590 return 8192; 591 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 592 return 16384; 593 return 0; 594 } 595 596 597 static enum MHD_Result 598 ahc_cancel (void *cls, 599 struct MHD_Connection *connection, 600 const char *url, 601 const char *method, 602 const char *version, 603 const char *upload_data, size_t *upload_data_size, 604 void **req_cls) 605 { 606 struct MHD_Response *response; 607 enum MHD_Result ret; 608 (void) cls; (void) url; (void) version; /* Unused. Silent compiler warning. */ 609 (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */ 610 611 if (0 != strcmp ("POST", method)) 612 { 613 fprintf (stderr, 614 "Unexpected method `%s'\n", method); 615 return MHD_NO; 616 } 617 618 if (*req_cls == NULL) 619 { 620 static int marker = 1; 621 *req_cls = ▮ 622 /* We don't want the body. Send a 500. */ 623 response = MHD_create_response_empty (MHD_RF_NONE); 624 ret = MHD_queue_response (connection, 500, response); 625 if (ret != MHD_YES) 626 fprintf (stderr, "Failed to queue response\n"); 627 MHD_destroy_response (response); 628 return ret; 629 } 630 else 631 { 632 fprintf (stderr, 633 "In ahc_cancel again. This should not happen.\n"); 634 return MHD_NO; 635 } 636 } 637 638 639 struct CRBC 640 { 641 const char *buffer; 642 size_t size; 643 size_t pos; 644 }; 645 646 647 static size_t 648 readBuffer (void *p, size_t size, size_t nmemb, void *opaque) 649 { 650 struct CRBC *data = opaque; 651 size_t required = size * nmemb; 652 size_t left = data->size - data->pos; 653 654 if (required > left) 655 required = left; 656 657 memcpy (p, data->buffer + data->pos, required); 658 data->pos += required; 659 660 return required / size; 661 } 662 663 664 static size_t 665 slowReadBuffer (void *p, size_t size, size_t nmemb, void *opaque) 666 { 667 (void) sleep (1); 668 return readBuffer (p, size, nmemb, opaque); 669 } 670 671 672 #define FLAG_EXPECT_CONTINUE 1 673 #define FLAG_CHUNKED 2 674 #define FLAG_FORM_DATA 4 675 #define FLAG_SLOW_READ 8 676 #define FLAG_COUNT 16 677 678 679 static unsigned int 680 testMultithreadedPostCancelPart (int flags) 681 { 682 struct MHD_Daemon *d; 683 CURL *c; 684 char buf[2048]; 685 struct CBC cbc; 686 CURLcode errornum; 687 struct curl_slist *headers = NULL; 688 long response_code; 689 CURLcode cc; 690 unsigned int result = 0; 691 struct CRBC crbc; 692 uint16_t port; 693 694 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 695 port = 0; 696 else 697 { 698 port = 1374; 699 if (oneone) 700 port += 10; 701 } 702 703 /* Don't test features that aren't available with HTTP/1.0 in 704 * HTTP/1.0 mode. */ 705 if (! oneone && (flags & (FLAG_EXPECT_CONTINUE | FLAG_CHUNKED))) 706 return 0; 707 708 cbc.buf = buf; 709 cbc.size = 2048; 710 cbc.pos = 0; 711 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 712 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 713 port, NULL, NULL, &ahc_cancel, NULL, 714 MHD_OPTION_END); 715 if (d == NULL) 716 return 32768; 717 if (0 == port) 718 { 719 const union MHD_DaemonInfo *dinfo; 720 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 721 if ((NULL == dinfo) || (0 == dinfo->port) ) 722 { 723 MHD_stop_daemon (d); return 32; 724 } 725 port = dinfo->port; 726 } 727 728 crbc.buffer = "Test content"; 729 crbc.size = strlen (crbc.buffer); 730 crbc.pos = 0; 731 732 c = curl_easy_init (); 733 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 734 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 735 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 736 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 737 curl_easy_setopt (c, CURLOPT_READFUNCTION, (flags & FLAG_SLOW_READ) ? 738 &slowReadBuffer : &readBuffer); 739 curl_easy_setopt (c, CURLOPT_READDATA, &crbc); 740 curl_easy_setopt (c, CURLOPT_POSTFIELDS, NULL); 741 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, crbc.size); 742 curl_easy_setopt (c, CURLOPT_POST, 1L); 743 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 744 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 745 if (oneone) 746 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 747 else 748 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 749 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 750 /* NOTE: use of CONNECTTIMEOUT without also 751 * setting NOSIGNAL results in really weird 752 * crashes on my system! */ 753 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 754 curl_easy_setopt (c, CURLOPT_PROXY, ""); 755 756 if (flags & FLAG_CHUNKED) 757 headers = curl_slist_append (headers, "Transfer-Encoding: chunked"); 758 if (! (flags & FLAG_FORM_DATA)) 759 headers = curl_slist_append (headers, 760 "Content-Type: application/octet-stream"); 761 if (flags & FLAG_EXPECT_CONTINUE) 762 headers = curl_slist_append (headers, "Expect: 100-Continue"); 763 curl_easy_setopt (c, CURLOPT_HTTPHEADER, headers); 764 765 if (CURLE_HTTP_RETURNED_ERROR != (errornum = curl_easy_perform (c))) 766 { 767 #ifdef _WIN32 768 curl_version_info_data *curlverd = curl_version_info (CURLVERSION_NOW); 769 if ((0 != (flags & FLAG_SLOW_READ)) && (CURLE_RECV_ERROR == errornum) && 770 ((curlverd == NULL) || (curlverd->ares_num < 0x073100) ) ) 771 { /* libcurl up to version 7.49.0 didn't have workaround for WinSock bug */ 772 fprintf (stderr, 773 "Ignored curl_easy_perform expected failure on W32 with \"slow read\".\n"); 774 result = 0; 775 } 776 else 777 #else /* ! _WIN32 */ 778 if (1) 779 #endif /* ! _WIN32 */ 780 { 781 fprintf (stderr, 782 "flibbet curl_easy_perform didn't fail as expected: `%s' %u\n", 783 curl_easy_strerror (errornum), (unsigned int) errornum); 784 result = 65536; 785 } 786 curl_easy_cleanup (c); 787 MHD_stop_daemon (d); 788 curl_slist_free_all (headers); 789 return result; 790 } 791 792 if (CURLE_OK != (cc = curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, 793 &response_code))) 794 { 795 fprintf (stderr, "curl_easy_getinfo failed: '%s'\n", 796 curl_easy_strerror (cc)); 797 result = 65536; 798 } 799 800 if (! result && (response_code != 500)) 801 { 802 fprintf (stderr, "Unexpected response code: %ld\n", response_code); 803 result = 131072; 804 } 805 806 if (! result && (cbc.pos != 0)) 807 result = 262144; 808 809 curl_easy_cleanup (c); 810 MHD_stop_daemon (d); 811 curl_slist_free_all (headers); 812 return result; 813 } 814 815 816 static unsigned int 817 testMultithreadedPostCancel (void) 818 { 819 unsigned int result = 0; 820 int flags; 821 for (flags = 0; flags < FLAG_COUNT; ++flags) 822 result |= testMultithreadedPostCancelPart (flags); 823 return result; 824 } 825 826 827 int 828 main (int argc, char *const *argv) 829 { 830 unsigned int errorCount = 0; 831 (void) argc; /* Unused. Silent compiler warning. */ 832 833 if ((NULL == argv) || (0 == argv[0])) 834 return 99; 835 oneone = has_in_name (argv[0], "11"); 836 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 837 return 2; 838 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 839 { 840 errorCount += testMultithreadedPostCancel (); 841 errorCount += testInternalPost (); 842 errorCount += testMultithreadedPost (); 843 errorCount += testMultithreadedPoolPost (); 844 } 845 errorCount += testExternalPost (); 846 if (errorCount != 0) 847 fprintf (stderr, "Error (code: %u)\n", errorCount); 848 curl_global_cleanup (); 849 return (0 == errorCount) ? 0 : 1; /* 0 == pass */ 850 }