3 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #define WIN32_LEAN_AND_MEAN
41 #undef WIN32_LEAN_AND_MEAN
46 * Basic support for HTTP serving.
48 * As libevent is a library for dealing with event notification and most
49 * interesting applications are networked today, I have often found the
50 * need to write HTTP code. The following prototypes and definitions provide
51 * an application with a minimal interface for making HTTP requests and for
52 * creating a very simple HTTP server.
57 #define HTTP_NOCONTENT 204
58 #define HTTP_MOVEPERM 301
59 #define HTTP_MOVETEMP 302
60 #define HTTP_NOTMODIFIED 304
61 #define HTTP_BADREQUEST 400
62 #define HTTP_NOTFOUND 404
63 #define HTTP_SERVUNAVAIL 503
66 struct evhttp_request
;
69 /** Create a new HTTP server
71 * @param base (optional) the event base to receive the HTTP events
72 * @return a pointer to a newly initialized evhttp server structure
74 struct evhttp
*evhttp_new(struct event_base
*base
);
77 * Binds an HTTP server on the specified address and port.
79 * Can be called multiple times to bind the same http server
80 * to multiple different ports.
82 * @param http a pointer to an evhttp object
83 * @param address a string containing the IP address to listen(2) on
84 * @param port the port number to listen on
85 * @return a newly allocated evhttp struct
88 int evhttp_bind_socket(struct evhttp
*http
, const char *address
, u_short port
);
91 * Makes an HTTP server accept connections on the specified socket
93 * This may be useful to create a socket and then fork multiple instances
94 * of an http server, or when a socket has been communicated via file
95 * descriptor passing in situations where an http servers does not have
96 * permissions to bind to a low-numbered port.
98 * Can be called multiple times to have the http server listen to
99 * multiple different sockets.
101 * @param http a pointer to an evhttp object
102 * @param fd a socket fd that is ready for accepting connections
103 * @return 0 on success, -1 on failure.
104 * @see evhttp_free(), evhttp_bind_socket()
106 int evhttp_accept_socket(struct evhttp
*http
, int fd
);
109 * Free the previously created HTTP server.
111 * Works only if no requests are currently being served.
113 * @param http the evhttp server object to be freed
114 * @see evhttp_start()
116 void evhttp_free(struct evhttp
* http
);
118 /** Set a callback for a specified URI */
119 void evhttp_set_cb(struct evhttp
*, const char *,
120 void (*)(struct evhttp_request
*, void *), void *);
122 /** Removes the callback for a specified URI */
123 int evhttp_del_cb(struct evhttp
*, const char *);
125 /** Set a callback for all requests that are not caught by specific callbacks
127 void evhttp_set_gencb(struct evhttp
*,
128 void (*)(struct evhttp_request
*, void *), void *);
131 * Set the timeout for an HTTP request.
133 * @param http an evhttp object
134 * @param timeout_in_secs the timeout, in seconds
136 void evhttp_set_timeout(struct evhttp
*, int timeout_in_secs
);
138 /* Request/Response functionality */
141 * Send an HTML error message to the client.
143 * @param req a request object
144 * @param error the HTTP error code
145 * @param reason a brief explanation of the error
147 void evhttp_send_error(struct evhttp_request
*req
, int error
,
151 * Send an HTML reply to the client.
153 * @param req a request object
154 * @param code the HTTP response code to send
155 * @param reason a brief message to send with the response code
156 * @param databuf the body of the response
158 void evhttp_send_reply(struct evhttp_request
*req
, int code
,
159 const char *reason
, struct evbuffer
*databuf
);
161 /* Low-level response interface, for streaming/chunked replies */
162 void evhttp_send_reply_start(struct evhttp_request
*, int, const char *);
163 void evhttp_send_reply_chunk(struct evhttp_request
*, struct evbuffer
*);
164 void evhttp_send_reply_end(struct evhttp_request
*);
167 * Start an HTTP server on the specified address and port
169 * DEPRECATED: it does not allow an event base to be specified
171 * @param address the address to which the HTTP server should be bound
172 * @param port the port number on which the HTTP server should listen
173 * @return an struct evhttp object
175 struct evhttp
*evhttp_start(const char *address
, u_short port
);
178 * Interfaces for making requests
180 enum evhttp_cmd_type
{ EVHTTP_REQ_GET
, EVHTTP_REQ_POST
, EVHTTP_REQ_HEAD
};
182 enum evhttp_request_kind
{ EVHTTP_REQUEST
, EVHTTP_RESPONSE
};
185 * the request structure that a server receives.
186 * WARNING: expect this structure to change. I will try to provide
187 * reasonable accessors.
189 struct evhttp_request
{
190 #if defined(TAILQ_ENTRY)
191 TAILQ_ENTRY(evhttp_request
) next
;
194 struct evhttp_request
*tqe_next
;
195 struct evhttp_request
**tqe_prev
;
199 /* the connection object that this request belongs to */
200 struct evhttp_connection
*evcon
;
202 #define EVHTTP_REQ_OWN_CONNECTION 0x0001
203 #define EVHTTP_PROXY_REQUEST 0x0002
205 struct evkeyvalq
*input_headers
;
206 struct evkeyvalq
*output_headers
;
208 /* address of the remote host and the port connection came from */
212 enum evhttp_request_kind kind
;
213 enum evhttp_cmd_type type
;
215 char *uri
; /* uri after HTTP request was parsed */
217 char major
; /* HTTP Major number */
218 char minor
; /* HTTP Minor number */
220 int response_code
; /* HTTP Response code */
221 char *response_code_line
; /* Readable response */
223 struct evbuffer
*input_buffer
; /* read data */
227 struct evbuffer
*output_buffer
; /* outgoing post or data */
230 void (*cb
)(struct evhttp_request
*, void *);
234 * Chunked data callback - call for each completed chunk if
235 * specified. If not specified, all the data is delivered via
236 * the regular callback.
238 void (*chunk_cb
)(struct evhttp_request
*, void *);
242 * Creates a new request object that needs to be filled in with the request
243 * parameters. The callback is executed when the request completed or an
246 struct evhttp_request
*evhttp_request_new(
247 void (*cb
)(struct evhttp_request
*, void *), void *arg
);
249 /** enable delivery of chunks to requestor */
250 void evhttp_request_set_chunked_cb(struct evhttp_request
*,
251 void (*cb
)(struct evhttp_request
*, void *));
253 /** Frees the request object and removes associated events. */
254 void evhttp_request_free(struct evhttp_request
*req
);
257 * A connection object that can be used to for making HTTP requests. The
258 * connection object tries to establish the connection when it is given an
259 * http request object.
261 struct evhttp_connection
*evhttp_connection_new(
262 const char *address
, unsigned short port
);
264 /** Frees an http connection */
265 void evhttp_connection_free(struct evhttp_connection
*evcon
);
267 /** sets the ip address from which http connections are made */
268 void evhttp_connection_set_local_address(struct evhttp_connection
*evcon
,
269 const char *address
);
271 /** sets the local port from which http connections are made */
272 void evhttp_connection_set_local_port(struct evhttp_connection
*evcon
,
273 unsigned short port
);
275 /** Sets the timeout for events related to this connection */
276 void evhttp_connection_set_timeout(struct evhttp_connection
*evcon
,
277 int timeout_in_secs
);
279 /** Sets the retry limit for this connection - -1 repeats indefnitely */
280 void evhttp_connection_set_retries(struct evhttp_connection
*evcon
,
283 /** Set a callback for connection close. */
284 void evhttp_connection_set_closecb(struct evhttp_connection
*evcon
,
285 void (*)(struct evhttp_connection
*, void *), void *);
288 * Associates an event base with the connection - can only be called
289 * on a freshly created connection object that has not been used yet.
291 void evhttp_connection_set_base(struct evhttp_connection
*evcon
,
292 struct event_base
*base
);
294 /** Get the remote address and port associated with this connection. */
295 void evhttp_connection_get_peer(struct evhttp_connection
*evcon
,
296 char **address
, u_short
*port
);
298 /** The connection gets ownership of the request */
299 int evhttp_make_request(struct evhttp_connection
*evcon
,
300 struct evhttp_request
*req
,
301 enum evhttp_cmd_type type
, const char *uri
);
303 const char *evhttp_request_uri(struct evhttp_request
*req
);
305 /* Interfaces for dealing with HTTP headers */
307 const char *evhttp_find_header(const struct evkeyvalq
*, const char *);
308 int evhttp_remove_header(struct evkeyvalq
*, const char *);
309 int evhttp_add_header(struct evkeyvalq
*, const char *, const char *);
310 void evhttp_clear_headers(struct evkeyvalq
*);
312 /* Miscellaneous utility functions */
316 Helper function to encode a URI.
318 The returned string must be freed by the caller.
320 @param uri an unencoded URI
321 @return a newly allocated URI-encoded string
323 char *evhttp_encode_uri(const char *uri
);
327 Helper function to decode a URI.
329 The returned string must be freed by the caller.
331 @param uri an encoded URI
332 @return a newly allocated unencoded URI
334 char *evhttp_decode_uri(const char *uri
);
338 * Helper function to parse out arguments in a query.
342 * http://foo.com/?q=test&s=some+thing
344 * will result in two entries in the key value queue.
346 * The first entry is: key="q", value="test"
347 * The second entry is: key="s", value="some thing"
349 * @param uri the request URI
350 * @param headers the head of the evkeyval queue
352 void evhttp_parse_query(const char *uri
, struct evkeyvalq
*headers
);
356 * Escape HTML character entities in a string.
358 * Replaces <, >, ", ' and & with <, >, ",
359 * ' and & correspondingly.
361 * The returned string needs to be freed by the caller.
363 * @param html an unescaped HTML string
364 * @return an escaped HTML string
366 char *evhttp_htmlescape(const char *html
);
372 #endif /* _EVHTTP_H_ */