libevent
http.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
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.
15  *
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.
26  */
27 #ifndef _EVENT2_HTTP_H_
28 #define _EVENT2_HTTP_H_
29 
30 /* For int types. */
31 #include <event2/util.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /* In case we haven't included the right headers yet. */
38 struct evbuffer;
39 struct event_base;
40 
52 /* Response codes */
53 #define HTTP_OK 200
54 #define HTTP_NOCONTENT 204
55 #define HTTP_MOVEPERM 301
56 #define HTTP_MOVETEMP 302
57 #define HTTP_NOTMODIFIED 304
58 #define HTTP_BADREQUEST 400
59 #define HTTP_NOTFOUND 404
60 #define HTTP_BADMETHOD 405
61 #define HTTP_ENTITYTOOLARGE 413
62 #define HTTP_EXPECTATIONFAILED 417
63 #define HTTP_INTERNAL 500
64 #define HTTP_NOTIMPLEMENTED 501
65 #define HTTP_SERVUNAVAIL 503
67 struct evhttp;
68 struct evhttp_request;
69 struct evkeyvalq;
70 struct evhttp_bound_socket;
71 struct evconnlistener;
72 
80 struct evhttp *evhttp_new(struct event_base *base);
81 
94 int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port);
95 
107 struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port);
108 
125 int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd);
126 
137 struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd);
138 
144 struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener);
145 
149 struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound);
150 
168 void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound_socket);
169 
177 evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_socket);
178 
187 void evhttp_free(struct evhttp* http);
188 
190 void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size);
192 void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size);
193 
205 void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods);
206 
216 int evhttp_set_cb(struct evhttp *http, const char *path,
217  void (*cb)(struct evhttp_request *, void *), void *cb_arg);
218 
220 int evhttp_del_cb(struct evhttp *, const char *);
221 
233 void evhttp_set_gencb(struct evhttp *http,
234  void (*cb)(struct evhttp_request *, void *), void *arg);
235 
258 int evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
259  struct evhttp* vhost);
260 
269 int evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost);
270 
279 int evhttp_add_server_alias(struct evhttp *http, const char *alias);
280 
288 int evhttp_remove_server_alias(struct evhttp *http, const char *alias);
289 
296 void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs);
297 
298 /* Request/Response functionality */
299 
308 void evhttp_send_error(struct evhttp_request *req, int error,
309  const char *reason);
310 
324 void evhttp_send_reply(struct evhttp_request *req, int code,
325  const char *reason, struct evbuffer *databuf);
326 
327 /* Low-level response interface, for streaming/chunked replies */
328 
343 void evhttp_send_reply_start(struct evhttp_request *req, int code,
344  const char *reason);
345 
357 void evhttp_send_reply_chunk(struct evhttp_request *req,
358  struct evbuffer *databuf);
364 void evhttp_send_reply_end(struct evhttp_request *req);
365 
366 /*
367  * Interfaces for making requests
368  */
369 
378  EVHTTP_REQ_GET = 1 << 0,
379  EVHTTP_REQ_POST = 1 << 1,
380  EVHTTP_REQ_HEAD = 1 << 2,
381  EVHTTP_REQ_PUT = 1 << 3,
382  EVHTTP_REQ_DELETE = 1 << 4,
383  EVHTTP_REQ_OPTIONS = 1 << 5,
384  EVHTTP_REQ_TRACE = 1 << 6,
385  EVHTTP_REQ_CONNECT = 1 << 7,
386  EVHTTP_REQ_PATCH = 1 << 8
387 };
388 
390 enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
391 
397 struct evhttp_request *evhttp_request_new(
398  void (*cb)(struct evhttp_request *, void *), void *arg);
399 
407 void evhttp_request_set_chunked_cb(struct evhttp_request *,
408  void (*cb)(struct evhttp_request *, void *));
409 
411 void evhttp_request_free(struct evhttp_request *req);
412 
413 struct evdns_base;
414 
427 struct evhttp_connection *evhttp_connection_base_new(
428  struct event_base *base, struct evdns_base *dnsbase,
429  const char *address, unsigned short port);
430 
435  struct evhttp_connection *evcon);
436 
442 void evhttp_request_own(struct evhttp_request *req);
443 
445 int evhttp_request_is_owned(struct evhttp_request *req);
446 
453 struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req);
454 
458 struct event_base *evhttp_connection_get_base(struct evhttp_connection *req);
459 
460 void evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon,
461  ev_ssize_t new_max_headers_size);
462 
463 void evhttp_connection_set_max_body_size(struct evhttp_connection* evcon,
464  ev_ssize_t new_max_body_size);
465 
467 void evhttp_connection_free(struct evhttp_connection *evcon);
468 
470 void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
471  const char *address);
472 
474 void evhttp_connection_set_local_port(struct evhttp_connection *evcon,
475  ev_uint16_t port);
476 
478 void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
479  int timeout_in_secs);
480 
482 void evhttp_connection_set_retries(struct evhttp_connection *evcon,
483  int retry_max);
484 
486 void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
487  void (*)(struct evhttp_connection *, void *), void *);
488 
490 void evhttp_connection_get_peer(struct evhttp_connection *evcon,
491  char **address, ev_uint16_t *port);
492 
506 int evhttp_make_request(struct evhttp_connection *evcon,
507  struct evhttp_request *req,
508  enum evhttp_cmd_type type, const char *uri);
509 
523 void evhttp_cancel_request(struct evhttp_request *req);
524 
528 struct evhttp_uri;
529 
531 const char *evhttp_request_get_uri(const struct evhttp_request *req);
533 const struct evhttp_uri *evhttp_request_get_evhttp_uri(const struct evhttp_request *req);
535 enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req);
536 
537 int evhttp_request_get_response_code(const struct evhttp_request *req);
538 
540 struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req);
542 struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req);
544 struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req);
546 struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req);
551 const char *evhttp_request_get_host(struct evhttp_request *req);
552 
553 /* Interfaces for dealing with HTTP headers */
554 
564 const char *evhttp_find_header(const struct evkeyvalq *headers,
565  const char *key);
566 
575 int evhttp_remove_header(struct evkeyvalq *headers, const char *key);
576 
586 int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);
587 
593 void evhttp_clear_headers(struct evkeyvalq *headers);
594 
595 /* Miscellaneous utility functions */
596 
597 
609 char *evhttp_encode_uri(const char *str);
610 
625 char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus);
626 
641 char *evhttp_decode_uri(const char *uri);
642 
658 char *evhttp_uridecode(const char *uri, int decode_plus,
659  size_t *size_out);
660 
680 int evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
681 
699 int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers);
700 
712 char *evhttp_htmlescape(const char *html);
713 
717 struct evhttp_uri *evhttp_uri_new(void);
718 
723 void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags);
724 
727 const char *evhttp_uri_get_scheme(const struct evhttp_uri *uri);
732 const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri);
745 const char *evhttp_uri_get_host(const struct evhttp_uri *uri);
747 int evhttp_uri_get_port(const struct evhttp_uri *uri);
749 const char *evhttp_uri_get_path(const struct evhttp_uri *uri);
752 const char *evhttp_uri_get_query(const struct evhttp_uri *uri);
755 const char *evhttp_uri_get_fragment(const struct evhttp_uri *uri);
756 
759 int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme);
762 int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo);
765 int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host);
768 int evhttp_uri_set_port(struct evhttp_uri *uri, int port);
771 int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path);
775 int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query);
779 int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment);
780 
815 struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri,
816  unsigned flags);
817 
830 #define EVHTTP_URI_NONCONFORMANT 0x01
831 
833 struct evhttp_uri *evhttp_uri_parse(const char *source_uri);
834 
842 void evhttp_uri_free(struct evhttp_uri *uri);
843 
857 char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit);
858 
859 #ifdef __cplusplus
860 }
861 #endif
862 
863 #endif /* _EVENT2_HTTP_H_ */
int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers)
Helper function to parse out arguments from the query portion of an HTTP URI.
int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme)
Set the scheme of an evhttp_uri, or clear the scheme if scheme==NULL.
int evhttp_make_request(struct evhttp_connection *evcon, struct evhttp_request *req, enum evhttp_cmd_type type, const char *uri)
Make an HTTP request over the specified connection.
char * evhttp_encode_uri(const char *str)
Helper function to encode a string for inclusion in a URI.
struct evkeyvalq * evhttp_request_get_input_headers(struct evhttp_request *req)
Returns the input headers.
int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment)
Set the fragment of an evhttp_uri, or clear the fragment if fragment==NULL.
void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs)
Set the timeout for an HTTP request.
char * evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus)
As evhttp_encode_uri, but if &#39;size&#39; is nonnegative, treat the string as being &#39;size&#39; bytes long...
void evhttp_connection_set_retries(struct evhttp_connection *evcon, int retry_max)
Sets the retry limit for this connection - -1 repeats indefinitely.
int evhttp_remove_virtual_host(struct evhttp *http, struct evhttp *vhost)
Removes a virtual host from the http server.
const char * evhttp_uri_get_path(const struct evhttp_uri *uri)
Return the path part of an evhttp_uri, or NULL if it has no path set.
const char * evhttp_uri_get_scheme(const struct evhttp_uri *uri)
Return the scheme of an evhttp_uri, or NULL if there is no scheme has been set and the evhttp_uri con...
int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo)
Set the userinfo of an evhttp_uri, or clear the userinfo if userinfo==NULL.
struct evbuffer * evhttp_request_get_input_buffer(struct evhttp_request *req)
Returns the input buffer.
void evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
Send another data chunk as part of an ongoing chunked reply.
struct evhttp * evhttp_new(struct event_base *base)
Create a new HTTP server.
void evhttp_connection_get_peer(struct evhttp_connection *evcon, char **address, ev_uint16_t *port)
Get the remote address and port associated with this connection.
int evhttp_del_cb(struct evhttp *, const char *)
Removes the callback for a specified URI.
int evhttp_parse_query(const char *uri, struct evkeyvalq *headers)
Helper function to parse out arguments in a query.
const struct evhttp_uri * evhttp_request_get_evhttp_uri(const struct evhttp_request *req)
Returns the request URI (parsed)
void evhttp_request_set_chunked_cb(struct evhttp_request *, void(*cb)(struct evhttp_request *, void *))
Enable delivery of chunks to requestor.
struct bufferevent * evhttp_connection_get_bufferevent(struct evhttp_connection *evcon)
Return the bufferevent that an evhttp_connection is using.
struct evkeyvalq * evhttp_request_get_output_headers(struct evhttp_request *req)
Returns the output headers.
void evhttp_send_reply_end(struct evhttp_request *req)
Complete a chunked reply, freeing the request as appropriate.
struct evhttp_request * evhttp_request_new(void(*cb)(struct evhttp_request *, void *), void *arg)
Creates a new request object that needs to be filled in with the request parameters.
int evhttp_remove_header(struct evkeyvalq *headers, const char *key)
Removes a header from a list of existing headers.
int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port)
Binds an HTTP server on the specified address and port.
struct evconnlistener * evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound)
Return the listener used to implement a bound socket.
void evhttp_send_error(struct evhttp_request *req, int error, const char *reason)
Send an HTML error message to the client.
struct evhttp_bound_socket * evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener)
The most low-level evhttp_bind/accept method: takes an evconnlistener, and returns an evhttp_bound_so...
int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query)
Set the query of an evhttp_uri, or clear the query if query==NULL.
evhttp_request_kind
a request object can represent either a request or a reply
Definition: http.h:390
void evhttp_free(struct evhttp *http)
Free the previously created HTTP server.
struct evhttp_uri * evhttp_uri_new(void)
Return a new empty evhttp_uri with no fields set.
void evhttp_connection_set_local_port(struct evhttp_connection *evcon, ev_uint16_t port)
sets the local port from which http connections are made
struct evhttp_uri * evhttp_uri_parse(const char *source_uri)
Alias for evhttp_uri_parse_with_flags(source_uri, 0)
struct event_base * evhttp_connection_get_base(struct evhttp_connection *req)
Returns the underlying event_base for this connection.
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:274
int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd)
Makes an HTTP server accept connections on the specified socket.
void evhttp_set_max_body_size(struct evhttp *http, ev_ssize_t max_body_size)
XXX Document.
struct evhttp_bound_socket * evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd)
Like evhttp_accept_socket(), but returns a handle for referencing the socket.
void evhttp_request_free(struct evhttp_request *req)
Frees the request object and removes associated events.
void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound_socket)
Makes an HTTP server stop accepting connections on the specified socket.
evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_socket)
Get the raw file descriptor referenced by an evhttp_bound_socket.
An evbuffer is an opaque data type for efficiently buffering data to be sent or received on the netwo...
Definition: buffer.h:95
int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path)
Set the path of an evhttp_uri, or clear the path if path==NULL.
void evhttp_set_max_headers_size(struct evhttp *http, ev_ssize_t max_headers_size)
XXX Document.
struct evhttp_uri * evhttp_uri_parse_with_flags(const char *source_uri, unsigned flags)
Helper function to parse a URI-Reference as specified by RFC3986.
void evhttp_request_own(struct evhttp_request *req)
Takes ownership of the request object.
void evhttp_cancel_request(struct evhttp_request *req)
Cancels a pending HTTP request.
const char * evhttp_uri_get_fragment(const struct evhttp_uri *uri)
Return the fragment part of an evhttp_uri (excluding the leading "#"), or NULL if it has no fragment ...
void evhttp_connection_set_timeout(struct evhttp_connection *evcon, int timeout_in_secs)
Sets the timeout for events related to this connection.
void evhttp_clear_headers(struct evkeyvalq *headers)
Removes all headers from the header list.
Common convenience functions for cross-platform portability and related socket manipulations.
enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req)
Returns the request command.
void evhttp_send_reply(struct evhttp_request *req, int code, const char *reason, struct evbuffer *databuf)
Send an HTML reply to the client.
void evhttp_uri_free(struct evhttp_uri *uri)
Free all memory allocated for a parsed uri.
const char * evhttp_uri_get_host(const struct evhttp_uri *uri)
Return the host part of an evhttp_uri, or NULL if it has no host set.
int evhttp_uri_set_port(struct evhttp_uri *uri, int port)
Set the port of an evhttp_uri, or clear the port if port==-1.
char * evhttp_uridecode(const char *uri, int decode_plus, size_t *size_out)
Helper function to decode a URI-escaped string or HTTP parameter.
char * evhttp_decode_uri(const char *uri)
Helper function to sort of decode a URI-encoded string.
void evhttp_send_reply_start(struct evhttp_request *req, int code, const char *reason)
Initiate a reply that uses Transfer-Encoding chunked.
const char * evhttp_request_get_uri(const struct evhttp_request *req)
Returns the request URI.
int evhttp_set_cb(struct evhttp *http, const char *path, void(*cb)(struct evhttp_request *, void *), void *cb_arg)
Set a callback for a specified URI.
void evhttp_set_gencb(struct evhttp *http, void(*cb)(struct evhttp_request *, void *), void *arg)
Set a callback for all requests that are not caught by specific callbacks.
int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host)
Set the host of an evhttp_uri, or clear the host if host==NULL.
int evhttp_uri_get_port(const struct evhttp_uri *uri)
Return the port part of an evhttp_uri, or -1 if there is no port set.
char * evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
Join together the uri parts from parsed data to form a URI-Reference.
void evhttp_set_allowed_methods(struct evhttp *http, ev_uint16_t methods)
Sets the what HTTP methods are supported in requests accepted by this server, and passed to user call...
const char * evhttp_uri_get_userinfo(const struct evhttp_uri *uri)
Return the userinfo part of an evhttp_uri, or NULL if it has no userinfo set.
int evhttp_request_is_owned(struct evhttp_request *req)
Returns 1 if the request is owned by the user.
void evhttp_connection_set_closecb(struct evhttp_connection *evcon, void(*)(struct evhttp_connection *, void *), void *)
Set a callback for connection close.
struct evhttp_connection * evhttp_request_get_connection(struct evhttp_request *req)
Returns the connection object associated with the request or NULL.
char * evhttp_htmlescape(const char *html)
Escape HTML character entities in a string.
struct evbuffer * evhttp_request_get_output_buffer(struct evhttp_request *req)
Returns the output buffer.
void evhttp_connection_free(struct evhttp_connection *evcon)
Frees an http connection.
void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags)
Changes the flags set on a given URI.
const char * evhttp_request_get_host(struct evhttp_request *req)
Returns the host associated with the request.
struct evhttp_connection * evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase, const char *address, unsigned short port)
A connection object that can be used to for making HTTP requests.
An opaque type for handling buffered IO.
Definition: bufferevent.h:111
void evhttp_connection_set_local_address(struct evhttp_connection *evcon, const char *address)
sets the ip address from which http connections are made
int evhttp_add_virtual_host(struct evhttp *http, const char *pattern, struct evhttp *vhost)
Adds a virtual host to the http server.
struct evhttp_bound_socket * evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port)
Like evhttp_bind_socket(), but returns a handle for referencing the socket.
int evhttp_remove_server_alias(struct evhttp *http, const char *alias)
Remove a server alias from an http object.
const char * evhttp_uri_get_query(const struct evhttp_uri *uri)
Return the query part of an evhttp_uri (excluding the leading "?"), or NULL if it has no query set...
int evhttp_add_server_alias(struct evhttp *http, const char *alias)
Add a server alias to an http object.
int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value)
Adds a header to a list of existing headers.
Structure to hold information and state for a Libevent dispatch loop.
Definition: event.h:213
const char * evhttp_find_header(const struct evkeyvalq *headers, const char *key)
Finds the value belonging to a header.
evhttp_cmd_type
The different request types supported by evhttp.
Definition: http.h:377