libevent
|
00001 /* 00002 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 00003 * Copyright (c) 2007-2010 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 00436 void evhttp_request_own(struct evhttp_request *req); 00437 00439 int evhttp_request_is_owned(struct evhttp_request *req); 00440 00447 struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req); 00448 00452 struct event_base *evhttp_connection_get_base(struct evhttp_connection *req); 00453 00454 void evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon, 00455 ev_ssize_t new_max_headers_size); 00456 00457 void evhttp_connection_set_max_body_size(struct evhttp_connection* evcon, 00458 ev_ssize_t new_max_body_size); 00459 00461 void evhttp_connection_free(struct evhttp_connection *evcon); 00462 00464 void evhttp_connection_set_local_address(struct evhttp_connection *evcon, 00465 const char *address); 00466 00468 void evhttp_connection_set_local_port(struct evhttp_connection *evcon, 00469 ev_uint16_t port); 00470 00472 void evhttp_connection_set_timeout(struct evhttp_connection *evcon, 00473 int timeout_in_secs); 00474 00476 void evhttp_connection_set_retries(struct evhttp_connection *evcon, 00477 int retry_max); 00478 00480 void evhttp_connection_set_closecb(struct evhttp_connection *evcon, 00481 void (*)(struct evhttp_connection *, void *), void *); 00482 00484 void evhttp_connection_get_peer(struct evhttp_connection *evcon, 00485 char **address, ev_uint16_t *port); 00486 00500 int evhttp_make_request(struct evhttp_connection *evcon, 00501 struct evhttp_request *req, 00502 enum evhttp_cmd_type type, const char *uri); 00503 00517 void evhttp_cancel_request(struct evhttp_request *req); 00518 00522 struct evhttp_uri; 00523 00525 const char *evhttp_request_get_uri(const struct evhttp_request *req); 00527 const struct evhttp_uri *evhttp_request_get_evhttp_uri(const struct evhttp_request *req); 00529 enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req); 00530 00531 int evhttp_request_get_response_code(const struct evhttp_request *req); 00532 00534 struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req); 00536 struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req); 00538 struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req); 00540 struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req); 00545 const char *evhttp_request_get_host(struct evhttp_request *req); 00546 00547 /* Interfaces for dealing with HTTP headers */ 00548 00558 const char *evhttp_find_header(const struct evkeyvalq *headers, 00559 const char *key); 00560 00569 int evhttp_remove_header(struct evkeyvalq *headers, const char *key); 00570 00580 int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value); 00581 00587 void evhttp_clear_headers(struct evkeyvalq *headers); 00588 00589 /* Miscellaneous utility functions */ 00590 00591 00603 char *evhttp_encode_uri(const char *str); 00604 00619 char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus); 00620 00635 char *evhttp_decode_uri(const char *uri); 00636 00652 char *evhttp_uridecode(const char *uri, int decode_plus, 00653 size_t *size_out); 00654 00674 int evhttp_parse_query(const char *uri, struct evkeyvalq *headers); 00675 00693 int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers); 00694 00706 char *evhttp_htmlescape(const char *html); 00707 00711 struct evhttp_uri *evhttp_uri_new(void); 00712 00717 void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags); 00718 00721 const char *evhttp_uri_get_scheme(const struct evhttp_uri *uri); 00726 const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri); 00739 const char *evhttp_uri_get_host(const struct evhttp_uri *uri); 00741 int evhttp_uri_get_port(const struct evhttp_uri *uri); 00743 const char *evhttp_uri_get_path(const struct evhttp_uri *uri); 00746 const char *evhttp_uri_get_query(const struct evhttp_uri *uri); 00749 const char *evhttp_uri_get_fragment(const struct evhttp_uri *uri); 00750 00753 int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme); 00756 int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo); 00759 int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host); 00762 int evhttp_uri_set_port(struct evhttp_uri *uri, int port); 00765 int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path); 00769 int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query); 00773 int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment); 00774 00809 struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri, 00810 unsigned flags); 00811 00824 #define EVHTTP_URI_NONCONFORMANT 0x01 00825 00827 struct evhttp_uri *evhttp_uri_parse(const char *source_uri); 00828 00836 void evhttp_uri_free(struct evhttp_uri *uri); 00837 00851 char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit); 00852 00853 #ifdef __cplusplus 00854 } 00855 #endif 00856 00857 #endif /* _EVENT2_HTTP_H_ */