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