internal.h (78119B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff 4 Copyright (C) 2014-2023 Evgeny Grin (Karlson2k) 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 /** 22 * @file microhttpd/internal.h 23 * @brief MHD internal shared structures 24 * @author Daniel Pittman 25 * @author Christian Grothoff 26 * @author Karlson2k (Evgeny Grin) 27 */ 28 29 #ifndef INTERNAL_H 30 #define INTERNAL_H 31 32 #include "mhd_options.h" 33 #include "platform.h" 34 #include "microhttpd.h" 35 #include "mhd_assert.h" 36 37 #ifdef HTTPS_SUPPORT 38 #include <gnutls/gnutls.h> 39 #if GNUTLS_VERSION_MAJOR >= 3 40 #include <gnutls/abstract.h> 41 #endif 42 #endif /* HTTPS_SUPPORT */ 43 44 #ifdef HAVE_STDBOOL_H 45 #include <stdbool.h> 46 #endif 47 48 #ifdef HAVE_INTTYPES_H 49 #include <inttypes.h> 50 #endif /* HAVE_INTTYPES_H */ 51 52 #ifndef PRIu64 53 #define PRIu64 "llu" 54 #endif /* ! PRIu64 */ 55 56 /* Must be included before other internal headers! */ 57 #include "mhd_panic.h" 58 59 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 60 #include "mhd_threads.h" 61 #endif 62 #include "mhd_locks.h" 63 #include "mhd_sockets.h" 64 #include "mhd_itc_types.h" 65 #include "mhd_str_types.h" 66 #if defined(BAUTH_SUPPORT) || defined(DAUTH_SUPPORT) 67 #include "gen_auth.h" 68 #endif /* BAUTH_SUPPORT || DAUTH_SUPPORT*/ 69 70 71 /** 72 * Macro to drop 'const' qualifier from pointer without compiler warning. 73 * To be used *only* to deal with broken external APIs, which require non-const 74 * pointer to unmodifiable data. 75 * Must not be used to transform pointers for MHD needs. 76 */ 77 #define _MHD_DROP_CONST(ptr) ((void *) ((uintptr_t) ((const void *) (ptr)))) 78 79 /** 80 * @def _MHD_MACRO_NO 81 * "Negative answer"/"false" for use in macros, meaningful for precompiler 82 */ 83 #define _MHD_MACRO_NO 0 84 85 /** 86 * @def _MHD_MACRO_YES 87 * "Positive answer"/"true" for use in macros, meaningful for precompiler 88 */ 89 #define _MHD_MACRO_YES 1 90 91 /** 92 * Close FD and abort execution if error is detected. 93 * @param fd the FD to close 94 */ 95 #define MHD_fd_close_chk_(fd) do { \ 96 if ( (0 != close ((fd)) && (EBADF == errno)) ) { \ 97 MHD_PANIC (_ ("Failed to close FD.\n")); \ 98 } \ 99 } while (0) 100 101 /* 102 #define EXTRA_CHECKS _MHD_MACRO_NO 103 * Not used. Behaviour is controlled by _DEBUG/NDEBUG macros. 104 */ 105 106 #ifndef _MHD_DEBUG_CONNECT 107 /** 108 * Print extra messages when establishing 109 * connections? (only adds non-error messages). 110 */ 111 #define _MHD_DEBUG_CONNECT _MHD_MACRO_NO 112 #endif /* ! _MHD_DEBUG_CONNECT */ 113 114 #ifndef _MHD_DEBUG_SEND_DATA 115 /** 116 * Should all data send be printed to stderr? 117 */ 118 #define _MHD_DEBUG_SEND_DATA _MHD_MACRO_NO 119 #endif /* ! _MHD_DEBUG_SEND_DATA */ 120 121 #ifndef _MHD_DEBUG_CLOSE 122 /** 123 * Add extra debug messages with reasons for closing connections 124 * (non-error reasons). 125 */ 126 #define _MHD_DEBUG_CLOSE _MHD_MACRO_NO 127 #endif /* ! _MHD_DEBUG_CLOSE */ 128 129 #define MHD_MAX(a,b) (((a)<(b)) ? (b) : (a)) 130 #define MHD_MIN(a,b) (((a)<(b)) ? (a) : (b)) 131 132 133 /** 134 * Minimum reasonable size by which MHD tries to increment read/write buffers. 135 * We usually begin with half the available pool space for the 136 * IO-buffer, but if absolutely needed we additively grow by the 137 * number of bytes given here (up to -- theoretically -- the full pool 138 * space). 139 * 140 * Currently set to reasonable maximum MSS size. 141 */ 142 #define MHD_BUF_INC_SIZE 1500 143 144 #ifndef MHD_STATICSTR_LEN_ 145 /** 146 * Determine length of static string / macro strings at compile time. 147 */ 148 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1) 149 #endif /* ! MHD_STATICSTR_LEN_ */ 150 151 152 /** 153 * Tri-state on/off/unknown 154 */ 155 enum MHD_tristate 156 { 157 _MHD_UNKNOWN = -1, /**< State is not yet checked nor set */ 158 _MHD_OFF = false, /**< State is "off" / "disabled" */ 159 _MHD_NO = false, /**< State is "off" / "disabled" */ 160 _MHD_ON = true, /**< State is "on" / "enabled" */ 161 _MHD_YES = true /**< State is "on" / "enabled" */ 162 } _MHD_FIXED_ENUM; 163 164 165 /** 166 * State of the socket with respect to epoll (bitmask). 167 */ 168 enum MHD_EpollState 169 { 170 171 /** 172 * The socket is not involved with a defined state in epoll() right 173 * now. 174 */ 175 MHD_EPOLL_STATE_UNREADY = 0, 176 177 /** 178 * epoll() told us that data was ready for reading, and we did 179 * not consume all of it yet. 180 */ 181 MHD_EPOLL_STATE_READ_READY = 1, 182 183 /** 184 * epoll() told us that space was available for writing, and we did 185 * not consume all of it yet. 186 */ 187 MHD_EPOLL_STATE_WRITE_READY = 2, 188 189 /** 190 * Is this connection currently in the 'eready' EDLL? 191 */ 192 MHD_EPOLL_STATE_IN_EREADY_EDLL = 4, 193 194 /** 195 * Is this connection currently in the epoll() set? 196 */ 197 MHD_EPOLL_STATE_IN_EPOLL_SET = 8, 198 199 /** 200 * Is this connection currently suspended? 201 */ 202 MHD_EPOLL_STATE_SUSPENDED = 16, 203 204 /** 205 * Is this connection in some error state? 206 */ 207 MHD_EPOLL_STATE_ERROR = 128 208 } _MHD_FIXED_FLAGS_ENUM; 209 210 211 /** 212 * What is this connection waiting for? 213 */ 214 enum MHD_ConnectionEventLoopInfo 215 { 216 /** 217 * We are waiting to be able to read. 218 */ 219 MHD_EVENT_LOOP_INFO_READ = 1 << 0, 220 221 /** 222 * We are waiting to be able to write. 223 */ 224 MHD_EVENT_LOOP_INFO_WRITE = 1 << 1, 225 226 /** 227 * We are waiting for the application to provide data. 228 */ 229 MHD_EVENT_LOOP_INFO_PROCESS = 1 << 2, 230 231 /** 232 * Some data is ready to be processed, but more data could 233 * be read. 234 */ 235 MHD_EVENT_LOOP_INFO_PROCESS_READ = 236 MHD_EVENT_LOOP_INFO_READ | MHD_EVENT_LOOP_INFO_PROCESS, 237 238 /** 239 * We are finished and are awaiting cleanup. 240 */ 241 MHD_EVENT_LOOP_INFO_CLEANUP = 1 << 3 242 } _MHD_FIXED_ENUM; 243 244 245 /** 246 * Additional test value for enum MHD_FLAG to check only for MHD_ALLOW_SUSPEND_RESUME and 247 * NOT for MHD_USE_ITC. 248 */ 249 #define MHD_TEST_ALLOW_SUSPEND_RESUME 8192 250 251 /** 252 * Maximum length of a nonce in digest authentication. 64(SHA-256 Hex) + 253 * 12(Timestamp Hex) + 1(NULL); hence 77 should suffice, but Opera 254 * (already) takes more (see Mantis #1633), so we've increased the 255 * value to support something longer... 256 */ 257 #define MAX_CLIENT_NONCE_LENGTH 129 258 259 /** 260 * The maximum size of MHD-generated nonce when printed with hexadecimal chars. 261 * 262 * This is equal to "(32 bytes for SHA-256 (or SHA-512/256) nonce plus 6 bytes 263 * for timestamp) multiplied by two hex chars per byte". 264 * Please keep it in sync with digestauth.c 265 */ 266 #if defined(MHD_SHA256_SUPPORT) || defined(MHD_SHA512_256_SUPPORT) 267 #define MAX_DIGEST_NONCE_LENGTH ((32 + 6) * 2) 268 #else /* !MHD_SHA256_SUPPORT && !MHD_SHA512_256_SUPPORT */ 269 #define MAX_DIGEST_NONCE_LENGTH ((16 + 6) * 2) 270 #endif /* !MHD_SHA256_SUPPORT && !MHD_SHA512_256_SUPPORT */ 271 272 /** 273 * A structure representing the internal holder of the 274 * nonce-nc map. 275 */ 276 struct MHD_NonceNc 277 { 278 279 /** 280 * Nonce counter, a value that increases for each subsequent 281 * request for the same nonce. Matches the largest last received 282 * 'nc' value. 283 * This 'nc' value was already used by the client. 284 */ 285 uint32_t nc; 286 287 /** 288 * Bitmask over the previous 64 nonce counter values (down to to nc-64). 289 * Used to allow out-of-order 'nc'. 290 * If bit in the bitmask is set to one, then this 'nc' value was already used 291 * by the client. 292 */ 293 uint64_t nmask; 294 295 /** 296 * Nonce value 297 */ 298 char nonce[MAX_DIGEST_NONCE_LENGTH + 1]; 299 300 }; 301 302 #ifdef HAVE_MESSAGES 303 /** 304 * fprintf()-like helper function for logging debug 305 * messages. 306 */ 307 void 308 MHD_DLOG (const struct MHD_Daemon *daemon, 309 const char *format, 310 ...); 311 312 #endif 313 314 315 /** 316 * Header or footer for HTTP response. 317 */ 318 struct MHD_HTTP_Res_Header 319 { 320 /** 321 * Headers are kept in a double-linked list. 322 */ 323 struct MHD_HTTP_Res_Header *next; 324 325 /** 326 * Headers are kept in a double-linked list. 327 */ 328 struct MHD_HTTP_Res_Header *prev; 329 330 /** 331 * The name of the header (key), without the colon. 332 */ 333 char *header; 334 335 /** 336 * The length of the @a header, not including the final zero termination. 337 */ 338 size_t header_size; 339 340 /** 341 * The value of the header. 342 */ 343 char *value; 344 345 /** 346 * The length of the @a value, not including the final zero termination. 347 */ 348 size_t value_size; 349 350 /** 351 * Type of the value. 352 */ 353 enum MHD_ValueKind kind; 354 355 }; 356 357 358 /** 359 * Header, footer, or cookie for HTTP request. 360 */ 361 struct MHD_HTTP_Req_Header 362 { 363 /** 364 * Headers are kept in a double-linked list. 365 */ 366 struct MHD_HTTP_Req_Header *next; 367 368 /** 369 * Headers are kept in a double-linked list. 370 */ 371 struct MHD_HTTP_Req_Header *prev; 372 373 /** 374 * The name of the header (key), without the colon. 375 */ 376 const char *header; 377 378 /** 379 * The length of the @a header, not including the final zero termination. 380 */ 381 size_t header_size; 382 383 /** 384 * The value of the header. 385 */ 386 const char *value; 387 388 /** 389 * The length of the @a value, not including the final zero termination. 390 */ 391 size_t value_size; 392 393 /** 394 * Type of the value. 395 */ 396 enum MHD_ValueKind kind; 397 398 }; 399 400 401 /** 402 * Automatically assigned flags 403 */ 404 enum MHD_ResponseAutoFlags 405 { 406 MHD_RAF_NO_FLAGS = 0, /**< No auto flags */ 407 MHD_RAF_HAS_CONNECTION_HDR = 1 << 0, /**< Has "Connection" header */ 408 MHD_RAF_HAS_CONNECTION_CLOSE = 1 << 1, /**< Has "Connection: close" */ 409 MHD_RAF_HAS_TRANS_ENC_CHUNKED = 1 << 2, /**< Has "Transfer-Encoding: chunked" */ 410 MHD_RAF_HAS_CONTENT_LENGTH = 1 << 3, /**< Has "Content-Length" header */ 411 MHD_RAF_HAS_DATE_HDR = 1 << 4 /**< Has "Date" header */ 412 } _MHD_FIXED_FLAGS_ENUM; 413 414 415 #if defined(MHD_WINSOCK_SOCKETS) 416 /** 417 * Internally used I/O vector type for use with winsock. 418 * Binary matches system "WSABUF". 419 */ 420 typedef struct _MHD_W32_iovec 421 { 422 unsigned long iov_len; 423 char *iov_base; 424 } MHD_iovec_; 425 #define MHD_IOV_ELMN_MAX_SIZE ULONG_MAX 426 typedef unsigned long MHD_iov_size_; 427 #elif defined(HAVE_SENDMSG) || defined(HAVE_WRITEV) 428 /** 429 * Internally used I/O vector type for use when writev or sendmsg 430 * is available. Matches system "struct iovec". 431 */ 432 typedef struct iovec MHD_iovec_; 433 #define MHD_IOV_ELMN_MAX_SIZE SIZE_MAX 434 typedef size_t MHD_iov_size_; 435 #else 436 /** 437 * Internally used I/O vector type for use when writev or sendmsg 438 * is not available. 439 */ 440 typedef struct MHD_IoVec MHD_iovec_; 441 #define MHD_IOV_ELMN_MAX_SIZE SIZE_MAX 442 typedef size_t MHD_iov_size_; 443 #endif 444 445 446 struct MHD_iovec_track_ 447 { 448 /** 449 * The copy of array of iovec elements. 450 * The copy of elements are updated during sending. 451 * The number of elements is not changed during lifetime. 452 */ 453 MHD_iovec_ *iov; 454 455 /** 456 * The number of elements in @a iov. 457 * This value is not changed during lifetime. 458 */ 459 size_t cnt; 460 461 /** 462 * The number of sent elements. 463 * At the same time, it is the index of the next (or current) element 464 * to send. 465 */ 466 size_t sent; 467 }; 468 469 /** 470 * Representation of a response. 471 */ 472 struct MHD_Response 473 { 474 475 /** 476 * Head of double-linked list of headers to send for the response. 477 */ 478 struct MHD_HTTP_Res_Header *first_header; 479 480 /** 481 * Tail of double-linked list of headers to send for the response. 482 */ 483 struct MHD_HTTP_Res_Header *last_header; 484 485 /** 486 * Buffer pointing to data that we are supposed 487 * to send as a response. 488 */ 489 const char *data; 490 491 /** 492 * Closure to give to the content reader @e crc 493 * and content reader free callback @e crfc. 494 */ 495 void *crc_cls; 496 497 /** 498 * How do we get more data? NULL if we are 499 * given all of the data up front. 500 */ 501 MHD_ContentReaderCallback crc; 502 503 /** 504 * NULL if data must not be freed, otherwise 505 * either user-specified callback or "&free". 506 */ 507 MHD_ContentReaderFreeCallback crfc; 508 509 #ifdef UPGRADE_SUPPORT 510 /** 511 * Application function to call once we are done sending the headers 512 * of the response; NULL unless this is a response created with 513 * #MHD_create_response_for_upgrade(). 514 */ 515 MHD_UpgradeHandler upgrade_handler; 516 517 /** 518 * Closure for @e uh. 519 */ 520 void *upgrade_handler_cls; 521 #endif /* UPGRADE_SUPPORT */ 522 523 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 524 /** 525 * Mutex to synchronize access to @e data, @e size and 526 * @e reference_count. 527 */ 528 MHD_mutex_ mutex; 529 #endif 530 531 /** 532 * The size of the response body. 533 * Set to #MHD_SIZE_UNKNOWN if size is not known. 534 */ 535 uint64_t total_size; 536 537 /** 538 * At what offset in the stream is the 539 * beginning of @e data located? 540 */ 541 uint64_t data_start; 542 543 /** 544 * Offset to start reading from when using @e fd. 545 */ 546 uint64_t fd_off; 547 548 /** 549 * Number of bytes ready in @e data (buffer may be larger 550 * than what is filled with payload). 551 */ 552 size_t data_size; 553 554 /** 555 * Size of the writable data buffer @e data. 556 */ 557 size_t data_buffer_size; 558 559 /** 560 * Reference count for this response. Free once the counter hits 561 * zero. 562 */ 563 unsigned int reference_count; 564 565 /** 566 * File-descriptor if this response is FD-backed. 567 */ 568 int fd; 569 570 /** 571 * Flags set for the MHD response. 572 */ 573 enum MHD_ResponseFlags flags; 574 575 /** 576 * Automatic flags set for the MHD response. 577 */ 578 enum MHD_ResponseAutoFlags flags_auto; 579 580 /** 581 * If the @e fd is a pipe (no sendfile()). 582 */ 583 bool is_pipe; 584 585 /** 586 * I/O vector used with MHD_create_response_from_iovec. 587 */ 588 MHD_iovec_ *data_iov; 589 590 /** 591 * Number of elements in data_iov. 592 */ 593 unsigned int data_iovcnt; 594 }; 595 596 597 /** 598 * States in a state machine for a connection. 599 * 600 * The main transitions are any-state to #MHD_CONNECTION_CLOSED, any 601 * state to state+1, #MHD_CONNECTION_FOOTERS_SENT to 602 * #MHD_CONNECTION_INIT. #MHD_CONNECTION_CLOSED is the terminal state 603 * and #MHD_CONNECTION_INIT the initial state. 604 * 605 * Note that transitions for *reading* happen only after the input has 606 * been processed; transitions for *writing* happen after the 607 * respective data has been put into the write buffer (the write does 608 * not have to be completed yet). A transition to 609 * #MHD_CONNECTION_CLOSED or #MHD_CONNECTION_INIT requires the write 610 * to be complete. 611 */ 612 enum MHD_CONNECTION_STATE 613 { 614 /** 615 * Connection just started (no headers received). 616 * Waiting for the line with the request type, URL and version. 617 */ 618 MHD_CONNECTION_INIT = 0, 619 620 /** 621 * Part of the request line was received. 622 * Wait for complete line. 623 */ 624 MHD_CONNECTION_REQ_LINE_RECEIVING = MHD_CONNECTION_INIT + 1, 625 626 /** 627 * We got the URL (and request type and version). Wait for a header line. 628 * 629 * A milestone state. No received data is processed in this state. 630 */ 631 MHD_CONNECTION_REQ_LINE_RECEIVED = MHD_CONNECTION_REQ_LINE_RECEIVING + 1, 632 633 /** 634 * Receiving request headers. Wait for the rest of the headers. 635 */ 636 MHD_CONNECTION_REQ_HEADERS_RECEIVING = MHD_CONNECTION_REQ_LINE_RECEIVED + 1, 637 638 /** 639 * We got the request headers. Process them. 640 */ 641 MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_REQ_HEADERS_RECEIVING + 1, 642 643 /** 644 * We have processed the request headers. Call application callback. 645 */ 646 MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, 647 648 /** 649 * We have processed the headers and need to send 100 CONTINUE. 650 */ 651 MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, 652 653 /** 654 * We have sent 100 CONTINUE (or do not need to). Read the message body. 655 */ 656 MHD_CONNECTION_BODY_RECEIVING = MHD_CONNECTION_CONTINUE_SENDING + 1, 657 658 /** 659 * We got the request body. 660 * 661 * A milestone state. No received data is processed in this state. 662 */ 663 MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_BODY_RECEIVING + 1, 664 665 /** 666 * We are reading the request footers. 667 */ 668 MHD_CONNECTION_FOOTERS_RECEIVING = MHD_CONNECTION_BODY_RECEIVED + 1, 669 670 /** 671 * We received the entire footer. 672 * 673 * A milestone state. No received data is processed in this state. 674 */ 675 MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTERS_RECEIVING + 1, 676 677 /** 678 * We received the entire request. 679 * Wait for a response to be queued. 680 */ 681 MHD_CONNECTION_FULL_REQ_RECEIVED = MHD_CONNECTION_FOOTERS_RECEIVED + 1, 682 683 /** 684 * Finished reading of the request and the response is ready. 685 * Switch internal logic from receiving to sending, prepare connection 686 * sending the reply and build the reply header. 687 */ 688 MHD_CONNECTION_START_REPLY = MHD_CONNECTION_FULL_REQ_RECEIVED + 1, 689 690 /** 691 * We have prepared the response headers in the write buffer. 692 * Send the response headers. 693 */ 694 MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_START_REPLY + 1, 695 696 /** 697 * We have sent the response headers. Get ready to send the body. 698 */ 699 MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, 700 701 /** 702 * We are waiting for the client to provide more 703 * data of a non-chunked body. 704 */ 705 MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_HEADERS_SENT + 1, 706 707 /** 708 * We are ready to send a part of a non-chunked body. Send it. 709 */ 710 MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, 711 712 /** 713 * We are waiting for the client to provide a chunk of the body. 714 */ 715 MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, 716 717 /** 718 * We are ready to send a chunk. 719 */ 720 MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, 721 722 /** 723 * We have sent the chunked response body. Prepare the footers. 724 */ 725 MHD_CONNECTION_CHUNKED_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_READY + 1, 726 727 /** 728 * We have prepared the response footer. Send it. 729 */ 730 MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_CHUNKED_BODY_SENT + 1, 731 732 /** 733 * We have sent the entire reply. 734 * Shutdown connection or restart processing to get a new request. 735 */ 736 MHD_CONNECTION_FULL_REPLY_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, 737 738 /** 739 * This connection is to be closed. 740 */ 741 MHD_CONNECTION_CLOSED = MHD_CONNECTION_FULL_REPLY_SENT + 1 742 743 #ifdef UPGRADE_SUPPORT 744 , 745 /** 746 * Connection was "upgraded" and socket is now under the 747 * control of the application. 748 */ 749 MHD_CONNECTION_UPGRADE = MHD_CONNECTION_CLOSED + 1 750 #endif /* UPGRADE_SUPPORT */ 751 752 } _MHD_FIXED_ENUM; 753 754 755 /** 756 * States of TLS transport layer. 757 */ 758 enum MHD_TLS_CONN_STATE 759 { 760 MHD_TLS_CONN_NO_TLS = 0, /**< Not a TLS connection (plain socket). */ 761 MHD_TLS_CONN_INIT, /**< TLS connection is not established yet. */ 762 MHD_TLS_CONN_HANDSHAKING, /**< TLS is in handshake process. */ 763 MHD_TLS_CONN_CONNECTED, /**< TLS is established. */ 764 MHD_TLS_CONN_WR_CLOSING, /**< Closing WR side of TLS layer. */ 765 MHD_TLS_CONN_WR_CLOSED, /**< WR side of TLS layer is closed. */ 766 MHD_TLS_CONN_TLS_CLOSING, /**< TLS session is terminating. */ 767 MHD_TLS_CONN_TLS_CLOSED, /**< TLS session is terminated. */ 768 MHD_TLS_CONN_TLS_FAILED, /**< TLS session failed. */ 769 MHD_TLS_CONN_INVALID_STATE/**< Sentinel. Not a valid value. */ 770 } _MHD_FIXED_ENUM; 771 772 /** 773 * Should all state transitions be printed to stderr? 774 */ 775 #define DEBUG_STATES _MHD_MACRO_NO 776 777 778 #ifdef HAVE_MESSAGES 779 #if DEBUG_STATES 780 const char * 781 MHD_state_to_string (enum MHD_CONNECTION_STATE state); 782 783 #endif 784 #endif 785 786 /** 787 * Function to receive plaintext data. 788 * 789 * @param conn the connection struct 790 * @param write_to where to write received data 791 * @param max_bytes maximum number of bytes to receive 792 * @return number of bytes written to @a write_to 793 */ 794 typedef ssize_t 795 (*ReceiveCallback) (struct MHD_Connection *conn, 796 void *write_to, 797 size_t max_bytes); 798 799 800 /** 801 * Function to transmit plaintext data. 802 * 803 * @param conn the connection struct 804 * @param read_from where to read data to transmit 805 * @param max_bytes maximum number of bytes to transmit 806 * @return number of bytes transmitted 807 */ 808 typedef ssize_t 809 (*TransmitCallback) (struct MHD_Connection *conn, 810 const void *read_from, 811 size_t max_bytes); 812 813 814 /** 815 * Ability to use same connection for next request 816 */ 817 enum MHD_ConnKeepAlive 818 { 819 /** 820 * Connection must be closed after sending response. 821 */ 822 MHD_CONN_MUST_CLOSE = -1, 823 824 /** 825 * KeelAlive state is not yet determined 826 */ 827 MHD_CONN_KEEPALIVE_UNKOWN = 0, 828 829 /** 830 * Connection can be used for serving next request 831 */ 832 MHD_CONN_USE_KEEPALIVE = 1, 833 834 /** 835 * Connection will be upgraded 836 */ 837 MHD_CONN_MUST_UPGRADE = 2 838 } _MHD_FIXED_ENUM; 839 840 enum MHD_HTTP_Version 841 { 842 /** 843 * Not a HTTP protocol or HTTP version is invalid. 844 */ 845 MHD_HTTP_VER_INVALID = -1, 846 847 /** 848 * HTTP version is not yet received from the client. 849 */ 850 MHD_HTTP_VER_UNKNOWN = 0, 851 852 /** 853 * HTTP version before 1.0, unsupported. 854 */ 855 MHD_HTTP_VER_TOO_OLD = 1, 856 857 /** 858 * HTTP version 1.0 859 */ 860 MHD_HTTP_VER_1_0 = 2, 861 862 /** 863 * HTTP version 1.1 864 */ 865 MHD_HTTP_VER_1_1 = 3, 866 867 /** 868 * HTTP version 1.2-1.9, must be used as 1.1 869 */ 870 MHD_HTTP_VER_1_2__1_9 = 4, 871 872 /** 873 * HTTP future version. Unsupported. 874 */ 875 MHD_HTTP_VER_FUTURE = 100 876 } _MHD_FIXED_ENUM; 877 878 /** 879 * Returns boolean 'true' if HTTP version is supported by MHD 880 */ 881 #define MHD_IS_HTTP_VER_SUPPORTED(ver) (MHD_HTTP_VER_1_0 <= (ver) && \ 882 MHD_HTTP_VER_1_2__1_9 >= (ver)) 883 884 /** 885 * Protocol should be used as HTTP/1.1 protocol. 886 * 887 * See the last paragraph of 888 * https://datatracker.ietf.org/doc/html/rfc7230#section-2.6 889 */ 890 #define MHD_IS_HTTP_VER_1_1_COMPAT(ver) (MHD_HTTP_VER_1_1 == (ver) || \ 891 MHD_HTTP_VER_1_2__1_9 == (ver)) 892 893 /** 894 * The HTTP method. 895 * 896 * Only primary methods (specified in RFC9110) are defined here. 897 */ 898 enum MHD_HTTP_Method 899 { 900 /** 901 * No request string has been received yet 902 */ 903 MHD_HTTP_MTHD_NO_METHOD = 0, 904 /** 905 * HTTP method GET 906 */ 907 MHD_HTTP_MTHD_GET = 1, 908 /** 909 * HTTP method HEAD 910 */ 911 MHD_HTTP_MTHD_HEAD = 2, 912 /** 913 * HTTP method POST 914 */ 915 MHD_HTTP_MTHD_POST = 3, 916 /** 917 * HTTP method PUT 918 */ 919 MHD_HTTP_MTHD_PUT = 4, 920 /** 921 * HTTP method DELETE 922 */ 923 MHD_HTTP_MTHD_DELETE = 5, 924 /** 925 * HTTP method CONNECT 926 */ 927 MHD_HTTP_MTHD_CONNECT = 6, 928 /** 929 * HTTP method OPTIONS 930 */ 931 MHD_HTTP_MTHD_OPTIONS = 7, 932 /** 933 * HTTP method TRACE 934 */ 935 MHD_HTTP_MTHD_TRACE = 8, 936 /** 937 * Other HTTP method. Check the string value. 938 */ 939 MHD_HTTP_MTHD_OTHER = 1000 940 } _MHD_FIXED_ENUM; 941 942 943 /** 944 * The request line processing data 945 */ 946 struct MHD_RequestLineProcessing 947 { 948 /** 949 * The position of the next character to be processed 950 */ 951 size_t proc_pos; 952 /** 953 * The number of empty lines skipped 954 */ 955 unsigned int skipped_empty_lines; 956 /** 957 * The position of the start of the current/last found whitespace block, 958 * zero if not found yet. 959 */ 960 size_t last_ws_start; 961 /** 962 * The position of the next character after the last known whitespace 963 * character in the current/last found whitespace block, 964 * zero if not found yet. 965 */ 966 size_t last_ws_end; 967 /** 968 * The pointer to the request target. 969 * The request URI will be formed based on it. 970 */ 971 char *rq_tgt; 972 /** 973 * The pointer to the first question mark in the @a rq_tgt. 974 */ 975 char *rq_tgt_qmark; 976 /** 977 * The number of whitespace characters in the request URI 978 */ 979 size_t num_ws_in_uri; 980 }; 981 982 /** 983 * The request header processing data 984 */ 985 struct MHD_HeaderProcessing 986 { 987 /** 988 * The position of the last processed character 989 */ 990 size_t proc_pos; 991 992 /** 993 * The position of the first whitespace character in current contiguous 994 * whitespace block. 995 * Zero when no whitespace found or found non-whitespace character after 996 * whitespace. 997 * Must be zero, if the current character is not whitespace. 998 */ 999 size_t ws_start; 1000 1001 /** 1002 * Indicates that end of the header (field) name found. 1003 * Must be false until the first colon in line is found. 1004 */ 1005 bool name_end_found; 1006 1007 /** 1008 * The length of the header name. 1009 * Must be zero until the first colon in line is found. 1010 * Name always starts at zero position. 1011 */ 1012 size_t name_len; 1013 1014 /** 1015 * The position of the first character of the header value. 1016 * Zero when the first character has not been found yet. 1017 */ 1018 size_t value_start; 1019 1020 /** 1021 * Line starts with whitespace. 1022 * It's meaningful only for the first line, as other lines should be handled 1023 * as "folded". 1024 */ 1025 bool starts_with_ws; 1026 }; 1027 1028 /** 1029 * The union of request line and header processing data 1030 */ 1031 union MHD_HeadersProcessing 1032 { 1033 /** 1034 * The request line processing data 1035 */ 1036 struct MHD_RequestLineProcessing rq_line; 1037 1038 /** 1039 * The request header processing data 1040 */ 1041 struct MHD_HeaderProcessing hdr; 1042 }; 1043 1044 1045 /** 1046 * The union of text staring point and the size of the text 1047 */ 1048 union MHD_StartOrSize 1049 { 1050 /** 1051 * The starting point of the text. 1052 * Valid when the text is being processed and the end of the text 1053 * is not yet determined. 1054 */ 1055 const char *start; 1056 /** 1057 * The size of the text. 1058 * Valid when the text has been processed and the end of the text 1059 * is known. 1060 */ 1061 size_t size; 1062 }; 1063 1064 /** 1065 * Request-specific values. 1066 * 1067 * Meaningful for the current request only. 1068 */ 1069 struct MHD_Request 1070 { 1071 /** 1072 * HTTP version string (i.e. http/1.1). Allocated 1073 * in pool. 1074 */ 1075 const char *version; 1076 1077 /** 1078 * HTTP protocol version as enum. 1079 */ 1080 enum MHD_HTTP_Version http_ver; 1081 1082 /** 1083 * Request method. Should be GET/POST/etc. Allocated in pool. 1084 */ 1085 const char *method; 1086 1087 /** 1088 * The request method as enum. 1089 */ 1090 enum MHD_HTTP_Method http_mthd; 1091 1092 /** 1093 * Requested URL (everything after "GET" only). Allocated 1094 * in pool. 1095 */ 1096 const char *url; 1097 1098 /** 1099 * The length of the @a url in characters, not including the terminating zero. 1100 */ 1101 size_t url_len; 1102 1103 /** 1104 * The original length of the request target. 1105 */ 1106 size_t req_target_len; 1107 1108 /** 1109 * Requested URL (everything after "GET" only). 1110 * Depending on daemon setting either the same as @a URL or NULL. 1111 */ 1112 const char *url_for_callback; 1113 1114 /** 1115 * Linked list of parsed headers. 1116 */ 1117 struct MHD_HTTP_Req_Header *headers_received; 1118 1119 /** 1120 * Tail of linked list of parsed headers. 1121 */ 1122 struct MHD_HTTP_Req_Header *headers_received_tail; 1123 1124 /** 1125 * Number of bytes we had in the HTTP header, set once we 1126 * pass #MHD_CONNECTION_HEADERS_RECEIVED. 1127 * This includes the request line, all request headers, the header section 1128 * terminating empty line, with all CRLF (or LF) characters. 1129 */ 1130 size_t header_size; 1131 1132 /** 1133 * The union of the size of all request field lines (headers) and 1134 * the starting point of the first request field line (the first header). 1135 * Until #MHD_CONNECTION_HEADERS_RECEIVED the @a start member is valid, 1136 * staring with #MHD_CONNECTION_HEADERS_RECEIVED the @a size member is valid. 1137 * The size includes CRLF (or LR) characters, but does not include 1138 * the terminating empty line. 1139 */ 1140 union MHD_StartOrSize field_lines; 1141 1142 /** 1143 * How many more bytes of the body do we expect 1144 * to read? #MHD_SIZE_UNKNOWN for unknown. 1145 */ 1146 uint64_t remaining_upload_size; 1147 1148 /** 1149 * Are we receiving with chunked encoding? 1150 * This will be set to #MHD_YES after we parse the headers and 1151 * are processing the body with chunks. 1152 * After we are done with the body and we are processing the footers; 1153 * once the footers are also done, this will be set to #MHD_NO again 1154 * (before the final call to the handler). 1155 * It is used only for requests, chunked encoding for response is 1156 * indicated by @a rp_props. 1157 */ 1158 bool have_chunked_upload; 1159 1160 /** 1161 * If we are receiving with chunked encoding, where are we right 1162 * now? 1163 * Set to 0 if we are waiting to receive the chunk size; 1164 * otherwise, this is the size of the current chunk. 1165 * A value of zero is also used when we're at the end of the chunks. 1166 */ 1167 uint64_t current_chunk_size; 1168 1169 /** 1170 * If we are receiving with chunked encoding, where are we currently 1171 * with respect to the current chunk (at what offset / position)? 1172 */ 1173 uint64_t current_chunk_offset; 1174 1175 /** 1176 * Indicate that some of the upload payload data (from the currently 1177 * processed chunk for chunked uploads) have been processed by the 1178 * last call of the connection handler. 1179 * If any data have been processed, but some data left in the buffer 1180 * for further processing, then MHD will use zero timeout before the 1181 * next data processing round. This allow the application handler 1182 * process the data by the fixed portions or other way suitable for 1183 * application developer. 1184 * If no data have been processed, than MHD will wait for more data 1185 * to come (as it makes no sense to call the same connection handler 1186 * under the same conditions). However this is dangerous as if buffer 1187 * is completely used then connection is aborted. Connection 1188 * suspension should be used in such case. 1189 */ 1190 bool some_payload_processed; 1191 1192 /** 1193 * We allow the main application to associate some pointer with the 1194 * HTTP request, which is passed to each #MHD_AccessHandlerCallback 1195 * and some other API calls. Here is where we store it. (MHD does 1196 * not know or care what it is). 1197 */ 1198 void *client_context; 1199 1200 /** 1201 * Did we ever call the "default_handler" on this request? 1202 * This flag determines if we have called the #MHD_OPTION_NOTIFY_COMPLETED 1203 * handler when the request finishes. 1204 */ 1205 bool client_aware; 1206 1207 #ifdef BAUTH_SUPPORT 1208 /** 1209 * Basic Authorization parameters. 1210 * The result of Basic Authorization header parsing. 1211 * Allocated in the connection's pool. 1212 */ 1213 const struct MHD_RqBAuth *bauth; 1214 1215 /** 1216 * Set to true if current request headers are checked for Basic Authorization 1217 */ 1218 bool bauth_tried; 1219 #endif /* BAUTH_SUPPORT */ 1220 #ifdef DAUTH_SUPPORT 1221 /** 1222 * Digest Authorization parameters. 1223 * The result of Digest Authorization header parsing. 1224 * Allocated in the connection's pool. 1225 */ 1226 const struct MHD_RqDAuth *dauth; 1227 1228 /** 1229 * Set to true if current request headers are checked for Digest Authorization 1230 */ 1231 bool dauth_tried; 1232 #endif /* DAUTH_SUPPORT */ 1233 /** 1234 * Number of bare CR characters that were replaced with space characters 1235 * in the request line or in the headers (field lines). 1236 */ 1237 size_t num_cr_sp_replaced; 1238 1239 /** 1240 * The number of header lines skipped because they have no colon 1241 */ 1242 size_t skipped_broken_lines; 1243 1244 /** 1245 * The data of the request line / request headers processing 1246 */ 1247 union MHD_HeadersProcessing hdrs; 1248 }; 1249 1250 1251 /** 1252 * Reply-specific properties. 1253 */ 1254 struct MHD_Reply_Properties 1255 { 1256 #ifdef _DEBUG 1257 bool set; /**< Indicates that other members are set and valid */ 1258 #endif /* _DEBUG */ 1259 bool use_reply_body_headers; /**< Use reply body-specific headers */ 1260 bool send_reply_body; /**< Send reply body (can be zero-sized) */ 1261 bool chunked; /**< Use chunked encoding for reply */ 1262 }; 1263 1264 #if defined(_MHD_HAVE_SENDFILE) 1265 enum MHD_resp_sender_ 1266 { 1267 MHD_resp_sender_std = 0, 1268 MHD_resp_sender_sendfile 1269 }; 1270 #endif /* _MHD_HAVE_SENDFILE */ 1271 1272 /** 1273 * Reply-specific values. 1274 * 1275 * Meaningful for the current reply only. 1276 */ 1277 struct MHD_Reply 1278 { 1279 /** 1280 * Response to transmit (initially NULL). 1281 */ 1282 struct MHD_Response *response; 1283 1284 /** 1285 * HTTP response code. Only valid if response object 1286 * is already set. 1287 */ 1288 unsigned int responseCode; 1289 1290 /** 1291 * The "ICY" response. 1292 * Reply begins with the SHOUTcast "ICY" line instead of "HTTP". 1293 */ 1294 bool responseIcy; 1295 1296 /** 1297 * Current write position in the actual response 1298 * (excluding headers, content only; should be 0 1299 * while sending headers). 1300 */ 1301 uint64_t rsp_write_position; 1302 1303 /** 1304 * The copy of iov response. 1305 * Valid if iovec response is used. 1306 * Updated during send. 1307 * Members are allocated in the pool. 1308 */ 1309 struct MHD_iovec_track_ resp_iov; 1310 1311 #if defined(_MHD_HAVE_SENDFILE) 1312 enum MHD_resp_sender_ resp_sender; 1313 #endif /* _MHD_HAVE_SENDFILE */ 1314 1315 /** 1316 * Reply-specific properties 1317 */ 1318 struct MHD_Reply_Properties props; 1319 }; 1320 1321 /** 1322 * State kept for each HTTP request. 1323 */ 1324 struct MHD_Connection 1325 { 1326 1327 #ifdef EPOLL_SUPPORT 1328 /** 1329 * Next pointer for the EDLL listing connections that are epoll-ready. 1330 */ 1331 struct MHD_Connection *nextE; 1332 1333 /** 1334 * Previous pointer for the EDLL listing connections that are epoll-ready. 1335 */ 1336 struct MHD_Connection *prevE; 1337 #endif 1338 1339 /** 1340 * Next pointer for the DLL describing our IO state. 1341 */ 1342 struct MHD_Connection *next; 1343 1344 /** 1345 * Previous pointer for the DLL describing our IO state. 1346 */ 1347 struct MHD_Connection *prev; 1348 1349 /** 1350 * Next pointer for the XDLL organizing connections by timeout. 1351 * This DLL can be either the 1352 * 'manual_timeout_head/manual_timeout_tail' or the 1353 * 'normal_timeout_head/normal_timeout_tail', depending on whether a 1354 * custom timeout is set for the connection. 1355 */ 1356 struct MHD_Connection *nextX; 1357 1358 /** 1359 * Previous pointer for the XDLL organizing connections by timeout. 1360 */ 1361 struct MHD_Connection *prevX; 1362 1363 /** 1364 * Reference to the MHD_Daemon struct. 1365 */ 1366 struct MHD_Daemon *daemon; 1367 1368 /** 1369 * Request-specific data 1370 */ 1371 struct MHD_Request rq; 1372 1373 /** 1374 * Reply-specific data 1375 */ 1376 struct MHD_Reply rp; 1377 1378 /** 1379 * The memory pool is created whenever we first read from the TCP 1380 * stream and destroyed at the end of each request (and re-created 1381 * for the next request). In the meantime, this pointer is NULL. 1382 * The pool is used for all connection-related data except for the 1383 * response (which maybe shared between connections) and the IP 1384 * address (which persists across individual requests). 1385 */ 1386 struct MemoryPool *pool; 1387 1388 /** 1389 * We allow the main application to associate some pointer with the 1390 * TCP connection (which may span multiple HTTP requests). Here is 1391 * where we store it. (MHD does not know or care what it is). 1392 * The location is given to the #MHD_NotifyConnectionCallback and 1393 * also accessible via #MHD_CONNECTION_INFO_SOCKET_CONTEXT. 1394 */ 1395 void *socket_context; 1396 1397 /** 1398 * Close connection after sending response? 1399 * Functions may change value from "Unknown" or "KeepAlive" to "Must close", 1400 * but no functions reset value "Must Close" to any other value. 1401 */ 1402 enum MHD_ConnKeepAlive keepalive; 1403 1404 /** 1405 * Buffer for reading requests. Allocated in pool. Actually one 1406 * byte larger than @e read_buffer_size (if non-NULL) to allow for 1407 * 0-termination. 1408 */ 1409 char *read_buffer; 1410 1411 /** 1412 * Buffer for writing response (headers only). Allocated 1413 * in pool. 1414 */ 1415 char *write_buffer; 1416 1417 /** 1418 * Foreign address (of length @e addr_len). MALLOCED (not 1419 * in pool!). 1420 */ 1421 struct sockaddr_storage *addr; 1422 1423 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 1424 /** 1425 * Thread handle for this connection (if we are using 1426 * one thread per connection). 1427 */ 1428 MHD_thread_handle_ID_ tid; 1429 #endif 1430 1431 /** 1432 * Size of @e read_buffer (in bytes). 1433 * This value indicates how many bytes we're willing to read 1434 * into the buffer. 1435 */ 1436 size_t read_buffer_size; 1437 1438 /** 1439 * Position where we currently append data in @e read_buffer (the 1440 * next char after the last valid position). 1441 */ 1442 size_t read_buffer_offset; 1443 1444 /** 1445 * Size of @e write_buffer (in bytes). 1446 */ 1447 size_t write_buffer_size; 1448 1449 /** 1450 * Offset where we are with sending from @e write_buffer. 1451 */ 1452 size_t write_buffer_send_offset; 1453 1454 /** 1455 * Last valid location in write_buffer (where do we 1456 * append and up to where is it safe to send?) 1457 */ 1458 size_t write_buffer_append_offset; 1459 1460 /** 1461 * Position in the 100 CONTINUE message that 1462 * we need to send when receiving http 1.1 requests. 1463 */ 1464 size_t continue_message_write_offset; 1465 1466 /** 1467 * Length of the foreign address. 1468 */ 1469 socklen_t addr_len; 1470 1471 /** 1472 * Last time this connection had any activity 1473 * (reading or writing). 1474 */ 1475 uint64_t last_activity; 1476 1477 /** 1478 * After how many milliseconds of inactivity should 1479 * this connection time out? 1480 * Zero for no timeout. 1481 */ 1482 uint64_t connection_timeout_ms; 1483 1484 /** 1485 * Socket for this connection. Set to #MHD_INVALID_SOCKET if 1486 * this connection has died (daemon should clean 1487 * up in that case). 1488 */ 1489 MHD_socket socket_fd; 1490 1491 /** 1492 * true if @e socket_fd is not TCP/IP (a UNIX domain socket, a pipe), 1493 * false (TCP/IP) otherwise. 1494 */ 1495 enum MHD_tristate is_nonip; 1496 1497 /** 1498 * true if #socket_fd is non-blocking, false otherwise. 1499 */ 1500 bool sk_nonblck; 1501 1502 /** 1503 * true if connection socket has set SIGPIPE suppression 1504 */ 1505 bool sk_spipe_suppress; 1506 1507 /** 1508 * Tracks TCP_CORK / TCP_NOPUSH of the connection socket. 1509 */ 1510 enum MHD_tristate sk_corked; 1511 1512 /** 1513 * Tracks TCP_NODELAY state of the connection socket. 1514 */ 1515 enum MHD_tristate sk_nodelay; 1516 1517 /** 1518 * Has this socket been closed for reading (i.e. other side closed 1519 * the connection)? If so, we must completely close the connection 1520 * once we are done sending our response (and stop trying to read 1521 * from this socket). 1522 */ 1523 bool read_closed; 1524 1525 /** 1526 * Some error happens during processing the connection therefore this 1527 * connection must be closed. 1528 * The error may come from the client side (like wrong request format), 1529 * from the application side (like data callback returned error), or from 1530 * the OS side (like out-of-memory). 1531 */ 1532 bool stop_with_error; 1533 1534 /** 1535 * Response queued early, before the request is fully processed, 1536 * the client upload is rejected. 1537 * The connection cannot be reused for additional requests as the current 1538 * request is incompletely read and it is unclear where is the initial 1539 * byte of the next request. 1540 */ 1541 bool discard_request; 1542 1543 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 1544 /** 1545 * Set to `true` if the thread has been joined. 1546 */ 1547 bool thread_joined; 1548 #endif 1549 1550 /** 1551 * Are we currently inside the "idle" handler (to avoid recursively 1552 * invoking it). 1553 */ 1554 bool in_idle; 1555 1556 /** 1557 * Connection is in the cleanup DL-linked list. 1558 */ 1559 bool in_cleanup; 1560 1561 #ifdef EPOLL_SUPPORT 1562 /** 1563 * What is the state of this socket in relation to epoll? 1564 */ 1565 enum MHD_EpollState epoll_state; 1566 #endif 1567 1568 /** 1569 * State in the FSM for this connection. 1570 */ 1571 enum MHD_CONNECTION_STATE state; 1572 1573 /** 1574 * What is this connection waiting for? 1575 */ 1576 enum MHD_ConnectionEventLoopInfo event_loop_info; 1577 1578 /** 1579 * Function used for reading HTTP request stream. 1580 */ 1581 ReceiveCallback recv_cls; 1582 1583 #ifdef UPGRADE_SUPPORT 1584 /** 1585 * If this connection was upgraded, this points to 1586 * the upgrade response details such that the 1587 * #thread_main_connection_upgrade()-logic can perform the 1588 * bi-directional forwarding. 1589 */ 1590 struct MHD_UpgradeResponseHandle *urh; 1591 #endif /* UPGRADE_SUPPORT */ 1592 1593 #ifdef HTTPS_SUPPORT 1594 1595 /** 1596 * State required for HTTPS/SSL/TLS support. 1597 */ 1598 gnutls_session_t tls_session; 1599 1600 /** 1601 * State of connection's TLS layer 1602 */ 1603 enum MHD_TLS_CONN_STATE tls_state; 1604 1605 /** 1606 * Could it be that we are ready to read due to TLS buffers 1607 * even though the socket is not? 1608 */ 1609 bool tls_read_ready; 1610 #endif /* HTTPS_SUPPORT */ 1611 1612 /** 1613 * Is the connection suspended? 1614 */ 1615 bool suspended; 1616 1617 /** 1618 * Are we currently in the #MHD_AccessHandlerCallback 1619 * for this connection (and thus eligible to receive 1620 * calls to #MHD_queue_response()?). 1621 */ 1622 bool in_access_handler; 1623 1624 /** 1625 * Is the connection wanting to resume? 1626 */ 1627 volatile bool resuming; 1628 1629 /** 1630 * Has the connection been resumed without its states having been 1631 * updated since? 1632 * 1633 * #MHD_connection_update_event_loop_info() deliberately does not 1634 * touch a suspended connection, so whatever the application did 1635 * while the connection was suspended -- queueing a response, or the 1636 * content reader reporting that it has no data yet and moving the 1637 * connection to #MHD_CONNECTION_NORMAL_BODY_UNREADY -- leaves 1638 * @e event_loop_info describing the state from before the 1639 * suspension. The event loop must not act on that stale value; see 1640 * the use in call_handlers(). 1641 */ 1642 bool resumed; 1643 1644 /** 1645 * Inter-thread communication channel used to wake up the thread that 1646 * handles this connection when the connection is resumed. 1647 * 1648 * Only initialised in thread-per-connection mode with 1649 * #MHD_ALLOW_SUSPEND_RESUME enabled, invalid otherwise. 1650 * 1651 * The daemon-wide ITC cannot serve this purpose: the daemon's own 1652 * thread waits on it as well, and #MHD_itc_clear_() drains it, so 1653 * whichever of the two threads runs first consumes the notification 1654 * and the other one sleeps through it. A channel that only this 1655 * connection's thread ever reads cannot lose the wake-up that way. 1656 */ 1657 struct MHD_itc_ resume_itc; 1658 1659 /** 1660 * Special member to be returned by #MHD_get_connection_info() 1661 */ 1662 union MHD_ConnectionInfo connection_info_dummy; 1663 }; 1664 1665 1666 #ifdef UPGRADE_SUPPORT 1667 /** 1668 * Buffer we use for upgrade response handling in the unlikely 1669 * case where the memory pool was so small it had no buffer 1670 * capacity left. Note that we don't expect to _ever_ use this 1671 * buffer, so it's mostly wasted memory (except that it allows 1672 * us to handle a tricky error condition nicely). So no need to 1673 * make this one big. Applications that want to perform well 1674 * should just pick an adequate size for the memory pools. 1675 */ 1676 #define RESERVE_EBUF_SIZE 8 1677 1678 /** 1679 * Context we pass to epoll() for each of the two sockets 1680 * of a `struct MHD_UpgradeResponseHandle`. We need to do 1681 * this so we can distinguish the two sockets when epoll() 1682 * gives us event notifications. 1683 */ 1684 struct UpgradeEpollHandle 1685 { 1686 /** 1687 * Reference to the overall response handle this struct is 1688 * included within. 1689 */ 1690 struct MHD_UpgradeResponseHandle *urh; 1691 1692 /** 1693 * The socket this event is kind-of about. Note that this is NOT 1694 * necessarily the socket we are polling on, as for when we read 1695 * from TLS, we epoll() on the connection's socket 1696 * (`urh->connection->socket_fd`), while this then the application's 1697 * socket (where the application will read from). Nevertheless, for 1698 * the application to read, we need to first read from TLS, hence 1699 * the two are related. 1700 * 1701 * Similarly, for writing to TLS, this epoll() will be on the 1702 * connection's `socket_fd`, and this will merely be the FD which 1703 * the application would write to. Hence this struct must always be 1704 * interpreted based on which field in `struct 1705 * MHD_UpgradeResponseHandle` it is (`app` or `mhd`). 1706 */ 1707 MHD_socket socket; 1708 1709 /** 1710 * IO-state of the @e socket (or the connection's `socket_fd`). 1711 */ 1712 enum MHD_EpollState celi; 1713 1714 }; 1715 1716 1717 /** 1718 * Handle given to the application to manage special 1719 * actions relating to MHD responses that "upgrade" 1720 * the HTTP protocol (i.e. to WebSockets). 1721 */ 1722 struct MHD_UpgradeResponseHandle 1723 { 1724 /** 1725 * The connection for which this is an upgrade handle. Note that 1726 * because a response may be shared over many connections, this may 1727 * not be the only upgrade handle for the response of this connection. 1728 */ 1729 struct MHD_Connection *connection; 1730 1731 #ifdef HTTPS_SUPPORT 1732 /** 1733 * Kept in a DLL per daemon. 1734 */ 1735 struct MHD_UpgradeResponseHandle *next; 1736 1737 /** 1738 * Kept in a DLL per daemon. 1739 */ 1740 struct MHD_UpgradeResponseHandle *prev; 1741 1742 #ifdef EPOLL_SUPPORT 1743 /** 1744 * Next pointer for the EDLL listing urhs that are epoll-ready. 1745 */ 1746 struct MHD_UpgradeResponseHandle *nextE; 1747 1748 /** 1749 * Previous pointer for the EDLL listing urhs that are epoll-ready. 1750 */ 1751 struct MHD_UpgradeResponseHandle *prevE; 1752 1753 /** 1754 * Specifies whether urh already in EDLL list of ready connections. 1755 */ 1756 bool in_eready_list; 1757 #endif 1758 1759 /** 1760 * The buffer for receiving data from TLS to 1761 * be passed to the application. Contains @e in_buffer_size 1762 * bytes (unless @e in_buffer_size is zero). Do not free! 1763 */ 1764 char *in_buffer; 1765 1766 /** 1767 * The buffer for receiving data from the application to 1768 * be passed to TLS. Contains @e out_buffer_size 1769 * bytes (unless @e out_buffer_size is zero). Do not free! 1770 */ 1771 char *out_buffer; 1772 1773 /** 1774 * Size of the @e in_buffer. 1775 * Set to 0 if the TLS connection went down for reading or socketpair 1776 * went down for writing. 1777 */ 1778 size_t in_buffer_size; 1779 1780 /** 1781 * Size of the @e out_buffer. 1782 * Set to 0 if the TLS connection went down for writing or socketpair 1783 * went down for reading. 1784 */ 1785 size_t out_buffer_size; 1786 1787 /** 1788 * Number of bytes actually in use in the @e in_buffer. Can be larger 1789 * than @e in_buffer_size if and only if @a in_buffer_size is zero and 1790 * we still have bytes that can be forwarded. 1791 * Reset to zero if all data was forwarded to socketpair or 1792 * if socketpair went down for writing. 1793 */ 1794 size_t in_buffer_used; 1795 1796 /** 1797 * Number of bytes actually in use in the @e out_buffer. Can be larger 1798 * than @e out_buffer_size if and only if @a out_buffer_size is zero and 1799 * we still have bytes that can be forwarded. 1800 * Reset to zero if all data was forwarded to TLS connection or 1801 * if TLS connection went down for writing. 1802 */ 1803 size_t out_buffer_used; 1804 1805 /** 1806 * The socket we gave to the application (r/w). 1807 */ 1808 struct UpgradeEpollHandle app; 1809 1810 /** 1811 * If @a app_sock was a socketpair, our end of it, otherwise 1812 * #MHD_INVALID_SOCKET; (r/w). 1813 */ 1814 struct UpgradeEpollHandle mhd; 1815 1816 /** 1817 * Emergency IO buffer we use in case the memory pool has literally 1818 * nothing left. 1819 */ 1820 char e_buf[RESERVE_EBUF_SIZE]; 1821 1822 #endif /* HTTPS_SUPPORT */ 1823 1824 /** 1825 * Set to true after the application finished with the socket 1826 * by #MHD_UPGRADE_ACTION_CLOSE. 1827 * 1828 * When BOTH @e was_closed (changed by command from application) 1829 * AND @e clean_ready (changed internally by MHD) are set to 1830 * #MHD_YES, function #MHD_resume_connection() will move this 1831 * connection to cleanup list. 1832 * @remark This flag could be changed from any thread. 1833 */ 1834 volatile bool was_closed; 1835 1836 /** 1837 * Set to true if connection is ready for cleanup. 1838 * 1839 * In TLS mode functions #MHD_connection_finish_forward_() must 1840 * be called before setting this flag to true. 1841 * 1842 * In thread-per-connection mode, true in this flag means 1843 * that connection's thread exited or about to exit and will 1844 * not use MHD_Connection::urh data anymore. 1845 * 1846 * In any mode true in this flag also means that 1847 * MHD_Connection::urh data will not be used for socketpair 1848 * forwarding and forwarding itself is finished. 1849 * 1850 * When BOTH @e was_closed (changed by command from application) 1851 * AND @e clean_ready (changed internally by MHD) are set to 1852 * true, function #MHD_resume_connection() will move this 1853 * connection to cleanup list. 1854 * @remark This flag could be changed from thread that process 1855 * connection's recv(), send() and response. 1856 */ 1857 volatile bool clean_ready; 1858 }; 1859 #endif /* UPGRADE_SUPPORT */ 1860 1861 1862 /** 1863 * Signature of function called to log URI accesses. 1864 * 1865 * @param cls closure 1866 * @param uri uri being accessed 1867 * @param con connection handle 1868 * @return new closure 1869 */ 1870 typedef void * 1871 (*LogCallback)(void *cls, 1872 const char *uri, 1873 struct MHD_Connection *con); 1874 1875 /** 1876 * Signature of function called to unescape URIs. See also 1877 * #MHD_http_unescape(). 1878 * 1879 * @param cls closure 1880 * @param conn connection handle 1881 * @param uri 0-terminated string to unescape (should be updated) 1882 * @return length of the resulting string 1883 */ 1884 typedef size_t 1885 (*UnescapeCallback)(void *cls, 1886 struct MHD_Connection *conn, 1887 char *uri); 1888 1889 1890 /** 1891 * State kept for each MHD daemon. All connections are kept in two 1892 * doubly-linked lists. The first one reflects the state of the 1893 * connection in terms of what operations we are waiting for (read, 1894 * write, locally blocked, cleanup) whereas the second is about its 1895 * timeout state (default or custom). 1896 */ 1897 struct MHD_Daemon 1898 { 1899 1900 /** 1901 * Callback function for all requests. 1902 */ 1903 MHD_AccessHandlerCallback default_handler; 1904 1905 /** 1906 * Closure argument to default_handler. 1907 */ 1908 void *default_handler_cls; 1909 1910 /** 1911 * Daemon's flags (bitfield). 1912 * 1913 * @remark Keep this member after pointer value to keep it 1914 * properly aligned as it will be used as member of union MHD_DaemonInfo. 1915 */ 1916 enum MHD_FLAG options; 1917 1918 /** 1919 * Head of doubly-linked list of new, externally added connections. 1920 */ 1921 struct MHD_Connection *new_connections_head; 1922 1923 /** 1924 * Tail of doubly-linked list of new, externally added connections. 1925 */ 1926 struct MHD_Connection *new_connections_tail; 1927 1928 /** 1929 * Head of doubly-linked list of our current, active connections. 1930 */ 1931 struct MHD_Connection *connections_head; 1932 1933 /** 1934 * Tail of doubly-linked list of our current, active connections. 1935 */ 1936 struct MHD_Connection *connections_tail; 1937 1938 /** 1939 * Head of doubly-linked list of our current but suspended connections. 1940 */ 1941 struct MHD_Connection *suspended_connections_head; 1942 1943 /** 1944 * Tail of doubly-linked list of our current but suspended connections. 1945 */ 1946 struct MHD_Connection *suspended_connections_tail; 1947 1948 /** 1949 * Head of doubly-linked list of connections to clean up. 1950 */ 1951 struct MHD_Connection *cleanup_head; 1952 1953 /** 1954 * Tail of doubly-linked list of connections to clean up. 1955 */ 1956 struct MHD_Connection *cleanup_tail; 1957 1958 /** 1959 * _MHD_YES if the @e listen_fd socket is a UNIX domain socket. 1960 */ 1961 enum MHD_tristate listen_is_unix; 1962 1963 #ifdef EPOLL_SUPPORT 1964 /** 1965 * Head of EDLL of connections ready for processing (in epoll mode). 1966 */ 1967 struct MHD_Connection *eready_head; 1968 1969 /** 1970 * Tail of EDLL of connections ready for processing (in epoll mode) 1971 */ 1972 struct MHD_Connection *eready_tail; 1973 1974 /** 1975 * File descriptor associated with our epoll loop. 1976 * 1977 * @remark Keep this member after pointer value to keep it 1978 * properly aligned as it will be used as member of union MHD_DaemonInfo. 1979 */ 1980 int epoll_fd; 1981 1982 /** 1983 * true if the @e listen_fd socket is in the 'epoll' set, 1984 * false if not. 1985 */ 1986 bool listen_socket_in_epoll; 1987 1988 #ifdef UPGRADE_SUPPORT 1989 #ifdef HTTPS_SUPPORT 1990 /** 1991 * File descriptor associated with the #run_epoll_for_upgrade() loop. 1992 * Only available if #MHD_USE_HTTPS_EPOLL_UPGRADE is set. 1993 */ 1994 int epoll_upgrade_fd; 1995 1996 /** 1997 * true if @e epoll_upgrade_fd is in the 'epoll' set, 1998 * false if not. 1999 */ 2000 bool upgrade_fd_in_epoll; 2001 #endif /* HTTPS_SUPPORT */ 2002 2003 /** 2004 * Head of EDLL of upgraded connections ready for processing (in epoll mode). 2005 */ 2006 struct MHD_UpgradeResponseHandle *eready_urh_head; 2007 2008 /** 2009 * Tail of EDLL of upgraded connections ready for processing (in epoll mode) 2010 */ 2011 struct MHD_UpgradeResponseHandle *eready_urh_tail; 2012 #endif /* UPGRADE_SUPPORT */ 2013 #endif /* EPOLL_SUPPORT */ 2014 2015 /** 2016 * Head of the XDLL of ALL connections with a default ('normal') 2017 * timeout, sorted by timeout (earliest at the tail, most recently 2018 * used connection at the head). MHD can just look at the tail of 2019 * this list to determine the timeout for all of its elements; 2020 * whenever there is an event of a connection, the connection is 2021 * moved back to the tail of the list. 2022 * 2023 * All connections by default start in this list; if a custom 2024 * timeout that does not match @e connection_timeout_ms is set, they 2025 * are moved to the @e manual_timeout_head-XDLL. 2026 * Not used in MHD_USE_THREAD_PER_CONNECTION mode as each thread 2027 * needs only one connection-specific timeout. 2028 */ 2029 struct MHD_Connection *normal_timeout_head; 2030 2031 /** 2032 * Tail of the XDLL of ALL connections with a default timeout, 2033 * sorted by timeout (earliest timeout at the tail). 2034 * Not used in MHD_USE_THREAD_PER_CONNECTION mode. 2035 */ 2036 struct MHD_Connection *normal_timeout_tail; 2037 2038 /** 2039 * Head of the XDLL of ALL connections with a non-default/custom 2040 * timeout, unsorted. MHD will do a O(n) scan over this list to 2041 * determine the current timeout. 2042 * Not used in MHD_USE_THREAD_PER_CONNECTION mode. 2043 */ 2044 struct MHD_Connection *manual_timeout_head; 2045 2046 /** 2047 * Tail of the XDLL of ALL connections with a non-default/custom 2048 * timeout, unsorted. 2049 * Not used in MHD_USE_THREAD_PER_CONNECTION mode. 2050 */ 2051 struct MHD_Connection *manual_timeout_tail; 2052 2053 /** 2054 * Function to call to check if we should accept or reject an 2055 * incoming request. May be NULL. 2056 */ 2057 MHD_AcceptPolicyCallback apc; 2058 2059 /** 2060 * Closure argument to apc. 2061 */ 2062 void *apc_cls; 2063 2064 /** 2065 * Function to call when we are done processing 2066 * a particular request. May be NULL. 2067 */ 2068 MHD_RequestCompletedCallback notify_completed; 2069 2070 /** 2071 * Closure argument to @e notify_completed. 2072 */ 2073 void *notify_completed_cls; 2074 2075 /** 2076 * Function to call when we are starting/stopping 2077 * a connection. May be NULL. 2078 */ 2079 MHD_NotifyConnectionCallback notify_connection; 2080 2081 /** 2082 * Closure argument to @e notify_connection. 2083 */ 2084 void *notify_connection_cls; 2085 2086 /** 2087 * Function to call with the full URI at the 2088 * beginning of request processing. May be NULL. 2089 * <p> 2090 * Returns the initial pointer to internal state 2091 * kept by the client for the request. 2092 */ 2093 LogCallback uri_log_callback; 2094 2095 /** 2096 * Closure argument to @e uri_log_callback. 2097 */ 2098 void *uri_log_callback_cls; 2099 2100 /** 2101 * Function to call when we unescape escape sequences. 2102 */ 2103 UnescapeCallback unescape_callback; 2104 2105 /** 2106 * Closure for @e unescape_callback. 2107 */ 2108 void *unescape_callback_cls; 2109 2110 /** 2111 * Listen port. 2112 * 2113 * @remark Keep this member after pointer value to keep it 2114 * properly aligned as it will be used as member of union MHD_DaemonInfo. 2115 */ 2116 uint16_t port; 2117 2118 #ifdef HAVE_MESSAGES 2119 /** 2120 * Function for logging error messages (if we 2121 * support error reporting). 2122 */ 2123 MHD_LogCallback custom_error_log; 2124 2125 /** 2126 * Closure argument to @e custom_error_log. 2127 */ 2128 void *custom_error_log_cls; 2129 #endif 2130 2131 /** 2132 * Pointer to master daemon (NULL if this is the master) 2133 */ 2134 struct MHD_Daemon *master; 2135 2136 /** 2137 * Listen socket. 2138 * 2139 * @remark Keep this member after pointer value to keep it 2140 * properly aligned as it will be used as member of union MHD_DaemonInfo. 2141 */ 2142 MHD_socket listen_fd; 2143 2144 /** 2145 * Listen socket is non-blocking. 2146 */ 2147 bool listen_nonblk; 2148 2149 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 2150 /** 2151 * Worker daemons (one per thread) 2152 */ 2153 struct MHD_Daemon *worker_pool; 2154 #endif 2155 2156 /** 2157 * Table storing number of connections per IP 2158 */ 2159 void *per_ip_connection_count; 2160 2161 /** 2162 * Number of active parallel connections. 2163 * 2164 * @remark Keep this member after pointer value to keep it 2165 * properly aligned as it will be used as member of union MHD_DaemonInfo. 2166 */ 2167 unsigned int connections; 2168 2169 /** 2170 * Size of the per-connection memory pools. 2171 */ 2172 size_t pool_size; 2173 2174 /** 2175 * Increment for growth of the per-connection memory pools. 2176 */ 2177 size_t pool_increment; 2178 2179 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 2180 /** 2181 * Size of threads created by MHD. 2182 */ 2183 size_t thread_stack_size; 2184 2185 /** 2186 * Number of worker daemons 2187 */ 2188 unsigned int worker_pool_size; 2189 2190 /** 2191 * The select thread handle (if we have internal select) 2192 */ 2193 MHD_thread_handle_ID_ tid; 2194 2195 /** 2196 * Mutex for per-IP connection counts. 2197 */ 2198 MHD_mutex_ per_ip_connection_mutex; 2199 2200 /** 2201 * Mutex for (modifying) access to the "cleanup", "normal_timeout" and 2202 * "manual_timeout" DLLs. 2203 */ 2204 MHD_mutex_ cleanup_connection_mutex; 2205 2206 /** 2207 * Mutex for any access to the "new connections" DL-list. 2208 */ 2209 MHD_mutex_ new_connections_mutex; 2210 2211 /** 2212 * Mutex serialising the listening socket's membership of the epoll 2213 * set: @a listen_socket_in_epoll, @a was_quiesced and the 2214 * epoll_ctl() calls that act on them. 2215 * 2216 * MHD_epoll() runs on the polling thread and MHD_quiesce_daemon() on 2217 * the application's; both test those flags and then add or remove the 2218 * listening socket, and without this lock the test and the act are 2219 * two steps, so both can decide to remove it and the loser gets 2220 * ENOENT. A dedicated mutex rather than @a cleanup_connection_mutex: 2221 * that one is held across an application callback 2222 * (@a notify_completed, in resume_suspended_connections()), so reusing 2223 * it would let an application deadlock itself by calling 2224 * MHD_quiesce_daemon() from that callback. 2225 */ 2226 MHD_mutex_ epoll_listen_mutex; 2227 #endif 2228 2229 /** 2230 * Our #MHD_OPTION_SERVER_INSANITY level, bits indicating 2231 * which sanity checks are off. 2232 */ 2233 enum MHD_DisableSanityCheck insanity_level; 2234 2235 /** 2236 * Whether to allow/disallow/ignore reuse of listening address. 2237 * The semantics is the following: 2238 * 0: ignore (user did not ask for neither allow/disallow, use SO_REUSEADDR 2239 * except W32) 2240 * >0: allow (use SO_REUSEPORT on most platforms, SO_REUSEADDR on Windows) 2241 * <0: disallow (mostly no action, SO_EXCLUSIVEADDRUSE on Windows or SO_EXCLBIND 2242 * on Solaris) 2243 */ 2244 int listening_address_reuse; 2245 2246 2247 /** 2248 * Inter-thread communication channel (also used to unblock 2249 * select() in non-threaded code). 2250 */ 2251 struct MHD_itc_ itc; 2252 2253 /** 2254 * Are we shutting down? 2255 */ 2256 volatile bool shutdown; 2257 2258 /** 2259 * Has this daemon been quiesced via #MHD_quiesce_daemon()? 2260 * If so, we should no longer use the @e listen_fd (including 2261 * removing it from the @e epoll_fd when possible). 2262 */ 2263 volatile bool was_quiesced; 2264 2265 /** 2266 * Did we hit some system or process-wide resource limit while 2267 * trying to accept() the last time? If so, we don't accept new 2268 * connections until we close an existing one. This effectively 2269 * temporarily lowers the "connection_limit" to the current 2270 * number of connections. 2271 */ 2272 bool at_limit; 2273 2274 /* 2275 * Do we need to process resuming connections? 2276 */ 2277 volatile bool resuming; 2278 2279 /** 2280 * Indicate that new connections in @e new_connections_head list 2281 * need to be processed. 2282 */ 2283 volatile bool have_new; 2284 2285 /** 2286 * 'True' if some data is already waiting to be processed. 2287 * If set to 'true' - zero timeout for select()/poll*() 2288 * is used. 2289 * Should be reset each time before processing connections 2290 * and raised by any connection which require additional 2291 * immediately processing (application does not provide 2292 * data for response, data waiting in TLS buffers etc.) 2293 */ 2294 bool data_already_pending; 2295 2296 /** 2297 * Limit on the number of parallel connections. 2298 */ 2299 unsigned int connection_limit; 2300 2301 /** 2302 * After how many milliseconds of inactivity should 2303 * this connection time out? 2304 * Zero for no timeout. 2305 */ 2306 uint64_t connection_timeout_ms; 2307 2308 /** 2309 * Maximum number of connections per IP, or 0 for 2310 * unlimited. 2311 */ 2312 unsigned int per_ip_connection_limit; 2313 2314 /** 2315 * The strictness level for parsing of incoming data. 2316 * @see #MHD_OPTION_CLIENT_DISCIPLINE_LVL 2317 */ 2318 int client_discipline; 2319 2320 /** 2321 * Allow binary zero in the URI (excluding query part). 2322 * When set to '1' then @a url parameter must be NULL, when binary zero 2323 * is used in URI. 2324 */ 2325 int allow_bzero_in_url; 2326 2327 #ifdef HAS_FD_SETSIZE_OVERRIDABLE 2328 /** 2329 * The value of FD_SETSIZE used by the daemon. 2330 * For external sockets polling this is the value provided by the application 2331 * via MHD_OPTION_APP_FD_SETSIZE or current FD_SETSIZE value. 2332 * For internal threads modes this is always current FD_SETSIZE value. 2333 */ 2334 int fdset_size; 2335 2336 /** 2337 * Indicates whether @a fdset_size value was set by application. 2338 * 'false' if default value is used. 2339 */ 2340 bool fdset_size_set_by_app; 2341 #endif /* HAS_FD_SETSIZE_OVERRIDABLE */ 2342 2343 /** 2344 * True if SIGPIPE is blocked 2345 */ 2346 bool sigpipe_blocked; 2347 2348 #ifdef HTTPS_SUPPORT 2349 #ifdef UPGRADE_SUPPORT 2350 /** 2351 * Head of DLL of upgrade response handles we are processing. 2352 * Used for upgraded TLS connections when thread-per-connection 2353 * is not used. 2354 */ 2355 struct MHD_UpgradeResponseHandle *urh_head; 2356 2357 /** 2358 * Tail of DLL of upgrade response handles we are processing. 2359 * Used for upgraded TLS connections when thread-per-connection 2360 * is not used. 2361 */ 2362 struct MHD_UpgradeResponseHandle *urh_tail; 2363 #endif /* UPGRADE_SUPPORT */ 2364 2365 /** 2366 * Desired cipher algorithms. 2367 */ 2368 gnutls_priority_t priority_cache; 2369 2370 /** 2371 * What kind of credentials are we offering 2372 * for SSL/TLS? 2373 */ 2374 gnutls_credentials_type_t cred_type; 2375 2376 /** 2377 * Server x509 credentials 2378 */ 2379 gnutls_certificate_credentials_t x509_cred; 2380 2381 /** 2382 * Diffie-Hellman parameters 2383 */ 2384 gnutls_dh_params_t dh_params; 2385 2386 /** 2387 * Server PSK credentials 2388 */ 2389 gnutls_psk_server_credentials_t psk_cred; 2390 2391 #if GNUTLS_VERSION_MAJOR >= 3 2392 /** 2393 * Function that can be used to obtain the certificate. Needed 2394 * for SNI support. See #MHD_OPTION_HTTPS_CERT_CALLBACK. 2395 */ 2396 gnutls_certificate_retrieve_function2 *cert_callback; 2397 2398 /** 2399 * Function that can be used to obtain the shared key. 2400 */ 2401 MHD_PskServerCredentialsCallback cred_callback; 2402 2403 /** 2404 * Closure for @e cred_callback. 2405 */ 2406 void *cred_callback_cls; 2407 #endif 2408 2409 #if GNUTLS_VERSION_NUMBER >= 0x030603 2410 /** 2411 * Function that can be used to obtain the certificate. Needed 2412 * for OCSP stapling support. See #MHD_OPTION_HTTPS_CERT_CALLBACK2. 2413 */ 2414 gnutls_certificate_retrieve_function3 *cert_callback2; 2415 #endif 2416 2417 /** 2418 * Pointer to our SSL/TLS key (in ASCII) in memory. 2419 */ 2420 const char *https_mem_key; 2421 2422 /** 2423 * Pointer to our SSL/TLS certificate (in ASCII) in memory. 2424 */ 2425 const char *https_mem_cert; 2426 2427 /** 2428 * Pointer to 0-terminated HTTPS passphrase in memory. 2429 */ 2430 const char *https_key_password; 2431 2432 /** 2433 * Pointer to our SSL/TLS certificate authority (in ASCII) in memory. 2434 */ 2435 const char *https_mem_trust; 2436 2437 /** 2438 * Our Diffie-Hellman parameters in memory. 2439 */ 2440 gnutls_dh_params_t https_mem_dhparams; 2441 2442 /** 2443 * true if we have initialized @e https_mem_dhparams. 2444 */ 2445 bool have_dhparams; 2446 2447 /** 2448 * true if ALPN is disabled. 2449 */ 2450 bool disable_alpn; 2451 2452 #endif /* HTTPS_SUPPORT */ 2453 2454 #ifdef DAUTH_SUPPORT 2455 2456 /** 2457 * Character array of random values. 2458 */ 2459 const char *digest_auth_random; 2460 2461 /** 2462 * Size of @a digest_auth_random. 2463 */ 2464 size_t digest_auth_rand_size; 2465 2466 /** 2467 * The malloc'ed copy of the @a digest_auth_random. 2468 */ 2469 void *digest_auth_random_copy; 2470 2471 /** 2472 * An array that contains the map nonce-nc. 2473 */ 2474 struct MHD_NonceNc *nnc; 2475 2476 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 2477 /** 2478 * A rw-lock for synchronizing access to @e nnc. 2479 */ 2480 MHD_mutex_ nnc_lock; 2481 #endif 2482 2483 /** 2484 * Size of the nonce-nc array. 2485 */ 2486 unsigned int nonce_nc_size; 2487 2488 /** 2489 * Nonce bind type. 2490 */ 2491 unsigned int dauth_bind_type; 2492 2493 /** 2494 * Default nonce validity length. 2495 */ 2496 unsigned int dauth_def_nonce_timeout; 2497 2498 /** 2499 * Default maximum nc (nonce count) value. 2500 */ 2501 uint32_t dauth_def_max_nc; 2502 #endif 2503 2504 #ifdef TCP_FASTOPEN 2505 /** 2506 * The queue size for incoming SYN + DATA packets. 2507 */ 2508 unsigned int fastopen_queue_size; 2509 #endif 2510 2511 /** 2512 * The size of queue for listen socket. 2513 */ 2514 unsigned int listen_backlog_size; 2515 2516 /* TODO: replace with a single member */ 2517 /** 2518 * The value to be returned by #MHD_get_daemon_info() 2519 */ 2520 union MHD_DaemonInfo daemon_info_dummy_listen_fd; 2521 2522 #ifdef EPOLL_SUPPORT 2523 /** 2524 * The value to be returned by #MHD_get_daemon_info() 2525 */ 2526 union MHD_DaemonInfo daemon_info_dummy_epoll_fd; 2527 #endif /* EPOLL_SUPPORT */ 2528 2529 /** 2530 * The value to be returned by #MHD_get_daemon_info() 2531 */ 2532 union MHD_DaemonInfo daemon_info_dummy_num_connections; 2533 2534 /** 2535 * The value to be returned by #MHD_get_daemon_info() 2536 */ 2537 union MHD_DaemonInfo daemon_info_dummy_flags; 2538 2539 /** 2540 * The value to be returned by #MHD_get_daemon_info() 2541 */ 2542 union MHD_DaemonInfo daemon_info_dummy_port; 2543 2544 #if defined(_DEBUG) && defined(HAVE_ACCEPT4) 2545 /** 2546 * If set to 'true', accept() function will be used instead of accept4() even 2547 * if accept4() is available. 2548 * This is a workaround for zzuf, which does not support sockets created 2549 * by accept4() function. 2550 * There is no API to change the value of this member, it can be flipped 2551 * only by direct access to the struct member. 2552 */ 2553 bool avoid_accept4; 2554 #endif /* _DEBUG */ 2555 }; 2556 2557 2558 #if defined(HAVE_POLL) && defined(EPOLL_SUPPORT) 2559 /** 2560 * Checks whether the @a d daemon is using select() 2561 */ 2562 #define MHD_D_IS_USING_SELECT_(d) \ 2563 (0 == (d->options & (MHD_USE_POLL | MHD_USE_EPOLL))) 2564 /** 2565 * Checks whether the @a d daemon is using poll() 2566 */ 2567 #define MHD_D_IS_USING_POLL_(d) (0 != ((d)->options & MHD_USE_POLL)) 2568 /** 2569 * Checks whether the @a d daemon is using epoll 2570 */ 2571 #define MHD_D_IS_USING_EPOLL_(d) (0 != ((d)->options & MHD_USE_EPOLL)) 2572 #elif defined(HAVE_POLL) 2573 /** 2574 * Checks whether the @a d daemon is using select() 2575 */ 2576 #define MHD_D_IS_USING_SELECT_(d) (0 == ((d)->options & MHD_USE_POLL)) 2577 /** 2578 * Checks whether the @a d daemon is using poll() 2579 */ 2580 #define MHD_D_IS_USING_POLL_(d) (0 != ((d)->options & MHD_USE_POLL)) 2581 /** 2582 * Checks whether the @a d daemon is using epoll 2583 */ 2584 #define MHD_D_IS_USING_EPOLL_(d) ((void) (d), 0) 2585 #elif defined(EPOLL_SUPPORT) 2586 /** 2587 * Checks whether the @a d daemon is using select() 2588 */ 2589 #define MHD_D_IS_USING_SELECT_(d) (0 == ((d)->options & MHD_USE_EPOLL)) 2590 /** 2591 * Checks whether the @a d daemon is using poll() 2592 */ 2593 #define MHD_D_IS_USING_POLL_(d) ((void) (d), 0) 2594 /** 2595 * Checks whether the @a d daemon is using epoll 2596 */ 2597 #define MHD_D_IS_USING_EPOLL_(d) (0 != ((d)->options & MHD_USE_EPOLL)) 2598 #else /* select() only */ 2599 /** 2600 * Checks whether the @a d daemon is using select() 2601 */ 2602 #define MHD_D_IS_USING_SELECT_(d) ((void) (d), ! 0) 2603 /** 2604 * Checks whether the @a d daemon is using poll() 2605 */ 2606 #define MHD_D_IS_USING_POLL_(d) ((void) (d), 0) 2607 /** 2608 * Checks whether the @a d daemon is using epoll 2609 */ 2610 #define MHD_D_IS_USING_EPOLL_(d) ((void) (d), 0) 2611 #endif /* select() only */ 2612 2613 #if defined(MHD_USE_THREADS) 2614 /** 2615 * Checks whether the @a d daemon is using internal polling thread 2616 */ 2617 #define MHD_D_IS_USING_THREADS_(d) \ 2618 (0 != (d->options & (MHD_USE_INTERNAL_POLLING_THREAD))) 2619 /** 2620 * Checks whether the @a d daemon is using thread-per-connection mode 2621 */ 2622 #define MHD_D_IS_USING_THREAD_PER_CONN_(d) \ 2623 (0 != ((d)->options & MHD_USE_THREAD_PER_CONNECTION)) 2624 2625 /** 2626 * Check whether the @a d daemon has thread-safety enabled. 2627 */ 2628 #define MHD_D_IS_THREAD_SAFE_(d) \ 2629 (0 == ((d)->options & MHD_USE_NO_THREAD_SAFETY)) 2630 #else /* ! MHD_USE_THREADS */ 2631 /** 2632 * Checks whether the @a d daemon is using internal polling thread 2633 */ 2634 #define MHD_D_IS_USING_THREADS_(d) ((void) d, 0) 2635 /** 2636 * Checks whether the @a d daemon is using thread-per-connection mode 2637 */ 2638 #define MHD_D_IS_USING_THREAD_PER_CONN_(d) ((void) d, 0) 2639 2640 /** 2641 * Check whether the @a d daemon has thread-safety enabled. 2642 */ 2643 #define MHD_D_IS_THREAD_SAFE_(d) ((void) d, 0) 2644 #endif /* ! MHD_USE_THREADS */ 2645 2646 #ifdef HAS_FD_SETSIZE_OVERRIDABLE 2647 /** 2648 * Get FD_SETSIZE used by the daemon @a d 2649 */ 2650 #define MHD_D_GET_FD_SETSIZE_(d) ((d)->fdset_size) 2651 #else /* ! HAS_FD_SETSIZE_OVERRIDABLE */ 2652 /** 2653 * Get FD_SETSIZE used by the daemon @a d 2654 */ 2655 #define MHD_D_GET_FD_SETSIZE_(d) (FD_SETSIZE) 2656 #endif /* ! HAS_FD_SETSIZE_OVERRIDABLE */ 2657 2658 /** 2659 * Check whether socket @a sckt fits fd_sets used by the daemon @a d 2660 */ 2661 #define MHD_D_DOES_SCKT_FIT_FDSET_(sckt,d) \ 2662 MHD_SCKT_FD_FITS_FDSET_SETSIZE_(sckt,NULL,MHD_D_GET_FD_SETSIZE_(d)) 2663 2664 2665 #ifdef DAUTH_SUPPORT 2666 2667 /** 2668 * Parameter of request's Digest Authorization header 2669 */ 2670 struct MHD_RqDAuthParam 2671 { 2672 /** 2673 * The string with length, NOT zero-terminated 2674 */ 2675 struct _MHD_str_w_len value; 2676 /** 2677 * True if string must be "unquoted" before processing. 2678 * This member is false if the string is used in DQUOTE marks, but no 2679 * backslash-escape is used in the string. 2680 */ 2681 bool quoted; 2682 }; 2683 2684 /** 2685 * Request client's Digest Authorization header parameters 2686 */ 2687 struct MHD_RqDAuth 2688 { 2689 struct MHD_RqDAuthParam nonce; 2690 struct MHD_RqDAuthParam opaque; 2691 struct MHD_RqDAuthParam response; 2692 struct MHD_RqDAuthParam username; 2693 struct MHD_RqDAuthParam username_ext; 2694 struct MHD_RqDAuthParam realm; 2695 struct MHD_RqDAuthParam uri; 2696 /* The raw QOP value, used in the 'response' calculation */ 2697 struct MHD_RqDAuthParam qop_raw; 2698 struct MHD_RqDAuthParam cnonce; 2699 struct MHD_RqDAuthParam nc; 2700 2701 /* Decoded values are below */ 2702 bool userhash; /* True if 'userhash' parameter has value 'true'. */ 2703 enum MHD_DigestAuthAlgo3 algo3; 2704 enum MHD_DigestAuthQOP qop; 2705 }; 2706 2707 2708 #endif /* DAUTH_SUPPORT */ 2709 2710 /** 2711 * Insert an element at the head of a DLL. Assumes that head, tail and 2712 * element are structs with prev and next fields. 2713 * 2714 * @param head pointer to the head of the DLL 2715 * @param tail pointer to the tail of the DLL 2716 * @param element element to insert 2717 */ 2718 #define DLL_insert(head,tail,element) do { \ 2719 mhd_assert (NULL == (element)->next); \ 2720 mhd_assert (NULL == (element)->prev); \ 2721 (element)->next = (head); \ 2722 (element)->prev = NULL; \ 2723 if ((tail) == NULL) { \ 2724 (tail) = element; \ 2725 } else { \ 2726 (head)->prev = element; \ 2727 } \ 2728 (head) = (element); } while (0) 2729 2730 2731 /** 2732 * Remove an element from a DLL. Assumes 2733 * that head, tail and element are structs 2734 * with prev and next fields. 2735 * 2736 * @param head pointer to the head of the DLL 2737 * @param tail pointer to the tail of the DLL 2738 * @param element element to remove 2739 */ 2740 #define DLL_remove(head,tail,element) do { \ 2741 mhd_assert ( (NULL != (element)->next) || ((element) == (tail))); \ 2742 mhd_assert ( (NULL != (element)->prev) || ((element) == (head))); \ 2743 if ((element)->prev == NULL) { \ 2744 (head) = (element)->next; \ 2745 } else { \ 2746 (element)->prev->next = (element)->next; \ 2747 } \ 2748 if ((element)->next == NULL) { \ 2749 (tail) = (element)->prev; \ 2750 } else { \ 2751 (element)->next->prev = (element)->prev; \ 2752 } \ 2753 (element)->next = NULL; \ 2754 (element)->prev = NULL; } while (0) 2755 2756 2757 /** 2758 * Insert an element at the head of a XDLL. Assumes that head, tail and 2759 * element are structs with prevX and nextX fields. 2760 * 2761 * @param head pointer to the head of the XDLL 2762 * @param tail pointer to the tail of the XDLL 2763 * @param element element to insert 2764 */ 2765 #define XDLL_insert(head,tail,element) do { \ 2766 mhd_assert (NULL == (element)->nextX); \ 2767 mhd_assert (NULL == (element)->prevX); \ 2768 (element)->nextX = (head); \ 2769 (element)->prevX = NULL; \ 2770 if (NULL == (tail)) { \ 2771 (tail) = element; \ 2772 } else { \ 2773 (head)->prevX = element; \ 2774 } \ 2775 (head) = (element); } while (0) 2776 2777 2778 /** 2779 * Remove an element from a XDLL. Assumes 2780 * that head, tail and element are structs 2781 * with prevX and nextX fields. 2782 * 2783 * @param head pointer to the head of the XDLL 2784 * @param tail pointer to the tail of the XDLL 2785 * @param element element to remove 2786 */ 2787 #define XDLL_remove(head,tail,element) do { \ 2788 mhd_assert ( (NULL != (element)->nextX) || ((element) == (tail))); \ 2789 mhd_assert ( (NULL != (element)->prevX) || ((element) == (head))); \ 2790 if (NULL == (element)->prevX) { \ 2791 (head) = (element)->nextX; \ 2792 } else { \ 2793 (element)->prevX->nextX = (element)->nextX; \ 2794 } \ 2795 if (NULL == (element)->nextX) { \ 2796 (tail) = (element)->prevX; \ 2797 } else { \ 2798 (element)->nextX->prevX = (element)->prevX; \ 2799 } \ 2800 (element)->nextX = NULL; \ 2801 (element)->prevX = NULL; } while (0) 2802 2803 2804 /** 2805 * Insert an element at the head of a EDLL. Assumes that head, tail and 2806 * element are structs with prevE and nextE fields. 2807 * 2808 * @param head pointer to the head of the EDLL 2809 * @param tail pointer to the tail of the EDLL 2810 * @param element element to insert 2811 */ 2812 #define EDLL_insert(head,tail,element) do { \ 2813 (element)->nextE = (head); \ 2814 (element)->prevE = NULL; \ 2815 if ((tail) == NULL) { \ 2816 (tail) = element; \ 2817 } else { \ 2818 (head)->prevE = element; \ 2819 } \ 2820 (head) = (element); } while (0) 2821 2822 2823 /** 2824 * Remove an element from a EDLL. Assumes 2825 * that head, tail and element are structs 2826 * with prevE and nextE fields. 2827 * 2828 * @param head pointer to the head of the EDLL 2829 * @param tail pointer to the tail of the EDLL 2830 * @param element element to remove 2831 */ 2832 #define EDLL_remove(head,tail,element) do { \ 2833 if ((element)->prevE == NULL) { \ 2834 (head) = (element)->nextE; \ 2835 } else { \ 2836 (element)->prevE->nextE = (element)->nextE; \ 2837 } \ 2838 if ((element)->nextE == NULL) { \ 2839 (tail) = (element)->prevE; \ 2840 } else { \ 2841 (element)->nextE->prevE = (element)->prevE; \ 2842 } \ 2843 (element)->nextE = NULL; \ 2844 (element)->prevE = NULL; } while (0) 2845 2846 2847 /** 2848 * Convert all occurrences of '+' to ' '. 2849 * 2850 * @param arg string that is modified (in place), must be 0-terminated 2851 */ 2852 void 2853 MHD_unescape_plus (char *arg); 2854 2855 2856 /** 2857 * Callback invoked when iterating over @a key / @a value 2858 * argument pairs during parsing. 2859 * 2860 * @param cls context of the iteration 2861 * @param key 0-terminated key string, never NULL 2862 * @param key_size number of bytes in key 2863 * @param value 0-terminated binary data, may include binary zeros, may be NULL 2864 * @param value_size number of bytes in value 2865 * @param kind origin of the key-value pair 2866 * @return #MHD_YES on success (continue to iterate) 2867 * #MHD_NO to signal failure (and abort iteration) 2868 */ 2869 typedef enum MHD_Result 2870 (*MHD_ArgumentIterator_)(void *cls, 2871 const char *key, 2872 size_t key_size, 2873 const char *value, 2874 size_t value_size, 2875 enum MHD_ValueKind kind); 2876 2877 2878 /** 2879 * Parse and unescape the arguments given by the client 2880 * as part of the HTTP request URI. 2881 * 2882 * @param kind header kind to pass to @a cb 2883 * @param connection connection to add headers to 2884 * @param[in,out] args argument URI string (after "?" in URI), 2885 * clobbered in the process! 2886 * @param cb function to call on each key-value pair found 2887 * @param cls the iterator context 2888 * @return #MHD_NO on failure (@a cb returned #MHD_NO), 2889 * #MHD_YES for success (parsing succeeded, @a cb always 2890 * returned #MHD_YES) 2891 */ 2892 enum MHD_Result 2893 MHD_parse_arguments_ (struct MHD_Connection *connection, 2894 enum MHD_ValueKind kind, 2895 char *args, 2896 MHD_ArgumentIterator_ cb, 2897 void *cls); 2898 2899 2900 /** 2901 * Check whether response header contains particular token. 2902 * 2903 * Token could be surrounded by spaces and tabs and delimited by comma. 2904 * Case-insensitive match used for header names and tokens. 2905 * 2906 * @param response the response to query 2907 * @param key header name 2908 * @param key_len the length of @a key, not including optional 2909 * terminating null-character. 2910 * @param token the token to find 2911 * @param token_len the length of @a token, not including optional 2912 * terminating null-character. 2913 * @return true if token is found in specified header, 2914 * false otherwise 2915 */ 2916 bool 2917 MHD_check_response_header_token_ci (const struct MHD_Response *response, 2918 const char *key, 2919 size_t key_len, 2920 const char *token, 2921 size_t token_len); 2922 2923 /** 2924 * Check whether response header contains particular static @a tkn. 2925 * 2926 * Token could be surrounded by spaces and tabs and delimited by comma. 2927 * Case-insensitive match used for header names and tokens. 2928 * @param r the response to query 2929 * @param k header name 2930 * @param tkn the static string of token to find 2931 * @return true if token is found in specified header, 2932 * false otherwise 2933 */ 2934 #define MHD_check_response_header_s_token_ci(r,k,tkn) \ 2935 MHD_check_response_header_token_ci ((r),(k),MHD_STATICSTR_LEN_ (k), \ 2936 (tkn),MHD_STATICSTR_LEN_ (tkn)) 2937 2938 2939 /** 2940 * Internal version of #MHD_suspend_connection(). 2941 * 2942 * @remark In thread-per-connection mode: can be called from any thread, 2943 * in any other mode: to be called only from thread that process 2944 * daemon's select()/poll()/etc. 2945 * 2946 * @param connection the connection to suspend 2947 */ 2948 void 2949 internal_suspend_connection_ (struct MHD_Connection *connection); 2950 2951 2952 /** 2953 * Trace up to and return master daemon. If the supplied daemon 2954 * is a master, then return the daemon itself. 2955 * 2956 * @param daemon handle to a daemon 2957 * @return master daemon handle 2958 */ 2959 _MHD_static_inline struct MHD_Daemon * 2960 MHD_get_master (struct MHD_Daemon *const daemon) 2961 { 2962 struct MHD_Daemon *ret; 2963 2964 if (NULL != daemon->master) 2965 ret = daemon->master; 2966 else 2967 ret = daemon; 2968 mhd_assert (NULL == ret->master); 2969 2970 return ret; 2971 } 2972 2973 2974 #ifdef UPGRADE_SUPPORT 2975 /** 2976 * Mark upgraded connection as closed by application. 2977 * 2978 * The @a connection pointer must not be used after call of this function 2979 * as it may be freed in other thread immediately. 2980 * @param connection the upgraded connection to mark as closed by application 2981 */ 2982 void 2983 MHD_upgraded_connection_mark_app_closed_ (struct MHD_Connection *connection); 2984 2985 #endif /* UPGRADE_SUPPORT */ 2986 2987 2988 #endif