Add per-user preferences support.
[chromium-blink-merge.git] / third_party / libevent / evhttp.h
blob30dee8bb9909e5eabad75ff0590f4a57d6fca711
1 /*
2 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef _EVHTTP_H_
28 #define _EVHTTP_H_
30 #include "event.h"
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 #ifdef WIN32
37 #define WIN32_LEAN_AND_MEAN
38 #include <winsock2.h>
39 #include <windows.h>
40 #undef WIN32_LEAN_AND_MEAN
41 #endif
43 /** @file evhttp.h
45 * Basic support for HTTP serving.
47 * As libevent is a library for dealing with event notification and most
48 * interesting applications are networked today, I have often found the
49 * need to write HTTP code. The following prototypes and definitions provide
50 * an application with a minimal interface for making HTTP requests and for
51 * creating a very simple HTTP server.
54 /* Response codes */
55 #define HTTP_OK 200
56 #define HTTP_NOCONTENT 204
57 #define HTTP_MOVEPERM 301
58 #define HTTP_MOVETEMP 302
59 #define HTTP_NOTMODIFIED 304
60 #define HTTP_BADREQUEST 400
61 #define HTTP_NOTFOUND 404
62 #define HTTP_SERVUNAVAIL 503
64 struct evhttp;
65 struct evhttp_request;
66 struct evkeyvalq;
68 /** Create a new HTTP server
70 * @param base (optional) the event base to receive the HTTP events
71 * @return a pointer to a newly initialized evhttp server structure
73 struct evhttp *evhttp_new(struct event_base *base);
75 /**
76 * Binds an HTTP server on the specified address and port.
78 * Can be called multiple times to bind the same http server
79 * to multiple different ports.
81 * @param http a pointer to an evhttp object
82 * @param address a string containing the IP address to listen(2) on
83 * @param port the port number to listen on
84 * @return a newly allocated evhttp struct
85 * @see evhttp_free()
87 int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port);
89 /**
90 * Makes an HTTP server accept connections on the specified socket
92 * This may be useful to create a socket and then fork multiple instances
93 * of an http server, or when a socket has been communicated via file
94 * descriptor passing in situations where an http servers does not have
95 * permissions to bind to a low-numbered port.
97 * Can be called multiple times to have the http server listen to
98 * multiple different sockets.
100 * @param http a pointer to an evhttp object
101 * @param fd a socket fd that is ready for accepting connections
102 * @return 0 on success, -1 on failure.
103 * @see evhttp_free(), evhttp_bind_socket()
105 int evhttp_accept_socket(struct evhttp *http, int fd);
108 * Free the previously created HTTP server.
110 * Works only if no requests are currently being served.
112 * @param http the evhttp server object to be freed
113 * @see evhttp_start()
115 void evhttp_free(struct evhttp* http);
117 /** Set a callback for a specified URI */
118 void evhttp_set_cb(struct evhttp *, const char *,
119 void (*)(struct evhttp_request *, void *), void *);
121 /** Removes the callback for a specified URI */
122 int evhttp_del_cb(struct evhttp *, const char *);
124 /** Set a callback for all requests that are not caught by specific callbacks
126 void evhttp_set_gencb(struct evhttp *,
127 void (*)(struct evhttp_request *, void *), void *);
130 * Set the timeout for an HTTP request.
132 * @param http an evhttp object
133 * @param timeout_in_secs the timeout, in seconds
135 void evhttp_set_timeout(struct evhttp *, int timeout_in_secs);
137 /* Request/Response functionality */
140 * Send an HTML error message to the client.
142 * @param req a request object
143 * @param error the HTTP error code
144 * @param reason a brief explanation of the error
146 void evhttp_send_error(struct evhttp_request *req, int error,
147 const char *reason);
150 * Send an HTML reply to the client.
152 * @param req a request object
153 * @param code the HTTP response code to send
154 * @param reason a brief message to send with the response code
155 * @param databuf the body of the response
157 void evhttp_send_reply(struct evhttp_request *req, int code,
158 const char *reason, struct evbuffer *databuf);
160 /* Low-level response interface, for streaming/chunked replies */
161 void evhttp_send_reply_start(struct evhttp_request *, int, const char *);
162 void evhttp_send_reply_chunk(struct evhttp_request *, struct evbuffer *);
163 void evhttp_send_reply_end(struct evhttp_request *);
166 * Start an HTTP server on the specified address and port
168 * DEPRECATED: it does not allow an event base to be specified
170 * @param address the address to which the HTTP server should be bound
171 * @param port the port number on which the HTTP server should listen
172 * @return an struct evhttp object
174 struct evhttp *evhttp_start(const char *address, u_short port);
177 * Interfaces for making requests
179 enum evhttp_cmd_type { EVHTTP_REQ_GET, EVHTTP_REQ_POST, EVHTTP_REQ_HEAD };
181 enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
184 * the request structure that a server receives.
185 * WARNING: expect this structure to change. I will try to provide
186 * reasonable accessors.
188 struct evhttp_request {
189 #if defined(TAILQ_ENTRY)
190 TAILQ_ENTRY(evhttp_request) next;
191 #else
192 struct {
193 struct evhttp_request *tqe_next;
194 struct evhttp_request **tqe_prev;
195 } next;
196 #endif
198 /* the connection object that this request belongs to */
199 struct evhttp_connection *evcon;
200 int flags;
201 #define EVHTTP_REQ_OWN_CONNECTION 0x0001
202 #define EVHTTP_PROXY_REQUEST 0x0002
204 struct evkeyvalq *input_headers;
205 struct evkeyvalq *output_headers;
207 /* address of the remote host and the port connection came from */
208 char *remote_host;
209 u_short remote_port;
211 enum evhttp_request_kind kind;
212 enum evhttp_cmd_type type;
214 char *uri; /* uri after HTTP request was parsed */
216 char major; /* HTTP Major number */
217 char minor; /* HTTP Minor number */
219 int response_code; /* HTTP Response code */
220 char *response_code_line; /* Readable response */
222 struct evbuffer *input_buffer; /* read data */
223 ev_int64_t ntoread;
224 int chunked;
226 struct evbuffer *output_buffer; /* outgoing post or data */
228 /* Callback */
229 void (*cb)(struct evhttp_request *, void *);
230 void *cb_arg;
233 * Chunked data callback - call for each completed chunk if
234 * specified. If not specified, all the data is delivered via
235 * the regular callback.
237 void (*chunk_cb)(struct evhttp_request *, void *);
241 * Creates a new request object that needs to be filled in with the request
242 * parameters. The callback is executed when the request completed or an
243 * error occurred.
245 struct evhttp_request *evhttp_request_new(
246 void (*cb)(struct evhttp_request *, void *), void *arg);
248 /** enable delivery of chunks to requestor */
249 void evhttp_request_set_chunked_cb(struct evhttp_request *,
250 void (*cb)(struct evhttp_request *, void *));
252 /** Frees the request object and removes associated events. */
253 void evhttp_request_free(struct evhttp_request *req);
256 * A connection object that can be used to for making HTTP requests. The
257 * connection object tries to establish the connection when it is given an
258 * http request object.
260 struct evhttp_connection *evhttp_connection_new(
261 const char *address, unsigned short port);
263 /** Frees an http connection */
264 void evhttp_connection_free(struct evhttp_connection *evcon);
266 /** sets the ip address from which http connections are made */
267 void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
268 const char *address);
270 /** sets the local port from which http connections are made */
271 void evhttp_connection_set_local_port(struct evhttp_connection *evcon,
272 unsigned short port);
274 /** Sets the timeout for events related to this connection */
275 void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
276 int timeout_in_secs);
278 /** Sets the retry limit for this connection - -1 repeats indefnitely */
279 void evhttp_connection_set_retries(struct evhttp_connection *evcon,
280 int retry_max);
282 /** Set a callback for connection close. */
283 void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
284 void (*)(struct evhttp_connection *, void *), void *);
287 * Associates an event base with the connection - can only be called
288 * on a freshly created connection object that has not been used yet.
290 void evhttp_connection_set_base(struct evhttp_connection *evcon,
291 struct event_base *base);
293 /** Get the remote address and port associated with this connection. */
294 void evhttp_connection_get_peer(struct evhttp_connection *evcon,
295 char **address, u_short *port);
297 /** The connection gets ownership of the request */
298 int evhttp_make_request(struct evhttp_connection *evcon,
299 struct evhttp_request *req,
300 enum evhttp_cmd_type type, const char *uri);
302 const char *evhttp_request_uri(struct evhttp_request *req);
304 /* Interfaces for dealing with HTTP headers */
306 const char *evhttp_find_header(const struct evkeyvalq *, const char *);
307 int evhttp_remove_header(struct evkeyvalq *, const char *);
308 int evhttp_add_header(struct evkeyvalq *, const char *, const char *);
309 void evhttp_clear_headers(struct evkeyvalq *);
311 /* Miscellaneous utility functions */
315 Helper function to encode a URI.
317 The returned string must be freed by the caller.
319 @param uri an unencoded URI
320 @return a newly allocated URI-encoded string
322 char *evhttp_encode_uri(const char *uri);
326 Helper function to decode a URI.
328 The returned string must be freed by the caller.
330 @param uri an encoded URI
331 @return a newly allocated unencoded URI
333 char *evhttp_decode_uri(const char *uri);
337 * Helper function to parse out arguments in a query.
339 * Parsing a uri like
341 * http://foo.com/?q=test&s=some+thing
343 * will result in two entries in the key value queue.
345 * The first entry is: key="q", value="test"
346 * The second entry is: key="s", value="some thing"
348 * @param uri the request URI
349 * @param headers the head of the evkeyval queue
351 void evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
355 * Escape HTML character entities in a string.
357 * Replaces <, >, ", ' and & with &lt;, &gt;, &quot;,
358 * &#039; and &amp; correspondingly.
360 * The returned string needs to be freed by the caller.
362 * @param html an unescaped HTML string
363 * @return an escaped HTML string
365 char *evhttp_htmlescape(const char *html);
367 #ifdef __cplusplus
369 #endif
371 #endif /* _EVHTTP_H_ */