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