test_get.c (30852B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2009, 2011 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 * @file test_get.c 23 * @brief Testcase for libmicrohttpd GET operations 24 * @author Christian Grothoff 25 * @author Karlson2k (Evgeny Grin) 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 44 #define EXPECTED_URI_PATH "/hello_world?a=%26&b=c" 45 46 #ifdef _WIN32 47 #ifndef WIN32_LEAN_AND_MEAN 48 #define WIN32_LEAN_AND_MEAN 1 49 #endif /* !WIN32_LEAN_AND_MEAN */ 50 #include <windows.h> 51 #endif 52 53 #ifndef WINDOWS 54 #include <unistd.h> 55 #include <sys/socket.h> 56 #endif 57 58 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 59 #undef MHD_CPU_COUNT 60 #endif 61 #if ! defined(MHD_CPU_COUNT) 62 #define MHD_CPU_COUNT 2 63 #endif 64 65 static int oneone; 66 static uint16_t global_port; 67 68 struct CBC 69 { 70 char *buf; 71 size_t pos; 72 size_t size; 73 }; 74 75 76 static size_t 77 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 78 { 79 struct CBC *cbc = ctx; 80 81 if (cbc->pos + size * nmemb > cbc->size) 82 return 0; /* overflow */ 83 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 84 cbc->pos += size * nmemb; 85 return size * nmemb; 86 } 87 88 89 static void * 90 log_cb (void *cls, 91 const char *uri, 92 struct MHD_Connection *con) 93 { 94 (void) cls; 95 (void) con; 96 if (0 != strcmp (uri, 97 EXPECTED_URI_PATH)) 98 { 99 fprintf (stderr, 100 "Wrong URI: `%s'\n", 101 uri); 102 _exit (22); 103 } 104 return NULL; 105 } 106 107 108 static enum MHD_Result 109 ahc_echo (void *cls, 110 struct MHD_Connection *connection, 111 const char *url, 112 const char *method, 113 const char *version, 114 const char *upload_data, size_t *upload_data_size, 115 void **req_cls) 116 { 117 static int ptr; 118 struct MHD_Response *response; 119 enum MHD_Result ret; 120 const char *v; 121 (void) cls; 122 (void) version; 123 (void) upload_data; 124 (void) upload_data_size; /* Unused. Silence compiler warning. */ 125 126 if (0 != strcmp (MHD_HTTP_METHOD_GET, method)) 127 return MHD_NO; /* unexpected method */ 128 if (&ptr != *req_cls) 129 { 130 *req_cls = &ptr; 131 return MHD_YES; 132 } 133 *req_cls = NULL; 134 v = MHD_lookup_connection_value (connection, 135 MHD_GET_ARGUMENT_KIND, 136 "a"); 137 if ( (NULL == v) || 138 (0 != strcmp ("&", 139 v)) ) 140 { 141 fprintf (stderr, "Found while looking for 'a=&': 'a=%s'\n", 142 NULL == v ? "NULL" : v); 143 _exit (17); 144 } 145 v = NULL; 146 if (MHD_YES != MHD_lookup_connection_value_n (connection, 147 MHD_GET_ARGUMENT_KIND, 148 "b", 149 1, 150 &v, 151 NULL)) 152 { 153 fprintf (stderr, "Not found 'b' GET argument.\n"); 154 _exit (18); 155 } 156 if ( (NULL == v) || 157 (0 != strcmp ("c", 158 v)) ) 159 { 160 fprintf (stderr, "Found while looking for 'b=c': 'b=%s'\n", 161 NULL == v ? "NULL" : v); 162 _exit (19); 163 } 164 response = MHD_create_response_from_buffer_copy (strlen (url), 165 (const void *) url); 166 ret = MHD_queue_response (connection, 167 MHD_HTTP_OK, 168 response); 169 MHD_destroy_response (response); 170 if (ret == MHD_NO) 171 { 172 fprintf (stderr, "Failed to queue response.\n"); 173 _exit (19); 174 } 175 return ret; 176 } 177 178 179 static unsigned int 180 testInternalGet (uint32_t poll_flag) 181 { 182 struct MHD_Daemon *d; 183 CURL *c; 184 char buf[2048]; 185 struct CBC cbc; 186 CURLcode errornum; 187 188 if ( (0 == global_port) && 189 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 190 { 191 global_port = 1220; 192 if (oneone) 193 global_port += 20; 194 } 195 196 cbc.buf = buf; 197 cbc.size = 2048; 198 cbc.pos = 0; 199 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 200 | (enum MHD_FLAG) poll_flag, 201 global_port, NULL, NULL, 202 &ahc_echo, NULL, 203 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 204 MHD_OPTION_END); 205 if (d == NULL) 206 return 1; 207 if (0 == global_port) 208 { 209 const union MHD_DaemonInfo *dinfo; 210 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 211 if ((NULL == dinfo) || (0 == dinfo->port) ) 212 { 213 MHD_stop_daemon (d); return 32; 214 } 215 global_port = dinfo->port; 216 } 217 c = curl_easy_init (); 218 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH); 219 curl_easy_setopt (c, CURLOPT_PORT, (long) global_port); 220 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 221 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 222 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 223 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 224 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 225 if (oneone) 226 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 227 else 228 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 229 /* NOTE: use of CONNECTTIMEOUT without also 230 setting NOSIGNAL results in really weird 231 crashes on my system!*/ 232 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 233 curl_easy_setopt (c, CURLOPT_PROXY, ""); 234 if (CURLE_OK != (errornum = curl_easy_perform (c))) 235 { 236 fprintf (stderr, 237 "curl_easy_perform failed: `%s'\n", 238 curl_easy_strerror (errornum)); 239 curl_easy_cleanup (c); 240 MHD_stop_daemon (d); 241 return 2; 242 } 243 curl_easy_cleanup (c); 244 MHD_stop_daemon (d); 245 if (cbc.pos != strlen ("/hello_world")) 246 return 4; 247 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 248 return 8; 249 return 0; 250 } 251 252 253 static unsigned int 254 testMultithreadedGet (uint32_t poll_flag) 255 { 256 struct MHD_Daemon *d; 257 CURL *c; 258 char buf[2048]; 259 struct CBC cbc; 260 CURLcode errornum; 261 262 if ( (0 == global_port) && 263 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 264 { 265 global_port = 1221; 266 if (oneone) 267 global_port += 20; 268 } 269 270 cbc.buf = buf; 271 cbc.size = 2048; 272 cbc.pos = 0; 273 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 274 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 275 | (enum MHD_FLAG) poll_flag, 276 global_port, NULL, NULL, 277 &ahc_echo, NULL, 278 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 279 MHD_OPTION_END); 280 if (d == NULL) 281 return 16; 282 if (0 == global_port) 283 { 284 const union MHD_DaemonInfo *dinfo; 285 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 286 if ((NULL == dinfo) || (0 == dinfo->port) ) 287 { 288 MHD_stop_daemon (d); return 32; 289 } 290 global_port = dinfo->port; 291 } 292 c = curl_easy_init (); 293 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH); 294 curl_easy_setopt (c, CURLOPT_PORT, (long) global_port); 295 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 296 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 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 testMultithreadedPoolGet (uint32_t poll_flag) 330 { 331 struct MHD_Daemon *d; 332 CURL *c; 333 char buf[2048]; 334 struct CBC cbc; 335 CURLcode errornum; 336 337 if ( (0 == global_port) && 338 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 339 { 340 global_port = 1222; 341 if (oneone) 342 global_port += 20; 343 } 344 345 cbc.buf = buf; 346 cbc.size = 2048; 347 cbc.pos = 0; 348 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 349 | (enum MHD_FLAG) poll_flag, 350 global_port, NULL, NULL, 351 &ahc_echo, NULL, 352 MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT, 353 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 354 MHD_OPTION_END); 355 if (d == NULL) 356 return 16; 357 if (0 == global_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 global_port = dinfo->port; 366 } 367 c = curl_easy_init (); 368 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH); 369 curl_easy_setopt (c, CURLOPT_PORT, (long) global_port); 370 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 371 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 372 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 373 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 374 if (oneone) 375 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 376 else 377 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 378 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 379 /* NOTE: use of CONNECTTIMEOUT without also 380 setting NOSIGNAL results in really weird 381 crashes on my system!*/ 382 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 383 curl_easy_setopt (c, CURLOPT_PROXY, ""); 384 if (CURLE_OK != (errornum = curl_easy_perform (c))) 385 { 386 fprintf (stderr, 387 "curl_easy_perform failed: `%s'\n", 388 curl_easy_strerror (errornum)); 389 curl_easy_cleanup (c); 390 MHD_stop_daemon (d); 391 return 32; 392 } 393 curl_easy_cleanup (c); 394 MHD_stop_daemon (d); 395 if (cbc.pos != strlen ("/hello_world")) 396 return 64; 397 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 398 return 128; 399 return 0; 400 } 401 402 403 static unsigned int 404 testExternalGet (int thread_unsafe) 405 { 406 struct MHD_Daemon *d; 407 CURL *c; 408 char buf[2048]; 409 struct CBC cbc; 410 CURLM *multi; 411 CURLMcode mret; 412 fd_set rs; 413 fd_set ws; 414 fd_set es; 415 MHD_socket maxsock; 416 int maxposixs; 417 int running; 418 struct CURLMsg *msg; 419 time_t start; 420 struct timeval tv; 421 422 if ( (0 == global_port) && 423 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 424 { 425 global_port = 1223; 426 if (oneone) 427 global_port += 20; 428 } 429 430 multi = NULL; 431 cbc.buf = buf; 432 cbc.size = 2048; 433 cbc.pos = 0; 434 d = MHD_start_daemon (MHD_USE_ERROR_LOG 435 | (thread_unsafe ? MHD_USE_NO_THREAD_SAFETY : 0), 436 global_port, NULL, NULL, 437 &ahc_echo, NULL, 438 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 439 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 440 MHD_OPTION_END); 441 if (d == NULL) 442 return 256; 443 if (0 == global_port) 444 { 445 const union MHD_DaemonInfo *dinfo; 446 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 447 if ((NULL == dinfo) || (0 == dinfo->port) ) 448 { 449 MHD_stop_daemon (d); return 32; 450 } 451 global_port = dinfo->port; 452 } 453 c = curl_easy_init (); 454 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH); 455 curl_easy_setopt (c, CURLOPT_PORT, (long) global_port); 456 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 457 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 458 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 459 if (oneone) 460 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 461 else 462 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 463 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 464 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 465 /* NOTE: use of CONNECTTIMEOUT without also 466 setting NOSIGNAL results in really weird 467 crashes on my system! */ 468 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 469 curl_easy_setopt (c, CURLOPT_PROXY, ""); 470 471 472 multi = curl_multi_init (); 473 if (multi == NULL) 474 { 475 curl_easy_cleanup (c); 476 MHD_stop_daemon (d); 477 return 512; 478 } 479 mret = curl_multi_add_handle (multi, c); 480 if (mret != CURLM_OK) 481 { 482 curl_multi_cleanup (multi); 483 curl_easy_cleanup (c); 484 MHD_stop_daemon (d); 485 return 1024; 486 } 487 start = time (NULL); 488 while ((time (NULL) - start < 5) && (multi != NULL)) 489 { 490 maxsock = MHD_INVALID_SOCKET; 491 maxposixs = -1; 492 FD_ZERO (&rs); 493 FD_ZERO (&ws); 494 FD_ZERO (&es); 495 curl_multi_perform (multi, &running); 496 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); 497 if (mret != CURLM_OK) 498 { 499 curl_multi_remove_handle (multi, c); 500 curl_multi_cleanup (multi); 501 curl_easy_cleanup (c); 502 MHD_stop_daemon (d); 503 return 2048; 504 } 505 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) 506 { 507 curl_multi_remove_handle (multi, c); 508 curl_multi_cleanup (multi); 509 curl_easy_cleanup (c); 510 MHD_stop_daemon (d); 511 return 4096; 512 } 513 tv.tv_sec = 0; 514 tv.tv_usec = 1000; 515 #ifdef MHD_POSIX_SOCKETS 516 if (maxsock > maxposixs) 517 maxposixs = maxsock; 518 #endif /* MHD_POSIX_SOCKETS */ 519 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) 520 { 521 #ifdef MHD_POSIX_SOCKETS 522 if (EINTR != errno) 523 { 524 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 525 (int) errno, __LINE__); 526 fflush (stderr); 527 exit (99); 528 } 529 #else 530 if ((WSAEINVAL != WSAGetLastError ()) || 531 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 532 { 533 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 534 (int) WSAGetLastError (), __LINE__); 535 fflush (stderr); 536 exit (99); 537 } 538 Sleep (1); 539 #endif 540 } 541 curl_multi_perform (multi, &running); 542 if (0 == running) 543 { 544 int pending; 545 int curl_fine = 0; 546 while (NULL != (msg = curl_multi_info_read (multi, &pending))) 547 { 548 if (msg->msg == CURLMSG_DONE) 549 { 550 if (msg->data.result == CURLE_OK) 551 curl_fine = 1; 552 else 553 { 554 fprintf (stderr, 555 "%s failed at %s:%d: `%s'\n", 556 "curl_multi_perform", 557 __FILE__, 558 __LINE__, curl_easy_strerror (msg->data.result)); 559 abort (); 560 } 561 } 562 } 563 if (! curl_fine) 564 { 565 fprintf (stderr, "libcurl haven't returned OK code\n"); 566 abort (); 567 } 568 curl_multi_remove_handle (multi, c); 569 curl_multi_cleanup (multi); 570 curl_easy_cleanup (c); 571 c = NULL; 572 multi = NULL; 573 } 574 MHD_run (d); 575 } 576 if (multi != NULL) 577 { 578 curl_multi_remove_handle (multi, c); 579 curl_easy_cleanup (c); 580 curl_multi_cleanup (multi); 581 } 582 MHD_stop_daemon (d); 583 if (cbc.pos != strlen ("/hello_world")) 584 return 8192; 585 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 586 return 16384; 587 return 0; 588 } 589 590 591 static unsigned int 592 testUnknownPortGet (uint32_t poll_flag) 593 { 594 struct MHD_Daemon *d; 595 const union MHD_DaemonInfo *di; 596 CURL *c; 597 char buf[2048]; 598 struct CBC cbc; 599 CURLcode errornum; 600 uint16_t port; 601 602 struct sockaddr_in addr; 603 socklen_t addr_len = sizeof(addr); 604 memset (&addr, 0, sizeof(addr)); 605 addr.sin_family = AF_INET; 606 addr.sin_port = 0; 607 addr.sin_addr.s_addr = INADDR_ANY; 608 609 cbc.buf = buf; 610 cbc.size = 2048; 611 cbc.pos = 0; 612 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 613 | (enum MHD_FLAG) poll_flag, 614 0, NULL, NULL, &ahc_echo, NULL, 615 MHD_OPTION_SOCK_ADDR, &addr, 616 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 617 MHD_OPTION_END); 618 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 619 { 620 di = MHD_get_daemon_info (d, MHD_DAEMON_INFO_LISTEN_FD); 621 if (di == NULL) 622 return 65536; 623 624 if (0 != getsockname (di->listen_fd, (struct sockaddr *) &addr, &addr_len)) 625 return 131072; 626 627 if (addr.sin_family != AF_INET) 628 return 26214; 629 port = ntohs (addr.sin_port); 630 } 631 else 632 { 633 const union MHD_DaemonInfo *dinfo; 634 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 635 if ((NULL == dinfo) || (0 == dinfo->port) ) 636 { 637 MHD_stop_daemon (d); return 32; 638 } 639 port = dinfo->port; 640 } 641 642 snprintf (buf, 643 sizeof(buf), 644 "http://127.0.0.1:%u%s", 645 (unsigned int) port, 646 EXPECTED_URI_PATH); 647 648 c = curl_easy_init (); 649 curl_easy_setopt (c, CURLOPT_URL, buf); 650 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 651 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 652 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 653 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 654 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 655 if (oneone) 656 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 657 else 658 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 659 /* NOTE: use of CONNECTTIMEOUT without also 660 setting NOSIGNAL results in really weird 661 crashes on my system! */ 662 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 663 curl_easy_setopt (c, CURLOPT_PROXY, ""); 664 if (CURLE_OK != (errornum = curl_easy_perform (c))) 665 { 666 fprintf (stderr, 667 "curl_easy_perform failed: `%s'\n", 668 curl_easy_strerror (errornum)); 669 curl_easy_cleanup (c); 670 MHD_stop_daemon (d); 671 return 524288; 672 } 673 curl_easy_cleanup (c); 674 MHD_stop_daemon (d); 675 if (cbc.pos != strlen ("/hello_world")) 676 return 1048576; 677 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 678 return 2097152; 679 return 0; 680 } 681 682 683 static unsigned int 684 testStopRace (uint32_t poll_flag) 685 { 686 struct sockaddr_in sin; 687 MHD_socket fd; 688 struct MHD_Daemon *d; 689 690 if ( (0 == global_port) && 691 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 692 { 693 global_port = 1224; 694 if (oneone) 695 global_port += 20; 696 } 697 698 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 699 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 700 | (enum MHD_FLAG) poll_flag, 701 global_port, NULL, NULL, 702 &ahc_echo, NULL, 703 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 704 MHD_OPTION_END); 705 if (d == NULL) 706 return 16; 707 if (0 == global_port) 708 { 709 const union MHD_DaemonInfo *dinfo; 710 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 711 if ((NULL == dinfo) || (0 == dinfo->port) ) 712 { 713 MHD_stop_daemon (d); return 32; 714 } 715 global_port = dinfo->port; 716 } 717 718 fd = socket (PF_INET, SOCK_STREAM, 0); 719 if (fd == MHD_INVALID_SOCKET) 720 { 721 fprintf (stderr, "socket error\n"); 722 return 256; 723 } 724 725 memset (&sin, 0, sizeof(sin)); 726 sin.sin_family = AF_INET; 727 sin.sin_port = htons (global_port); 728 sin.sin_addr.s_addr = htonl (0x7f000001); 729 730 if (connect (fd, (struct sockaddr *) (&sin), sizeof(sin)) < 0) 731 { 732 fprintf (stderr, "connect error\n"); 733 MHD_socket_close_chk_ (fd); 734 return 512; 735 } 736 737 /* printf("Waiting\n"); */ 738 /* Let the thread get going. */ 739 usleep (500000); 740 741 /* printf("Stopping daemon\n"); */ 742 MHD_stop_daemon (d); 743 744 MHD_socket_close_chk_ (fd); 745 746 /* printf("good\n"); */ 747 return 0; 748 } 749 750 751 static enum MHD_Result 752 ahc_empty (void *cls, 753 struct MHD_Connection *connection, 754 const char *url, 755 const char *method, 756 const char *version, 757 const char *upload_data, 758 size_t *upload_data_size, 759 void **req_cls) 760 { 761 static int ptr; 762 struct MHD_Response *response; 763 enum MHD_Result ret; 764 (void) cls; 765 (void) url; 766 (void) url; 767 (void) version; /* Unused. Silent compiler warning. */ 768 (void) upload_data; 769 (void) upload_data_size; /* Unused. Silent compiler warning. */ 770 771 if (0 != strcmp ("GET", method)) 772 return MHD_NO; /* unexpected method */ 773 if (&ptr != *req_cls) 774 { 775 *req_cls = &ptr; 776 return MHD_YES; 777 } 778 *req_cls = NULL; 779 response = MHD_create_response_empty (MHD_RF_NONE); 780 ret = MHD_queue_response (connection, 781 MHD_HTTP_OK, 782 response); 783 MHD_destroy_response (response); 784 if (ret == MHD_NO) 785 { 786 fprintf (stderr, "Failed to queue response.\n"); 787 _exit (20); 788 } 789 return ret; 790 } 791 792 793 static int 794 curlExcessFound (CURL *c, 795 curl_infotype type, 796 char *data, 797 size_t size, 798 void *cls) 799 { 800 static const char *excess_found = "Excess found"; 801 const size_t str_size = strlen (excess_found); 802 (void) c; /* Unused. Silent compiler warning. */ 803 804 if ((CURLINFO_TEXT == type) 805 && (size >= str_size) 806 && (0 == strncmp (excess_found, data, str_size))) 807 *(int *) cls = 1; 808 return 0; 809 } 810 811 812 static unsigned int 813 testEmptyGet (uint32_t poll_flag) 814 { 815 struct MHD_Daemon *d; 816 CURL *c; 817 char buf[2048]; 818 struct CBC cbc; 819 CURLcode errornum; 820 int excess_found = 0; 821 822 if ( (0 == global_port) && 823 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 824 { 825 global_port = 1225; 826 if (oneone) 827 global_port += 20; 828 } 829 830 cbc.buf = buf; 831 cbc.size = 2048; 832 cbc.pos = 0; 833 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 834 | (enum MHD_FLAG) poll_flag, 835 global_port, NULL, NULL, 836 &ahc_empty, NULL, 837 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 838 MHD_OPTION_END); 839 if (d == NULL) 840 return 4194304; 841 if (0 == global_port) 842 { 843 const union MHD_DaemonInfo *dinfo; 844 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 845 if ((NULL == dinfo) || (0 == dinfo->port) ) 846 { 847 MHD_stop_daemon (d); return 32; 848 } 849 global_port = dinfo->port; 850 } 851 c = curl_easy_init (); 852 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH); 853 curl_easy_setopt (c, CURLOPT_PORT, (long) global_port); 854 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 855 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 856 curl_easy_setopt (c, CURLOPT_DEBUGFUNCTION, &curlExcessFound); 857 curl_easy_setopt (c, CURLOPT_DEBUGDATA, &excess_found); 858 curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); 859 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 860 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 861 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 862 if (oneone) 863 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 864 else 865 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 866 /* NOTE: use of CONNECTTIMEOUT without also 867 setting NOSIGNAL results in really weird 868 crashes on my system!*/ 869 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 870 curl_easy_setopt (c, CURLOPT_PROXY, ""); 871 if (CURLE_OK != (errornum = curl_easy_perform (c))) 872 { 873 fprintf (stderr, 874 "curl_easy_perform failed: `%s'\n", 875 curl_easy_strerror (errornum)); 876 curl_easy_cleanup (c); 877 MHD_stop_daemon (d); 878 return 8388608; 879 } 880 curl_easy_cleanup (c); 881 MHD_stop_daemon (d); 882 if (cbc.pos != 0) 883 return 16777216; 884 if (excess_found) 885 return 33554432; 886 return 0; 887 } 888 889 890 int 891 main (int argc, char *const *argv) 892 { 893 unsigned int errorCount = 0; 894 unsigned int test_result = 0; 895 int verbose = 0; 896 897 if ((NULL == argv) || (0 == argv[0])) 898 return 99; 899 oneone = has_in_name (argv[0], "11"); 900 verbose = has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose"); 901 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 902 return 2; 903 global_port = 0; 904 test_result = testExternalGet (! 0); 905 if (test_result) 906 fprintf (stderr, "FAILED: testExternalGet (!0) - %u.\n", test_result); 907 else if (verbose) 908 printf ("PASSED: testExternalGet ().\n"); 909 errorCount += test_result; 910 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 911 { 912 test_result += testExternalGet (0); 913 if (test_result) 914 fprintf (stderr, "FAILED: testExternalGet (0) - %u.\n", test_result); 915 else if (verbose) 916 printf ("PASSED: testExternalGet ().\n"); 917 errorCount += test_result; 918 test_result += testInternalGet (0); 919 if (test_result) 920 fprintf (stderr, "FAILED: testInternalGet (0) - %u.\n", test_result); 921 else if (verbose) 922 printf ("PASSED: testInternalGet (0).\n"); 923 errorCount += test_result; 924 test_result += testMultithreadedGet (0); 925 if (test_result) 926 fprintf (stderr, "FAILED: testMultithreadedGet (0) - %u.\n", test_result); 927 else if (verbose) 928 printf ("PASSED: testMultithreadedGet (0).\n"); 929 errorCount += test_result; 930 test_result += testMultithreadedPoolGet (0); 931 if (test_result) 932 fprintf (stderr, "FAILED: testMultithreadedPoolGet (0) - %u.\n", 933 test_result); 934 else if (verbose) 935 printf ("PASSED: testMultithreadedPoolGet (0).\n"); 936 errorCount += test_result; 937 test_result += testUnknownPortGet (0); 938 if (test_result) 939 fprintf (stderr, "FAILED: testUnknownPortGet (0) - %u.\n", test_result); 940 else if (verbose) 941 printf ("PASSED: testUnknownPortGet (0).\n"); 942 errorCount += test_result; 943 test_result += testStopRace (0); 944 if (test_result) 945 fprintf (stderr, "FAILED: testStopRace (0) - %u.\n", test_result); 946 else if (verbose) 947 printf ("PASSED: testStopRace (0).\n"); 948 errorCount += test_result; 949 test_result += testEmptyGet (0); 950 if (test_result) 951 fprintf (stderr, "FAILED: testEmptyGet (0) - %u.\n", test_result); 952 else if (verbose) 953 printf ("PASSED: testEmptyGet (0).\n"); 954 errorCount += test_result; 955 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL)) 956 { 957 test_result += testInternalGet (MHD_USE_POLL); 958 if (test_result) 959 fprintf (stderr, "FAILED: testInternalGet (MHD_USE_POLL) - %u.\n", 960 test_result); 961 else if (verbose) 962 printf ("PASSED: testInternalGet (MHD_USE_POLL).\n"); 963 errorCount += test_result; 964 test_result += testMultithreadedGet (MHD_USE_POLL); 965 if (test_result) 966 fprintf (stderr, "FAILED: testMultithreadedGet (MHD_USE_POLL) - %u.\n", 967 test_result); 968 else if (verbose) 969 printf ("PASSED: testMultithreadedGet (MHD_USE_POLL).\n"); 970 errorCount += test_result; 971 test_result += testMultithreadedPoolGet (MHD_USE_POLL); 972 if (test_result) 973 fprintf (stderr, 974 "FAILED: testMultithreadedPoolGet (MHD_USE_POLL) - %u.\n", 975 test_result); 976 else if (verbose) 977 printf ("PASSED: testMultithreadedPoolGet (MHD_USE_POLL).\n"); 978 errorCount += test_result; 979 test_result += testUnknownPortGet (MHD_USE_POLL); 980 if (test_result) 981 fprintf (stderr, "FAILED: testUnknownPortGet (MHD_USE_POLL) - %u.\n", 982 test_result); 983 else if (verbose) 984 printf ("PASSED: testUnknownPortGet (MHD_USE_POLL).\n"); 985 errorCount += test_result; 986 test_result += testStopRace (MHD_USE_POLL); 987 if (test_result) 988 fprintf (stderr, "FAILED: testStopRace (MHD_USE_POLL) - %u.\n", 989 test_result); 990 else if (verbose) 991 printf ("PASSED: testStopRace (MHD_USE_POLL).\n"); 992 errorCount += test_result; 993 test_result += testEmptyGet (MHD_USE_POLL); 994 if (test_result) 995 fprintf (stderr, "FAILED: testEmptyGet (MHD_USE_POLL) - %u.\n", 996 test_result); 997 else if (verbose) 998 printf ("PASSED: testEmptyGet (MHD_USE_POLL).\n"); 999 errorCount += test_result; 1000 } 1001 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL)) 1002 { 1003 test_result += testInternalGet (MHD_USE_EPOLL); 1004 if (test_result) 1005 fprintf (stderr, "FAILED: testInternalGet (MHD_USE_EPOLL) - %u.\n", 1006 test_result); 1007 else if (verbose) 1008 printf ("PASSED: testInternalGet (MHD_USE_EPOLL).\n"); 1009 errorCount += test_result; 1010 test_result += testMultithreadedPoolGet (MHD_USE_EPOLL); 1011 if (test_result) 1012 fprintf (stderr, 1013 "FAILED: testMultithreadedPoolGet (MHD_USE_EPOLL) - %u.\n", 1014 test_result); 1015 else if (verbose) 1016 printf ("PASSED: testMultithreadedPoolGet (MHD_USE_EPOLL).\n"); 1017 errorCount += test_result; 1018 test_result += testUnknownPortGet (MHD_USE_EPOLL); 1019 if (test_result) 1020 fprintf (stderr, "FAILED: testUnknownPortGet (MHD_USE_EPOLL) - %u.\n", 1021 test_result); 1022 else if (verbose) 1023 printf ("PASSED: testUnknownPortGet (MHD_USE_EPOLL).\n"); 1024 errorCount += test_result; 1025 test_result += testEmptyGet (MHD_USE_EPOLL); 1026 if (test_result) 1027 fprintf (stderr, "FAILED: testEmptyGet (MHD_USE_EPOLL) - %u.\n", 1028 test_result); 1029 else if (verbose) 1030 printf ("PASSED: testEmptyGet (MHD_USE_EPOLL).\n"); 1031 errorCount += test_result; 1032 } 1033 } 1034 if (0 != errorCount) 1035 fprintf (stderr, 1036 "Error (code: %u)\n", 1037 errorCount); 1038 else if (verbose) 1039 printf ("All tests passed.\n"); 1040 curl_global_cleanup (); 1041 return (0 == errorCount) ? 0 : 1; /* 0 == pass */ 1042 }