]> granicus.if.org Git - libevent/blob - http-internal.h
Fix memleak in regress tests
[libevent] / http-internal.h
1 /*
2  * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright 2007-2012 Niels Provos and Nick Mathewson
4  *
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.
8  */
9
10 #ifndef HTTP_INTERNAL_H_INCLUDED_
11 #define HTTP_INTERNAL_H_INCLUDED_
12
13 #include "event2/event_struct.h"
14 #include "util-internal.h"
15 #include "defer-internal.h"
16
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
21
22 enum message_read_status {
23         ALL_DATA_READ = 1,
24         MORE_DATA_EXPECTED = 0,
25         DATA_CORRUPTED = -1,
26         REQUEST_CANCELED = -2,
27         DATA_TOO_LONG = -3
28 };
29
30 struct evbuffer;
31 struct addrinfo;
32 struct evhttp_request;
33
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 */
44 };
45
46 struct event_base;
47
48 /* A client or server connection. */
49 struct evhttp_connection {
50         /* we use this tailq only if this connection was created for an http
51          * server */
52         TAILQ_ENTRY(evhttp_connection) next;
53
54         struct bufferevent *bufev;
55
56         struct event retry_ev;          /* for retrying connects */
57
58         char *bind_address;             /* address to use for binding the src */
59         ev_uint16_t bind_port;          /* local port for binding the src */
60
61         char *address;                  /* address to connect to */
62         ev_uint16_t port;
63
64 #ifndef _WIN32
65         char *unixsocket;
66 #endif
67         size_t max_headers_size;
68         ev_uint64_t max_body_size;
69
70         int flags;
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)
81
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 */
85
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
90                                                * before retry */
91
92         enum evhttp_connection_state state;
93
94         /* for server connections, the http server they are connected with */
95         struct evhttp *http_server;
96
97         TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
98
99         void (*cb)(struct evhttp_connection *, void *);
100         void *cb_arg;
101
102         void (*closecb)(struct evhttp_connection *, void *);
103         void *closecb_arg;
104
105         struct event_callback read_more_deferred_cb;
106
107         struct event_base *base;
108         struct evdns_base *dns_base;
109         int ai_family;
110
111         evhttp_ext_method_cb ext_method_cmp;
112 };
113
114 /* A callback for an http server */
115 struct evhttp_cb {
116         TAILQ_ENTRY(evhttp_cb) next;
117
118         char *what;
119
120         void (*cb)(struct evhttp_request *req, void *);
121         void *cbarg;
122 };
123
124 /* both the http server as well as the rpc system need to queue connections */
125 TAILQ_HEAD(evconq, evhttp_connection);
126
127 /* WebSockets connections */
128 TAILQ_HEAD(evwsq, evws_connection);
129
130 /* each bound socket is stored in one of these */
131 struct evhttp_bound_socket {
132         TAILQ_ENTRY(evhttp_bound_socket) next;
133
134         struct evhttp *http;
135         struct bufferevent* (*bevcb)(struct event_base *, void *);
136         void *bevcbarg;
137
138         struct evconnlistener *listener;
139 };
140
141 /* server alias list item. */
142 struct evhttp_server_alias {
143         TAILQ_ENTRY(evhttp_server_alias) next;
144
145         char *alias; /* the server alias. */
146 };
147
148 struct evhttp {
149         /* Next vhost, if this is a vhost. */
150         TAILQ_ENTRY(evhttp) next_vhost;
151
152         /* All listeners for this host */
153         TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
154
155         TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
156
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;
161         int connection_max;
162         int connection_cnt;
163
164         TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
165
166         TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
167
168         /* NULL if this server is not a vhost */
169         char *vhost_pattern;
170
171         struct timeval timeout_read;            /* timeout for read */
172         struct timeval timeout_write;           /* timeout for write */
173
174         size_t default_max_headers_size;
175         ev_uint64_t default_max_body_size;
176         int flags;
177         const char *default_content_type;
178
179         /* Bitmask of all HTTP methods that we accept and pass to user
180          * callbacks. */
181         ev_uint32_t allowed_methods;
182
183         /* Fallback callback if all the other callbacks for this connection
184            don't match. */
185         void (*gencb)(struct evhttp_request *req, void *);
186         void *gencbarg;
187
188         struct bufferevent* (*bevcb)(struct event_base *, void *);
189         void *bevcbarg;
190         int (*newreqcb)(struct evhttp_request *req, void *);
191         void *newreqcbarg;
192
193         int (*errorcb)(struct evhttp_request *, struct evbuffer *, int, const char *, void *);
194         void *errorcbarg;
195
196         struct event_base *base;
197
198         evhttp_ext_method_cb ext_method_cmp;
199 };
200
201 /* XXX most of these functions could be static. */
202
203 /* resets the connection; can be reused for more requests */
204 void evhttp_connection_reset_(struct evhttp_connection *, int);
205
206 /* connects if necessary */
207 int evhttp_connection_connect_(struct evhttp_connection *);
208
209 enum evhttp_request_error;
210 /* notifies the current request that it failed; resets connection */
211 EVENT2_EXPORT_SYMBOL
212 void evhttp_connection_fail_(struct evhttp_connection *,
213     enum evhttp_request_error error);
214
215 enum message_read_status;
216
217 EVENT2_EXPORT_SYMBOL
218 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
219 EVENT2_EXPORT_SYMBOL
220 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
221
222 void evhttp_start_read_(struct evhttp_connection *);
223 void evhttp_start_write_(struct evhttp_connection *);
224
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 *);
228
229 struct bufferevent * evhttp_start_ws_(struct evhttp_request *req);
230
231 /* [] has been stripped */
232 #define _EVHTTP_URI_HOST_HAS_BRACKETS 0x02
233
234 EVENT2_EXPORT_SYMBOL
235 int evhttp_decode_uri_internal(const char *uri, size_t length,
236     char *ret, int decode_plus);
237
238 #endif /* HTTP_INTERNAL_H_INCLUDED_ */