test_tricky.c (37408B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2014-2025 Evgeny Grin (Karlson2k) 4 Copyright (C) 2007, 2009, 2011 Christian Grothoff 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 * @file test_toolarge.c 23 * @brief Testcase for handling of untypical data. 24 * @author Karlson2k (Evgeny Grin) 25 * @author Christian Grothoff 26 */ 27 #include "MHD_config.h" 28 #include "platform.h" 29 #include <curl/curl.h> 30 #include <microhttpd.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <time.h> 34 #include <errno.h> 35 #include "mhd_has_in_name.h" 36 #include "mhd_has_param.h" 37 #include "mhd_sockets.h" /* only macros used */ 38 39 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 40 test into a marked, classifiable test error (TESTING.md, P5). */ 41 #include "mhd_panic_tripwire.h" 42 43 #ifdef HAVE_STRINGS_H 44 #include <strings.h> 45 #endif /* HAVE_STRINGS_H */ 46 47 #ifdef _WIN32 48 #ifndef WIN32_LEAN_AND_MEAN 49 #define WIN32_LEAN_AND_MEAN 1 50 #endif /* !WIN32_LEAN_AND_MEAN */ 51 #include <windows.h> 52 #endif 53 54 #ifndef WINDOWS 55 #include <unistd.h> 56 #include <sys/socket.h> 57 #endif 58 59 #ifdef HAVE_LIMITS_H 60 #include <limits.h> 61 #endif /* HAVE_LIMITS_H */ 62 63 #ifndef CURL_VERSION_BITS 64 #define CURL_VERSION_BITS(x,y,z) ((x)<<16|(y)<<8|(z)) 65 #endif /* ! CURL_VERSION_BITS */ 66 #ifndef CURL_AT_LEAST_VERSION 67 #define CURL_AT_LEAST_VERSION(x,y,z) \ 68 (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS(x, y, z)) 69 #endif /* ! CURL_AT_LEAST_VERSION */ 70 71 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 72 #undef MHD_CPU_COUNT 73 #endif 74 #if ! defined(MHD_CPU_COUNT) 75 #define MHD_CPU_COUNT 2 76 #endif 77 #if MHD_CPU_COUNT > 32 78 #undef MHD_CPU_COUNT 79 /* Limit to reasonable value */ 80 #define MHD_CPU_COUNT 32 81 #endif /* MHD_CPU_COUNT > 32 */ 82 83 84 #if defined(HAVE___FUNC__) 85 #define externalErrorExit(ignore) \ 86 _externalErrorExit_func(NULL, __func__, __LINE__) 87 #define externalErrorExitDesc(errDesc) \ 88 _externalErrorExit_func(errDesc, __func__, __LINE__) 89 #define libcurlErrorExit(ignore) \ 90 _libcurlErrorExit_func(NULL, __func__, __LINE__) 91 #define libcurlErrorExitDesc(errDesc) \ 92 _libcurlErrorExit_func(errDesc, __func__, __LINE__) 93 #define mhdErrorExit(ignore) \ 94 _mhdErrorExit_func(NULL, __func__, __LINE__) 95 #define mhdErrorExitDesc(errDesc) \ 96 _mhdErrorExit_func(errDesc, __func__, __LINE__) 97 #elif defined(HAVE___FUNCTION__) 98 #define externalErrorExit(ignore) \ 99 _externalErrorExit_func(NULL, __FUNCTION__, __LINE__) 100 #define externalErrorExitDesc(errDesc) \ 101 _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__) 102 #define libcurlErrorExit(ignore) \ 103 _libcurlErrorExit_func(NULL, __FUNCTION__, __LINE__) 104 #define libcurlErrorExitDesc(errDesc) \ 105 _libcurlErrorExit_func(errDesc, __FUNCTION__, __LINE__) 106 #define mhdErrorExit(ignore) \ 107 _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__) 108 #define mhdErrorExitDesc(errDesc) \ 109 _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__) 110 #else 111 #define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__) 112 #define externalErrorExitDesc(errDesc) \ 113 _externalErrorExit_func(errDesc, NULL, __LINE__) 114 #define libcurlErrorExit(ignore) _libcurlErrorExit_func(NULL, NULL, __LINE__) 115 #define libcurlErrorExitDesc(errDesc) \ 116 _libcurlErrorExit_func(errDesc, NULL, __LINE__) 117 #define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__) 118 #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__) 119 #endif 120 121 122 _MHD_NORETURN static void 123 _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 124 { 125 if ((NULL != errDesc) && (0 != errDesc[0])) 126 fprintf (stderr, "%s", errDesc); 127 else 128 fprintf (stderr, "System or external library call failed"); 129 if ((NULL != funcName) && (0 != funcName[0])) 130 fprintf (stderr, " in %s", funcName); 131 if (0 < lineNum) 132 fprintf (stderr, " at line %d", lineNum); 133 134 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 135 strerror (errno)); 136 #ifdef MHD_WINSOCK_SOCKETS 137 fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ()); 138 #endif /* MHD_WINSOCK_SOCKETS */ 139 fflush (stderr); 140 exit (99); 141 } 142 143 144 static char libcurl_errbuf[CURL_ERROR_SIZE] = ""; 145 146 _MHD_NORETURN static void 147 _libcurlErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 148 { 149 if ((NULL != errDesc) && (0 != errDesc[0])) 150 fprintf (stderr, "%s", errDesc); 151 else 152 fprintf (stderr, "CURL library call failed"); 153 if ((NULL != funcName) && (0 != funcName[0])) 154 fprintf (stderr, " in %s", funcName); 155 if (0 < lineNum) 156 fprintf (stderr, " at line %d", lineNum); 157 158 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 159 strerror (errno)); 160 if (0 != libcurl_errbuf[0]) 161 fprintf (stderr, "Last libcurl error details: %s\n", libcurl_errbuf); 162 163 fflush (stderr); 164 exit (99); 165 } 166 167 168 _MHD_NORETURN static void 169 _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 170 { 171 if ((NULL != errDesc) && (0 != errDesc[0])) 172 fprintf (stderr, "%s", errDesc); 173 else 174 fprintf (stderr, "MHD unexpected error"); 175 if ((NULL != funcName) && (0 != funcName[0])) 176 fprintf (stderr, " in %s", funcName); 177 if (0 < lineNum) 178 fprintf (stderr, " at line %d", lineNum); 179 180 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 181 strerror (errno)); 182 183 fflush (stderr); 184 exit (8); 185 } 186 187 188 /* Could be increased to facilitate debugging */ 189 #define TIMEOUTS_VAL 5 190 191 #define EXPECTED_URI_BASE_PATH "/a" 192 193 #define EXPECTED_URI_BASE_PATH_TRICKY "/one\rtwo" 194 195 #define URL_SCHEME "http:/" "/" 196 197 #define URL_HOST "127.0.0.1" 198 199 #define URL_SCHEME_HOST URL_SCHEME URL_HOST 200 201 #define HEADER1_NAME "First" 202 #define HEADER1_VALUE "1st" 203 #define HEADER1 HEADER1_NAME ": " HEADER1_VALUE 204 #define HEADER2_NAME "Second" 205 #define HEADER2CR_VALUE "2\rnd" 206 #define HEADER2CR HEADER2_NAME ": " HEADER2CR_VALUE 207 /* Use headers when it would be properly supported by MHD 208 #define HEADER3CR_NAME "Thi\rrd" 209 #define HEADER3CR_VALUE "3r\rd" 210 #define HEADER3CR HEADER3CR_NAME ": " HEADER3CR_VALUE 211 */ 212 #define HEADER4_NAME "Normal" 213 #define HEADER4_VALUE "it's fine" 214 #define HEADER4 HEADER4_NAME ": " HEADER4_VALUE 215 216 /* Global parameters */ 217 static int verbose; /**< Be verbose */ 218 static int oneone; /**< If false use HTTP/1.0 for requests*/ 219 static uint16_t global_port; /**< MHD daemons listen port number */ 220 static int response_timeout_val = TIMEOUTS_VAL; 221 222 static int tricky_url; /**< Tricky request URL */ 223 static int tricky_header2; /**< Tricky request header2 */ 224 225 /* Current test parameters */ 226 /* * Moved to local variables * */ 227 228 /* Static helper variables */ 229 /* * None for this test * */ 230 231 static void 232 test_global_init (void) 233 { 234 libcurl_errbuf[0] = 0; 235 236 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 237 externalErrorExit (); 238 } 239 240 241 static void 242 test_global_cleanup (void) 243 { 244 curl_global_cleanup (); 245 } 246 247 248 struct headers_check_result 249 { 250 int dummy; /* no checks in this test */ 251 }; 252 253 254 static size_t 255 lcurl_hdr_callback (char *buffer, size_t size, size_t nitems, 256 void *userdata) 257 { 258 const size_t data_size = size * nitems; 259 struct headers_check_result *check_res = 260 (struct headers_check_result *) userdata; 261 262 /* no checks in this test */ 263 (void) check_res; (void) buffer; 264 265 return data_size; 266 } 267 268 269 struct lcurl_data_cb_param 270 { 271 char *buf; 272 size_t pos; 273 size_t size; 274 }; 275 276 277 static size_t 278 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 279 { 280 struct lcurl_data_cb_param *cbc = ctx; 281 282 if (cbc->pos + size * nmemb > cbc->size) 283 externalErrorExit (); /* overflow */ 284 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 285 cbc->pos += size * nmemb; 286 return size * nmemb; 287 } 288 289 290 struct check_uri_cls 291 { 292 const char *volatile uri; 293 }; 294 295 static void * 296 check_uri_cb (void *cls, 297 const char *uri, 298 struct MHD_Connection *con) 299 { 300 struct check_uri_cls *param = (struct check_uri_cls *) cls; 301 (void) con; 302 303 if (0 != strcmp (param->uri, 304 uri)) 305 { 306 fprintf (stderr, 307 "Wrong URI: '%s', line: %d\n", 308 uri, __LINE__); 309 exit (22); 310 } 311 return NULL; 312 } 313 314 315 struct mhd_header_checker_param 316 { 317 int found_header1; 318 int found_header2; 319 int found_header4; 320 }; 321 322 static enum MHD_Result 323 headerCheckerInterator (void *cls, 324 enum MHD_ValueKind kind, 325 const char *key, 326 size_t key_size, 327 const char *value, 328 size_t value_size) 329 { 330 struct mhd_header_checker_param *const param = 331 (struct mhd_header_checker_param *) cls; 332 333 if (NULL == param) 334 mhdErrorExitDesc ("cls parameter is NULL"); 335 336 if (MHD_HEADER_KIND != kind) 337 return MHD_YES; /* Continue iteration */ 338 339 if (0 == key_size) 340 mhdErrorExitDesc ("Zero key length"); 341 342 if ((strlen (HEADER1_NAME) == key_size) && 343 (0 == memcmp (key, HEADER1_NAME, key_size))) 344 { 345 if ((strlen (HEADER1_VALUE) == value_size) && 346 (0 == memcmp (value, HEADER1_VALUE, value_size))) 347 param->found_header1 = 1; 348 else 349 fprintf (stderr, "Unexpected header value: '%.*s', expected: '%s'\n", 350 (int) value_size, value, HEADER1_VALUE); 351 } 352 else if ((strlen (HEADER2_NAME) == key_size) && 353 (0 == memcmp (key, HEADER2_NAME, key_size))) 354 { 355 if ((strlen (HEADER2CR_VALUE) == value_size) && 356 (0 == memcmp (value, HEADER2CR_VALUE, value_size))) 357 param->found_header2 = 1; 358 else 359 fprintf (stderr, "Unexpected header value: '%.*s', expected: '%s'\n", 360 (int) value_size, value, HEADER2CR_VALUE); 361 } 362 else if ((strlen (HEADER4_NAME) == key_size) && 363 (0 == memcmp (key, HEADER4_NAME, key_size))) 364 { 365 if ((strlen (HEADER4_VALUE) == value_size) && 366 (0 == memcmp (value, HEADER4_VALUE, value_size))) 367 param->found_header4 = 1; 368 else 369 fprintf (stderr, "Unexpected header value: '%.*s', expected: '%s'\n", 370 (int) value_size, value, HEADER4_VALUE); 371 } 372 return MHD_YES; 373 } 374 375 376 struct ahc_cls_type 377 { 378 const char *volatile rp_data; 379 volatile size_t rp_data_size; 380 struct mhd_header_checker_param header_check_param; 381 const char *volatile rq_method; 382 const char *volatile rq_url; 383 }; 384 385 386 static enum MHD_Result 387 ahcCheck (void *cls, 388 struct MHD_Connection *connection, 389 const char *url, 390 const char *method, 391 const char *version, 392 const char *upload_data, size_t *upload_data_size, 393 void **req_cls) 394 { 395 static int ptr; 396 struct MHD_Response *response; 397 enum MHD_Result ret; 398 struct ahc_cls_type *const param = (struct ahc_cls_type *) cls; 399 400 if (NULL == param) 401 mhdErrorExitDesc ("cls parameter is NULL"); 402 403 if (0 != strcmp (version, MHD_HTTP_VERSION_1_1)) 404 mhdErrorExitDesc ("Unexpected HTTP version"); 405 406 if (0 != strcmp (url, param->rq_url)) 407 mhdErrorExitDesc ("Unexpected URI"); 408 409 if (NULL != upload_data) 410 mhdErrorExitDesc ("'upload_data' is not NULL"); 411 412 if (NULL == upload_data_size) 413 mhdErrorExitDesc ("'upload_data_size' pointer is NULL"); 414 415 if (0 != *upload_data_size) 416 mhdErrorExitDesc ("'*upload_data_size' value is not zero"); 417 418 if (0 != strcmp (param->rq_method, method)) 419 mhdErrorExitDesc ("Unexpected request method"); 420 421 if (&ptr != *req_cls) 422 { 423 *req_cls = &ptr; 424 return MHD_YES; 425 } 426 *req_cls = NULL; 427 428 if (1 > MHD_get_connection_values_n (connection, MHD_HEADER_KIND, 429 &headerCheckerInterator, 430 ¶m->header_check_param)) 431 mhdErrorExitDesc ("Wrong number of headers in the request"); 432 433 response = 434 MHD_create_response_from_buffer_copy (param->rp_data_size, 435 (const void *) param->rp_data); 436 if (NULL == response) 437 mhdErrorExitDesc ("Failed to create response"); 438 439 ret = MHD_queue_response (connection, 440 MHD_HTTP_OK, 441 response); 442 MHD_destroy_response (response); 443 if (MHD_YES != ret) 444 mhdErrorExitDesc ("Failed to queue response"); 445 446 return ret; 447 } 448 449 450 struct curlQueryParams 451 { 452 /* Destination path for CURL query */ 453 const char *queryPath; 454 455 #if CURL_AT_LEAST_VERSION (7, 62, 0) 456 CURLU *url; 457 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 458 459 #if CURL_AT_LEAST_VERSION (7, 55, 0) 460 /* A string used as the request target directly, without modifications */ 461 const char *queryTarget; 462 #endif /* CURL_AT_LEAST_VERSION(7, 55, 0) */ 463 464 /* Custom query method, NULL for default */ 465 const char *method; 466 467 /* Destination port for CURL query */ 468 uint16_t queryPort; 469 470 /* List of additional request headers */ 471 struct curl_slist *headers; 472 473 /* CURL query result error flag */ 474 volatile unsigned int queryError; 475 476 /* Response HTTP code, zero if no response */ 477 volatile int responseCode; 478 }; 479 480 481 static CURL * 482 curlEasyInitForTest (struct curlQueryParams *p, 483 struct lcurl_data_cb_param *dcbp, 484 struct headers_check_result *hdr_chk_result) 485 { 486 CURL *c; 487 488 c = curl_easy_init (); 489 if (NULL == c) 490 libcurlErrorExitDesc ("curl_easy_init() failed"); 491 492 #if CURL_AT_LEAST_VERSION (7, 62, 0) 493 if (NULL != p->url) 494 { 495 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_CURLU, p->url)) 496 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 497 } 498 else /* combined with the next 'if()' */ 499 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 500 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, p->queryPath)) 501 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 502 503 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) || 504 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) || 505 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) p->queryPort)) || 506 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, 507 ©Buffer)) || 508 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, dcbp)) || 509 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 510 (long) response_timeout_val)) || 511 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT, 512 (long) response_timeout_val)) || 513 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, 514 libcurl_errbuf)) || 515 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, 516 lcurl_hdr_callback)) || 517 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HEADERDATA, 518 hdr_chk_result)) || 519 #if CURL_AT_LEAST_VERSION (7, 42, 0) 520 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PATH_AS_IS, 521 (long) 1)) || 522 #endif /* CURL_AT_LEAST_VERSION(7, 42, 0) */ 523 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) || 524 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, 525 (oneone) ? 526 CURL_HTTP_VERSION_1_1 : 527 CURL_HTTP_VERSION_1_0))) 528 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 529 530 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, p->method)) 531 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 532 533 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPHEADER, p->headers)) 534 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 535 536 537 #if CURL_AT_LEAST_VERSION (7, 55, 0) 538 if (NULL != p->queryTarget) 539 { 540 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_REQUEST_TARGET, 541 p->queryTarget)) 542 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 543 } 544 #endif /* CURL_AT_LEAST_VERSION(7, 55, 0) */ 545 546 return c; 547 } 548 549 550 static CURLcode 551 performQueryExternal (struct MHD_Daemon *d, CURL *c) 552 { 553 CURLM *multi; 554 time_t start; 555 struct timeval tv; 556 CURLcode ret; 557 558 ret = CURLE_FAILED_INIT; /* will be replaced with real result */ 559 multi = NULL; 560 multi = curl_multi_init (); 561 if (multi == NULL) 562 libcurlErrorExitDesc ("curl_multi_init() failed"); 563 if (CURLM_OK != curl_multi_add_handle (multi, c)) 564 libcurlErrorExitDesc ("curl_multi_add_handle() failed"); 565 566 start = time (NULL); 567 while (time (NULL) - start <= TIMEOUTS_VAL) 568 { 569 fd_set rs; 570 fd_set ws; 571 fd_set es; 572 MHD_socket maxMhdSk; 573 int maxCurlSk; 574 int running; 575 576 maxMhdSk = MHD_INVALID_SOCKET; 577 maxCurlSk = -1; 578 FD_ZERO (&rs); 579 FD_ZERO (&ws); 580 FD_ZERO (&es); 581 if (NULL != multi) 582 { 583 curl_multi_perform (multi, &running); 584 if (0 == running) 585 { 586 struct CURLMsg *msg; 587 int msgLeft; 588 int totalMsgs = 0; 589 do 590 { 591 msg = curl_multi_info_read (multi, &msgLeft); 592 if (NULL == msg) 593 libcurlErrorExitDesc ("curl_multi_info_read() failed"); 594 totalMsgs++; 595 if (CURLMSG_DONE == msg->msg) 596 ret = msg->data.result; 597 } while (msgLeft > 0); 598 if (1 != totalMsgs) 599 { 600 fprintf (stderr, 601 "curl_multi_info_read returned wrong " 602 "number of results (%d).\n", 603 totalMsgs); 604 externalErrorExit (); 605 } 606 curl_multi_remove_handle (multi, c); 607 curl_multi_cleanup (multi); 608 multi = NULL; 609 } 610 else 611 { 612 if (CURLM_OK != curl_multi_fdset (multi, &rs, &ws, &es, &maxCurlSk)) 613 libcurlErrorExitDesc ("curl_multi_fdset() failed"); 614 } 615 } 616 if (NULL == multi) 617 { /* libcurl has finished, check whether MHD still needs to perform cleanup */ 618 if (0 != MHD_get_timeout64s (d)) 619 break; /* MHD finished as well */ 620 } 621 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxMhdSk)) 622 mhdErrorExitDesc ("MHD_get_fdset() failed"); 623 tv.tv_sec = 0; 624 tv.tv_usec = 1000; 625 #ifdef MHD_POSIX_SOCKETS 626 if (maxMhdSk > maxCurlSk) 627 maxCurlSk = maxMhdSk; 628 #endif /* MHD_POSIX_SOCKETS */ 629 if (-1 == select (maxCurlSk + 1, &rs, &ws, &es, &tv)) 630 { 631 #ifdef MHD_POSIX_SOCKETS 632 if (EINTR != errno) 633 externalErrorExitDesc ("Unexpected select() error"); 634 #else 635 if ((WSAEINVAL != WSAGetLastError ()) || 636 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 637 externalErrorExitDesc ("Unexpected select() error"); 638 Sleep (1); 639 #endif 640 } 641 if (MHD_YES != MHD_run_from_select (d, &rs, &ws, &es)) 642 mhdErrorExitDesc ("MHD_run_from_select() failed"); 643 } 644 645 return ret; 646 } 647 648 649 /* Returns zero for successful response and non-zero for failed response */ 650 static unsigned int 651 doCurlQueryInThread (struct MHD_Daemon *d, 652 struct curlQueryParams *p, 653 struct headers_check_result *hdr_res, 654 const char *expected_data, 655 size_t expected_data_size) 656 { 657 const union MHD_DaemonInfo *dinfo; 658 CURL *c; 659 struct lcurl_data_cb_param dcbp; 660 CURLcode errornum; 661 int use_external_poll; 662 long resp_code; 663 664 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_FLAGS); 665 if (NULL == dinfo) 666 mhdErrorExitDesc ("MHD_get_daemon_info() failed"); 667 use_external_poll = (0 == (dinfo->flags 668 & MHD_USE_INTERNAL_POLLING_THREAD)); 669 670 if (NULL == p->queryPath 671 #if CURL_AT_LEAST_VERSION (7, 62, 0) 672 && NULL == p->url 673 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 674 ) 675 abort (); 676 677 if (0 == p->queryPort) 678 abort (); 679 680 /* Test must not fail due to test's internal buffer shortage */ 681 dcbp.size = expected_data_size * 2 + 1; 682 dcbp.buf = malloc (dcbp.size); 683 if (NULL == dcbp.buf) 684 externalErrorExit (); 685 dcbp.pos = 0; 686 687 memset (hdr_res, 0, sizeof(*hdr_res)); 688 689 c = curlEasyInitForTest (p, 690 &dcbp, hdr_res); 691 692 if (! use_external_poll) 693 errornum = curl_easy_perform (c); 694 else 695 errornum = performQueryExternal (d, c); 696 697 if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &resp_code)) 698 libcurlErrorExitDesc ("curl_easy_getinfo() failed"); 699 700 p->responseCode = (int) resp_code; 701 if ((CURLE_OK == errornum) && (200 != resp_code)) 702 { 703 fprintf (stderr, 704 "Got reply with unexpected status code: %d\n", 705 p->responseCode); 706 mhdErrorExit (); 707 } 708 709 if (CURLE_OK != errornum) 710 { 711 if ((CURLE_GOT_NOTHING != errornum) && (CURLE_RECV_ERROR != errornum) 712 && (CURLE_HTTP_RETURNED_ERROR != errornum)) 713 { 714 if (CURLE_OPERATION_TIMEDOUT == errornum) 715 mhdErrorExitDesc ("Request was aborted due to timeout"); 716 fprintf (stderr, "libcurl returned unexpected error: %s\n", 717 curl_easy_strerror (errornum)); 718 mhdErrorExitDesc ("Request failed due to unexpected error"); 719 } 720 p->queryError = 1; 721 if ((0 != resp_code) && 722 ((499 < resp_code) || (400 > resp_code))) /* TODO: add all expected error codes */ 723 { 724 fprintf (stderr, 725 "Got reply with unexpected status code: %ld\n", 726 resp_code); 727 mhdErrorExit (); 728 } 729 } 730 else 731 { 732 if (dcbp.pos != expected_data_size) 733 mhdErrorExit ("libcurl reports wrong size of MHD reply body data"); 734 else if (0 != memcmp (expected_data, dcbp.buf, 735 expected_data_size)) 736 mhdErrorExit ("libcurl reports wrong MHD reply body data"); 737 else 738 p->queryError = 0; 739 } 740 741 curl_easy_cleanup (c); 742 free (dcbp.buf); 743 744 return p->queryError; 745 } 746 747 748 /* Perform test queries, shut down MHD daemon, and free parameters */ 749 static unsigned int 750 performTestQueries (struct MHD_Daemon *d, uint16_t d_port, 751 struct ahc_cls_type *ahc_param, 752 struct check_uri_cls *uri_cb_param) 753 { 754 struct curlQueryParams qParam; 755 unsigned int ret = 0; /* Return value */ 756 struct headers_check_result rp_headers_check; 757 struct curl_slist *curl_headers; 758 curl_headers = NULL; 759 760 /* Common parameters, to be individually overridden by specific test cases */ 761 qParam.queryPort = d_port; 762 qParam.method = NULL; /* Use libcurl default: GET */ 763 qParam.queryPath = URL_SCHEME_HOST EXPECTED_URI_BASE_PATH; 764 #if CURL_AT_LEAST_VERSION (7, 55, 0) 765 qParam.queryTarget = NULL; 766 #endif /* CURL_AT_LEAST_VERSION(7, 55, 0) */ 767 #if CURL_AT_LEAST_VERSION (7, 62, 0) 768 qParam.url = NULL; 769 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 770 qParam.headers = NULL; /* No additional headers */ 771 uri_cb_param->uri = EXPECTED_URI_BASE_PATH; 772 ahc_param->rq_url = EXPECTED_URI_BASE_PATH; 773 ahc_param->rq_method = "GET"; /* Default expected method */ 774 775 ahc_param->rp_data = "~"; 776 ahc_param->rp_data_size = 1; 777 778 curl_headers = curl_slist_append (curl_headers, HEADER1); 779 if (NULL == curl_headers) 780 externalErrorExit (); 781 curl_headers = curl_slist_append (curl_headers, HEADER4); 782 if (NULL == curl_headers) 783 externalErrorExit (); 784 qParam.headers = curl_headers; 785 786 memset (&ahc_param->header_check_param, 0, 787 sizeof (ahc_param->header_check_param)); 788 789 if (tricky_url) 790 { 791 #if CURL_AT_LEAST_VERSION (7, 55, 0) 792 #if CURL_AT_LEAST_VERSION (7, 62, 0) 793 unsigned int urlu_flags = CURLU_PATH_AS_IS; 794 CURLU *url; 795 url = curl_url (); 796 if (NULL == url) 797 externalErrorExit (); 798 qParam.url = url; 799 800 #ifdef CURLU_ALLOW_SPACE 801 urlu_flags |= CURLU_ALLOW_SPACE; 802 #endif /* CURLU_ALLOW_SPACE */ 803 804 if ((CURLUE_OK != curl_url_set (qParam.url, CURLUPART_SCHEME, "http", 0)) || 805 (CURLUE_OK != curl_url_set (qParam.url, CURLUPART_HOST, URL_HOST, 806 urlu_flags)) || 807 (CURLUE_OK != curl_url_set (qParam.url, CURLUPART_PATH, 808 EXPECTED_URI_BASE_PATH_TRICKY, 809 urlu_flags))) 810 libcurlErrorExit (); 811 812 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 813 814 qParam.queryTarget = EXPECTED_URI_BASE_PATH_TRICKY; 815 uri_cb_param->uri = EXPECTED_URI_BASE_PATH_TRICKY; 816 ahc_param->rq_url = EXPECTED_URI_BASE_PATH_TRICKY; 817 818 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 819 ahc_param->rp_data, 820 ahc_param->rp_data_size)) 821 { 822 /* TODO: Allow fail only if relevant MHD mode set */ 823 if (0 == qParam.responseCode) 824 { 825 fprintf (stderr, "Request failed without any valid response.\n"); 826 ret = 1; 827 } 828 else 829 { 830 if (verbose) 831 printf ("Request failed with %d response code.\n", 832 qParam.responseCode); 833 (void) qParam.responseCode; /* TODO: check for the right response code */ 834 ret = 0; 835 } 836 } 837 else 838 { 839 if (200 != qParam.responseCode) 840 { 841 fprintf (stderr, "Request succeed with wrong response code: %d.\n", 842 qParam.responseCode); 843 ret = 1; 844 } 845 else 846 { 847 ret = 0; 848 if (verbose) 849 printf ("Request succeed.\n"); 850 } 851 852 if (! ahc_param->header_check_param.found_header1) 853 mhdErrorExitDesc ("Required header1 was not detected in request"); 854 if (! ahc_param->header_check_param.found_header4) 855 mhdErrorExitDesc ("Required header4 was not detected in request"); 856 } 857 #if CURL_AT_LEAST_VERSION (7, 62, 0) 858 curl_url_cleanup (url); 859 #endif /* CURL_AT_LEAST_VERSION(7, 62, 0) */ 860 #else 861 fprintf (stderr, "This test requires libcurl version 7.55.0 or newer.\n"); 862 abort (); 863 #endif /* CURL_AT_LEAST_VERSION(7, 55, 0) */ 864 } 865 else if (tricky_header2) 866 { 867 /* Reset libcurl headers */ 868 qParam.headers = NULL; 869 curl_slist_free_all (curl_headers); 870 curl_headers = NULL; 871 872 /* Set special libcurl headers */ 873 curl_headers = curl_slist_append (curl_headers, HEADER1); 874 if (NULL == curl_headers) 875 externalErrorExit (); 876 curl_headers = curl_slist_append (curl_headers, HEADER2CR); 877 if (NULL == curl_headers) 878 externalErrorExit (); 879 curl_headers = curl_slist_append (curl_headers, HEADER4); 880 if (NULL == curl_headers) 881 externalErrorExit (); 882 qParam.headers = curl_headers; 883 884 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 885 ahc_param->rp_data, 886 ahc_param->rp_data_size)) 887 { 888 /* TODO: Allow fail only if relevant MHD mode set */ 889 if (0 == qParam.responseCode) 890 { 891 fprintf (stderr, "Request failed without any valid response.\n"); 892 ret = 1; 893 } 894 else 895 { 896 if (verbose) 897 printf ("Request failed with %d response code.\n", 898 qParam.responseCode); 899 (void) qParam.responseCode; /* TODO: check for the right response code */ 900 ret = 0; 901 } 902 } 903 else 904 { 905 if (200 != qParam.responseCode) 906 { 907 fprintf (stderr, "Request succeed with wrong response code: %d.\n", 908 qParam.responseCode); 909 ret = 1; 910 } 911 else 912 { 913 ret = 0; 914 if (verbose) 915 printf ("Request succeed.\n"); 916 } 917 918 if (! ahc_param->header_check_param.found_header1) 919 mhdErrorExitDesc ("Required header1 was not detected in request"); 920 if (! ahc_param->header_check_param.found_header2) 921 mhdErrorExitDesc ("Required header2 was not detected in request"); 922 if (! ahc_param->header_check_param.found_header4) 923 mhdErrorExitDesc ("Required header4 was not detected in request"); 924 } 925 } 926 else 927 externalErrorExitDesc ("No valid test test was selected"); 928 929 MHD_stop_daemon (d); 930 curl_slist_free_all (curl_headers); 931 free (uri_cb_param); 932 free (ahc_param); 933 934 return ret; 935 } 936 937 938 enum testMhdThreadsType 939 { 940 testMhdThreadExternal = 0, 941 testMhdThreadInternal = MHD_USE_INTERNAL_POLLING_THREAD, 942 testMhdThreadInternalPerConnection = MHD_USE_THREAD_PER_CONNECTION 943 | MHD_USE_INTERNAL_POLLING_THREAD, 944 testMhdThreadInternalPool 945 }; 946 947 enum testMhdPollType 948 { 949 testMhdPollBySelect = 0, 950 testMhdPollByPoll = MHD_USE_POLL, 951 testMhdPollByEpoll = MHD_USE_EPOLL, 952 testMhdPollAuto = MHD_USE_AUTO 953 }; 954 955 /* Get number of threads for thread pool depending 956 * on used poll function and test type. */ 957 static unsigned int 958 testNumThreadsForPool (enum testMhdPollType pollType) 959 { 960 unsigned int numThreads = MHD_CPU_COUNT; 961 (void) pollType; /* Don't care about pollType for this test */ 962 return numThreads; /* No practical limit for non-cleanup test */ 963 } 964 965 966 static struct MHD_Daemon * 967 startTestMhdDaemon (enum testMhdThreadsType thrType, 968 enum testMhdPollType pollType, uint16_t *pport, 969 struct ahc_cls_type **ahc_param, 970 struct check_uri_cls **uri_cb_param) 971 { 972 struct MHD_Daemon *d; 973 const union MHD_DaemonInfo *dinfo; 974 975 if ((NULL == ahc_param) || (NULL == uri_cb_param)) 976 abort (); 977 978 *ahc_param = (struct ahc_cls_type *) malloc (sizeof(struct ahc_cls_type)); 979 if (NULL == *ahc_param) 980 externalErrorExit (); 981 *uri_cb_param = 982 (struct check_uri_cls *) malloc (sizeof(struct check_uri_cls)); 983 if (NULL == *uri_cb_param) 984 externalErrorExit (); 985 986 if ( (0 == *pport) && 987 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 988 { 989 *pport = 4150; 990 if (tricky_url) 991 *pport += 1; 992 if (tricky_header2) 993 *pport += 2; 994 if (! oneone) 995 *pport += 16; 996 } 997 998 if (testMhdThreadExternal == thrType) 999 d = MHD_start_daemon (((unsigned int) thrType) | ((unsigned int) pollType) 1000 | (verbose ? MHD_USE_ERROR_LOG : 0) 1001 | MHD_USE_NO_THREAD_SAFETY, 1002 *pport, NULL, NULL, 1003 &ahcCheck, *ahc_param, 1004 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1005 *uri_cb_param, 1006 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 1007 MHD_OPTION_END); 1008 else if (testMhdThreadInternalPool != thrType) 1009 d = MHD_start_daemon (((unsigned int) thrType) | ((unsigned int) pollType) 1010 | (verbose ? MHD_USE_ERROR_LOG : 0), 1011 *pport, NULL, NULL, 1012 &ahcCheck, *ahc_param, 1013 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1014 *uri_cb_param, 1015 MHD_OPTION_END); 1016 else 1017 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD 1018 | ((unsigned int) pollType) 1019 | (verbose ? MHD_USE_ERROR_LOG : 0), 1020 *pport, NULL, NULL, 1021 &ahcCheck, *ahc_param, 1022 MHD_OPTION_THREAD_POOL_SIZE, 1023 testNumThreadsForPool (pollType), 1024 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1025 *uri_cb_param, 1026 MHD_OPTION_END); 1027 1028 if (NULL == d) 1029 { 1030 fprintf (stderr, "Failed to start MHD daemon, errno=%d.\n", errno); 1031 abort (); 1032 } 1033 1034 if (0 == *pport) 1035 { 1036 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 1037 if ((NULL == dinfo) || (0 == dinfo->port) ) 1038 { 1039 fprintf (stderr, "MHD_get_daemon_info() failed.\n"); 1040 abort (); 1041 } 1042 *pport = dinfo->port; 1043 if (0 == global_port) 1044 global_port = *pport; /* Reuse the same port for all tests */ 1045 } 1046 1047 return d; 1048 } 1049 1050 1051 /* Test runners */ 1052 1053 1054 static unsigned int 1055 testExternalGet (void) 1056 { 1057 struct MHD_Daemon *d; 1058 uint16_t d_port = global_port; /* Daemon's port */ 1059 struct ahc_cls_type *ahc_param; 1060 struct check_uri_cls *uri_cb_param; 1061 1062 d = startTestMhdDaemon (testMhdThreadExternal, testMhdPollBySelect, &d_port, 1063 &ahc_param, &uri_cb_param); 1064 1065 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1066 } 1067 1068 1069 static unsigned int 1070 testInternalGet (enum testMhdPollType pollType) 1071 { 1072 struct MHD_Daemon *d; 1073 uint16_t d_port = global_port; /* Daemon's port */ 1074 struct ahc_cls_type *ahc_param; 1075 struct check_uri_cls *uri_cb_param; 1076 1077 d = startTestMhdDaemon (testMhdThreadInternal, pollType, &d_port, 1078 &ahc_param, &uri_cb_param); 1079 1080 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1081 } 1082 1083 1084 static unsigned int 1085 testMultithreadedGet (enum testMhdPollType pollType) 1086 { 1087 struct MHD_Daemon *d; 1088 uint16_t d_port = global_port; /* Daemon's port */ 1089 struct ahc_cls_type *ahc_param; 1090 struct check_uri_cls *uri_cb_param; 1091 1092 d = startTestMhdDaemon (testMhdThreadInternalPerConnection, pollType, &d_port, 1093 &ahc_param, &uri_cb_param); 1094 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1095 } 1096 1097 1098 static unsigned int 1099 testMultithreadedPoolGet (enum testMhdPollType pollType) 1100 { 1101 struct MHD_Daemon *d; 1102 uint16_t d_port = global_port; /* Daemon's port */ 1103 struct ahc_cls_type *ahc_param; 1104 struct check_uri_cls *uri_cb_param; 1105 1106 d = startTestMhdDaemon (testMhdThreadInternalPool, pollType, &d_port, 1107 &ahc_param, &uri_cb_param); 1108 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1109 } 1110 1111 1112 static void 1113 check_test_can_be_used (void) 1114 { 1115 #if ! CURL_AT_LEAST_VERSION (7, 55, 0) 1116 if (tricky_url) 1117 { 1118 fprintf (stderr, "This test requires libcurl version 7.55.0 or newer.\n"); 1119 exit (77); 1120 } 1121 #endif /* ! CURL_AT_LEAST_VERSION(7, 55, 0) */ 1122 return; 1123 } 1124 1125 1126 int 1127 main (int argc, char *const *argv) 1128 { 1129 unsigned int errorCount = 0; 1130 unsigned int test_result = 0; 1131 verbose = 0; 1132 1133 if ((NULL == argv) || (0 == argv[0])) 1134 return 99; 1135 oneone = ! has_in_name (argv[0], "10"); 1136 tricky_url = has_in_name (argv[0], "_url") ? 1 : 0; 1137 tricky_header2 = has_in_name (argv[0], "_header2") ? 1 : 0; 1138 if (1 != tricky_url + tricky_header2) 1139 return 99; 1140 verbose = ! (has_param (argc, argv, "-q") || 1141 has_param (argc, argv, "--quiet") || 1142 has_param (argc, argv, "-s") || 1143 has_param (argc, argv, "--silent")); 1144 1145 check_test_can_be_used (); 1146 1147 test_global_init (); 1148 1149 /* Could be set to non-zero value to enforce using specific port 1150 * in the test */ 1151 global_port = 0; 1152 test_result = testExternalGet (); 1153 if (test_result) 1154 fprintf (stderr, "FAILED: testExternalGet () - %u.\n", test_result); 1155 else if (verbose) 1156 printf ("PASSED: testExternalGet ().\n"); 1157 errorCount += test_result; 1158 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 1159 { 1160 test_result = testInternalGet (testMhdPollAuto); 1161 if (test_result) 1162 fprintf (stderr, "FAILED: testInternalGet (testMhdPollAuto) - %u.\n", 1163 test_result); 1164 else if (verbose) 1165 printf ("PASSED: testInternalGet (testMhdPollBySelect).\n"); 1166 errorCount += test_result; 1167 #ifdef _MHD_HEAVY_TESTS 1168 /* Actually tests are not heavy, but took too long to complete while 1169 * not really provide any additional results. */ 1170 test_result = testInternalGet (testMhdPollBySelect); 1171 if (test_result) 1172 fprintf (stderr, "FAILED: testInternalGet (testMhdPollBySelect) - %u.\n", 1173 test_result); 1174 else if (verbose) 1175 printf ("PASSED: testInternalGet (testMhdPollBySelect).\n"); 1176 errorCount += test_result; 1177 test_result = testMultithreadedPoolGet (testMhdPollBySelect); 1178 if (test_result) 1179 fprintf (stderr, 1180 "FAILED: testMultithreadedPoolGet (testMhdPollBySelect) - %u.\n", 1181 test_result); 1182 else if (verbose) 1183 printf ("PASSED: testMultithreadedPoolGet (testMhdPollBySelect).\n"); 1184 errorCount += test_result; 1185 test_result = testMultithreadedGet (testMhdPollBySelect); 1186 if (test_result) 1187 fprintf (stderr, 1188 "FAILED: testMultithreadedGet (testMhdPollBySelect) - %u.\n", 1189 test_result); 1190 else if (verbose) 1191 printf ("PASSED: testMultithreadedGet (testMhdPollBySelect).\n"); 1192 errorCount += test_result; 1193 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL)) 1194 { 1195 test_result = testInternalGet (testMhdPollByPoll); 1196 if (test_result) 1197 fprintf (stderr, "FAILED: testInternalGet (testMhdPollByPoll) - %u.\n", 1198 test_result); 1199 else if (verbose) 1200 printf ("PASSED: testInternalGet (testMhdPollByPoll).\n"); 1201 errorCount += test_result; 1202 } 1203 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL)) 1204 { 1205 test_result = testInternalGet (testMhdPollByEpoll); 1206 if (test_result) 1207 fprintf (stderr, "FAILED: testInternalGet (testMhdPollByEpoll) - %u.\n", 1208 test_result); 1209 else if (verbose) 1210 printf ("PASSED: testInternalGet (testMhdPollByEpoll).\n"); 1211 errorCount += test_result; 1212 } 1213 #else 1214 /* Mute compiler warnings */ 1215 (void) testMultithreadedGet; 1216 (void) testMultithreadedPoolGet; 1217 #endif /* _MHD_HEAVY_TESTS */ 1218 } 1219 if (0 != errorCount) 1220 fprintf (stderr, 1221 "Error (code: %u)\n", 1222 errorCount); 1223 else if (verbose) 1224 printf ("All tests passed.\n"); 1225 1226 test_global_cleanup (); 1227 1228 return (errorCount == 0) ? 0 : 1; /* 0 == pass */ 1229 }