xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/http-internal.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: http-internal.h,v 1.1.1.1 2013/12/27 23:31:22 christos Exp $	*/
2 
3 /*
4  * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright 2007-2012 Niels Provos and Nick Mathewson
6  *
7  * This header file contains definitions for dealing with HTTP requests
8  * that are internal to libevent.  As user of the library, you should not
9  * need to know about these.
10  */
11 
12 #ifndef HTTP_INTERNAL_H_INCLUDED_
13 #define HTTP_INTERNAL_H_INCLUDED_
14 
15 #include "event2/event_struct.h"
16 #include "util-internal.h"
17 #include "defer-internal.h"
18 
19 #define HTTP_CONNECT_TIMEOUT	45
20 #define HTTP_WRITE_TIMEOUT	50
21 #define HTTP_READ_TIMEOUT	50
22 
23 #define HTTP_PREFIX		"http://"
24 #define HTTP_DEFAULTPORT	80
25 
26 enum message_read_status {
27 	ALL_DATA_READ = 1,
28 	MORE_DATA_EXPECTED = 0,
29 	DATA_CORRUPTED = -1,
30 	REQUEST_CANCELED = -2,
31 	DATA_TOO_LONG = -3
32 };
33 
34 enum evhttp_connection_error {
35 	EVCON_HTTP_TIMEOUT,
36 	EVCON_HTTP_EOF,
37 	EVCON_HTTP_INVALID_HEADER,
38 	EVCON_HTTP_BUFFER_ERROR,
39 	EVCON_HTTP_REQUEST_CANCEL
40 };
41 
42 struct evbuffer;
43 struct addrinfo;
44 struct evhttp_request;
45 
46 /* Indicates an unknown request method. */
47 #define EVHTTP_REQ_UNKNOWN_ (1<<15)
48 
49 enum evhttp_connection_state {
50 	EVCON_DISCONNECTED,	/**< not currently connected not trying either*/
51 	EVCON_CONNECTING,	/**< tries to currently connect */
52 	EVCON_IDLE,		/**< connection is established */
53 	EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
54 				 **< Status-Line (outgoing conn) */
55 	EVCON_READING_HEADERS,	/**< reading request/response headers */
56 	EVCON_READING_BODY,	/**< reading request/response body */
57 	EVCON_READING_TRAILER,	/**< reading request/response chunked trailer */
58 	EVCON_WRITING		/**< writing request/response headers/body */
59 };
60 
61 struct event_base;
62 
63 /* A client or server connection. */
64 struct evhttp_connection {
65 	/* we use this tailq only if this connection was created for an http
66 	 * server */
67 	TAILQ_ENTRY(evhttp_connection) next;
68 
69 	evutil_socket_t fd;
70 	struct bufferevent *bufev;
71 
72 	struct event retry_ev;		/* for retrying connects */
73 
74 	char *bind_address;		/* address to use for binding the src */
75 	u_short bind_port;		/* local port for binding the src */
76 
77 	char *address;			/* address to connect to */
78 	u_short port;
79 
80 	size_t max_headers_size;
81 	ev_uint64_t max_body_size;
82 
83 	int flags;
84 #define EVHTTP_CON_INCOMING	0x0001	/* only one request on it ever */
85 #define EVHTTP_CON_OUTGOING	0x0002  /* multiple requests possible */
86 #define EVHTTP_CON_CLOSEDETECT  0x0004  /* detecting if persistent close */
87 
88 	struct timeval timeout;		/* timeout for events */
89 	int retry_cnt;			/* retry count */
90 	int retry_max;			/* maximum number of retries */
91 	struct timeval initial_retry_timeout; /* Timeout for low long to wait
92 					       * after first failing attempt
93 					       * before retry */
94 
95 	enum evhttp_connection_state state;
96 
97 	/* for server connections, the http server they are connected with */
98 	struct evhttp *http_server;
99 
100 	TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
101 
102 	void (*cb)(struct evhttp_connection *, void *);
103 	void *cb_arg;
104 
105 	void (*closecb)(struct evhttp_connection *, void *);
106 	void *closecb_arg;
107 
108 	struct event_callback read_more_deferred_cb;
109 
110 	struct event_base *base;
111 	struct evdns_base *dns_base;
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 /* each bound socket is stored in one of these */
128 struct evhttp_bound_socket {
129 	TAILQ_ENTRY(evhttp_bound_socket) next;
130 
131 	struct evconnlistener *listener;
132 };
133 
134 /* server alias list item. */
135 struct evhttp_server_alias {
136 	TAILQ_ENTRY(evhttp_server_alias) next;
137 
138 	char *alias; /* the server alias. */
139 };
140 
141 struct evhttp {
142 	/* Next vhost, if this is a vhost. */
143 	TAILQ_ENTRY(evhttp) next_vhost;
144 
145 	/* All listeners for this host */
146 	TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
147 
148 	TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
149 
150 	/* All live connections on this host. */
151 	struct evconq connections;
152 
153 	TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
154 
155 	TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
156 
157 	/* NULL if this server is not a vhost */
158 	char *vhost_pattern;
159 
160 	struct timeval timeout;
161 
162 	size_t default_max_headers_size;
163 	ev_uint64_t default_max_body_size;
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 /* notifies the current request that it failed; resets connection */
188 void evhttp_connection_fail_(struct evhttp_connection *,
189     enum evhttp_connection_error error);
190 
191 enum message_read_status;
192 
193 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
194 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
195 
196 void evhttp_start_read_(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 int evhttp_decode_uri_internal(const char *uri, size_t length,
203     char *ret, int decode_plus);
204 
205 #endif /* _HTTP_H */
206