blob: feaf436d7421acf7b62c7b671a4ed13400ddc807 [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
Christopher Wileye8679812015-07-01 13:36:18 -070021enum message_read_status {
22 ALL_DATA_READ = 1,
23 MORE_DATA_EXPECTED = 0,
24 DATA_CORRUPTED = -1,
25 REQUEST_CANCELED = -2,
26 DATA_TOO_LONG = -3
27};
28
Christopher Wileye8679812015-07-01 13:36:18 -070029struct evbuffer;
30struct addrinfo;
31struct evhttp_request;
32
33/* Indicates an unknown request method. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010034#define EVHTTP_REQ_UNKNOWN_ (1<<15)
Christopher Wileye8679812015-07-01 13:36:18 -070035
36enum evhttp_connection_state {
37 EVCON_DISCONNECTED, /**< not currently connected not trying either*/
38 EVCON_CONNECTING, /**< tries to currently connect */
39 EVCON_IDLE, /**< connection is established */
40 EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
41 **< Status-Line (outgoing conn) */
42 EVCON_READING_HEADERS, /**< reading request/response headers */
43 EVCON_READING_BODY, /**< reading request/response body */
44 EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
45 EVCON_WRITING /**< writing request/response headers/body */
46};
47
48struct event_base;
49
50/* A client or server connection. */
51struct evhttp_connection {
52 /* we use this tailq only if this connection was created for an http
53 * server */
54 TAILQ_ENTRY(evhttp_connection) next;
55
56 evutil_socket_t fd;
57 struct bufferevent *bufev;
58
59 struct event retry_ev; /* for retrying connects */
60
61 char *bind_address; /* address to use for binding the src */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010062 ev_uint16_t bind_port; /* local port for binding the src */
Christopher Wileye8679812015-07-01 13:36:18 -070063
64 char *address; /* address to connect to */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010065 ev_uint16_t port;
Christopher Wileye8679812015-07-01 13:36:18 -070066
67 size_t max_headers_size;
68 ev_uint64_t max_body_size;
69
70 int flags;
Narayan Kamathfc74cb42017-09-13 12:53:52 +010071#define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
72#define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
73#define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
74/* set when we want to auto free the connection */
75#define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
76/* Installed when attempt to read HTTP error after write failed, see
77 * EVHTTP_CON_READ_ON_WRITE_ERROR */
78#define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
Christopher Wileye8679812015-07-01 13:36:18 -070079
Narayan Kamathfc74cb42017-09-13 12:53:52 +010080 struct timeval timeout; /* timeout for events */
Christopher Wileye8679812015-07-01 13:36:18 -070081 int retry_cnt; /* retry count */
82 int retry_max; /* maximum number of retries */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010083 struct timeval initial_retry_timeout; /* Timeout for low long to wait
84 * after first failing attempt
85 * before retry */
Christopher Wileye8679812015-07-01 13:36:18 -070086
87 enum evhttp_connection_state state;
88
89 /* for server connections, the http server they are connected with */
90 struct evhttp *http_server;
91
92 TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
93
94 void (*cb)(struct evhttp_connection *, void *);
95 void *cb_arg;
96
97 void (*closecb)(struct evhttp_connection *, void *);
98 void *closecb_arg;
99
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100100 struct event_callback read_more_deferred_cb;
Christopher Wileye8679812015-07-01 13:36:18 -0700101
102 struct event_base *base;
103 struct evdns_base *dns_base;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100104 int ai_family;
Christopher Wileye8679812015-07-01 13:36:18 -0700105};
106
107/* A callback for an http server */
108struct evhttp_cb {
109 TAILQ_ENTRY(evhttp_cb) next;
110
111 char *what;
112
113 void (*cb)(struct evhttp_request *req, void *);
114 void *cbarg;
115};
116
117/* both the http server as well as the rpc system need to queue connections */
118TAILQ_HEAD(evconq, evhttp_connection);
119
120/* each bound socket is stored in one of these */
121struct evhttp_bound_socket {
122 TAILQ_ENTRY(evhttp_bound_socket) next;
123
124 struct evconnlistener *listener;
125};
126
127/* server alias list item. */
128struct evhttp_server_alias {
129 TAILQ_ENTRY(evhttp_server_alias) next;
130
131 char *alias; /* the server alias. */
132};
133
134struct evhttp {
135 /* Next vhost, if this is a vhost. */
136 TAILQ_ENTRY(evhttp) next_vhost;
137
138 /* All listeners for this host */
139 TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
140
141 TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
142
143 /* All live connections on this host. */
144 struct evconq connections;
145
146 TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
147
148 TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
149
150 /* NULL if this server is not a vhost */
151 char *vhost_pattern;
152
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100153 struct timeval timeout;
Christopher Wileye8679812015-07-01 13:36:18 -0700154
155 size_t default_max_headers_size;
156 ev_uint64_t default_max_body_size;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100157 int flags;
158 const char *default_content_type;
Christopher Wileye8679812015-07-01 13:36:18 -0700159
160 /* Bitmask of all HTTP methods that we accept and pass to user
161 * callbacks. */
162 ev_uint16_t allowed_methods;
163
164 /* Fallback callback if all the other callbacks for this connection
165 don't match. */
166 void (*gencb)(struct evhttp_request *req, void *);
167 void *gencbarg;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100168 struct bufferevent* (*bevcb)(struct event_base *, void *);
169 void *bevcbarg;
Christopher Wileye8679812015-07-01 13:36:18 -0700170
171 struct event_base *base;
172};
173
174/* XXX most of these functions could be static. */
175
176/* resets the connection; can be reused for more requests */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100177void evhttp_connection_reset_(struct evhttp_connection *);
Christopher Wileye8679812015-07-01 13:36:18 -0700178
179/* connects if necessary */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100180int evhttp_connection_connect_(struct evhttp_connection *);
Christopher Wileye8679812015-07-01 13:36:18 -0700181
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100182enum evhttp_request_error;
Christopher Wileye8679812015-07-01 13:36:18 -0700183/* notifies the current request that it failed; resets connection */
Haibo Huangb2279672019-05-31 16:12:39 -0700184EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100185void evhttp_connection_fail_(struct evhttp_connection *,
186 enum evhttp_request_error error);
Christopher Wileye8679812015-07-01 13:36:18 -0700187
188enum message_read_status;
189
Haibo Huangb2279672019-05-31 16:12:39 -0700190EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100191enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
Haibo Huangb2279672019-05-31 16:12:39 -0700192EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100193enum 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
Haibo Huangb2279672019-05-31 16:12:39 -0700202EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100203int evhttp_decode_uri_internal(const char *uri, size_t length,
204 char *ret, int decode_plus);
Christopher Wileye8679812015-07-01 13:36:18 -0700205
Haibo Huangb2279672019-05-31 16:12:39 -0700206#endif /* HTTP_INTERNAL_H_INCLUDED_ */