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