blob: 45555171c2cfd79e15531504ff909c17b075bed3 [file] [log] [blame]
Christopher Wileye8679812015-07-01 13:36:18 -07001/*
2 * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2007-2012 Niels Provos and Nick Mathewson
4 *
5 * This header file contains definitions for dealing with HTTP requests
6 * that are internal to libevent. As user of the library, you should not
7 * need to know about these.
8 */
9
Narayan Kamathfc74cb42017-09-13 12:53:52 +010010#ifndef HTTP_INTERNAL_H_INCLUDED_
11#define HTTP_INTERNAL_H_INCLUDED_
Christopher Wileye8679812015-07-01 13:36:18 -070012
13#include "event2/event_struct.h"
14#include "util-internal.h"
15#include "defer-internal.h"
16
17#define HTTP_CONNECT_TIMEOUT 45
18#define HTTP_WRITE_TIMEOUT 50
19#define HTTP_READ_TIMEOUT 50
20
21#define HTTP_PREFIX "http://"
22#define HTTP_DEFAULTPORT 80
23
24enum message_read_status {
25 ALL_DATA_READ = 1,
26 MORE_DATA_EXPECTED = 0,
27 DATA_CORRUPTED = -1,
28 REQUEST_CANCELED = -2,
29 DATA_TOO_LONG = -3
30};
31
Christopher Wileye8679812015-07-01 13:36:18 -070032struct evbuffer;
33struct addrinfo;
34struct evhttp_request;
35
36/* Indicates an unknown request method. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010037#define EVHTTP_REQ_UNKNOWN_ (1<<15)
Christopher Wileye8679812015-07-01 13:36:18 -070038
39enum evhttp_connection_state {
40 EVCON_DISCONNECTED, /**< not currently connected not trying either*/
41 EVCON_CONNECTING, /**< tries to currently connect */
42 EVCON_IDLE, /**< connection is established */
43 EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
44 **< Status-Line (outgoing conn) */
45 EVCON_READING_HEADERS, /**< reading request/response headers */
46 EVCON_READING_BODY, /**< reading request/response body */
47 EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
48 EVCON_WRITING /**< writing request/response headers/body */
49};
50
51struct event_base;
52
53/* A client or server connection. */
54struct evhttp_connection {
55 /* we use this tailq only if this connection was created for an http
56 * server */
57 TAILQ_ENTRY(evhttp_connection) next;
58
59 evutil_socket_t fd;
60 struct bufferevent *bufev;
61
62 struct event retry_ev; /* for retrying connects */
63
64 char *bind_address; /* address to use for binding the src */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010065 ev_uint16_t bind_port; /* local port for binding the src */
Christopher Wileye8679812015-07-01 13:36:18 -070066
67 char *address; /* address to connect to */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010068 ev_uint16_t port;
Christopher Wileye8679812015-07-01 13:36:18 -070069
70 size_t max_headers_size;
71 ev_uint64_t max_body_size;
72
73 int flags;
Narayan Kamathfc74cb42017-09-13 12:53:52 +010074#define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
75#define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
76#define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
77/* set when we want to auto free the connection */
78#define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
79/* Installed when attempt to read HTTP error after write failed, see
80 * EVHTTP_CON_READ_ON_WRITE_ERROR */
81#define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
Christopher Wileye8679812015-07-01 13:36:18 -070082
Narayan Kamathfc74cb42017-09-13 12:53:52 +010083 struct timeval timeout; /* timeout for events */
Christopher Wileye8679812015-07-01 13:36:18 -070084 int retry_cnt; /* retry count */
85 int retry_max; /* maximum number of retries */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010086 struct timeval initial_retry_timeout; /* Timeout for low long to wait
87 * after first failing attempt
88 * before retry */
Christopher Wileye8679812015-07-01 13:36:18 -070089
90 enum evhttp_connection_state state;
91
92 /* for server connections, the http server they are connected with */
93 struct evhttp *http_server;
94
95 TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
96
97 void (*cb)(struct evhttp_connection *, void *);
98 void *cb_arg;
99
100 void (*closecb)(struct evhttp_connection *, void *);
101 void *closecb_arg;
102
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100103 struct event_callback read_more_deferred_cb;
Christopher Wileye8679812015-07-01 13:36:18 -0700104
105 struct event_base *base;
106 struct evdns_base *dns_base;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100107 int ai_family;
Christopher Wileye8679812015-07-01 13:36:18 -0700108};
109
110/* A callback for an http server */
111struct 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 */
121TAILQ_HEAD(evconq, evhttp_connection);
122
123/* each bound socket is stored in one of these */
124struct evhttp_bound_socket {
125 TAILQ_ENTRY(evhttp_bound_socket) next;
126
127 struct evconnlistener *listener;
128};
129
130/* server alias list item. */
131struct evhttp_server_alias {
132 TAILQ_ENTRY(evhttp_server_alias) next;
133
134 char *alias; /* the server alias. */
135};
136
137struct 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
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100156 struct timeval timeout;
Christopher Wileye8679812015-07-01 13:36:18 -0700157
158 size_t default_max_headers_size;
159 ev_uint64_t default_max_body_size;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100160 int flags;
161 const char *default_content_type;
Christopher Wileye8679812015-07-01 13:36:18 -0700162
163 /* Bitmask of all HTTP methods that we accept and pass to user
164 * callbacks. */
165 ev_uint16_t allowed_methods;
166
167 /* Fallback callback if all the other callbacks for this connection
168 don't match. */
169 void (*gencb)(struct evhttp_request *req, void *);
170 void *gencbarg;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100171 struct bufferevent* (*bevcb)(struct event_base *, void *);
172 void *bevcbarg;
Christopher Wileye8679812015-07-01 13:36:18 -0700173
174 struct event_base *base;
175};
176
177/* XXX most of these functions could be static. */
178
179/* resets the connection; can be reused for more requests */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100180void evhttp_connection_reset_(struct evhttp_connection *);
Christopher Wileye8679812015-07-01 13:36:18 -0700181
182/* connects if necessary */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100183int evhttp_connection_connect_(struct evhttp_connection *);
Christopher Wileye8679812015-07-01 13:36:18 -0700184
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100185enum evhttp_request_error;
Christopher Wileye8679812015-07-01 13:36:18 -0700186/* notifies the current request that it failed; resets connection */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100187void evhttp_connection_fail_(struct evhttp_connection *,
188 enum evhttp_request_error error);
Christopher Wileye8679812015-07-01 13:36:18 -0700189
190enum message_read_status;
191
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100192enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
193enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
Christopher Wileye8679812015-07-01 13:36:18 -0700194
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100195void evhttp_start_read_(struct evhttp_connection *);
196void evhttp_start_write_(struct evhttp_connection *);
Christopher Wileye8679812015-07-01 13:36:18 -0700197
198/* response sending HTML the data in the buffer */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100199void evhttp_response_code_(struct evhttp_request *, int, const char *);
200void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
201
202int evhttp_decode_uri_internal(const char *uri, size_t length,
203 char *ret, int decode_plus);
Christopher Wileye8679812015-07-01 13:36:18 -0700204
205#endif /* _HTTP_H */