1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007-2015 Daniel Pittman and Christian Grothoff 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file microhttpd/internal.h 22 * @brief internal shared structures 23 * @author Daniel Pittman 24 * @author Christian Grothoff 25 */ 26 27 #ifndef INTERNAL_H 28 #define INTERNAL_H 29 30 #include "platform.h" 31 #include "microhttpd.h" 32 #include "platform_interface.h" 33 #if HTTPS_SUPPORT 34 #include <gnutls/gnutls.h> 35 #if GNUTLS_VERSION_MAJOR >= 3 36 #include <gnutls/abstract.h> 37 #endif 38 #endif 39 #if EPOLL_SUPPORT 40 #include <sys/epoll.h> 41 #endif 42 #if HAVE_NETINET_TCP_H 43 /* for TCP_FASTOPEN */ 44 #include <netinet/tcp.h> 45 #endif 46 47 48 /** 49 * Should we perform additional sanity checks at runtime (on our internal 50 * invariants)? This may lead to aborts, but can be useful for debugging. 51 */ 52 #define EXTRA_CHECKS MHD_NO 53 54 #define MHD_MAX(a,b) ((a)<(b)) ? (b) : (a) 55 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b) 56 57 58 /** 59 * Minimum size by which MHD tries to increment read/write buffers. 60 * We usually begin with half the available pool space for the 61 * IO-buffer, but if absolutely needed we additively grow by the 62 * number of bytes given here (up to -- theoretically -- the full pool 63 * space). 64 */ 65 #define MHD_BUF_INC_SIZE 1024 66 67 68 /** 69 * Handler for fatal errors. 70 */ 71 extern MHD_PanicCallback mhd_panic; 72 73 /** 74 * Closure argument for "mhd_panic". 75 */ 76 extern void *mhd_panic_cls; 77 78 /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */ 79 #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 80 #define BUILTIN_NOT_REACHED __builtin_unreachable() 81 #else 82 #define BUILTIN_NOT_REACHED 83 #endif 84 85 86 #if HAVE_MESSAGES 87 /** 88 * Trigger 'panic' action based on fatal errors. 89 * 90 * @param msg error message (const char *) 91 */ 92 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0) 93 #else 94 /** 95 * Trigger 'panic' action based on fatal errors. 96 * 97 * @param msg error message (const char *) 98 */ 99 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0) 100 #endif 101 102 103 /** 104 * State of the socket with respect to epoll (bitmask). 105 */ 106 enum MHD_EpollState 107 { 108 109 /** 110 * The socket is not involved with a defined state in epoll right 111 * now. 112 */ 113 MHD_EPOLL_STATE_UNREADY = 0, 114 115 /** 116 * epoll told us that data was ready for reading, and we did 117 * not consume all of it yet. 118 */ 119 MHD_EPOLL_STATE_READ_READY = 1, 120 121 /** 122 * epoll told us that space was available for writing, and we did 123 * not consume all of it yet. 124 */ 125 MHD_EPOLL_STATE_WRITE_READY = 2, 126 127 /** 128 * Is this connection currently in the 'eready' EDLL? 129 */ 130 MHD_EPOLL_STATE_IN_EREADY_EDLL = 4, 131 132 /** 133 * Is this connection currently in the 'epoll' set? 134 */ 135 MHD_EPOLL_STATE_IN_EPOLL_SET = 8, 136 137 /** 138 * Is this connection currently suspended? 139 */ 140 MHD_EPOLL_STATE_SUSPENDED = 16 141 }; 142 143 144 /** 145 * What is this connection waiting for? 146 */ 147 enum MHD_ConnectionEventLoopInfo 148 { 149 /** 150 * We are waiting to be able to read. 151 */ 152 MHD_EVENT_LOOP_INFO_READ = 0, 153 154 /** 155 * We are waiting to be able to write. 156 */ 157 MHD_EVENT_LOOP_INFO_WRITE = 1, 158 159 /** 160 * We are waiting for the application to provide data. 161 */ 162 MHD_EVENT_LOOP_INFO_BLOCK = 2, 163 164 /** 165 * We are finished and are awaiting cleanup. 166 */ 167 MHD_EVENT_LOOP_INFO_CLEANUP = 3 168 }; 169 170 171 /** 172 * Maximum length of a nonce in digest authentication. 32(MD5 Hex) + 173 * 8(Timestamp Hex) + 1(NULL); hence 41 should suffice, but Opera 174 * (already) takes more (see Mantis #1633), so we've increased the 175 * value to support something longer... 176 */ 177 #define MAX_NONCE_LENGTH 129 178 179 180 /** 181 * A structure representing the internal holder of the 182 * nonce-nc map. 183 */ 184 struct MHD_NonceNc 185 { 186 187 /** 188 * Nonce counter, a value that increases for each subsequent 189 * request for the same nonce. 190 */ 191 unsigned long int nc; 192 193 /** 194 * Nonce value: 195 */ 196 char nonce[MAX_NONCE_LENGTH]; 197 198 }; 199 200 #if HAVE_MESSAGES 201 /** 202 * fprintf-like helper function for logging debug 203 * messages. 204 */ 205 void 206 MHD_DLOG (const struct MHD_Daemon *daemon, 207 const char *format, ...); 208 #endif 209 210 211 /** 212 * Header or cookie in HTTP request or response. 213 */ 214 struct MHD_HTTP_Header 215 { 216 /** 217 * Headers are kept in a linked list. 218 */ 219 struct MHD_HTTP_Header *next; 220 221 /** 222 * The name of the header (key), without 223 * the colon. 224 */ 225 char *header; 226 227 /** 228 * The value of the header. 229 */ 230 char *value; 231 232 /** 233 * Type of the header (where in the HTTP 234 * protocol is this header from). 235 */ 236 enum MHD_ValueKind kind; 237 238 }; 239 240 241 /** 242 * Representation of a response. 243 */ 244 struct MHD_Response 245 { 246 247 /** 248 * Headers to send for the response. Initially 249 * the linked list is created in inverse order; 250 * the order should be inverted before sending! 251 */ 252 struct MHD_HTTP_Header *first_header; 253 254 /** 255 * Buffer pointing to data that we are supposed 256 * to send as a response. 257 */ 258 char *data; 259 260 /** 261 * Closure to give to the content reader @e crc 262 * and content reader free callback @e crfc. 263 */ 264 void *crc_cls; 265 266 /** 267 * How do we get more data? NULL if we are 268 * given all of the data up front. 269 */ 270 MHD_ContentReaderCallback crc; 271 272 /** 273 * NULL if data must not be freed, otherwise 274 * either user-specified callback or "&free". 275 */ 276 MHD_ContentReaderFreeCallback crfc; 277 278 /** 279 * Mutex to synchronize access to @e data, @e size and 280 * @e reference_count. 281 */ 282 MHD_mutex_ mutex; 283 284 /** 285 * Set to #MHD_SIZE_UNKNOWN if size is not known. 286 */ 287 uint64_t total_size; 288 289 /** 290 * At what offset in the stream is the 291 * beginning of @e data located? 292 */ 293 uint64_t data_start; 294 295 /** 296 * Offset to start reading from when using @e fd. 297 */ 298 off_t fd_off; 299 300 /** 301 * Number of bytes ready in @e data (buffer may be larger 302 * than what is filled with payload). 303 */ 304 size_t data_size; 305 306 /** 307 * Size of the data buffer @e data. 308 */ 309 size_t data_buffer_size; 310 311 /** 312 * Reference count for this response. Free 313 * once the counter hits zero. 314 */ 315 unsigned int reference_count; 316 317 /** 318 * File-descriptor if this response is FD-backed. 319 */ 320 int fd; 321 322 /** 323 * Flags set for the MHD response. 324 */ 325 enum MHD_ResponseFlags flags; 326 327 }; 328 329 330 /** 331 * States in a state machine for a connection. 332 * 333 * Transitions are any-state to CLOSED, any state to state+1, 334 * FOOTERS_SENT to INIT. CLOSED is the terminal state and 335 * INIT the initial state. 336 * 337 * Note that transitions for *reading* happen only after 338 * the input has been processed; transitions for 339 * *writing* happen after the respective data has been 340 * put into the write buffer (the write does not have 341 * to be completed yet). A transition to CLOSED or INIT 342 * requires the write to be complete. 343 */ 344 enum MHD_CONNECTION_STATE 345 { 346 /** 347 * Connection just started (no headers received). 348 * Waiting for the line with the request type, URL and version. 349 */ 350 MHD_CONNECTION_INIT = 0, 351 352 /** 353 * 1: We got the URL (and request type and version). Wait for a header line. 354 */ 355 MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1, 356 357 /** 358 * 2: We got part of a multi-line request header. Wait for the rest. 359 */ 360 MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1, 361 362 /** 363 * 3: We got the request headers. Process them. 364 */ 365 MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1, 366 367 /** 368 * 4: We have processed the request headers. Send 100 continue. 369 */ 370 MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, 371 372 /** 373 * 5: We have processed the headers and need to send 100 CONTINUE. 374 */ 375 MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, 376 377 /** 378 * 6: We have sent 100 CONTINUE (or do not need to). Read the message body. 379 */ 380 MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1, 381 382 /** 383 * 7: We got the request body. Wait for a line of the footer. 384 */ 385 MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1, 386 387 /** 388 * 8: We got part of a line of the footer. Wait for the 389 * rest. 390 */ 391 MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1, 392 393 /** 394 * 9: We received the entire footer. Wait for a response to be queued 395 * and prepare the response headers. 396 */ 397 MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1, 398 399 /** 400 * 10: We have prepared the response headers in the writ buffer. 401 * Send the response headers. 402 */ 403 MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1, 404 405 /** 406 * 11: We have sent the response headers. Get ready to send the body. 407 */ 408 MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, 409 410 /** 411 * 12: We are ready to send a part of a non-chunked body. Send it. 412 */ 413 MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1, 414 415 /** 416 * 13: We are waiting for the client to provide more 417 * data of a non-chunked body. 418 */ 419 MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, 420 421 /** 422 * 14: We are ready to send a chunk. 423 */ 424 MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, 425 426 /** 427 * 15: We are waiting for the client to provide a chunk of the body. 428 */ 429 MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1, 430 431 /** 432 * 16: We have sent the response body. Prepare the footers. 433 */ 434 MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, 435 436 /** 437 * 17: We have prepared the response footer. Send it. 438 */ 439 MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1, 440 441 /** 442 * 18: We have sent the response footer. Shutdown or restart. 443 */ 444 MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, 445 446 /** 447 * 19: This connection is to be closed. 448 */ 449 MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1, 450 451 /** 452 * 20: This connection is finished (only to be freed) 453 */ 454 MHD_CONNECTION_IN_CLEANUP = MHD_CONNECTION_CLOSED + 1, 455 456 /* 457 * SSL/TLS connection states 458 */ 459 460 /** 461 * The initial connection state for all secure connectoins 462 * Handshake messages will be processed in this state & while 463 * in the 'MHD_TLS_HELLO_REQUEST' state 464 */ 465 MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_IN_CLEANUP + 1 466 467 }; 468 469 /** 470 * Should all state transitions be printed to stderr? 471 */ 472 #define DEBUG_STATES MHD_NO 473 474 475 #if HAVE_MESSAGES 476 #if DEBUG_STATES 477 const char * 478 MHD_state_to_string (enum MHD_CONNECTION_STATE state); 479 #endif 480 #endif 481 482 /** 483 * Function to receive plaintext data. 484 * 485 * @param conn the connection struct 486 * @param write_to where to write received data 487 * @param max_bytes maximum number of bytes to receive 488 * @return number of bytes written to write_to 489 */ 490 typedef ssize_t 491 (*ReceiveCallback) (struct MHD_Connection *conn, 492 void *write_to, 493 size_t max_bytes); 494 495 496 /** 497 * Function to transmit plaintext data. 498 * 499 * @param conn the connection struct 500 * @param read_from where to read data to transmit 501 * @param max_bytes maximum number of bytes to transmit 502 * @return number of bytes transmitted 503 */ 504 typedef ssize_t 505 (*TransmitCallback) (struct MHD_Connection *conn, 506 const void *write_to, 507 size_t max_bytes); 508 509 510 /** 511 * State kept for each HTTP request. 512 */ 513 struct MHD_Connection 514 { 515 516 #if EPOLL_SUPPORT 517 /** 518 * Next pointer for the EDLL listing connections that are epoll-ready. 519 */ 520 struct MHD_Connection *nextE; 521 522 /** 523 * Previous pointer for the EDLL listing connections that are epoll-ready. 524 */ 525 struct MHD_Connection *prevE; 526 #endif 527 528 /** 529 * Next pointer for the DLL describing our IO state. 530 */ 531 struct MHD_Connection *next; 532 533 /** 534 * Previous pointer for the DLL describing our IO state. 535 */ 536 struct MHD_Connection *prev; 537 538 /** 539 * Next pointer for the XDLL organizing connections by timeout. 540 * This DLL can be either the 541 * 'manual_timeout_head/manual_timeout_tail' or the 542 * 'normal_timeout_head/normal_timeout_tail', depending on whether a 543 * custom timeout is set for the connection. 544 */ 545 struct MHD_Connection *nextX; 546 547 /** 548 * Previous pointer for the XDLL organizing connections by timeout. 549 */ 550 struct MHD_Connection *prevX; 551 552 /** 553 * Reference to the MHD_Daemon struct. 554 */ 555 struct MHD_Daemon *daemon; 556 557 /** 558 * Linked list of parsed headers. 559 */ 560 struct MHD_HTTP_Header *headers_received; 561 562 /** 563 * Tail of linked list of parsed headers. 564 */ 565 struct MHD_HTTP_Header *headers_received_tail; 566 567 /** 568 * Response to transmit (initially NULL). 569 */ 570 struct MHD_Response *response; 571 572 /** 573 * The memory pool is created whenever we first read 574 * from the TCP stream and destroyed at the end of 575 * each request (and re-created for the next request). 576 * In the meantime, this pointer is NULL. The 577 * pool is used for all connection-related data 578 * except for the response (which maybe shared between 579 * connections) and the IP address (which persists 580 * across individual requests). 581 */ 582 struct MemoryPool *pool; 583 584 /** 585 * We allow the main application to associate some pointer with the 586 * HTTP request, which is passed to each #MHD_AccessHandlerCallback 587 * and some other API calls. Here is where we store it. (MHD does 588 * not know or care what it is). 589 */ 590 void *client_context; 591 592 /** 593 * We allow the main application to associate some pointer with the 594 * TCP connection (which may span multiple HTTP requests). Here is 595 * where we store it. (MHD does not know or care what it is). 596 * The location is given to the #MHD_NotifyConnectionCallback and 597 * also accessible via #MHD_CONNECTION_INFO_SOCKET_CONTEXT. 598 */ 599 void *socket_context; 600 601 /** 602 * Request method. Should be GET/POST/etc. Allocated 603 * in pool. 604 */ 605 char *method; 606 607 /** 608 * Requested URL (everything after "GET" only). Allocated 609 * in pool. 610 */ 611 char *url; 612 613 /** 614 * HTTP version string (i.e. http/1.1). Allocated 615 * in pool. 616 */ 617 char *version; 618 619 /** 620 * Buffer for reading requests. Allocated 621 * in pool. Actually one byte larger than 622 * @e read_buffer_size (if non-NULL) to allow for 623 * 0-termination. 624 */ 625 char *read_buffer; 626 627 /** 628 * Buffer for writing response (headers only). Allocated 629 * in pool. 630 */ 631 char *write_buffer; 632 633 /** 634 * Last incomplete header line during parsing of headers. 635 * Allocated in pool. Only valid if state is 636 * either #MHD_CONNECTION_HEADER_PART_RECEIVED or 637 * #MHD_CONNECTION_FOOTER_PART_RECEIVED. 638 */ 639 char *last; 640 641 /** 642 * Position after the colon on the last incomplete header 643 * line during parsing of headers. 644 * Allocated in pool. Only valid if state is 645 * either #MHD_CONNECTION_HEADER_PART_RECEIVED or 646 * #MHD_CONNECTION_FOOTER_PART_RECEIVED. 647 */ 648 char *colon; 649 650 /** 651 * Foreign address (of length @e addr_len). MALLOCED (not 652 * in pool!). 653 */ 654 struct sockaddr *addr; 655 656 /** 657 * Thread handle for this connection (if we are using 658 * one thread per connection). 659 */ 660 MHD_thread_handle_ pid; 661 662 /** 663 * Size of read_buffer (in bytes). This value indicates 664 * how many bytes we're willing to read into the buffer; 665 * the real buffer is one byte longer to allow for 666 * adding zero-termination (when needed). 667 */ 668 size_t read_buffer_size; 669 670 /** 671 * Position where we currently append data in 672 * read_buffer (last valid position). 673 */ 674 size_t read_buffer_offset; 675 676 /** 677 * Size of write_buffer (in bytes). 678 */ 679 size_t write_buffer_size; 680 681 /** 682 * Offset where we are with sending from write_buffer. 683 */ 684 size_t write_buffer_send_offset; 685 686 /** 687 * Last valid location in write_buffer (where do we 688 * append and up to where is it safe to send?) 689 */ 690 size_t write_buffer_append_offset; 691 692 /** 693 * How many more bytes of the body do we expect 694 * to read? #MHD_SIZE_UNKNOWN for unknown. 695 */ 696 uint64_t remaining_upload_size; 697 698 /** 699 * Current write position in the actual response 700 * (excluding headers, content only; should be 0 701 * while sending headers). 702 */ 703 uint64_t response_write_position; 704 705 /** 706 * Position in the 100 CONTINUE message that 707 * we need to send when receiving http 1.1 requests. 708 */ 709 size_t continue_message_write_offset; 710 711 /** 712 * Length of the foreign address. 713 */ 714 socklen_t addr_len; 715 716 /** 717 * Last time this connection had any activity 718 * (reading or writing). 719 */ 720 time_t last_activity; 721 722 /** 723 * After how many seconds of inactivity should 724 * this connection time out? Zero for no timeout. 725 */ 726 unsigned int connection_timeout; 727 728 /** 729 * Did we ever call the "default_handler" on this connection? 730 * (this flag will determine if we call the 'notify_completed' 731 * handler when the connection closes down). 732 */ 733 int client_aware; 734 735 /** 736 * Socket for this connection. Set to #MHD_INVALID_SOCKET if 737 * this connection has died (daemon should clean 738 * up in that case). 739 */ 740 MHD_socket socket_fd; 741 742 /** 743 * Has this socket been closed for reading (i.e. other side closed 744 * the connection)? If so, we must completely close the connection 745 * once we are done sending our response (and stop trying to read 746 * from this socket). 747 */ 748 int read_closed; 749 750 /** 751 * Set to #MHD_YES if the thread has been joined. 752 */ 753 int thread_joined; 754 755 /** 756 * Are we currently inside the "idle" handler (to avoid recursively invoking it). 757 */ 758 int in_idle; 759 760 #if EPOLL_SUPPORT 761 /** 762 * What is the state of this socket in relation to epoll? 763 */ 764 enum MHD_EpollState epoll_state; 765 #endif 766 767 /** 768 * State in the FSM for this connection. 769 */ 770 enum MHD_CONNECTION_STATE state; 771 772 /** 773 * What is this connection waiting for? 774 */ 775 enum MHD_ConnectionEventLoopInfo event_loop_info; 776 777 /** 778 * HTTP response code. Only valid if response object 779 * is already set. 780 */ 781 unsigned int responseCode; 782 783 /** 784 * Set to MHD_YES if the response's content reader 785 * callback failed to provide data the last time 786 * we tried to read from it. In that case, the 787 * write socket should be marked as unready until 788 * the CRC call succeeds. 789 */ 790 int response_unready; 791 792 /** 793 * Are we receiving with chunked encoding? This will be set to 794 * MHD_YES after we parse the headers and are processing the body 795 * with chunks. After we are done with the body and we are 796 * processing the footers; once the footers are also done, this will 797 * be set to MHD_NO again (before the final call to the handler). 798 */ 799 int have_chunked_upload; 800 801 /** 802 * If we are receiving with chunked encoding, where are we right 803 * now? Set to 0 if we are waiting to receive the chunk size; 804 * otherwise, this is the size of the current chunk. A value of 805 * zero is also used when we're at the end of the chunks. 806 */ 807 size_t current_chunk_size; 808 809 /** 810 * If we are receiving with chunked encoding, where are we currently 811 * with respect to the current chunk (at what offset / position)? 812 */ 813 size_t current_chunk_offset; 814 815 /** 816 * Handler used for processing read connection operations 817 */ 818 int (*read_handler) (struct MHD_Connection *connection); 819 820 /** 821 * Handler used for processing write connection operations 822 */ 823 int (*write_handler) (struct MHD_Connection *connection); 824 825 /** 826 * Handler used for processing idle connection operations 827 */ 828 int (*idle_handler) (struct MHD_Connection *connection); 829 830 /** 831 * Function used for reading HTTP request stream. 832 */ 833 ReceiveCallback recv_cls; 834 835 /** 836 * Function used for writing HTTP response stream. 837 */ 838 TransmitCallback send_cls; 839 840 #if HTTPS_SUPPORT 841 /** 842 * State required for HTTPS/SSL/TLS support. 843 */ 844 gnutls_session_t tls_session; 845 846 /** 847 * Memory location to return for protocol session info. 848 */ 849 int protocol; 850 851 /** 852 * Memory location to return for protocol session info. 853 */ 854 int cipher; 855 856 /** 857 * Could it be that we are ready to read due to TLS buffers 858 * even though the socket is not? 859 */ 860 int tls_read_ready; 861 #endif 862 863 /** 864 * Is the connection suspended? 865 */ 866 int suspended; 867 868 /** 869 * Is the connection wanting to resume? 870 */ 871 int resuming; 872 }; 873 874 /** 875 * Signature of function called to log URI accesses. 876 * 877 * @param cls closure 878 * @param uri uri being accessed 879 * @param con connection handle 880 * @return new closure 881 */ 882 typedef void * 883 (*LogCallback)(void * cls, 884 const char * uri, 885 struct MHD_Connection *con); 886 887 /** 888 * Signature of function called to unescape URIs. See also 889 * #MHD_http_unescape(). 890 * 891 * @param cls closure 892 * @param conn connection handle 893 * @param uri 0-terminated string to unescape (should be updated) 894 * @return length of the resulting string 895 */ 896 typedef size_t 897 (*UnescapeCallback)(void *cls, 898 struct MHD_Connection *conn, 899 char *uri); 900 901 902 /** 903 * State kept for each MHD daemon. All connections are kept in two 904 * doubly-linked lists. The first one reflects the state of the 905 * connection in terms of what operations we are waiting for (read, 906 * write, locally blocked, cleanup) whereas the second is about its 907 * timeout state (default or custom). 908 */ 909 struct MHD_Daemon 910 { 911 912 /** 913 * Callback function for all requests. 914 */ 915 MHD_AccessHandlerCallback default_handler; 916 917 /** 918 * Closure argument to default_handler. 919 */ 920 void *default_handler_cls; 921 922 /** 923 * Head of doubly-linked list of our current, active connections. 924 */ 925 struct MHD_Connection *connections_head; 926 927 /** 928 * Tail of doubly-linked list of our current, active connections. 929 */ 930 struct MHD_Connection *connections_tail; 931 932 /** 933 * Head of doubly-linked list of our current but suspended connections. 934 */ 935 struct MHD_Connection *suspended_connections_head; 936 937 /** 938 * Tail of doubly-linked list of our current but suspended connections. 939 */ 940 struct MHD_Connection *suspended_connections_tail; 941 942 /** 943 * Head of doubly-linked list of connections to clean up. 944 */ 945 struct MHD_Connection *cleanup_head; 946 947 /** 948 * Tail of doubly-linked list of connections to clean up. 949 */ 950 struct MHD_Connection *cleanup_tail; 951 952 #if EPOLL_SUPPORT 953 /** 954 * Head of EDLL of connections ready for processing (in epoll mode). 955 */ 956 struct MHD_Connection *eready_head; 957 958 /** 959 * Tail of EDLL of connections ready for processing (in epoll mode) 960 */ 961 struct MHD_Connection *eready_tail; 962 #endif 963 964 /** 965 * Head of the XDLL of ALL connections with a default ('normal') 966 * timeout, sorted by timeout (earliest at the tail, most recently 967 * used connection at the head). MHD can just look at the tail of 968 * this list to determine the timeout for all of its elements; 969 * whenever there is an event of a connection, the connection is 970 * moved back to the tail of the list. 971 * 972 * All connections by default start in this list; if a custom 973 * timeout that does not match 'connection_timeout' is set, they 974 * are moved to the 'manual_timeout_head'-XDLL. 975 */ 976 struct MHD_Connection *normal_timeout_head; 977 978 /** 979 * Tail of the XDLL of ALL connections with a default timeout, 980 * sorted by timeout (earliest timeout at the tail). 981 */ 982 struct MHD_Connection *normal_timeout_tail; 983 984 /** 985 * Head of the XDLL of ALL connections with a non-default/custom 986 * timeout, unsorted. MHD will do a O(n) scan over this list to 987 * determine the current timeout. 988 */ 989 struct MHD_Connection *manual_timeout_head; 990 991 /** 992 * Tail of the XDLL of ALL connections with a non-default/custom 993 * timeout, unsorted. 994 */ 995 struct MHD_Connection *manual_timeout_tail; 996 997 /** 998 * Function to call to check if we should accept or reject an 999 * incoming request. May be NULL. 1000 */ 1001 MHD_AcceptPolicyCallback apc; 1002 1003 /** 1004 * Closure argument to apc. 1005 */ 1006 void *apc_cls; 1007 1008 /** 1009 * Function to call when we are done processing 1010 * a particular request. May be NULL. 1011 */ 1012 MHD_RequestCompletedCallback notify_completed; 1013 1014 /** 1015 * Closure argument to notify_completed. 1016 */ 1017 void *notify_completed_cls; 1018 1019 /** 1020 * Function to call when we are starting/stopping 1021 * a connection. May be NULL. 1022 */ 1023 MHD_NotifyConnectionCallback notify_connection; 1024 1025 /** 1026 * Closure argument to notify_connection. 1027 */ 1028 void *notify_connection_cls; 1029 1030 /** 1031 * Function to call with the full URI at the 1032 * beginning of request processing. May be NULL. 1033 * <p> 1034 * Returns the initial pointer to internal state 1035 * kept by the client for the request. 1036 */ 1037 LogCallback uri_log_callback; 1038 1039 /** 1040 * Closure argument to @e uri_log_callback. 1041 */ 1042 void *uri_log_callback_cls; 1043 1044 /** 1045 * Function to call when we unescape escape sequences. 1046 */ 1047 UnescapeCallback unescape_callback; 1048 1049 /** 1050 * Closure for @e unescape_callback. 1051 */ 1052 void *unescape_callback_cls; 1053 1054 #if HAVE_MESSAGES 1055 /** 1056 * Function for logging error messages (if we 1057 * support error reporting). 1058 */ 1059 void (*custom_error_log) (void *cls, const char *fmt, va_list va); 1060 1061 /** 1062 * Closure argument to custom_error_log. 1063 */ 1064 void *custom_error_log_cls; 1065 #endif 1066 1067 /** 1068 * Pointer to master daemon (NULL if this is the master) 1069 */ 1070 struct MHD_Daemon *master; 1071 1072 /** 1073 * Worker daemons (one per thread) 1074 */ 1075 struct MHD_Daemon *worker_pool; 1076 1077 /** 1078 * Table storing number of connections per IP 1079 */ 1080 void *per_ip_connection_count; 1081 1082 /** 1083 * Size of the per-connection memory pools. 1084 */ 1085 size_t pool_size; 1086 1087 /** 1088 * Increment for growth of the per-connection memory pools. 1089 */ 1090 size_t pool_increment; 1091 1092 /** 1093 * Size of threads created by MHD. 1094 */ 1095 size_t thread_stack_size; 1096 1097 /** 1098 * Number of worker daemons 1099 */ 1100 unsigned int worker_pool_size; 1101 1102 /** 1103 * The select thread handle (if we have internal select) 1104 */ 1105 MHD_thread_handle_ pid; 1106 1107 /** 1108 * Mutex for per-IP connection counts. 1109 */ 1110 MHD_mutex_ per_ip_connection_mutex; 1111 1112 /** 1113 * Mutex for (modifying) access to the "cleanup" connection DLL. 1114 */ 1115 MHD_mutex_ cleanup_connection_mutex; 1116 1117 /** 1118 * Listen socket. 1119 */ 1120 MHD_socket socket_fd; 1121 1122 /** 1123 * Whether to allow/disallow/ignore reuse of listening address. 1124 * The semantics is the following: 1125 * 0: ignore (user did not ask for neither allow/disallow, use SO_REUSEADDR) 1126 * >0: allow (use SO_REUSEPORT on most platforms, SO_REUSEADDR on Windows) 1127 * <0: disallow (mostly no action, SO_EXCLUSIVEADDRUSE on Windows) 1128 */ 1129 int listening_address_reuse; 1130 1131 #if EPOLL_SUPPORT 1132 /** 1133 * File descriptor associated with our epoll loop. 1134 */ 1135 int epoll_fd; 1136 1137 /** 1138 * MHD_YES if the listen socket is in the 'epoll' set, 1139 * MHD_NO if not. 1140 */ 1141 int listen_socket_in_epoll; 1142 #endif 1143 1144 /** 1145 * Pipe we use to signal shutdown, unless 1146 * 'HAVE_LISTEN_SHUTDOWN' is defined AND we have a listen 1147 * socket (which we can then 'shutdown' to stop listening). 1148 * MHD can be build with usage of socketpair instead of 1149 * pipe (forced on W32). 1150 */ 1151 MHD_pipe wpipe[2]; 1152 1153 /** 1154 * Are we shutting down? 1155 */ 1156 int shutdown; 1157 1158 /* 1159 * Do we need to process resuming connections? 1160 */ 1161 int resuming; 1162 1163 /** 1164 * Number of active parallel connections. 1165 */ 1166 unsigned int connections; 1167 1168 /** 1169 * Limit on the number of parallel connections. 1170 */ 1171 unsigned int connection_limit; 1172 1173 /** 1174 * After how many seconds of inactivity should 1175 * connections time out? Zero for no timeout. 1176 */ 1177 unsigned int connection_timeout; 1178 1179 /** 1180 * Maximum number of connections per IP, or 0 for 1181 * unlimited. 1182 */ 1183 unsigned int per_ip_connection_limit; 1184 1185 /** 1186 * Daemon's flags (bitfield). 1187 */ 1188 enum MHD_FLAG options; 1189 1190 /** 1191 * Listen port. 1192 */ 1193 uint16_t port; 1194 1195 #if HTTPS_SUPPORT 1196 /** 1197 * Desired cipher algorithms. 1198 */ 1199 gnutls_priority_t priority_cache; 1200 1201 /** 1202 * What kind of credentials are we offering 1203 * for SSL/TLS? 1204 */ 1205 gnutls_credentials_type_t cred_type; 1206 1207 /** 1208 * Server x509 credentials 1209 */ 1210 gnutls_certificate_credentials_t x509_cred; 1211 1212 /** 1213 * Diffie-Hellman parameters 1214 */ 1215 gnutls_dh_params_t dh_params; 1216 1217 #if GNUTLS_VERSION_MAJOR >= 3 1218 /** 1219 * Function that can be used to obtain the certificate. Needed 1220 * for SNI support. See #MHD_OPTION_HTTPS_CERT_CALLBACK. 1221 */ 1222 gnutls_certificate_retrieve_function2 *cert_callback; 1223 #endif 1224 1225 /** 1226 * Pointer to our SSL/TLS key (in ASCII) in memory. 1227 */ 1228 const char *https_mem_key; 1229 1230 /** 1231 * Pointer to our SSL/TLS certificate (in ASCII) in memory. 1232 */ 1233 const char *https_mem_cert; 1234 1235 /** 1236 * Pointer to 0-terminated HTTPS passphrase in memory. 1237 */ 1238 const char *https_key_password; 1239 1240 /** 1241 * Pointer to our SSL/TLS certificate authority (in ASCII) in memory. 1242 */ 1243 const char *https_mem_trust; 1244 1245 /** 1246 * Our Diffie-Hellman parameters in memory. 1247 */ 1248 gnutls_dh_params_t https_mem_dhparams; 1249 1250 /** 1251 * #MHD_YES if we have initialized @e https_mem_dhparams. 1252 */ 1253 int have_dhparams; 1254 1255 /** 1256 * For how many connections do we have 'tls_read_ready' set to MHD_YES? 1257 * Used to avoid O(n) traversal over all connections when determining 1258 * event-loop timeout (as it needs to be zero if there is any connection 1259 * which might have ready data within TLS). 1260 */ 1261 unsigned int num_tls_read_ready; 1262 1263 #endif 1264 1265 #ifdef DAUTH_SUPPORT 1266 1267 /** 1268 * Character array of random values. 1269 */ 1270 const char *digest_auth_random; 1271 1272 /** 1273 * An array that contains the map nonce-nc. 1274 */ 1275 struct MHD_NonceNc *nnc; 1276 1277 /** 1278 * A rw-lock for synchronizing access to `nnc'. 1279 */ 1280 MHD_mutex_ nnc_lock; 1281 1282 /** 1283 * Size of `digest_auth_random. 1284 */ 1285 size_t digest_auth_rand_size; 1286 1287 /** 1288 * Size of the nonce-nc array. 1289 */ 1290 unsigned int nonce_nc_size; 1291 1292 #endif 1293 1294 #ifdef TCP_FASTOPEN 1295 /** 1296 * The queue size for incoming SYN + DATA packets. 1297 */ 1298 unsigned int fastopen_queue_size; 1299 #endif 1300 }; 1301 1302 1303 #if EXTRA_CHECKS 1304 #define EXTRA_CHECK(a) do { if (!(a)) abort(); } while (0) 1305 #else 1306 #define EXTRA_CHECK(a) 1307 #endif 1308 1309 1310 /** 1311 * Insert an element at the head of a DLL. Assumes that head, tail and 1312 * element are structs with prev and next fields. 1313 * 1314 * @param head pointer to the head of the DLL 1315 * @param tail pointer to the tail of the DLL 1316 * @param element element to insert 1317 */ 1318 #define DLL_insert(head,tail,element) do { \ 1319 EXTRA_CHECK (NULL == (element)->next); \ 1320 EXTRA_CHECK (NULL == (element)->prev); \ 1321 (element)->next = (head); \ 1322 (element)->prev = NULL; \ 1323 if ((tail) == NULL) \ 1324 (tail) = element; \ 1325 else \ 1326 (head)->prev = element; \ 1327 (head) = (element); } while (0) 1328 1329 1330 /** 1331 * Remove an element from a DLL. Assumes 1332 * that head, tail and element are structs 1333 * with prev and next fields. 1334 * 1335 * @param head pointer to the head of the DLL 1336 * @param tail pointer to the tail of the DLL 1337 * @param element element to remove 1338 */ 1339 #define DLL_remove(head,tail,element) do { \ 1340 EXTRA_CHECK ( (NULL != (element)->next) || ((element) == (tail))); \ 1341 EXTRA_CHECK ( (NULL != (element)->prev) || ((element) == (head))); \ 1342 if ((element)->prev == NULL) \ 1343 (head) = (element)->next; \ 1344 else \ 1345 (element)->prev->next = (element)->next; \ 1346 if ((element)->next == NULL) \ 1347 (tail) = (element)->prev; \ 1348 else \ 1349 (element)->next->prev = (element)->prev; \ 1350 (element)->next = NULL; \ 1351 (element)->prev = NULL; } while (0) 1352 1353 1354 1355 /** 1356 * Insert an element at the head of a XDLL. Assumes that head, tail and 1357 * element are structs with prevX and nextX fields. 1358 * 1359 * @param head pointer to the head of the XDLL 1360 * @param tail pointer to the tail of the XDLL 1361 * @param element element to insert 1362 */ 1363 #define XDLL_insert(head,tail,element) do { \ 1364 EXTRA_CHECK (NULL == (element)->nextX); \ 1365 EXTRA_CHECK (NULL == (element)->prevX); \ 1366 (element)->nextX = (head); \ 1367 (element)->prevX = NULL; \ 1368 if (NULL == (tail)) \ 1369 (tail) = element; \ 1370 else \ 1371 (head)->prevX = element; \ 1372 (head) = (element); } while (0) 1373 1374 1375 /** 1376 * Remove an element from a XDLL. Assumes 1377 * that head, tail and element are structs 1378 * with prevX and nextX fields. 1379 * 1380 * @param head pointer to the head of the XDLL 1381 * @param tail pointer to the tail of the XDLL 1382 * @param element element to remove 1383 */ 1384 #define XDLL_remove(head,tail,element) do { \ 1385 EXTRA_CHECK ( (NULL != (element)->nextX) || ((element) == (tail))); \ 1386 EXTRA_CHECK ( (NULL != (element)->prevX) || ((element) == (head))); \ 1387 if (NULL == (element)->prevX) \ 1388 (head) = (element)->nextX; \ 1389 else \ 1390 (element)->prevX->nextX = (element)->nextX; \ 1391 if (NULL == (element)->nextX) \ 1392 (tail) = (element)->prevX; \ 1393 else \ 1394 (element)->nextX->prevX = (element)->prevX; \ 1395 (element)->nextX = NULL; \ 1396 (element)->prevX = NULL; } while (0) 1397 1398 1399 /** 1400 * Insert an element at the head of a EDLL. Assumes that head, tail and 1401 * element are structs with prevE and nextE fields. 1402 * 1403 * @param head pointer to the head of the EDLL 1404 * @param tail pointer to the tail of the EDLL 1405 * @param element element to insert 1406 */ 1407 #define EDLL_insert(head,tail,element) do { \ 1408 (element)->nextE = (head); \ 1409 (element)->prevE = NULL; \ 1410 if ((tail) == NULL) \ 1411 (tail) = element; \ 1412 else \ 1413 (head)->prevE = element; \ 1414 (head) = (element); } while (0) 1415 1416 1417 /** 1418 * Remove an element from a EDLL. Assumes 1419 * that head, tail and element are structs 1420 * with prevE and nextE fields. 1421 * 1422 * @param head pointer to the head of the EDLL 1423 * @param tail pointer to the tail of the EDLL 1424 * @param element element to remove 1425 */ 1426 #define EDLL_remove(head,tail,element) do { \ 1427 if ((element)->prevE == NULL) \ 1428 (head) = (element)->nextE; \ 1429 else \ 1430 (element)->prevE->nextE = (element)->nextE; \ 1431 if ((element)->nextE == NULL) \ 1432 (tail) = (element)->prevE; \ 1433 else \ 1434 (element)->nextE->prevE = (element)->prevE; \ 1435 (element)->nextE = NULL; \ 1436 (element)->prevE = NULL; } while (0) 1437 1438 1439 /** 1440 * Equivalent to `time(NULL)` but tries to use some sort of monotonic 1441 * clock that isn't affected by someone setting the system real time 1442 * clock. 1443 * 1444 * @return 'current' time 1445 */ 1446 time_t 1447 MHD_monotonic_time(void); 1448 1449 1450 /** 1451 * Convert all occurences of '+' to ' '. 1452 * 1453 * @param arg string that is modified (in place), must be 0-terminated 1454 */ 1455 void 1456 MHD_unescape_plus (char *arg); 1457 1458 1459 #endif 1460