test_long_header.c (15053B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007 Christian Grothoff 4 Copyright (C) 2016-2023 Evgeny Grin (Karlson2k) 5 6 libmicrohttpd is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published 8 by the Free Software Foundation; either version 2, or (at your 9 option) any later version. 10 11 libmicrohttpd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with libmicrohttpd; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 Boston, MA 02110-1301, USA. 20 */ 21 22 /** 23 * @file test_long_header.c 24 * @brief Testcase for libmicrohttpd handling of very long headers 25 * @author Christian Grothoff 26 * @author Karlson2k (Evgeny Grin) 27 */ 28 29 #include "mhd_options.h" 30 #include "platform.h" 31 #include <curl/curl.h> 32 #include <microhttpd.h> 33 #include <stdlib.h> 34 #include <stdio.h> 35 #include <string.h> 36 #include <time.h> 37 #include "mhd_has_in_name.h" 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 #ifndef WINDOWS 44 #include <unistd.h> 45 #endif 46 47 #ifndef MHD_STATICSTR_LEN_ 48 /** 49 * Determine length of static string / macro strings at compile time. 50 */ 51 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1) 52 #endif /* ! MHD_STATICSTR_LEN_ */ 53 54 #ifndef CURL_VERSION_BITS 55 #define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z)) 56 #endif /* ! CURL_VERSION_BITS */ 57 #ifndef CURL_AT_LEAST_VERSION 58 #define CURL_AT_LEAST_VERSION(x,y,z) \ 59 (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z)) 60 #endif /* ! CURL_AT_LEAST_VERSION */ 61 62 /** 63 * We will set the memory available per connection to 64 * half of this value, so the actual value does not have 65 * to be big at all... 66 */ 67 #define VERY_LONG (1024 * 8) 68 69 /* Could be increased to facilitate debugging */ 70 #define TIMEOUTS_VAL 5 71 72 #define EXPECTED_URI_BASE_PATH "/" 73 74 #define URL_SCHEME "http:/" "/" 75 76 #define URL_HOST "127.0.0.1" 77 78 #define URL_SCHEME_HOST_PATH URL_SCHEME URL_HOST EXPECTED_URI_BASE_PATH 79 80 81 static int oneone; 82 83 static uint16_t daemon_port; 84 85 86 static char libcurl_err_buf[CURL_ERROR_SIZE]; 87 88 struct CBC 89 { 90 char *buf; 91 size_t pos; 92 size_t size; 93 }; 94 95 static size_t 96 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 97 { 98 (void) ptr; (void) ctx; /* Unused. Silent compiler warning. */ 99 return size * nmemb; 100 } 101 102 103 /* Return non-zero on success, zero on failure */ 104 static int 105 setup_easy_handler_params (CURL *c, struct CBC *pcbc, const char *url, 106 struct curl_slist *header) 107 { 108 libcurl_err_buf[0] = 0; /* Reset error message */ 109 110 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_err_buf)) 111 { 112 fprintf (stderr, "Failed to set CURLOPT_ERRORBUFFER option.\n"); 113 return 0; 114 } 115 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) 116 { 117 fprintf (stderr, "Failed to set CURLOPT_NOSIGNAL option.\n"); 118 return 0; 119 } 120 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) 121 { 122 fprintf (stderr, "Failed to set CURLOPT_PROXY option.\n"); 123 return 0; 124 } 125 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, 126 ©Buffer)) 127 { 128 fprintf (stderr, "Failed to set CURLOPT_WRITEFUNCTION option.\n"); 129 return 0; 130 } 131 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, pcbc)) 132 { 133 fprintf (stderr, "Failed to set CURLOPT_WRITEDATA option.\n"); 134 return 0; 135 } 136 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 137 ((long) TIMEOUTS_VAL))) 138 { 139 fprintf (stderr, "Failed to set CURLOPT_CONNECTTIMEOUT option.\n"); 140 return 0; 141 } 142 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT, 143 ((long) TIMEOUTS_VAL))) 144 { 145 fprintf (stderr, "Failed to set CURLOPT_TIMEOUT option.\n"); 146 return 0; 147 } 148 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, 149 (oneone) ? 150 CURL_HTTP_VERSION_1_1 : 151 CURL_HTTP_VERSION_1_0)) 152 { 153 fprintf (stderr, "Failed to set CURLOPT_HTTP_VERSION option.\n"); 154 return 0; 155 } 156 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 0L)) 157 { 158 fprintf (stderr, "Failed to set CURLOPT_FAILONERROR option.\n"); 159 return 0; 160 } 161 #ifdef _DEBUG 162 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_VERBOSE, 1L)) 163 { 164 fprintf (stderr, "Failed to set CURLOPT_VERBOSE option.\n"); 165 return 0; 166 } 167 #endif /* _DEBUG */ 168 #if CURL_AT_LEAST_VERSION (7, 45, 0) 169 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_DEFAULT_PROTOCOL, "http")) 170 { 171 fprintf (stderr, "Failed to set CURLOPT_DEFAULT_PROTOCOL option.\n"); 172 return 0; 173 } 174 #endif /* CURL_AT_LEAST_VERSION (7, 45, 0) */ 175 #if CURL_AT_LEAST_VERSION (7, 85, 0) 176 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS_STR, "http")) 177 { 178 fprintf (stderr, "Failed to set CURLOPT_PROTOCOLS_STR option.\n"); 179 return 0; 180 } 181 #elif CURL_AT_LEAST_VERSION (7, 19, 4) 182 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP)) 183 { 184 fprintf (stderr, "Failed to set CURLOPT_PROTOCOLS option.\n"); 185 return 0; 186 } 187 #endif /* CURL_AT_LEAST_VERSION (7, 19, 4) */ 188 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, 189 (NULL != url) ? 190 url : URL_SCHEME_HOST_PATH)) 191 { 192 fprintf (stderr, "Failed to set CURLOPT_PROTOCOLS option.\n"); 193 return 0; 194 } 195 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, ((long) daemon_port))) 196 { 197 fprintf (stderr, "Failed to set CURLOPT_PROTOCOLS option.\n"); 198 return 0; 199 } 200 201 if (NULL != header) 202 { 203 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPHEADER, header)) 204 { 205 fprintf (stderr, "Failed to set CURLOPT_HTTPHEADER option.\n"); 206 return 0; 207 } 208 } 209 return ! 0; 210 } 211 212 213 static CURL * 214 setup_easy_handler (struct CBC *pcbc, const char *url, struct 215 curl_slist *header) 216 { 217 CURL *c; 218 219 c = curl_easy_init (); 220 if (NULL == c) 221 { 222 fprintf (stderr, "curl_easy_init() error.\n"); 223 return NULL; 224 } 225 if (setup_easy_handler_params (c, pcbc, url, header)) 226 { 227 return c; /* Success exit point */ 228 } 229 curl_easy_cleanup (c); 230 return NULL; 231 } 232 233 234 static enum MHD_Result 235 ahc_echo (void *cls, 236 struct MHD_Connection *connection, 237 const char *url, 238 const char *method, 239 const char *version, 240 const char *upload_data, size_t *upload_data_size, 241 void **req_cls) 242 { 243 struct MHD_Response *response; 244 enum MHD_Result ret; 245 static int marker; 246 247 (void) cls; 248 (void) version; (void) upload_data; /* Unused. Silent compiler warning. */ 249 (void) upload_data_size; (void) req_cls; /* Unused. Silent compiler warning. */ 250 251 if (&marker != *req_cls) 252 { 253 *req_cls = ▮ 254 return MHD_YES; 255 } 256 if (0 != strcmp (MHD_HTTP_METHOD_GET, method)) 257 return MHD_NO; /* unexpected method */ 258 response = MHD_create_response_from_buffer_copy (strlen (url), 259 (const void *) url); 260 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 261 MHD_destroy_response (response); 262 return ret; 263 } 264 265 266 static unsigned int 267 testLongUrlGet (size_t buff_size) 268 { 269 struct MHD_Daemon *d; 270 unsigned int ret; 271 272 ret = 1; /* Error value, shall be reset to zero if succeed */ 273 d = 274 MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD /* | MHD_USE_ERROR_LOG */, 275 daemon_port, 276 NULL, 277 NULL, 278 &ahc_echo, NULL, 279 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 280 (size_t) buff_size, MHD_OPTION_END); 281 if (d == NULL) 282 { 283 fprintf (stderr, "MHD_start_daemon() failed.\n"); 284 return 16; 285 } 286 if (0 == daemon_port) 287 { 288 const union MHD_DaemonInfo *dinfo; 289 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 290 if ((NULL == dinfo) || (0 == dinfo->port) ) 291 fprintf (stderr, "MHD_get_daemon_info(d, MHD_DAEMON_INFO_BIND_PORT) " \ 292 "failed.\n"); 293 else 294 daemon_port = dinfo->port; 295 } 296 if (0 != daemon_port) 297 { 298 char *url_str; 299 url_str = malloc (VERY_LONG); 300 if (NULL == url_str) 301 fprintf (stderr, "malloc (VERY_LONG) failed.\n"); 302 else 303 { 304 CURL *c; 305 char buf[2048]; 306 struct CBC cbc; 307 308 memset (url_str, 'a', VERY_LONG); 309 url_str[VERY_LONG - 1] = '\0'; 310 memcpy (url_str, URL_SCHEME_HOST_PATH, 311 MHD_STATICSTR_LEN_ (URL_SCHEME_HOST_PATH)); 312 313 cbc.buf = buf; 314 cbc.size = sizeof (buf); 315 cbc.pos = 0; 316 317 c = setup_easy_handler (&cbc, url_str, NULL); 318 if (NULL != c) 319 { 320 CURLcode r; 321 r = curl_easy_perform (c); 322 if (CURLE_OK != r) 323 { 324 fprintf (stderr, "curl_easy_perform() failed. Error message: %s\n", 325 curl_easy_strerror (r)); 326 if (0 != libcurl_err_buf[0]) 327 fprintf (stderr, "Detailed error message: %s\n", libcurl_err_buf); 328 } 329 else 330 { 331 long code; 332 333 r = curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code); 334 if (CURLE_OK != r) 335 { 336 fprintf (stderr, "curl_easy_getinfo() failed. " 337 "Error message: %s\n", 338 curl_easy_strerror (r)); 339 if (0 != libcurl_err_buf[0]) 340 fprintf (stderr, "Detailed error message: %s\n", 341 libcurl_err_buf); 342 } 343 else 344 { 345 if (code != MHD_HTTP_URI_TOO_LONG) 346 { 347 fprintf (stderr, "testLongHeaderGet(%lu) failed. HTTP " 348 "response code is %ld, while it should be %ld.\n", 349 (unsigned long) buff_size, 350 code, 351 (long) MHD_HTTP_URI_TOO_LONG); 352 } 353 else 354 { 355 printf ("testLongHeaderGet(%lu) succeed. HTTP " 356 "response code is %ld, as expected.\n", 357 (unsigned long) buff_size, 358 (long) MHD_HTTP_URI_TOO_LONG); 359 ret = 0; /* Success */ 360 } 361 } 362 } 363 curl_easy_cleanup (c); 364 } 365 free (url_str); 366 } 367 } 368 MHD_stop_daemon (d); 369 return ret; 370 } 371 372 373 static unsigned int 374 testLongHeaderGet (size_t buff_size) 375 { 376 struct MHD_Daemon *d; 377 unsigned int ret; 378 379 ret = 1; /* Error value, shall be reset to zero if succeed */ 380 d = 381 MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD /* | MHD_USE_ERROR_LOG */, 382 daemon_port, 383 NULL, 384 NULL, 385 &ahc_echo, NULL, 386 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 387 (size_t) buff_size, MHD_OPTION_END); 388 if (d == NULL) 389 { 390 fprintf (stderr, "MHD_start_daemon() failed.\n"); 391 return 16; 392 } 393 if (0 == daemon_port) 394 { 395 const union MHD_DaemonInfo *dinfo; 396 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 397 if ((NULL == dinfo) || (0 == dinfo->port) ) 398 fprintf (stderr, "MHD_get_daemon_info(d, MHD_DAEMON_INFO_BIND_PORT) " \ 399 "failed.\n"); 400 else 401 daemon_port = dinfo->port; 402 } 403 if (0 != daemon_port) 404 { 405 char *header_str; 406 header_str = malloc (VERY_LONG); 407 if (NULL == header_str) 408 fprintf (stderr, "malloc (VERY_LONG) failed.\n"); 409 else 410 { 411 struct curl_slist *header = NULL; 412 413 memset (header_str, 'a', VERY_LONG); 414 header_str[VERY_LONG - 1] = '\0'; 415 header_str[VERY_LONG / 2] = ':'; 416 header_str[VERY_LONG / 2 + 1] = ' '; 417 418 header = curl_slist_append (header, header_str); 419 if (NULL == header) 420 fprintf (stderr, "curl_slist_append () failed.\n"); 421 else 422 { 423 CURL *c; 424 char buf[2048]; 425 struct CBC cbc; 426 427 cbc.buf = buf; 428 cbc.size = sizeof (buf); 429 cbc.pos = 0; 430 431 c = setup_easy_handler (&cbc, NULL, header); 432 if (NULL != c) 433 { 434 CURLcode r; 435 r = curl_easy_perform (c); 436 if (CURLE_OK != r) 437 { 438 fprintf (stderr, "curl_easy_perform() failed. Error message: %s\n", 439 curl_easy_strerror (r)); 440 if (0 != libcurl_err_buf[0]) 441 fprintf (stderr, "Detailed error message: %s\n", libcurl_err_buf); 442 } 443 else 444 { 445 long code; 446 447 r = curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code); 448 if (CURLE_OK != r) 449 { 450 fprintf (stderr, "curl_easy_getinfo() failed. " 451 "Error message: %s\n", 452 curl_easy_strerror (r)); 453 if (0 != libcurl_err_buf[0]) 454 fprintf (stderr, "Detailed error message: %s\n", 455 libcurl_err_buf); 456 } 457 else 458 { 459 if (code != MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE) 460 { 461 fprintf (stderr, "testLongHeaderGet(%lu) failed. HTTP " 462 "response code is %ld, while it should be %ld.\n", 463 (unsigned long) buff_size, 464 code, 465 (long) MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE); 466 } 467 else 468 { 469 printf ("testLongHeaderGet(%lu) succeed. HTTP " 470 "response code is %ld, as expected.\n", 471 (unsigned long) buff_size, 472 (long) MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE); 473 ret = 0; /* Success */ 474 } 475 } 476 } 477 curl_easy_cleanup (c); 478 } 479 curl_slist_free_all (header); 480 } 481 free (header_str); 482 } 483 } 484 MHD_stop_daemon (d); 485 return ret; 486 } 487 488 489 int 490 main (int argc, char *const *argv) 491 { 492 unsigned int errorCount = 0; 493 (void) argc; /* Unused. Silent compiler warning. */ 494 495 if ((NULL == argv) || (0 == argv[0])) 496 return 99; 497 oneone = has_in_name (argv[0], "11"); 498 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 499 return 2; 500 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 501 daemon_port = 0; 502 else 503 daemon_port = oneone ? 1336 : 1331; 504 505 errorCount += testLongUrlGet (VERY_LONG / 2); 506 errorCount += testLongUrlGet (VERY_LONG / 2 + 978); 507 errorCount += testLongHeaderGet (VERY_LONG / 2); 508 errorCount += testLongHeaderGet (VERY_LONG / 2 + 1893); 509 if (errorCount != 0) 510 fprintf (stderr, "Error (code: %u)\n", errorCount); 511 else 512 printf ("Test succeed.\n"); 513 curl_global_cleanup (); 514 return (0 == errorCount) ? 0 : 1; /* 0 == pass */ 515 }