xref: /freebsd/contrib/libevent/http-internal.h (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
3c43e99fdSEd Maste  * Copyright 2007-2012 Niels Provos and Nick Mathewson
4c43e99fdSEd Maste  *
5c43e99fdSEd Maste  * This header file contains definitions for dealing with HTTP requests
6c43e99fdSEd Maste  * that are internal to libevent.  As user of the library, you should not
7c43e99fdSEd Maste  * need to know about these.
8c43e99fdSEd Maste  */
9c43e99fdSEd Maste 
10c43e99fdSEd Maste #ifndef HTTP_INTERNAL_H_INCLUDED_
11c43e99fdSEd Maste #define HTTP_INTERNAL_H_INCLUDED_
12c43e99fdSEd Maste 
13c43e99fdSEd Maste #include "event2/event_struct.h"
14c43e99fdSEd Maste #include "util-internal.h"
15c43e99fdSEd Maste #include "defer-internal.h"
16c43e99fdSEd Maste 
17c43e99fdSEd Maste #define HTTP_CONNECT_TIMEOUT	45
18c43e99fdSEd Maste #define HTTP_WRITE_TIMEOUT	50
19c43e99fdSEd Maste #define HTTP_READ_TIMEOUT	50
20c43e99fdSEd Maste 
21c43e99fdSEd Maste enum message_read_status {
22c43e99fdSEd Maste 	ALL_DATA_READ = 1,
23c43e99fdSEd Maste 	MORE_DATA_EXPECTED = 0,
24c43e99fdSEd Maste 	DATA_CORRUPTED = -1,
25c43e99fdSEd Maste 	REQUEST_CANCELED = -2,
26c43e99fdSEd Maste 	DATA_TOO_LONG = -3
27c43e99fdSEd Maste };
28c43e99fdSEd Maste 
29c43e99fdSEd Maste struct evbuffer;
30c43e99fdSEd Maste struct addrinfo;
31c43e99fdSEd Maste struct evhttp_request;
32c43e99fdSEd Maste 
33c43e99fdSEd Maste /* Indicates an unknown request method. */
34c43e99fdSEd Maste #define EVHTTP_REQ_UNKNOWN_ (1<<15)
35c43e99fdSEd Maste 
36c43e99fdSEd Maste enum evhttp_connection_state {
37c43e99fdSEd Maste 	EVCON_DISCONNECTED,	/**< not currently connected not trying either*/
38c43e99fdSEd Maste 	EVCON_CONNECTING,	/**< tries to currently connect */
39c43e99fdSEd Maste 	EVCON_IDLE,		/**< connection is established */
40c43e99fdSEd Maste 	EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
41c43e99fdSEd Maste 				 **< Status-Line (outgoing conn) */
42c43e99fdSEd Maste 	EVCON_READING_HEADERS,	/**< reading request/response headers */
43c43e99fdSEd Maste 	EVCON_READING_BODY,	/**< reading request/response body */
44c43e99fdSEd Maste 	EVCON_READING_TRAILER,	/**< reading request/response chunked trailer */
45c43e99fdSEd Maste 	EVCON_WRITING		/**< writing request/response headers/body */
46c43e99fdSEd Maste };
47c43e99fdSEd Maste 
48c43e99fdSEd Maste struct event_base;
49c43e99fdSEd Maste 
50c43e99fdSEd Maste /* A client or server connection. */
51c43e99fdSEd Maste struct evhttp_connection {
52c43e99fdSEd Maste 	/* we use this tailq only if this connection was created for an http
53c43e99fdSEd Maste 	 * server */
54c43e99fdSEd Maste 	TAILQ_ENTRY(evhttp_connection) next;
55c43e99fdSEd Maste 
56c43e99fdSEd Maste 	evutil_socket_t fd;
57c43e99fdSEd Maste 	struct bufferevent *bufev;
58c43e99fdSEd Maste 
59c43e99fdSEd Maste 	struct event retry_ev;		/* for retrying connects */
60c43e99fdSEd Maste 
61c43e99fdSEd Maste 	char *bind_address;		/* address to use for binding the src */
62c43e99fdSEd Maste 	ev_uint16_t bind_port;		/* local port for binding the src */
63c43e99fdSEd Maste 
64c43e99fdSEd Maste 	char *address;			/* address to connect to */
65c43e99fdSEd Maste 	ev_uint16_t port;
66c43e99fdSEd Maste 
67c43e99fdSEd Maste 	size_t max_headers_size;
68c43e99fdSEd Maste 	ev_uint64_t max_body_size;
69c43e99fdSEd Maste 
70c43e99fdSEd Maste 	int flags;
71c43e99fdSEd Maste #define EVHTTP_CON_INCOMING	0x0001       /* only one request on it ever */
72c43e99fdSEd Maste #define EVHTTP_CON_OUTGOING	0x0002       /* multiple requests possible */
73c43e99fdSEd Maste #define EVHTTP_CON_CLOSEDETECT	0x0004   /* detecting if persistent close */
74c43e99fdSEd Maste /* set when we want to auto free the connection */
75c43e99fdSEd Maste #define EVHTTP_CON_AUTOFREE	EVHTTP_CON_PUBLIC_FLAGS_END
76c43e99fdSEd Maste /* Installed when attempt to read HTTP error after write failed, see
77c43e99fdSEd Maste  * EVHTTP_CON_READ_ON_WRITE_ERROR */
78c43e99fdSEd Maste #define EVHTTP_CON_READING_ERROR	(EVHTTP_CON_AUTOFREE << 1)
79c43e99fdSEd Maste 
80c43e99fdSEd Maste 	struct timeval timeout;		/* timeout for events */
81c43e99fdSEd Maste 	int retry_cnt;			/* retry count */
82c43e99fdSEd Maste 	int retry_max;			/* maximum number of retries */
83c43e99fdSEd Maste 	struct timeval initial_retry_timeout; /* Timeout for low long to wait
84c43e99fdSEd Maste 					       * after first failing attempt
85c43e99fdSEd Maste 					       * before retry */
86c43e99fdSEd Maste 
87c43e99fdSEd Maste 	enum evhttp_connection_state state;
88c43e99fdSEd Maste 
89c43e99fdSEd Maste 	/* for server connections, the http server they are connected with */
90c43e99fdSEd Maste 	struct evhttp *http_server;
91c43e99fdSEd Maste 
92c43e99fdSEd Maste 	TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
93c43e99fdSEd Maste 
94c43e99fdSEd Maste 	void (*cb)(struct evhttp_connection *, void *);
95c43e99fdSEd Maste 	void *cb_arg;
96c43e99fdSEd Maste 
97c43e99fdSEd Maste 	void (*closecb)(struct evhttp_connection *, void *);
98c43e99fdSEd Maste 	void *closecb_arg;
99c43e99fdSEd Maste 
100c43e99fdSEd Maste 	struct event_callback read_more_deferred_cb;
101c43e99fdSEd Maste 
102c43e99fdSEd Maste 	struct event_base *base;
103c43e99fdSEd Maste 	struct evdns_base *dns_base;
104c43e99fdSEd Maste 	int ai_family;
105c43e99fdSEd Maste };
106c43e99fdSEd Maste 
107c43e99fdSEd Maste /* A callback for an http server */
108c43e99fdSEd Maste struct evhttp_cb {
109c43e99fdSEd Maste 	TAILQ_ENTRY(evhttp_cb) next;
110c43e99fdSEd Maste 
111c43e99fdSEd Maste 	char *what;
112c43e99fdSEd Maste 
113c43e99fdSEd Maste 	void (*cb)(struct evhttp_request *req, void *);
114c43e99fdSEd Maste 	void *cbarg;
115c43e99fdSEd Maste };
116c43e99fdSEd Maste 
117c43e99fdSEd Maste /* both the http server as well as the rpc system need to queue connections */
118c43e99fdSEd Maste TAILQ_HEAD(evconq, evhttp_connection);
119c43e99fdSEd Maste 
120c43e99fdSEd Maste /* each bound socket is stored in one of these */
121c43e99fdSEd Maste struct evhttp_bound_socket {
122c43e99fdSEd Maste 	TAILQ_ENTRY(evhttp_bound_socket) next;
123c43e99fdSEd Maste 
124c43e99fdSEd Maste 	struct evconnlistener *listener;
125c43e99fdSEd Maste };
126c43e99fdSEd Maste 
127c43e99fdSEd Maste /* server alias list item. */
128c43e99fdSEd Maste struct evhttp_server_alias {
129c43e99fdSEd Maste 	TAILQ_ENTRY(evhttp_server_alias) next;
130c43e99fdSEd Maste 
131c43e99fdSEd Maste 	char *alias; /* the server alias. */
132c43e99fdSEd Maste };
133c43e99fdSEd Maste 
134c43e99fdSEd Maste struct evhttp {
135c43e99fdSEd Maste 	/* Next vhost, if this is a vhost. */
136c43e99fdSEd Maste 	TAILQ_ENTRY(evhttp) next_vhost;
137c43e99fdSEd Maste 
138c43e99fdSEd Maste 	/* All listeners for this host */
139c43e99fdSEd Maste 	TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
140c43e99fdSEd Maste 
141c43e99fdSEd Maste 	TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
142c43e99fdSEd Maste 
143c43e99fdSEd Maste 	/* All live connections on this host. */
144c43e99fdSEd Maste 	struct evconq connections;
145c43e99fdSEd Maste 
146c43e99fdSEd Maste 	TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
147c43e99fdSEd Maste 
148c43e99fdSEd Maste 	TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
149c43e99fdSEd Maste 
150c43e99fdSEd Maste 	/* NULL if this server is not a vhost */
151c43e99fdSEd Maste 	char *vhost_pattern;
152c43e99fdSEd Maste 
153c43e99fdSEd Maste 	struct timeval timeout;
154c43e99fdSEd Maste 
155c43e99fdSEd Maste 	size_t default_max_headers_size;
156c43e99fdSEd Maste 	ev_uint64_t default_max_body_size;
157c43e99fdSEd Maste 	int flags;
158c43e99fdSEd Maste 	const char *default_content_type;
159c43e99fdSEd Maste 
160c43e99fdSEd Maste 	/* Bitmask of all HTTP methods that we accept and pass to user
161c43e99fdSEd Maste 	 * callbacks. */
162c43e99fdSEd Maste 	ev_uint16_t allowed_methods;
163c43e99fdSEd Maste 
164c43e99fdSEd Maste 	/* Fallback callback if all the other callbacks for this connection
165c43e99fdSEd Maste 	   don't match. */
166c43e99fdSEd Maste 	void (*gencb)(struct evhttp_request *req, void *);
167c43e99fdSEd Maste 	void *gencbarg;
168c43e99fdSEd Maste 	struct bufferevent* (*bevcb)(struct event_base *, void *);
169c43e99fdSEd Maste 	void *bevcbarg;
170c43e99fdSEd Maste 
171c43e99fdSEd Maste 	struct event_base *base;
172c43e99fdSEd Maste };
173c43e99fdSEd Maste 
174c43e99fdSEd Maste /* XXX most of these functions could be static. */
175c43e99fdSEd Maste 
176c43e99fdSEd Maste /* resets the connection; can be reused for more requests */
177c43e99fdSEd Maste void evhttp_connection_reset_(struct evhttp_connection *);
178c43e99fdSEd Maste 
179c43e99fdSEd Maste /* connects if necessary */
180c43e99fdSEd Maste int evhttp_connection_connect_(struct evhttp_connection *);
181c43e99fdSEd Maste 
182c43e99fdSEd Maste enum evhttp_request_error;
183c43e99fdSEd Maste /* notifies the current request that it failed; resets connection */
184*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
185c43e99fdSEd Maste void evhttp_connection_fail_(struct evhttp_connection *,
186c43e99fdSEd Maste     enum evhttp_request_error error);
187c43e99fdSEd Maste 
188c43e99fdSEd Maste enum message_read_status;
189c43e99fdSEd Maste 
190*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
191c43e99fdSEd Maste enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
192*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
193c43e99fdSEd Maste enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
194c43e99fdSEd Maste 
195c43e99fdSEd Maste void evhttp_start_read_(struct evhttp_connection *);
196c43e99fdSEd Maste void evhttp_start_write_(struct evhttp_connection *);
197c43e99fdSEd Maste 
198c43e99fdSEd Maste /* response sending HTML the data in the buffer */
199c43e99fdSEd Maste void evhttp_response_code_(struct evhttp_request *, int, const char *);
200c43e99fdSEd Maste void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
201c43e99fdSEd Maste 
202*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
203c43e99fdSEd Maste int evhttp_decode_uri_internal(const char *uri, size_t length,
204c43e99fdSEd Maste     char *ret, int decode_plus);
205c43e99fdSEd Maste 
206*b50261e2SCy Schubert #endif /* HTTP_INTERNAL_H_INCLUDED_ */
207