libevent
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_ */