1147b71e3SNiels Provos /* 2b85b710cSNick Mathewson * Copyright 2001-2007 Niels Provos <[email protected]> 3e49e2891SNick Mathewson * Copyright 2007-2012 Niels Provos and Nick Mathewson 4147b71e3SNiels Provos * 5147b71e3SNiels Provos * This header file contains definitions for dealing with HTTP requests 6147b71e3SNiels Provos * that are internal to libevent. As user of the library, you should not 7147b71e3SNiels Provos * need to know about these. 8147b71e3SNiels Provos */ 9147b71e3SNiels Provos 103f8c7cd0SNick Mathewson #ifndef HTTP_INTERNAL_H_INCLUDED_ 113f8c7cd0SNick Mathewson #define HTTP_INTERNAL_H_INCLUDED_ 127defe4cbSNick Mathewson 137defe4cbSNick Mathewson #include "event2/event_struct.h" 144e8cdc6fSNick Mathewson #include "util-internal.h" 1574c0e862SNick Mathewson #include "defer-internal.h" 16147b71e3SNiels Provos 17147b71e3SNiels Provos #define HTTP_CONNECT_TIMEOUT 45 18147b71e3SNiels Provos #define HTTP_WRITE_TIMEOUT 50 19147b71e3SNiels Provos #define HTTP_READ_TIMEOUT 50 20147b71e3SNiels Provos 219998c0cbSNiels Provos enum message_read_status { 229998c0cbSNiels Provos ALL_DATA_READ = 1, 239998c0cbSNiels Provos MORE_DATA_EXPECTED = 0, 249998c0cbSNiels Provos DATA_CORRUPTED = -1, 2547bad8abSNick Mathewson REQUEST_CANCELED = -2, 2647bad8abSNick Mathewson DATA_TOO_LONG = -3 279998c0cbSNiels Provos }; 289998c0cbSNiels Provos 29147b71e3SNiels Provos struct evbuffer; 30147b71e3SNiels Provos struct addrinfo; 31ba7262ebSNiels Provos struct evhttp_request; 32147b71e3SNiels Provos 33536311a4SNick Mathewson /* Indicates an unknown request method. */ 34cb9da0bfSNick Mathewson #define EVHTTP_REQ_UNKNOWN_ (1<<15) 35536311a4SNick Mathewson 36ba7262ebSNiels Provos enum evhttp_connection_state { 379998c0cbSNiels Provos EVCON_DISCONNECTED, /**< not currently connected not trying either*/ 389998c0cbSNiels Provos EVCON_CONNECTING, /**< tries to currently connect */ 399998c0cbSNiels Provos EVCON_IDLE, /**< connection is established */ 409998c0cbSNiels Provos EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or 419998c0cbSNiels Provos **< Status-Line (outgoing conn) */ 429998c0cbSNiels Provos EVCON_READING_HEADERS, /**< reading request/response headers */ 439998c0cbSNiels Provos EVCON_READING_BODY, /**< reading request/response body */ 449998c0cbSNiels Provos EVCON_READING_TRAILER, /**< reading request/response chunked trailer */ 459998c0cbSNiels Provos EVCON_WRITING /**< writing request/response headers/body */ 46ba7262ebSNiels Provos }; 47ba7262ebSNiels Provos 4867947ce3SNiels Provos struct event_base; 4967947ce3SNiels Provos 5090b3ed5bSNick Mathewson /* A client or server connection. */ 51147b71e3SNiels Provos struct evhttp_connection { 522a3b5872SNick Mathewson /* we use this tailq only if this connection was created for an http 532a3b5872SNick Mathewson * server */ 5460433a0aSGilad Benjamini TAILQ_ENTRY(evhttp_connection) next; 5536212f9dSNiels Provos 561120f04fSNick Mathewson evutil_socket_t fd; 57e44ef375SNiels Provos struct bufferevent *bufev; 58e44ef375SNiels Provos 59e44ef375SNiels Provos struct event retry_ev; /* for retrying connects */ 60147b71e3SNiels Provos 6198f9616bSNiels Provos char *bind_address; /* address to use for binding the src */ 62e9837124SThomas Bernard ev_uint16_t bind_port; /* local port for binding the src */ 6398f9616bSNiels Provos 6498f9616bSNiels Provos char *address; /* address to connect to */ 65e9837124SThomas Bernard ev_uint16_t port; 66147b71e3SNiels Provos 6747bad8abSNick Mathewson size_t max_headers_size; 6834f28e08SNick Mathewson ev_uint64_t max_body_size; 6947bad8abSNick Mathewson 70ba7262ebSNiels Provos int flags; 71ba7262ebSNiels Provos #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */ 72ba7262ebSNiels Provos #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */ 73942656bbSNiels Provos #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */ 744dc09795SAzat Khuzhin /* set when we want to auto free the connection */ 754dc09795SAzat Khuzhin #define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END 762ff164abSAzat Khuzhin /* Installed when attempt to read HTTP error after write failed, see 772ff164abSAzat Khuzhin * EVHTTP_CON_READ_ON_WRITE_ERROR */ 782ff164abSAzat Khuzhin #define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1) 792d028ef6SNiels Provos 806350e6c4SConstantine Verutin struct timeval timeout; /* timeout for events */ 81852d05a3SNiels Provos int retry_cnt; /* retry count */ 82852d05a3SNiels Provos int retry_max; /* maximum number of retries */ 83350a3c40SNick Mathewson struct timeval initial_retry_timeout; /* Timeout for low long to wait 84350a3c40SNick Mathewson * after first failing attempt 85350a3c40SNick Mathewson * before retry */ 86ba7262ebSNiels Provos 87ba7262ebSNiels Provos enum evhttp_connection_state state; 88ba7262ebSNiels Provos 8936212f9dSNiels Provos /* for server connections, the http server they are connected with */ 9036212f9dSNiels Provos struct evhttp *http_server; 9136212f9dSNiels Provos 92ba7262ebSNiels Provos TAILQ_HEAD(evcon_requestq, evhttp_request) requests; 93ba7262ebSNiels Provos 94147b71e3SNiels Provos void (*cb)(struct evhttp_connection *, void *); 95147b71e3SNiels Provos void *cb_arg; 96de7db33aSNiels Provos 97de7db33aSNiels Provos void (*closecb)(struct evhttp_connection *, void *); 98de7db33aSNiels Provos void *closecb_arg; 9967947ce3SNiels Provos 100a4079aa8SNick Mathewson struct event_callback read_more_deferred_cb; 10174c0e862SNick Mathewson 10267947ce3SNiels Provos struct event_base *base; 103c698b77dSNick Mathewson struct evdns_base *dns_base; 10412c29b0fSAzat Khuzhin int ai_family; 105147b71e3SNiels Provos }; 106147b71e3SNiels Provos 10790b3ed5bSNick Mathewson /* A callback for an http server */ 108147b71e3SNiels Provos struct evhttp_cb { 109147b71e3SNiels Provos TAILQ_ENTRY(evhttp_cb) next; 110147b71e3SNiels Provos 111147b71e3SNiels Provos char *what; 112147b71e3SNiels Provos 113147b71e3SNiels Provos void (*cb)(struct evhttp_request *req, void *); 114147b71e3SNiels Provos void *cbarg; 115147b71e3SNiels Provos }; 116147b71e3SNiels Provos 117fda1216bSNiels Provos /* both the http server as well as the rpc system need to queue connections */ 118fda1216bSNiels Provos TAILQ_HEAD(evconq, evhttp_connection); 119fda1216bSNiels Provos 120f940eb4bSNiels Provos /* each bound socket is stored in one of these */ 121f940eb4bSNiels Provos struct evhttp_bound_socket { 12260433a0aSGilad Benjamini TAILQ_ENTRY(evhttp_bound_socket) next; 123f940eb4bSNiels Provos 124ec34533aSNick Mathewson struct evconnlistener *listener; 125f940eb4bSNiels Provos }; 126f940eb4bSNiels Provos 1274feedef9SChristopher Davis /* server alias list item. */ 128aab8c38bSChristopher Davis struct evhttp_server_alias { 129aab8c38bSChristopher Davis TAILQ_ENTRY(evhttp_server_alias) next; 1304feedef9SChristopher Davis 1314feedef9SChristopher Davis char *alias; /* the server alias. */ 132aab8c38bSChristopher Davis }; 133aab8c38bSChristopher Davis 134f940eb4bSNiels Provos struct evhttp { 13590b3ed5bSNick Mathewson /* Next vhost, if this is a vhost. */ 13690b3ed5bSNick Mathewson TAILQ_ENTRY(evhttp) next_vhost; 137f2a81fbcSNiels Provos 13890b3ed5bSNick Mathewson /* All listeners for this host */ 139f940eb4bSNiels Provos TAILQ_HEAD(boundq, evhttp_bound_socket) sockets; 140147b71e3SNiels Provos 141147b71e3SNiels Provos TAILQ_HEAD(httpcbq, evhttp_cb) callbacks; 14290b3ed5bSNick Mathewson 14390b3ed5bSNick Mathewson /* All live connections on this host. */ 144fda1216bSNiels Provos struct evconq connections; 145147b71e3SNiels Provos 146f2a81fbcSNiels Provos TAILQ_HEAD(vhostsq, evhttp) virtualhosts; 147f2a81fbcSNiels Provos 148aab8c38bSChristopher Davis TAILQ_HEAD(aliasq, evhttp_server_alias) aliases; 149aab8c38bSChristopher Davis 150f2a81fbcSNiels Provos /* NULL if this server is not a vhost */ 151f2a81fbcSNiels Provos char *vhost_pattern; 152f2a81fbcSNiels Provos 1536350e6c4SConstantine Verutin struct timeval timeout; 154942656bbSNiels Provos 15547bad8abSNick Mathewson size_t default_max_headers_size; 15634f28e08SNick Mathewson ev_uint64_t default_max_body_size; 1579fde5189SAzat Khuzhin int flags; 1585a5acd9aSNicolas Martyanoff const char *default_content_type; 15947bad8abSNick Mathewson 160f5b391e2SNick Mathewson /* Bitmask of all HTTP methods that we accept and pass to user 161f5b391e2SNick Mathewson * callbacks. */ 162f5b391e2SNick Mathewson ev_uint16_t allowed_methods; 16375a73414SFelix Nawothnig 16490b3ed5bSNick Mathewson /* Fallback callback if all the other callbacks for this connection 16590b3ed5bSNick Mathewson don't match. */ 166147b71e3SNiels Provos void (*gencb)(struct evhttp_request *req, void *); 167147b71e3SNiels Provos void *gencbarg; 1688d3a8500SNick Mathewson struct bufferevent* (*bevcb)(struct event_base *, void *); 1698d3a8500SNick Mathewson void *bevcbarg; 17067947ce3SNiels Provos 17167947ce3SNiels Provos struct event_base *base; 172147b71e3SNiels Provos }; 173147b71e3SNiels Provos 17490b3ed5bSNick Mathewson /* XXX most of these functions could be static. */ 17590b3ed5bSNick Mathewson 176ba7262ebSNiels Provos /* resets the connection; can be reused for more requests */ 1778ac3c4c2SNick Mathewson void evhttp_connection_reset_(struct evhttp_connection *); 178ba7262ebSNiels Provos 179ba7262ebSNiels Provos /* connects if necessary */ 1808ac3c4c2SNick Mathewson int evhttp_connection_connect_(struct evhttp_connection *); 181ba7262ebSNiels Provos 1827b077194SAzat Khuzhin enum evhttp_request_error; 183ba7262ebSNiels Provos /* notifies the current request that it failed; resets connection */ 1849806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL 1858ac3c4c2SNick Mathewson void evhttp_connection_fail_(struct evhttp_connection *, 1867b077194SAzat Khuzhin enum evhttp_request_error error); 187ba7262ebSNiels Provos 1889516df0eSNick Mathewson enum message_read_status; 1899516df0eSNick Mathewson 1909806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL 1918ac3c4c2SNick Mathewson enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*); 1929806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL 1938ac3c4c2SNick Mathewson enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*); 194147b71e3SNiels Provos 1958ac3c4c2SNick Mathewson void evhttp_start_read_(struct evhttp_connection *); 1960b46b39eSAzat Khuzhin void evhttp_start_write_(struct evhttp_connection *); 197147b71e3SNiels Provos 198147b71e3SNiels Provos /* response sending HTML the data in the buffer */ 1998ac3c4c2SNick Mathewson void evhttp_response_code_(struct evhttp_request *, int, const char *); 2008ac3c4c2SNick Mathewson void evhttp_send_page_(struct evhttp_request *, struct evbuffer *); 201147b71e3SNiels Provos 2029806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL 203de8101a8SAzat Khuzhin int evhttp_decode_uri_internal(const char *uri, size_t length, 204de8101a8SAzat Khuzhin char *ret, int decode_plus); 205de8101a8SAzat Khuzhin 206*51853472SAzat Khuzhin #endif /* HTTP_INTERNAL_H_INCLUDED_ */ 207