test_put.c (16348B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007 Christian Grothoff 4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) 5 6 libmicrohttpd is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published 8 by the Free Software Foundation; either version 2, or (at your 9 option) any later version. 10 11 libmicrohttpd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with libmicrohttpd; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 Boston, MA 02110-1301, USA. 20 */ 21 22 /** 23 * @file daemontest_put.c 24 * @brief Testcase for libmicrohttpd PUT operations 25 * @author Christian Grothoff 26 * @author Karlson2k (Evgeny Grin) 27 */ 28 29 #include "MHD_config.h" 30 #include "platform.h" 31 #include <curl/curl.h> 32 #include <microhttpd.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <time.h> 36 #include <errno.h> 37 #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 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 48 #undef MHD_CPU_COUNT 49 #endif 50 #if ! defined(MHD_CPU_COUNT) 51 #define MHD_CPU_COUNT 2 52 #endif 53 54 static int oneone; 55 56 struct CBC 57 { 58 char *buf; 59 size_t pos; 60 size_t size; 61 }; 62 63 static size_t 64 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr) 65 { 66 size_t *pos = ptr; 67 size_t wrt; 68 69 wrt = size * nmemb; 70 if (wrt > 8 - (*pos)) 71 wrt = 8 - (*pos); 72 memcpy (stream, &("Hello123"[*pos]), wrt); 73 (*pos) += wrt; 74 return wrt; 75 } 76 77 78 static size_t 79 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 80 { 81 struct CBC *cbc = ctx; 82 83 if (cbc->pos + size * nmemb > cbc->size) 84 return 0; /* overflow */ 85 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 86 cbc->pos += size * nmemb; 87 return size * nmemb; 88 } 89 90 91 static enum MHD_Result 92 ahc_echo (void *cls, 93 struct MHD_Connection *connection, 94 const char *url, 95 const char *method, 96 const char *version, 97 const char *upload_data, size_t *upload_data_size, 98 void **req_cls) 99 { 100 int *done = cls; 101 struct MHD_Response *response; 102 enum MHD_Result ret; 103 (void) version; (void) req_cls; /* Unused. Silent compiler warning. */ 104 105 if (0 != strcmp ("PUT", method)) 106 return MHD_NO; /* unexpected method */ 107 if ((*done) == 0) 108 { 109 if (*upload_data_size != 8) 110 return MHD_YES; /* not yet ready */ 111 if (0 == memcmp (upload_data, "Hello123", 8)) 112 { 113 *upload_data_size = 0; 114 } 115 else 116 { 117 printf ("Invalid upload data `%8s'!\n", upload_data); 118 return MHD_NO; 119 } 120 *done = 1; 121 return MHD_YES; 122 } 123 response = MHD_create_response_from_buffer_copy (strlen (url), 124 (const void *) url); 125 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 126 MHD_destroy_response (response); 127 return ret; 128 } 129 130 131 static unsigned int 132 testInternalPut (void) 133 { 134 struct MHD_Daemon *d; 135 CURL *c; 136 char buf[2048]; 137 struct CBC cbc; 138 size_t pos = 0; 139 int done_flag = 0; 140 CURLcode errornum; 141 uint16_t port; 142 143 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 144 port = 0; 145 else 146 { 147 port = 1450; 148 if (oneone) 149 port += 10; 150 } 151 152 cbc.buf = buf; 153 cbc.size = 2048; 154 cbc.pos = 0; 155 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 156 port, 157 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); 158 if (d == NULL) 159 return 1; 160 if (0 == port) 161 { 162 const union MHD_DaemonInfo *dinfo; 163 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 164 if ((NULL == dinfo) || (0 == dinfo->port) ) 165 { 166 MHD_stop_daemon (d); return 32; 167 } 168 port = dinfo->port; 169 } 170 c = curl_easy_init (); 171 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 172 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 173 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 174 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 175 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 176 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 177 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 178 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); 179 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 180 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 181 if (oneone) 182 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 183 else 184 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 185 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 186 /* NOTE: use of CONNECTTIMEOUT without also 187 * setting NOSIGNAL results in really weird 188 * crashes on my system! */ 189 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 190 curl_easy_setopt (c, CURLOPT_PROXY, ""); 191 if (CURLE_OK != (errornum = curl_easy_perform (c))) 192 { 193 fprintf (stderr, 194 "curl_easy_perform failed: `%s'\n", 195 curl_easy_strerror (errornum)); 196 curl_easy_cleanup (c); 197 MHD_stop_daemon (d); 198 return 2; 199 } 200 curl_easy_cleanup (c); 201 MHD_stop_daemon (d); 202 if (cbc.pos != strlen ("/hello_world")) 203 return 4; 204 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 205 return 8; 206 return 0; 207 } 208 209 210 static unsigned int 211 testMultithreadedPut (void) 212 { 213 struct MHD_Daemon *d; 214 CURL *c; 215 char buf[2048]; 216 struct CBC cbc; 217 size_t pos = 0; 218 int done_flag = 0; 219 CURLcode errornum; 220 uint16_t port; 221 222 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 223 port = 0; 224 else 225 { 226 port = 1451; 227 if (oneone) 228 port += 10; 229 } 230 231 cbc.buf = buf; 232 cbc.size = 2048; 233 cbc.pos = 0; 234 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 235 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 236 port, 237 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); 238 if (d == NULL) 239 return 16; 240 if (0 == port) 241 { 242 const union MHD_DaemonInfo *dinfo; 243 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 244 if ((NULL == dinfo) || (0 == dinfo->port) ) 245 { 246 MHD_stop_daemon (d); return 32; 247 } 248 port = dinfo->port; 249 } 250 c = curl_easy_init (); 251 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 252 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 253 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 254 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 255 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 256 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 257 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 258 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); 259 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 260 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 261 if (oneone) 262 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 263 else 264 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 265 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 266 /* NOTE: use of CONNECTTIMEOUT without also 267 * setting NOSIGNAL results in really weird 268 * crashes on my system! */ 269 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 270 curl_easy_setopt (c, CURLOPT_PROXY, ""); 271 if (CURLE_OK != (errornum = curl_easy_perform (c))) 272 { 273 fprintf (stderr, 274 "curl_easy_perform failed: `%s'\n", 275 curl_easy_strerror (errornum)); 276 curl_easy_cleanup (c); 277 MHD_stop_daemon (d); 278 return 32; 279 } 280 curl_easy_cleanup (c); 281 MHD_stop_daemon (d); 282 if (cbc.pos != strlen ("/hello_world")) 283 return 64; 284 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 285 return 128; 286 287 return 0; 288 } 289 290 291 static unsigned int 292 testMultithreadedPoolPut (void) 293 { 294 struct MHD_Daemon *d; 295 CURL *c; 296 char buf[2048]; 297 struct CBC cbc; 298 size_t pos = 0; 299 int done_flag = 0; 300 CURLcode errornum; 301 uint16_t port; 302 303 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 304 port = 0; 305 else 306 { 307 port = 1452; 308 if (oneone) 309 port += 10; 310 } 311 312 cbc.buf = buf; 313 cbc.size = 2048; 314 cbc.pos = 0; 315 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 316 port, 317 NULL, NULL, &ahc_echo, &done_flag, 318 MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT, 319 MHD_OPTION_END); 320 if (d == NULL) 321 return 16; 322 if (0 == port) 323 { 324 const union MHD_DaemonInfo *dinfo; 325 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 326 if ((NULL == dinfo) || (0 == dinfo->port) ) 327 { 328 MHD_stop_daemon (d); return 32; 329 } 330 port = dinfo->port; 331 } 332 c = curl_easy_init (); 333 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 334 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 335 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 336 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 337 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 338 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 339 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 340 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); 341 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 342 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 343 if (oneone) 344 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 345 else 346 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 347 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 348 /* NOTE: use of CONNECTTIMEOUT without also 349 * setting NOSIGNAL results in really weird 350 * crashes on my system! */ 351 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 352 curl_easy_setopt (c, CURLOPT_PROXY, ""); 353 if (CURLE_OK != (errornum = curl_easy_perform (c))) 354 { 355 fprintf (stderr, 356 "curl_easy_perform failed: `%s'\n", 357 curl_easy_strerror (errornum)); 358 curl_easy_cleanup (c); 359 MHD_stop_daemon (d); 360 return 32; 361 } 362 curl_easy_cleanup (c); 363 MHD_stop_daemon (d); 364 if (cbc.pos != strlen ("/hello_world")) 365 return 64; 366 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 367 return 128; 368 369 return 0; 370 } 371 372 373 static unsigned int 374 testExternalPut (void) 375 { 376 struct MHD_Daemon *d; 377 CURL *c; 378 char buf[2048]; 379 struct CBC cbc; 380 CURLM *multi; 381 CURLMcode mret; 382 fd_set rs; 383 fd_set ws; 384 fd_set es; 385 MHD_socket maxsock; 386 int maxposixs; /* Max socket number unused on W32 */ 387 int running; 388 struct CURLMsg *msg; 389 time_t start; 390 struct timeval tv; 391 size_t pos = 0; 392 int done_flag = 0; 393 uint16_t port; 394 395 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 396 port = 0; 397 else 398 { 399 port = 1453; 400 if (oneone) 401 port += 10; 402 } 403 404 multi = NULL; 405 cbc.buf = buf; 406 cbc.size = 2048; 407 cbc.pos = 0; 408 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 409 port, 410 NULL, NULL, &ahc_echo, &done_flag, 411 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 412 MHD_OPTION_END); 413 if (d == NULL) 414 return 256; 415 if (0 == port) 416 { 417 const union MHD_DaemonInfo *dinfo; 418 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 419 if ((NULL == dinfo) || (0 == dinfo->port) ) 420 { 421 MHD_stop_daemon (d); return 32; 422 } 423 port = dinfo->port; 424 } 425 c = curl_easy_init (); 426 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 427 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 428 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); 429 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 430 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 431 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 432 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 433 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); 434 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 435 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 436 if (oneone) 437 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 438 else 439 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 440 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 441 /* NOTE: use of CONNECTTIMEOUT without also 442 * setting NOSIGNAL results in really weird 443 * crashes on my system! */ 444 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 445 curl_easy_setopt (c, CURLOPT_PROXY, ""); 446 447 448 multi = curl_multi_init (); 449 if (multi == NULL) 450 { 451 curl_easy_cleanup (c); 452 MHD_stop_daemon (d); 453 return 512; 454 } 455 mret = curl_multi_add_handle (multi, c); 456 if (mret != CURLM_OK) 457 { 458 curl_multi_cleanup (multi); 459 curl_easy_cleanup (c); 460 MHD_stop_daemon (d); 461 return 1024; 462 } 463 start = time (NULL); 464 while ((time (NULL) - start < 5) && (multi != NULL)) 465 { 466 maxsock = MHD_INVALID_SOCKET; 467 maxposixs = -1; 468 FD_ZERO (&rs); 469 FD_ZERO (&ws); 470 FD_ZERO (&es); 471 curl_multi_perform (multi, &running); 472 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); 473 if (mret != CURLM_OK) 474 { 475 curl_multi_remove_handle (multi, c); 476 curl_multi_cleanup (multi); 477 curl_easy_cleanup (c); 478 MHD_stop_daemon (d); 479 return 2048; 480 } 481 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) 482 { 483 curl_multi_remove_handle (multi, c); 484 curl_multi_cleanup (multi); 485 curl_easy_cleanup (c); 486 MHD_stop_daemon (d); 487 return 4096; 488 } 489 #ifdef MHD_POSIX_SOCKETS 490 if (maxsock > maxposixs) 491 maxposixs = maxsock; 492 #endif /* MHD_POSIX_SOCKETS */ 493 tv.tv_sec = 0; 494 tv.tv_usec = 1000; 495 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) 496 { 497 #ifdef MHD_POSIX_SOCKETS 498 if (EINTR != errno) 499 { 500 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 501 (int) errno, __LINE__); 502 fflush (stderr); 503 exit (99); 504 } 505 #else 506 if ((WSAEINVAL != WSAGetLastError ()) || 507 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 508 { 509 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 510 (int) WSAGetLastError (), __LINE__); 511 fflush (stderr); 512 exit (99); 513 } 514 Sleep (1); 515 #endif 516 } 517 curl_multi_perform (multi, &running); 518 if (0 == running) 519 { 520 int pending; 521 int curl_fine = 0; 522 while (NULL != (msg = curl_multi_info_read (multi, &pending))) 523 { 524 if (msg->msg == CURLMSG_DONE) 525 { 526 if (msg->data.result == CURLE_OK) 527 curl_fine = 1; 528 else 529 { 530 fprintf (stderr, 531 "%s failed at %s:%d: `%s'\n", 532 "curl_multi_perform", 533 __FILE__, 534 __LINE__, curl_easy_strerror (msg->data.result)); 535 abort (); 536 } 537 } 538 } 539 if (! curl_fine) 540 { 541 fprintf (stderr, "libcurl haven't returned OK code\n"); 542 abort (); 543 } 544 curl_multi_remove_handle (multi, c); 545 curl_multi_cleanup (multi); 546 curl_easy_cleanup (c); 547 c = NULL; 548 multi = NULL; 549 } 550 MHD_run (d); 551 } 552 if (multi != NULL) 553 { 554 curl_multi_remove_handle (multi, c); 555 curl_easy_cleanup (c); 556 curl_multi_cleanup (multi); 557 } 558 MHD_stop_daemon (d); 559 if (cbc.pos != strlen ("/hello_world")) 560 return 8192; 561 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 562 return 16384; 563 return 0; 564 } 565 566 567 int 568 main (int argc, char *const *argv) 569 { 570 unsigned int errorCount = 0; 571 (void) argc; /* Unused. Silent compiler warning. */ 572 573 if ((NULL == argv) || (0 == argv[0])) 574 return 99; 575 oneone = has_in_name (argv[0], "11"); 576 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 577 return 2; 578 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 579 { 580 errorCount += testInternalPut (); 581 errorCount += testMultithreadedPut (); 582 errorCount += testMultithreadedPoolPut (); 583 } 584 errorCount += testExternalPut (); 585 if (errorCount != 0) 586 fprintf (stderr, "Error (code: %u)\n", errorCount); 587 curl_global_cleanup (); 588 return (0 == errorCount) ? 0 : 1; /* 0 == pass */ 589 }