internal.h

Go to the documentation of this file.
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 
00040 #define EXTRA_CHECKS MHD_NO
00041 
00042 #define MHD_MAX(a,b) ((a)<(b)) ? (b) : (a)
00043 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b)
00044 
00050 #define MHD_BUF_INC_SIZE 2048
00051 
00055 extern MHD_PanicCallback mhd_panic;
00056 
00060 extern void *mhd_panic_cls;
00061 
00066 enum MHD_PollActions
00067   {
00071     MHD_POLL_ACTION_NOTHING = 0,
00072 
00076     MHD_POLL_ACTION_IN = 1,
00077 
00081     MHD_POLL_ACTION_OUT = 2
00082   };
00083 
00084 
00088 struct MHD_Pollfd 
00089 {
00093   int fd;
00094 
00098   enum MHD_PollActions events;
00099 };
00100 
00101 
00108 #define MAX_NONCE_LENGTH 129
00109 
00110 
00115 struct MHD_NonceNc 
00116 {
00117   
00122   unsigned int nc;
00123 
00127   char nonce[MAX_NONCE_LENGTH];
00128 
00129 };
00130 
00131 #if HAVE_MESSAGES
00132 
00136 void MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...);
00137 
00138 #endif
00139 
00150 size_t MHD_http_unescape (void *cls,
00151                           struct MHD_Connection *connection,
00152                           char *val);
00153 
00157 struct MHD_HTTP_Header
00158 {
00162   struct MHD_HTTP_Header *next;
00163 
00168   char *header;
00169 
00173   char *value;
00174 
00179   enum MHD_ValueKind kind;
00180 
00181 };
00182 
00186 struct MHD_Response
00187 {
00188 
00194   struct MHD_HTTP_Header *first_header;
00195 
00200   char *data;
00201 
00206   void *crc_cls;
00207 
00212   MHD_ContentReaderCallback crc;
00213 
00218   MHD_ContentReaderFreeCallback crfc;
00219 
00224   pthread_mutex_t mutex;
00225 
00229   uint64_t total_size;
00230 
00235   uint64_t data_start;
00236 
00240   off_t fd_off;
00241 
00245   size_t data_size;
00246 
00250   size_t data_buffer_size;
00251 
00256   unsigned int reference_count;
00257 
00261   int fd;
00262 
00263 };
00264 
00279 enum MHD_CONNECTION_STATE
00280 {
00285   MHD_CONNECTION_INIT = 0,
00286 
00290   MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1,
00291 
00295   MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1,
00296 
00300   MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1,
00301 
00305   MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1,
00306 
00310   MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1,
00311 
00315   MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1,
00316 
00320   MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1,
00321 
00326   MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1,
00327 
00332   MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1,
00333 
00338   MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1,
00339 
00343   MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1,
00344 
00348   MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1,
00349 
00354   MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1,
00355 
00359   MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1,
00360 
00364   MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1,
00365 
00369   MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1,
00370 
00374   MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1,
00375 
00379   MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1,
00380 
00385   MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1,
00386 
00387   /*
00388    *  SSL/TLS connection states
00389    */
00390 
00396   MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_CLOSED + 1
00397 
00398 };
00399 
00403 #define DEBUG_STATES MHD_NO
00404 
00405 #if HAVE_MESSAGES
00406 #if DEBUG_STATES
00407 const char *
00408 MHD_state_to_string (enum MHD_CONNECTION_STATE state);
00409 #endif
00410 #endif
00411 
00420 typedef ssize_t (*ReceiveCallback) (struct MHD_Connection * conn,
00421                                     void *write_to, size_t max_bytes);
00422 
00423 
00432 typedef ssize_t (*TransmitCallback) (struct MHD_Connection * conn,
00433                                      const void *write_to, size_t max_bytes);
00434 
00435 
00439 struct MHD_Connection
00440 {
00441 
00445   struct MHD_Connection *next;
00446 
00450   struct MHD_Daemon *daemon;
00451 
00455   struct MHD_HTTP_Header *headers_received;
00456 
00460   struct MHD_Response *response;
00461 
00472   struct MemoryPool *pool;
00473 
00480   void *client_context;
00481 
00486   char *method;
00487 
00492   char *url;
00493 
00498   char *version;
00499 
00506   char *read_buffer;
00507 
00512   char *write_buffer;
00513 
00519   char *last;
00520 
00527   char *colon;
00528 
00533   struct sockaddr *addr;
00534 
00539   pthread_t pid;
00540 
00547   size_t read_buffer_size;
00548 
00553   size_t read_buffer_offset;
00554 
00558   size_t write_buffer_size;
00559 
00563   size_t write_buffer_send_offset;
00564 
00569   size_t write_buffer_append_offset;
00570 
00575   uint64_t remaining_upload_size;
00576 
00582   uint64_t response_write_position;
00583 
00588   size_t continue_message_write_offset;
00589 
00593   socklen_t addr_len;
00594 
00599   time_t last_activity;
00600 
00606   int client_aware;
00607 
00613   int socket_fd;
00614 
00622   int read_closed;
00623 
00627   enum MHD_CONNECTION_STATE state;
00628 
00633   unsigned int responseCode;
00634 
00642   int response_unready;
00643 
00647   int have_chunked_response;
00648 
00656   int have_chunked_upload;
00657 
00664   unsigned int current_chunk_size;
00665 
00670   unsigned int current_chunk_offset;
00671 
00675   int (*read_handler) (struct MHD_Connection * connection);
00676 
00680   int (*write_handler) (struct MHD_Connection * connection);
00681 
00685   int (*idle_handler) (struct MHD_Connection * connection);
00686 
00690   ReceiveCallback recv_cls;
00691 
00695   TransmitCallback send_cls;
00696 
00697 #if HTTPS_SUPPORT
00698 
00701   gnutls_session_t tls_session;
00702 
00706   int protocol;
00707 
00711   int cipher;
00712 
00713 #endif
00714 };
00715 
00723 typedef void * (*LogCallback)(void * cls, const char * uri);
00724 
00734 typedef size_t (*UnescapeCallback)(void *cls,
00735                                    struct MHD_Connection *conn,
00736                                    char *uri);
00737 
00741 struct MHD_Daemon
00742 {
00743 
00747   MHD_AccessHandlerCallback default_handler;
00748 
00752   void *default_handler_cls;
00753 
00757   struct MHD_Connection *connections;
00758 
00764   MHD_AcceptPolicyCallback apc;
00765 
00769   void *apc_cls;
00770 
00775   MHD_RequestCompletedCallback notify_completed;
00776 
00780   void *notify_completed_cls;
00781 
00789   LogCallback uri_log_callback;
00790 
00794   void *uri_log_callback_cls;
00795 
00799   UnescapeCallback unescape_callback;
00800 
00804   void *unescape_callback_cls;
00805 
00806 #if HAVE_MESSAGES
00807 
00811   void (*custom_error_log) (void *cls, const char *fmt, va_list va);
00812 
00816   void *custom_error_log_cls;
00817 #endif
00818 
00822   struct MHD_Daemon *master;
00823 
00827   struct MHD_Daemon *worker_pool;
00828 
00832   void *per_ip_connection_count;
00833 
00837   size_t pool_size;
00838 
00842   size_t thread_stack_size;
00843 
00847   unsigned int worker_pool_size;
00848 
00852   pthread_t pid;
00853 
00857   pthread_mutex_t per_ip_connection_mutex;
00858 
00862   int socket_fd;
00863 
00867   int shutdown;
00868 
00872   unsigned int max_connections;
00873 
00878   unsigned int connection_timeout;
00879 
00884   unsigned int per_ip_connection_limit;
00885 
00889   enum MHD_OPTION options;
00890 
00894   uint16_t port;
00895 
00896 #if HTTPS_SUPPORT
00897 
00900   gnutls_priority_t priority_cache;
00901 
00906   gnutls_credentials_type_t cred_type;
00907 
00911   gnutls_certificate_credentials_t x509_cred;
00912 
00916   gnutls_dh_params_t dh_params;
00917 
00921   const char *https_mem_key;
00922 
00926   const char *https_mem_cert;
00927 
00931   const char *https_mem_trust;
00932 
00933 #endif
00934 
00935 #ifdef DAUTH_SUPPORT
00936 
00940   const char *digest_auth_random;
00941 
00945   struct MHD_NonceNc *nnc;
00946 
00950   pthread_mutex_t nnc_lock;
00951 
00955   unsigned int digest_auth_rand_size;
00956 
00960   unsigned int nonce_nc_size;
00961 
00962 #endif
00963 
00964 };
00965 
00966 
00967 #if EXTRA_CHECKS
00968 #define EXTRA_CHECK(a) if (!(a)) abort();
00969 #else
00970 #define EXTRA_CHECK(a)
00971 #endif
00972 
00973 
00974 
00975 #endif