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