libevent
event2/http.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
00003  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #ifndef _EVENT2_HTTP_H_
00028 #define _EVENT2_HTTP_H_
00029 
00030 /* For int types. */
00031 #include <event2/util.h>
00032 
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036 
00037 /* In case we haven't included the right headers yet. */
00038 struct evbuffer;
00039 struct event_base;
00040 
00052 /* Response codes */
00053 #define HTTP_OK                 200     
00054 #define HTTP_NOCONTENT          204     
00055 #define HTTP_MOVEPERM           301     
00056 #define HTTP_MOVETEMP           302     
00057 #define HTTP_NOTMODIFIED        304     
00058 #define HTTP_BADREQUEST         400     
00059 #define HTTP_NOTFOUND           404     
00060 #define HTTP_BADMETHOD          405     
00061 #define HTTP_ENTITYTOOLARGE     413     
00062 #define HTTP_EXPECTATIONFAILED  417     
00063 #define HTTP_INTERNAL           500     
00064 #define HTTP_NOTIMPLEMENTED     501     
00065 #define HTTP_SERVUNAVAIL        503     
00067 struct evhttp;
00068 struct evhttp_request;
00069 struct evkeyvalq;
00070 struct evhttp_bound_socket;
00071 struct evconnlistener;
00072 
00080 struct evhttp *evhttp_new(struct event_base *base);
00081 
00094 int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port);
00095 
00107 struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port);
00108 
00125 int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd);
00126 
00137 struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd);
00138 
00144 struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener);
00145 
00149 struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound);
00150 
00168 void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound_socket);
00169 
00177 evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_socket);
00178 
00187 void evhttp_free(struct evhttp* http);
00188 
00190 void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size);
00192 void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size);
00193 
00205 void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods);
00206 
00216 int evhttp_set_cb(struct evhttp *http, const char *path,
00217     void (*cb)(struct evhttp_request *, void *), void *cb_arg);
00218 
00220 int evhttp_del_cb(struct evhttp *, const char *);
00221 
00233 void evhttp_set_gencb(struct evhttp *http,
00234     void (*cb)(struct evhttp_request *, void *), void *arg);
00235 
00258 int evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
00259     struct evhttp* vhost);
00260 
00269 int evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost);
00270 
00279 int evhttp_add_server_alias(struct evhttp *http, const char *alias);
00280 
00288 int evhttp_remove_server_alias(struct evhttp *http, const char *alias);
00289 
00296 void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs);
00297 
00298 /* Request/Response functionality */
00299 
00308 void evhttp_send_error(struct evhttp_request *req, int error,
00309     const char *reason);
00310 
00324 void evhttp_send_reply(struct evhttp_request *req, int code,
00325     const char *reason, struct evbuffer *databuf);
00326 
00327 /* Low-level response interface, for streaming/chunked replies */
00328 
00343 void evhttp_send_reply_start(struct evhttp_request *req, int code,
00344     const char *reason);
00345 
00357 void evhttp_send_reply_chunk(struct evhttp_request *req,
00358     struct evbuffer *databuf);
00364 void evhttp_send_reply_end(struct evhttp_request *req);
00365 
00366 /*
00367  * Interfaces for making requests
00368  */
00369 
00377 enum evhttp_cmd_type {
00378         EVHTTP_REQ_GET     = 1 << 0,
00379         EVHTTP_REQ_POST    = 1 << 1,
00380         EVHTTP_REQ_HEAD    = 1 << 2,
00381         EVHTTP_REQ_PUT     = 1 << 3,
00382         EVHTTP_REQ_DELETE  = 1 << 4,
00383         EVHTTP_REQ_OPTIONS = 1 << 5,
00384         EVHTTP_REQ_TRACE   = 1 << 6,
00385         EVHTTP_REQ_CONNECT = 1 << 7,
00386         EVHTTP_REQ_PATCH   = 1 << 8
00387 };
00388 
00390 enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
00391 
00397 struct evhttp_request *evhttp_request_new(
00398         void (*cb)(struct evhttp_request *, void *), void *arg);
00399 
00407 void evhttp_request_set_chunked_cb(struct evhttp_request *,
00408     void (*cb)(struct evhttp_request *, void *));
00409 
00411 void evhttp_request_free(struct evhttp_request *req);
00412 
00413 struct evdns_base;
00414 
00427 struct evhttp_connection *evhttp_connection_base_new(
00428         struct event_base *base, struct evdns_base *dnsbase,
00429         const char *address, unsigned short port);
00430 
00434 struct bufferevent *evhttp_connection_get_bufferevent(
00435         struct evhttp_connection *evcon);
00436 
00442 void evhttp_request_own(struct evhttp_request *req);
00443 
00445 int evhttp_request_is_owned(struct evhttp_request *req);
00446 
00453 struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req);
00454 
00458 struct event_base *evhttp_connection_get_base(struct evhttp_connection *req);
00459 
00460 void evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon,
00461     ev_ssize_t new_max_headers_size);
00462 
00463 void evhttp_connection_set_max_body_size(struct evhttp_connection* evcon,
00464     ev_ssize_t new_max_body_size);
00465 
00467 void evhttp_connection_free(struct evhttp_connection *evcon);
00468 
00470 void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
00471     const char *address);
00472 
00474 void evhttp_connection_set_local_port(struct evhttp_connection *evcon,
00475     ev_uint16_t port);
00476 
00478 void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
00479     int timeout_in_secs);
00480 
00482 void evhttp_connection_set_retries(struct evhttp_connection *evcon,
00483     int retry_max);
00484 
00486 void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
00487     void (*)(struct evhttp_connection *, void *), void *);
00488 
00490 void evhttp_connection_get_peer(struct evhttp_connection *evcon,
00491     char **address, ev_uint16_t *port);
00492 
00506 int evhttp_make_request(struct evhttp_connection *evcon,
00507     struct evhttp_request *req,
00508     enum evhttp_cmd_type type, const char *uri);
00509 
00523 void evhttp_cancel_request(struct evhttp_request *req);
00524 
00528 struct evhttp_uri;
00529 
00531 const char *evhttp_request_get_uri(const struct evhttp_request *req);
00533 const struct evhttp_uri *evhttp_request_get_evhttp_uri(const struct evhttp_request *req);
00535 enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req);
00536 
00537 int evhttp_request_get_response_code(const struct evhttp_request *req);
00538 
00540 struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req);
00542 struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req);
00544 struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req);
00546 struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req);
00551 const char *evhttp_request_get_host(struct evhttp_request *req);
00552 
00553 /* Interfaces for dealing with HTTP headers */
00554 
00564 const char *evhttp_find_header(const struct evkeyvalq *headers,
00565     const char *key);
00566 
00575 int evhttp_remove_header(struct evkeyvalq *headers, const char *key);
00576 
00586 int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);
00587 
00593 void evhttp_clear_headers(struct evkeyvalq *headers);
00594 
00595 /* Miscellaneous utility functions */
00596 
00597 
00609 char *evhttp_encode_uri(const char *str);
00610 
00625 char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus);
00626 
00641 char *evhttp_decode_uri(const char *uri);
00642 
00658 char *evhttp_uridecode(const char *uri, int decode_plus,
00659     size_t *size_out);
00660 
00680 int evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
00681 
00699 int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers);
00700 
00712 char *evhttp_htmlescape(const char *html);
00713 
00717 struct evhttp_uri *evhttp_uri_new(void);
00718 
00723 void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags);
00724 
00727 const char *evhttp_uri_get_scheme(const struct evhttp_uri *uri);
00732 const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri);
00745 const char *evhttp_uri_get_host(const struct evhttp_uri *uri);
00747 int evhttp_uri_get_port(const struct evhttp_uri *uri);
00749 const char *evhttp_uri_get_path(const struct evhttp_uri *uri);
00752 const char *evhttp_uri_get_query(const struct evhttp_uri *uri);
00755 const char *evhttp_uri_get_fragment(const struct evhttp_uri *uri);
00756 
00759 int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme);
00762 int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo);
00765 int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host);
00768 int evhttp_uri_set_port(struct evhttp_uri *uri, int port);
00771 int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path);
00775 int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query);
00779 int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment);
00780 
00815 struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri,
00816     unsigned flags);
00817 
00830 #define EVHTTP_URI_NONCONFORMANT 0x01
00831 
00833 struct evhttp_uri *evhttp_uri_parse(const char *source_uri);
00834 
00842 void evhttp_uri_free(struct evhttp_uri *uri);
00843 
00857 char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit);
00858 
00859 #ifdef __cplusplus
00860 }
00861 #endif
00862 
00863 #endif /* _EVENT2_HTTP_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines