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