GNU libmicrohttpd 0.9.5
|
00001 /* 00002 This file is part of libmicrohttpd 00003 (C) 2007, 2008, 2009, 2010 Daniel Pittman and Christian Grothoff 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00027 #ifndef INTERNAL_H 00028 #define INTERNAL_H 00029 00030 #include "platform.h" 00031 #include "microhttpd.h" 00032 #if HTTPS_SUPPORT 00033 #include <gnutls/gnutls.h> 00034 #endif 00035 00036 #define EXTRA_CHECKS MHD_NO 00037 00038 #define MHD_MAX(a,b) ((a)<(b)) ? (b) : (a) 00039 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b) 00040 00046 #define MHD_BUF_INC_SIZE 2048 00047 00051 extern MHD_PanicCallback mhd_panic; 00052 00056 extern void *mhd_panic_cls; 00057 00062 enum MHD_PollActions 00063 { 00067 MHD_POLL_ACTION_NOTHING = 0, 00068 00072 MHD_POLL_ACTION_IN = 1, 00073 00077 MHD_POLL_ACTION_OUT = 2 00078 }; 00079 00080 00084 struct MHD_Pollfd 00085 { 00089 int fd; 00090 00094 enum MHD_PollActions events; 00095 }; 00096 00097 00104 #define MAX_NONCE_LENGTH 129 00105 00106 00111 struct MHD_NonceNc 00112 { 00113 00118 unsigned int nc; 00119 00123 char nonce[MAX_NONCE_LENGTH]; 00124 00125 }; 00126 00127 #if HAVE_MESSAGES 00128 00132 void MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...); 00133 00134 #endif 00135 00146 size_t MHD_http_unescape (void *cls, 00147 struct MHD_Connection *connection, 00148 char *val); 00149 00153 struct MHD_HTTP_Header 00154 { 00158 struct MHD_HTTP_Header *next; 00159 00164 char *header; 00165 00169 char *value; 00170 00175 enum MHD_ValueKind kind; 00176 00177 }; 00178 00182 struct MHD_Response 00183 { 00184 00190 struct MHD_HTTP_Header *first_header; 00191 00196 char *data; 00197 00202 void *crc_cls; 00203 00208 MHD_ContentReaderCallback crc; 00209 00214 MHD_ContentReaderFreeCallback crfc; 00215 00220 pthread_mutex_t mutex; 00221 00225 uint64_t total_size; 00226 00231 uint64_t data_start; 00232 00236 off_t fd_off; 00237 00241 size_t data_size; 00242 00246 size_t data_buffer_size; 00247 00252 unsigned int reference_count; 00253 00257 int fd; 00258 00259 }; 00260 00275 enum MHD_CONNECTION_STATE 00276 { 00281 MHD_CONNECTION_INIT = 0, 00282 00286 MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1, 00287 00291 MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1, 00292 00296 MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1, 00297 00301 MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, 00302 00306 MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, 00307 00311 MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1, 00312 00316 MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1, 00317 00322 MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1, 00323 00328 MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1, 00329 00334 MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1, 00335 00339 MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, 00340 00344 MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1, 00345 00350 MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, 00351 00355 MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, 00356 00360 MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1, 00361 00365 MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, 00366 00370 MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1, 00371 00375 MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, 00376 00381 MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1, 00382 00383 /* 00384 * SSL/TLS connection states 00385 */ 00386 00392 MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_CLOSED + 1 00393 00394 }; 00395 00399 #define DEBUG_STATES MHD_NO 00400 00401 #if HAVE_MESSAGES 00402 #if DEBUG_STATES 00403 const char * 00404 MHD_state_to_string (enum MHD_CONNECTION_STATE state); 00405 #endif 00406 #endif 00407 00416 typedef ssize_t (*ReceiveCallback) (struct MHD_Connection * conn, 00417 void *write_to, size_t max_bytes); 00418 00419 00428 typedef ssize_t (*TransmitCallback) (struct MHD_Connection * conn, 00429 const void *write_to, size_t max_bytes); 00430 00431 00435 struct MHD_Connection 00436 { 00437 00441 struct MHD_Connection *next; 00442 00446 struct MHD_Daemon *daemon; 00447 00451 struct MHD_HTTP_Header *headers_received; 00452 00456 struct MHD_Response *response; 00457 00468 struct MemoryPool *pool; 00469 00476 void *client_context; 00477 00482 char *method; 00483 00488 char *url; 00489 00494 char *version; 00495 00502 char *read_buffer; 00503 00508 char *write_buffer; 00509 00515 char *last; 00516 00523 char *colon; 00524 00529 struct sockaddr_in *addr; 00530 00535 pthread_t pid; 00536 00543 size_t read_buffer_size; 00544 00549 size_t read_buffer_offset; 00550 00554 size_t write_buffer_size; 00555 00559 size_t write_buffer_send_offset; 00560 00565 size_t write_buffer_append_offset; 00566 00571 uint64_t remaining_upload_size; 00572 00578 uint64_t response_write_position; 00579 00584 size_t continue_message_write_offset; 00585 00589 socklen_t addr_len; 00590 00595 time_t last_activity; 00596 00602 int client_aware; 00603 00609 int socket_fd; 00610 00618 int read_closed; 00619 00623 enum MHD_CONNECTION_STATE state; 00624 00629 unsigned int responseCode; 00630 00638 int response_unready; 00639 00643 int have_chunked_response; 00644 00652 int have_chunked_upload; 00653 00660 unsigned int current_chunk_size; 00661 00666 unsigned int current_chunk_offset; 00667 00671 int (*read_handler) (struct MHD_Connection * connection); 00672 00676 int (*write_handler) (struct MHD_Connection * connection); 00677 00681 int (*idle_handler) (struct MHD_Connection * connection); 00682 00686 ReceiveCallback recv_cls; 00687 00691 TransmitCallback send_cls; 00692 00693 #if HTTPS_SUPPORT 00694 00697 gnutls_session_t tls_session; 00698 00702 int protocol; 00703 00707 int cipher; 00708 00709 #endif 00710 }; 00711 00719 typedef void * (*LogCallback)(void * cls, const char * uri); 00720 00730 typedef size_t (*UnescapeCallback)(void *cls, 00731 struct MHD_Connection *conn, 00732 char *uri); 00733 00737 struct MHD_Daemon 00738 { 00739 00743 MHD_AccessHandlerCallback default_handler; 00744 00748 void *default_handler_cls; 00749 00753 struct MHD_Connection *connections; 00754 00760 MHD_AcceptPolicyCallback apc; 00761 00765 void *apc_cls; 00766 00771 MHD_RequestCompletedCallback notify_completed; 00772 00776 void *notify_completed_cls; 00777 00785 LogCallback uri_log_callback; 00786 00790 void *uri_log_callback_cls; 00791 00795 UnescapeCallback unescape_callback; 00796 00800 void *unescape_callback_cls; 00801 00802 #if HAVE_MESSAGES 00803 00807 void (*custom_error_log) (void *cls, const char *fmt, va_list va); 00808 00812 void *custom_error_log_cls; 00813 #endif 00814 00818 struct MHD_Daemon *master; 00819 00823 struct MHD_Daemon *worker_pool; 00824 00828 void *per_ip_connection_count; 00829 00833 size_t pool_size; 00834 00838 size_t thread_stack_size; 00839 00843 unsigned int worker_pool_size; 00844 00848 pthread_t pid; 00849 00853 pthread_mutex_t per_ip_connection_mutex; 00854 00858 int socket_fd; 00859 00863 int shutdown; 00864 00868 unsigned int max_connections; 00869 00874 unsigned int connection_timeout; 00875 00880 unsigned int per_ip_connection_limit; 00881 00885 enum MHD_OPTION options; 00886 00890 uint16_t port; 00891 00892 #if HTTPS_SUPPORT 00893 00896 gnutls_priority_t priority_cache; 00897 00902 gnutls_credentials_type_t cred_type; 00903 00907 gnutls_certificate_credentials_t x509_cred; 00908 00912 gnutls_dh_params_t dh_params; 00913 00917 const char *https_mem_key; 00918 00922 const char *https_mem_cert; 00923 00927 const char *https_mem_trust; 00928 00929 #endif 00930 00931 #ifdef DAUTH_SUPPORT 00932 00936 const char *digest_auth_random; 00937 00941 struct MHD_NonceNc *nnc; 00942 00946 pthread_mutex_t nnc_lock; 00947 00951 unsigned int digest_auth_rand_size; 00952 00956 unsigned int nonce_nc_size; 00957 00958 #endif 00959 00960 }; 00961 00962 00963 #if EXTRA_CHECKS 00964 #define EXTRA_CHECK(a) if (!(a)) abort(); 00965 #else 00966 #define EXTRA_CHECK(a) 00967 #endif 00968 00969 00970 00971 #endif