xref: /netbsd-src/external/bsd/libevent/dist/http-internal.h (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1 /*	$NetBSD: http-internal.h,v 1.1.1.4 2021/04/07 02:43:12 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_INCLUDED_
12 #define HTTP_INTERNAL_H_INCLUDED_
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 enum message_read_status {
23 	ALL_DATA_READ = 1,
24 	MORE_DATA_EXPECTED = 0,
25 	DATA_CORRUPTED = -1,
26 	REQUEST_CANCELED = -2,
27 	DATA_TOO_LONG = -3
28 };
29 
30 struct evbuffer;
31 struct addrinfo;
32 struct evhttp_request;
33 
34 /* Indicates an unknown request method. */
35 #define EVHTTP_REQ_UNKNOWN_ (1<<15)
36 
37 enum evhttp_connection_state {
38 	EVCON_DISCONNECTED,	/**< not currently connected not trying either*/
39 	EVCON_CONNECTING,	/**< tries to currently connect */
40 	EVCON_IDLE,		/**< connection is established */
41 	EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
42 				 **< Status-Line (outgoing conn) */
43 	EVCON_READING_HEADERS,	/**< reading request/response headers */
44 	EVCON_READING_BODY,	/**< reading request/response body */
45 	EVCON_READING_TRAILER,	/**< reading request/response chunked trailer */
46 	EVCON_WRITING		/**< writing request/response headers/body */
47 };
48 
49 struct event_base;
50 
51 /* A client or server connection. */
52 struct evhttp_connection {
53 	/* we use this tailq only if this connection was created for an http
54 	 * server */
55 	TAILQ_ENTRY(evhttp_connection) next;
56 
57 	evutil_socket_t fd;
58 	struct bufferevent *bufev;
59 
60 	struct event retry_ev;		/* for retrying connects */
61 
62 	char *bind_address;		/* address to use for binding the src */
63 	ev_uint16_t bind_port;		/* local port for binding the src */
64 
65 	char *address;			/* address to connect to */
66 	ev_uint16_t port;
67 
68 	size_t max_headers_size;
69 	ev_uint64_t max_body_size;
70 
71 	int flags;
72 #define EVHTTP_CON_INCOMING	0x0001       /* only one request on it ever */
73 #define EVHTTP_CON_OUTGOING	0x0002       /* multiple requests possible */
74 #define EVHTTP_CON_CLOSEDETECT	0x0004   /* detecting if persistent close */
75 /* set when we want to auto free the connection */
76 #define EVHTTP_CON_AUTOFREE	EVHTTP_CON_PUBLIC_FLAGS_END
77 /* Installed when attempt to read HTTP error after write failed, see
78  * EVHTTP_CON_READ_ON_WRITE_ERROR */
79 #define EVHTTP_CON_READING_ERROR	(EVHTTP_CON_AUTOFREE << 1)
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 
108 /* A callback for an http server */
109 struct evhttp_cb {
110 	TAILQ_ENTRY(evhttp_cb) next;
111 
112 	char *what;
113 
114 	void (*cb)(struct evhttp_request *req, void *);
115 	void *cbarg;
116 };
117 
118 /* both the http server as well as the rpc system need to queue connections */
119 TAILQ_HEAD(evconq, evhttp_connection);
120 
121 /* each bound socket is stored in one of these */
122 struct evhttp_bound_socket {
123 	TAILQ_ENTRY(evhttp_bound_socket) next;
124 
125 	struct evconnlistener *listener;
126 };
127 
128 /* server alias list item. */
129 struct evhttp_server_alias {
130 	TAILQ_ENTRY(evhttp_server_alias) next;
131 
132 	char *alias; /* the server alias. */
133 };
134 
135 struct evhttp {
136 	/* Next vhost, if this is a vhost. */
137 	TAILQ_ENTRY(evhttp) next_vhost;
138 
139 	/* All listeners for this host */
140 	TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
141 
142 	TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
143 
144 	/* All live connections on this host. */
145 	struct evconq connections;
146 
147 	TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
148 
149 	TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
150 
151 	/* NULL if this server is not a vhost */
152 	char *vhost_pattern;
153 
154 	struct timeval timeout;
155 
156 	size_t default_max_headers_size;
157 	ev_uint64_t default_max_body_size;
158 	int flags;
159 	const char *default_content_type;
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 	struct bufferevent* (*bevcb)(struct event_base *, void *);
170 	void *bevcbarg;
171 
172 	struct event_base *base;
173 };
174 
175 /* XXX most of these functions could be static. */
176 
177 /* resets the connection; can be reused for more requests */
178 void evhttp_connection_reset_(struct evhttp_connection *);
179 
180 /* connects if necessary */
181 int evhttp_connection_connect_(struct evhttp_connection *);
182 
183 enum evhttp_request_error;
184 /* notifies the current request that it failed; resets connection */
185 EVENT2_EXPORT_SYMBOL
186 void evhttp_connection_fail_(struct evhttp_connection *,
187     enum evhttp_request_error error);
188 
189 enum message_read_status;
190 
191 EVENT2_EXPORT_SYMBOL
192 enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
193 EVENT2_EXPORT_SYMBOL
194 enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
195 
196 void evhttp_start_read_(struct evhttp_connection *);
197 void evhttp_start_write_(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 EVENT2_EXPORT_SYMBOL
204 int evhttp_decode_uri_internal(const char *uri, size_t length,
205     char *ret, int decode_plus);
206 
207 #endif /* HTTP_INTERNAL_H_INCLUDED_ */
208