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