12b15cb3dSCy Schubert /* 22b15cb3dSCy Schubert * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu> 32b15cb3dSCy Schubert * Copyright 2007-2012 Niels Provos and Nick Mathewson 42b15cb3dSCy Schubert * 52b15cb3dSCy Schubert * This header file contains definitions for dealing with HTTP requests 62b15cb3dSCy Schubert * that are internal to libevent. As user of the library, you should not 72b15cb3dSCy Schubert * need to know about these. 82b15cb3dSCy Schubert */ 92b15cb3dSCy Schubert 102b15cb3dSCy Schubert #ifndef HTTP_INTERNAL_H_INCLUDED_ 112b15cb3dSCy Schubert #define HTTP_INTERNAL_H_INCLUDED_ 122b15cb3dSCy Schubert 132b15cb3dSCy Schubert #include "event2/event_struct.h" 142b15cb3dSCy Schubert #include "util-internal.h" 152b15cb3dSCy Schubert #include "defer-internal.h" 162b15cb3dSCy Schubert 172b15cb3dSCy Schubert #define HTTP_CONNECT_TIMEOUT 45 182b15cb3dSCy Schubert #define HTTP_WRITE_TIMEOUT 50 192b15cb3dSCy Schubert #define HTTP_READ_TIMEOUT 50 202b15cb3dSCy Schubert 212b15cb3dSCy Schubert enum message_read_status { 222b15cb3dSCy Schubert ALL_DATA_READ = 1, 232b15cb3dSCy Schubert MORE_DATA_EXPECTED = 0, 242b15cb3dSCy Schubert DATA_CORRUPTED = -1, 252b15cb3dSCy Schubert REQUEST_CANCELED = -2, 262b15cb3dSCy Schubert DATA_TOO_LONG = -3 272b15cb3dSCy Schubert }; 282b15cb3dSCy Schubert 292b15cb3dSCy Schubert struct evbuffer; 302b15cb3dSCy Schubert struct addrinfo; 312b15cb3dSCy Schubert struct evhttp_request; 322b15cb3dSCy Schubert 332b15cb3dSCy Schubert /* Indicates an unknown request method. */ 342b15cb3dSCy Schubert #define EVHTTP_REQ_UNKNOWN_ (1<<15) 352b15cb3dSCy Schubert 362b15cb3dSCy Schubert enum evhttp_connection_state { 372b15cb3dSCy Schubert EVCON_DISCONNECTED, /**< not currently connected not trying either*/ 382b15cb3dSCy Schubert EVCON_CONNECTING, /**< tries to currently connect */ 392b15cb3dSCy Schubert EVCON_IDLE, /**< connection is established */ 402b15cb3dSCy Schubert EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or 412b15cb3dSCy Schubert **< Status-Line (outgoing conn) */ 422b15cb3dSCy Schubert EVCON_READING_HEADERS, /**< reading request/response headers */ 432b15cb3dSCy Schubert EVCON_READING_BODY, /**< reading request/response body */ 442b15cb3dSCy Schubert EVCON_READING_TRAILER, /**< reading request/response chunked trailer */ 452b15cb3dSCy Schubert EVCON_WRITING /**< writing request/response headers/body */ 462b15cb3dSCy Schubert }; 472b15cb3dSCy Schubert 482b15cb3dSCy Schubert struct event_base; 492b15cb3dSCy Schubert 502b15cb3dSCy Schubert /* A client or server connection. */ 512b15cb3dSCy Schubert struct evhttp_connection { 522b15cb3dSCy Schubert /* we use this tailq only if this connection was created for an http 532b15cb3dSCy Schubert * server */ 542b15cb3dSCy Schubert TAILQ_ENTRY(evhttp_connection) next; 552b15cb3dSCy Schubert 562b15cb3dSCy Schubert evutil_socket_t fd; 572b15cb3dSCy Schubert struct bufferevent *bufev; 582b15cb3dSCy Schubert 592b15cb3dSCy Schubert struct event retry_ev; /* for retrying connects */ 602b15cb3dSCy Schubert 612b15cb3dSCy Schubert char *bind_address; /* address to use for binding the src */ 62*a466cc55SCy Schubert ev_uint16_t bind_port; /* local port for binding the src */ 632b15cb3dSCy Schubert 642b15cb3dSCy Schubert char *address; /* address to connect to */ 65*a466cc55SCy Schubert ev_uint16_t port; 662b15cb3dSCy Schubert 672b15cb3dSCy Schubert size_t max_headers_size; 682b15cb3dSCy Schubert ev_uint64_t max_body_size; 692b15cb3dSCy Schubert 702b15cb3dSCy Schubert int flags; 712b15cb3dSCy Schubert #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */ 722b15cb3dSCy Schubert #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */ 732b15cb3dSCy Schubert #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */ 74*a466cc55SCy Schubert /* set when we want to auto free the connection */ 75*a466cc55SCy Schubert #define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END 76*a466cc55SCy Schubert /* Installed when attempt to read HTTP error after write failed, see 77*a466cc55SCy Schubert * EVHTTP_CON_READ_ON_WRITE_ERROR */ 78*a466cc55SCy Schubert #define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1) 792b15cb3dSCy Schubert 802b15cb3dSCy Schubert struct timeval timeout; /* timeout for events */ 812b15cb3dSCy Schubert int retry_cnt; /* retry count */ 822b15cb3dSCy Schubert int retry_max; /* maximum number of retries */ 832b15cb3dSCy Schubert struct timeval initial_retry_timeout; /* Timeout for low long to wait 842b15cb3dSCy Schubert * after first failing attempt 852b15cb3dSCy Schubert * before retry */ 862b15cb3dSCy Schubert 872b15cb3dSCy Schubert enum evhttp_connection_state state; 882b15cb3dSCy Schubert 892b15cb3dSCy Schubert /* for server connections, the http server they are connected with */ 902b15cb3dSCy Schubert struct evhttp *http_server; 912b15cb3dSCy Schubert 922b15cb3dSCy Schubert TAILQ_HEAD(evcon_requestq, evhttp_request) requests; 932b15cb3dSCy Schubert 942b15cb3dSCy Schubert void (*cb)(struct evhttp_connection *, void *); 952b15cb3dSCy Schubert void *cb_arg; 962b15cb3dSCy Schubert 972b15cb3dSCy Schubert void (*closecb)(struct evhttp_connection *, void *); 982b15cb3dSCy Schubert void *closecb_arg; 992b15cb3dSCy Schubert 1002b15cb3dSCy Schubert struct event_callback read_more_deferred_cb; 1012b15cb3dSCy Schubert 1022b15cb3dSCy Schubert struct event_base *base; 1032b15cb3dSCy Schubert struct evdns_base *dns_base; 104a25439b6SCy Schubert int ai_family; 1052b15cb3dSCy Schubert }; 1062b15cb3dSCy Schubert 1072b15cb3dSCy Schubert /* A callback for an http server */ 1082b15cb3dSCy Schubert struct evhttp_cb { 1092b15cb3dSCy Schubert TAILQ_ENTRY(evhttp_cb) next; 1102b15cb3dSCy Schubert 1112b15cb3dSCy Schubert char *what; 1122b15cb3dSCy Schubert 1132b15cb3dSCy Schubert void (*cb)(struct evhttp_request *req, void *); 1142b15cb3dSCy Schubert void *cbarg; 1152b15cb3dSCy Schubert }; 1162b15cb3dSCy Schubert 1172b15cb3dSCy Schubert /* both the http server as well as the rpc system need to queue connections */ 1182b15cb3dSCy Schubert TAILQ_HEAD(evconq, evhttp_connection); 1192b15cb3dSCy Schubert 1202b15cb3dSCy Schubert /* each bound socket is stored in one of these */ 1212b15cb3dSCy Schubert struct evhttp_bound_socket { 1222b15cb3dSCy Schubert TAILQ_ENTRY(evhttp_bound_socket) next; 1232b15cb3dSCy Schubert 1242b15cb3dSCy Schubert struct evconnlistener *listener; 1252b15cb3dSCy Schubert }; 1262b15cb3dSCy Schubert 1272b15cb3dSCy Schubert /* server alias list item. */ 1282b15cb3dSCy Schubert struct evhttp_server_alias { 1292b15cb3dSCy Schubert TAILQ_ENTRY(evhttp_server_alias) next; 1302b15cb3dSCy Schubert 1312b15cb3dSCy Schubert char *alias; /* the server alias. */ 1322b15cb3dSCy Schubert }; 1332b15cb3dSCy Schubert 1342b15cb3dSCy Schubert struct evhttp { 1352b15cb3dSCy Schubert /* Next vhost, if this is a vhost. */ 1362b15cb3dSCy Schubert TAILQ_ENTRY(evhttp) next_vhost; 1372b15cb3dSCy Schubert 1382b15cb3dSCy Schubert /* All listeners for this host */ 1392b15cb3dSCy Schubert TAILQ_HEAD(boundq, evhttp_bound_socket) sockets; 1402b15cb3dSCy Schubert 1412b15cb3dSCy Schubert TAILQ_HEAD(httpcbq, evhttp_cb) callbacks; 1422b15cb3dSCy Schubert 1432b15cb3dSCy Schubert /* All live connections on this host. */ 1442b15cb3dSCy Schubert struct evconq connections; 1452b15cb3dSCy Schubert 1462b15cb3dSCy Schubert TAILQ_HEAD(vhostsq, evhttp) virtualhosts; 1472b15cb3dSCy Schubert 1482b15cb3dSCy Schubert TAILQ_HEAD(aliasq, evhttp_server_alias) aliases; 1492b15cb3dSCy Schubert 1502b15cb3dSCy Schubert /* NULL if this server is not a vhost */ 1512b15cb3dSCy Schubert char *vhost_pattern; 1522b15cb3dSCy Schubert 1532b15cb3dSCy Schubert struct timeval timeout; 1542b15cb3dSCy Schubert 1552b15cb3dSCy Schubert size_t default_max_headers_size; 1562b15cb3dSCy Schubert ev_uint64_t default_max_body_size; 157*a466cc55SCy Schubert int flags; 1582b15cb3dSCy Schubert const char *default_content_type; 1592b15cb3dSCy Schubert 1602b15cb3dSCy Schubert /* Bitmask of all HTTP methods that we accept and pass to user 1612b15cb3dSCy Schubert * callbacks. */ 1622b15cb3dSCy Schubert ev_uint16_t allowed_methods; 1632b15cb3dSCy Schubert 1642b15cb3dSCy Schubert /* Fallback callback if all the other callbacks for this connection 1652b15cb3dSCy Schubert don't match. */ 1662b15cb3dSCy Schubert void (*gencb)(struct evhttp_request *req, void *); 1672b15cb3dSCy Schubert void *gencbarg; 1682b15cb3dSCy Schubert struct bufferevent* (*bevcb)(struct event_base *, void *); 1692b15cb3dSCy Schubert void *bevcbarg; 1702b15cb3dSCy Schubert 1712b15cb3dSCy Schubert struct event_base *base; 1722b15cb3dSCy Schubert }; 1732b15cb3dSCy Schubert 1742b15cb3dSCy Schubert /* XXX most of these functions could be static. */ 1752b15cb3dSCy Schubert 1762b15cb3dSCy Schubert /* resets the connection; can be reused for more requests */ 1772b15cb3dSCy Schubert void evhttp_connection_reset_(struct evhttp_connection *); 1782b15cb3dSCy Schubert 1792b15cb3dSCy Schubert /* connects if necessary */ 1802b15cb3dSCy Schubert int evhttp_connection_connect_(struct evhttp_connection *); 1812b15cb3dSCy Schubert 1822b15cb3dSCy Schubert enum evhttp_request_error; 1832b15cb3dSCy Schubert /* notifies the current request that it failed; resets connection */ 184*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 1852b15cb3dSCy Schubert void evhttp_connection_fail_(struct evhttp_connection *, 1862b15cb3dSCy Schubert enum evhttp_request_error error); 1872b15cb3dSCy Schubert 1882b15cb3dSCy Schubert enum message_read_status; 1892b15cb3dSCy Schubert 190*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 1912b15cb3dSCy Schubert enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*); 192*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 1932b15cb3dSCy Schubert enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*); 1942b15cb3dSCy Schubert 1952b15cb3dSCy Schubert void evhttp_start_read_(struct evhttp_connection *); 196*a466cc55SCy Schubert void evhttp_start_write_(struct evhttp_connection *); 1972b15cb3dSCy Schubert 1982b15cb3dSCy Schubert /* response sending HTML the data in the buffer */ 1992b15cb3dSCy Schubert void evhttp_response_code_(struct evhttp_request *, int, const char *); 2002b15cb3dSCy Schubert void evhttp_send_page_(struct evhttp_request *, struct evbuffer *); 2012b15cb3dSCy Schubert 202*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2032b15cb3dSCy Schubert int evhttp_decode_uri_internal(const char *uri, size_t length, 2042b15cb3dSCy Schubert char *ret, int decode_plus); 2052b15cb3dSCy Schubert 206*a466cc55SCy Schubert #endif /* HTTP_INTERNAL_H_INCLUDED_ */ 207