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
21 enum message_read_status {
23 MORE_DATA_EXPECTED = 0,
25 REQUEST_CANCELED = -2,
31 struct evhttp_request;
33 /* Indicates an unknown request method. */
34 #define EVHTTP_REQ_UNKNOWN_ (1<<15)
36 enum evhttp_connection_state {
37 EVCON_DISCONNECTED, /**< not currently connected not trying either*/
38 EVCON_CONNECTING, /**< tries to currently connect */
39 EVCON_IDLE, /**< connection is established */
40 EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
41 **< Status-Line (outgoing conn) */
42 EVCON_READING_HEADERS, /**< reading request/response headers */
43 EVCON_READING_BODY, /**< reading request/response body */
44 EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
45 EVCON_WRITING /**< writing request/response headers/body */
50 /* A client or server connection. */
51 struct evhttp_connection {
52 /* we use this tailq only if this connection was created for an http
54 TAILQ_ENTRY(evhttp_connection) next;
57 struct bufferevent *bufev;
59 struct event retry_ev; /* for retrying connects */
61 char *bind_address; /* address to use for binding the src */
62 ev_uint16_t bind_port; /* local port for binding the src */
64 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)
80 struct timeval timeout; /* timeout for events */
81 int retry_cnt; /* retry count */
82 int retry_max; /* maximum number of retries */
83 struct timeval initial_retry_timeout; /* Timeout for low long to wait
84 * after first failing attempt
87 enum evhttp_connection_state state;
89 /* for server connections, the http server they are connected with */
90 struct evhttp *http_server;
92 TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
94 void (*cb)(struct evhttp_connection *, void *);
97 void (*closecb)(struct evhttp_connection *, void *);
100 struct event_callback read_more_deferred_cb;
102 struct event_base *base;
103 struct evdns_base *dns_base;
107 /* A callback for an http server */
109 TAILQ_ENTRY(evhttp_cb) next;
113 void (*cb)(struct evhttp_request *req, void *);
117 /* both the http server as well as the rpc system need to queue connections */
118 TAILQ_HEAD(evconq, evhttp_connection);
120 /* each bound socket is stored in one of these */
121 struct evhttp_bound_socket {
122 TAILQ_ENTRY(evhttp_bound_socket) next;
124 struct evconnlistener *listener;
127 /* server alias list item. */
128 struct evhttp_server_alias {
129 TAILQ_ENTRY(evhttp_server_alias) next;
131 char *alias; /* the server alias. */
135 /* Next vhost, if this is a vhost. */
136 TAILQ_ENTRY(evhttp) next_vhost;
138 /* All listeners for this host */
139 TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
141 TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
143 /* All live connections on this host. */
144 struct evconq connections;
146 TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
148 TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
150 /* NULL if this server is not a vhost */
153 struct timeval timeout;
155 size_t default_max_headers_size;
156 ev_uint64_t default_max_body_size;
158 const char *default_content_type;
160 /* Bitmask of all HTTP methods that we accept and pass to user
162 ev_uint16_t allowed_methods;
164 /* Fallback callback if all the other callbacks for this connection
166 void (*gencb)(struct evhttp_request *req, void *);
168 struct bufferevent* (*bevcb)(struct event_base *, void *);
171 struct event_base *base;
174 /* XXX most of these functions could be static. */
176 /* resets the connection; can be reused for more requests */
177 void evhttp_connection_reset_(struct evhttp_connection *);
179 /* connects if necessary */
180 int evhttp_connection_connect_(struct evhttp_connection *);
182 enum evhttp_request_error;
183 /* notifies the current request that it failed; resets connection */
185 void evhttp_connection_fail_(struct evhttp_connection *,
186 enum evhttp_request_error error);
188 enum message_read_status;
191 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
193 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
195 void evhttp_start_read_(struct evhttp_connection *);
196 void evhttp_start_write_(struct evhttp_connection *);
198 /* response sending HTML the data in the buffer */
199 void evhttp_response_code_(struct evhttp_request *, int, const char *);
200 void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
203 int evhttp_decode_uri_internal(const char *uri, size_t length,
204 char *ret, int decode_plus);
206 #endif /* HTTP_INTERNAL_H_INCLUDED_ */