2 * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2007-2012 Niels Provos and Nick Mathewson
5 * This header file contains definitions for dealing with HTTP requests
6 * that are internal to libevent. As user of the library, you should not
7 * need to know about these.
10 #ifndef HTTP_INTERNAL_H_INCLUDED_
11 #define HTTP_INTERNAL_H_INCLUDED_
13 #include "event2/event_struct.h"
14 #include "util-internal.h"
15 #include "defer-internal.h"
17 #define HTTP_CONNECT_TIMEOUT 45
18 #define HTTP_WRITE_TIMEOUT 50
19 #define HTTP_READ_TIMEOUT 50
20 #define HTTP_INITIAL_RETRY_TIMEOUT 2
22 enum message_read_status {
24 MORE_DATA_EXPECTED = 0,
26 REQUEST_CANCELED = -2,
32 struct evhttp_request;
34 enum evhttp_connection_state {
35 EVCON_DISCONNECTED, /**< not currently connected not trying either*/
36 EVCON_CONNECTING, /**< tries to currently connect */
37 EVCON_IDLE, /**< connection is established */
38 EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
39 **< Status-Line (outgoing conn) */
40 EVCON_READING_HEADERS, /**< reading request/response headers */
41 EVCON_READING_BODY, /**< reading request/response body */
42 EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
43 EVCON_WRITING /**< writing request/response headers/body */
48 /* A client or server connection. */
49 struct evhttp_connection {
50 /* we use this tailq only if this connection was created for an http
52 TAILQ_ENTRY(evhttp_connection) next;
54 struct bufferevent *bufev;
56 struct event retry_ev; /* for retrying connects */
58 char *bind_address; /* address to use for binding the src */
59 ev_uint16_t bind_port; /* local port for binding the src */
61 char *address; /* address to connect to */
67 size_t max_headers_size;
68 ev_uint64_t max_body_size;
71 #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
72 #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
73 #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
74 /* set when we want to auto free the connection */
75 #define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
76 /* Installed when attempt to read HTTP error after write failed, see
77 * EVHTTP_CON_READ_ON_WRITE_ERROR */
78 #define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
79 /* Timeout is not default */
80 #define EVHTTP_CON_TIMEOUT_ADJUSTED (EVHTTP_CON_READING_ERROR << 1)
82 struct timeval timeout_connect; /* timeout for connect phase */
83 struct timeval timeout_read; /* timeout for read */
84 struct timeval timeout_write; /* timeout for write */
86 int retry_cnt; /* retry count */
87 int retry_max; /* maximum number of retries */
88 struct timeval initial_retry_timeout; /* Timeout for low long to wait
89 * after first failing attempt
92 enum evhttp_connection_state state;
94 /* for server connections, the http server they are connected with */
95 struct evhttp *http_server;
97 TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
99 void (*cb)(struct evhttp_connection *, void *);
102 void (*closecb)(struct evhttp_connection *, void *);
105 struct event_callback read_more_deferred_cb;
107 struct event_base *base;
108 struct evdns_base *dns_base;
111 evhttp_ext_method_cb ext_method_cmp;
114 /* A callback for an http server */
116 TAILQ_ENTRY(evhttp_cb) next;
120 void (*cb)(struct evhttp_request *req, void *);
124 /* both the http server as well as the rpc system need to queue connections */
125 TAILQ_HEAD(evconq, evhttp_connection);
127 /* WebSockets connections */
128 TAILQ_HEAD(evwsq, evws_connection);
130 /* each bound socket is stored in one of these */
131 struct evhttp_bound_socket {
132 TAILQ_ENTRY(evhttp_bound_socket) next;
135 struct bufferevent* (*bevcb)(struct event_base *, void *);
138 struct evconnlistener *listener;
141 /* server alias list item. */
142 struct evhttp_server_alias {
143 TAILQ_ENTRY(evhttp_server_alias) next;
145 char *alias; /* the server alias. */
149 /* Next vhost, if this is a vhost. */
150 TAILQ_ENTRY(evhttp) next_vhost;
152 /* All listeners for this host */
153 TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
155 TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
157 /* All live HTTP connections on this host. */
158 struct evconq connections;
159 /* All live WebSockets sessions on this host. */
160 struct evwsq ws_sessions;
164 TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
166 TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
168 /* NULL if this server is not a vhost */
171 struct timeval timeout_read; /* timeout for read */
172 struct timeval timeout_write; /* timeout for write */
174 size_t default_max_headers_size;
175 ev_uint64_t default_max_body_size;
177 const char *default_content_type;
179 /* Bitmask of all HTTP methods that we accept and pass to user
181 ev_uint32_t allowed_methods;
183 /* Fallback callback if all the other callbacks for this connection
185 void (*gencb)(struct evhttp_request *req, void *);
188 struct bufferevent* (*bevcb)(struct event_base *, void *);
190 int (*newreqcb)(struct evhttp_request *req, void *);
193 int (*errorcb)(struct evhttp_request *, struct evbuffer *, int, const char *, void *);
196 struct event_base *base;
198 evhttp_ext_method_cb ext_method_cmp;
201 /* XXX most of these functions could be static. */
203 /* resets the connection; can be reused for more requests */
204 void evhttp_connection_reset_(struct evhttp_connection *, int);
206 /* connects if necessary */
207 int evhttp_connection_connect_(struct evhttp_connection *);
209 enum evhttp_request_error;
210 /* notifies the current request that it failed; resets connection */
212 void evhttp_connection_fail_(struct evhttp_connection *,
213 enum evhttp_request_error error);
215 enum message_read_status;
218 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
220 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
222 void evhttp_start_read_(struct evhttp_connection *);
223 void evhttp_start_write_(struct evhttp_connection *);
225 /* response sending HTML the data in the buffer */
226 void evhttp_response_code_(struct evhttp_request *, int, const char *);
227 void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
229 struct bufferevent * evhttp_start_ws_(struct evhttp_request *req);
231 /* [] has been stripped */
232 #define _EVHTTP_URI_HOST_HAS_BRACKETS 0x02
235 int evhttp_decode_uri_internal(const char *uri, size_t length,
236 char *ret, int decode_plus);
238 #endif /* HTTP_INTERNAL_H_INCLUDED_ */