Imported Upstream version 2.1.10
[platform/upstream/libevent.git] / 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
21 enum message_read_status {
22         ALL_DATA_READ = 1,
23         MORE_DATA_EXPECTED = 0,
24         DATA_CORRUPTED = -1,
25         REQUEST_CANCELED = -2,
26         DATA_TOO_LONG = -3
27 };
28
29 struct evbuffer;
30 struct addrinfo;
31 struct evhttp_request;
32
33 /* Indicates an unknown request method. */
34 #define EVHTTP_REQ_UNKNOWN_ (1<<15)
35
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 */
46 };
47
48 struct event_base;
49
50 /* A client or server connection. */
51 struct evhttp_connection {
52         /* we use this tailq only if this connection was created for an http
53          * server */
54         TAILQ_ENTRY(evhttp_connection) next;
55
56         evutil_socket_t fd;
57         struct bufferevent *bufev;
58
59         struct event retry_ev;          /* for retrying connects */
60
61         char *bind_address;             /* address to use for binding the src */
62         ev_uint16_t bind_port;          /* local port for binding the src */
63
64         char *address;                  /* address to connect to */
65         ev_uint16_t port;
66
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
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
85                                                * before retry */
86
87         enum evhttp_connection_state state;
88
89         /* for server connections, the http server they are connected with */
90         struct evhttp *http_server;
91
92         TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
93
94         void (*cb)(struct evhttp_connection *, void *);
95         void *cb_arg;
96
97         void (*closecb)(struct evhttp_connection *, void *);
98         void *closecb_arg;
99
100         struct event_callback read_more_deferred_cb;
101
102         struct event_base *base;
103         struct evdns_base *dns_base;
104         int ai_family;
105 };
106
107 /* A callback for an http server */
108 struct evhttp_cb {
109         TAILQ_ENTRY(evhttp_cb) next;
110
111         char *what;
112
113         void (*cb)(struct evhttp_request *req, void *);
114         void *cbarg;
115 };
116
117 /* both the http server as well as the rpc system need to queue connections */
118 TAILQ_HEAD(evconq, evhttp_connection);
119
120 /* each bound socket is stored in one of these */
121 struct evhttp_bound_socket {
122         TAILQ_ENTRY(evhttp_bound_socket) next;
123
124         struct evconnlistener *listener;
125 };
126
127 /* server alias list item. */
128 struct evhttp_server_alias {
129         TAILQ_ENTRY(evhttp_server_alias) next;
130
131         char *alias; /* the server alias. */
132 };
133
134 struct evhttp {
135         /* Next vhost, if this is a vhost. */
136         TAILQ_ENTRY(evhttp) next_vhost;
137
138         /* All listeners for this host */
139         TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
140
141         TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
142
143         /* All live connections on this host. */
144         struct evconq connections;
145
146         TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
147
148         TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
149
150         /* NULL if this server is not a vhost */
151         char *vhost_pattern;
152
153         struct timeval timeout;
154
155         size_t default_max_headers_size;
156         ev_uint64_t default_max_body_size;
157         int flags;
158         const char *default_content_type;
159
160         /* Bitmask of all HTTP methods that we accept and pass to user
161          * callbacks. */
162         ev_uint16_t allowed_methods;
163
164         /* Fallback callback if all the other callbacks for this connection
165            don't match. */
166         void (*gencb)(struct evhttp_request *req, void *);
167         void *gencbarg;
168         struct bufferevent* (*bevcb)(struct event_base *, void *);
169         void *bevcbarg;
170
171         struct event_base *base;
172 };
173
174 /* XXX most of these functions could be static. */
175
176 /* resets the connection; can be reused for more requests */
177 void evhttp_connection_reset_(struct evhttp_connection *);
178
179 /* connects if necessary */
180 int evhttp_connection_connect_(struct evhttp_connection *);
181
182 enum evhttp_request_error;
183 /* notifies the current request that it failed; resets connection */
184 EVENT2_EXPORT_SYMBOL
185 void evhttp_connection_fail_(struct evhttp_connection *,
186     enum evhttp_request_error error);
187
188 enum message_read_status;
189
190 EVENT2_EXPORT_SYMBOL
191 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
192 EVENT2_EXPORT_SYMBOL
193 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
194
195 void evhttp_start_read_(struct evhttp_connection *);
196 void evhttp_start_write_(struct evhttp_connection *);
197
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 *);
201
202 EVENT2_EXPORT_SYMBOL
203 int evhttp_decode_uri_internal(const char *uri, size_t length,
204     char *ret, int decode_plus);
205
206 #endif /* HTTP_INTERNAL_H_INCLUDED_ */