1c43e99fdSEd Maste /* 2c43e99fdSEd Maste * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 3c43e99fdSEd Maste * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4c43e99fdSEd Maste * 5c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without 6c43e99fdSEd Maste * modification, are permitted provided that the following conditions 7c43e99fdSEd Maste * are met: 8c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright 9c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer. 10c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 11c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the 12c43e99fdSEd Maste * documentation and/or other materials provided with the distribution. 13c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products 14c43e99fdSEd Maste * derived from this software without specific prior written permission. 15c43e99fdSEd Maste * 16c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26c43e99fdSEd Maste */ 27c43e99fdSEd Maste #ifndef EVENT2_HTTP_H_INCLUDED_ 28c43e99fdSEd Maste #define EVENT2_HTTP_H_INCLUDED_ 29c43e99fdSEd Maste 30c43e99fdSEd Maste /* For int types. */ 31c43e99fdSEd Maste #include <event2/util.h> 32c43e99fdSEd Maste #include <event2/visibility.h> 33c43e99fdSEd Maste 34c43e99fdSEd Maste #ifdef __cplusplus 35c43e99fdSEd Maste extern "C" { 36c43e99fdSEd Maste #endif 37c43e99fdSEd Maste 38c43e99fdSEd Maste /* In case we haven't included the right headers yet. */ 39c43e99fdSEd Maste struct evbuffer; 40c43e99fdSEd Maste struct event_base; 41c43e99fdSEd Maste struct bufferevent; 42c43e99fdSEd Maste struct evhttp_connection; 43c43e99fdSEd Maste 44c43e99fdSEd Maste /** @file event2/http.h 45c43e99fdSEd Maste * 46c43e99fdSEd Maste * Basic support for HTTP serving. 47c43e99fdSEd Maste * 48c43e99fdSEd Maste * As Libevent is a library for dealing with event notification and most 49c43e99fdSEd Maste * interesting applications are networked today, I have often found the 50c43e99fdSEd Maste * need to write HTTP code. The following prototypes and definitions provide 51c43e99fdSEd Maste * an application with a minimal interface for making HTTP requests and for 52c43e99fdSEd Maste * creating a very simple HTTP server. 53c43e99fdSEd Maste */ 54c43e99fdSEd Maste 55c43e99fdSEd Maste /* Response codes */ 56c43e99fdSEd Maste #define HTTP_OK 200 /**< request completed ok */ 57c43e99fdSEd Maste #define HTTP_NOCONTENT 204 /**< request does not have content */ 58c43e99fdSEd Maste #define HTTP_MOVEPERM 301 /**< the uri moved permanently */ 59c43e99fdSEd Maste #define HTTP_MOVETEMP 302 /**< the uri moved temporarily */ 60c43e99fdSEd Maste #define HTTP_NOTMODIFIED 304 /**< page was not modified from last */ 61c43e99fdSEd Maste #define HTTP_BADREQUEST 400 /**< invalid http request was made */ 62c43e99fdSEd Maste #define HTTP_NOTFOUND 404 /**< could not find content for uri */ 63c43e99fdSEd Maste #define HTTP_BADMETHOD 405 /**< method not allowed for this uri */ 64c43e99fdSEd Maste #define HTTP_ENTITYTOOLARGE 413 /**< */ 65c43e99fdSEd Maste #define HTTP_EXPECTATIONFAILED 417 /**< we can't handle this expectation */ 66c43e99fdSEd Maste #define HTTP_INTERNAL 500 /**< internal error */ 67c43e99fdSEd Maste #define HTTP_NOTIMPLEMENTED 501 /**< not implemented */ 68c43e99fdSEd Maste #define HTTP_SERVUNAVAIL 503 /**< the server is not available */ 69c43e99fdSEd Maste 70c43e99fdSEd Maste struct evhttp; 71c43e99fdSEd Maste struct evhttp_request; 72c43e99fdSEd Maste struct evkeyvalq; 73c43e99fdSEd Maste struct evhttp_bound_socket; 74c43e99fdSEd Maste struct evconnlistener; 75c43e99fdSEd Maste struct evdns_base; 76c43e99fdSEd Maste 77c43e99fdSEd Maste /** 78c43e99fdSEd Maste * Create a new HTTP server. 79c43e99fdSEd Maste * 80c43e99fdSEd Maste * @param base (optional) the event base to receive the HTTP events 81*b50261e2SCy Schubert * @return a pointer to a newly initialized evhttp server structure or NULL 82*b50261e2SCy Schubert * on error 83c43e99fdSEd Maste * @see evhttp_free() 84c43e99fdSEd Maste */ 85c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 86c43e99fdSEd Maste struct evhttp *evhttp_new(struct event_base *base); 87c43e99fdSEd Maste 88c43e99fdSEd Maste /** 89c43e99fdSEd Maste * Binds an HTTP server on the specified address and port. 90c43e99fdSEd Maste * 91c43e99fdSEd Maste * Can be called multiple times to bind the same http server 92c43e99fdSEd Maste * to multiple different ports. 93c43e99fdSEd Maste * 94c43e99fdSEd Maste * @param http a pointer to an evhttp object 95c43e99fdSEd Maste * @param address a string containing the IP address to listen(2) on 96c43e99fdSEd Maste * @param port the port number to listen on 97c43e99fdSEd Maste * @return 0 on success, -1 on failure. 98c43e99fdSEd Maste * @see evhttp_accept_socket() 99c43e99fdSEd Maste */ 100c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 101c43e99fdSEd Maste int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port); 102c43e99fdSEd Maste 103c43e99fdSEd Maste /** 104c43e99fdSEd Maste * Like evhttp_bind_socket(), but returns a handle for referencing the socket. 105c43e99fdSEd Maste * 106c43e99fdSEd Maste * The returned pointer is not valid after \a http is freed. 107c43e99fdSEd Maste * 108c43e99fdSEd Maste * @param http a pointer to an evhttp object 109c43e99fdSEd Maste * @param address a string containing the IP address to listen(2) on 110c43e99fdSEd Maste * @param port the port number to listen on 111c43e99fdSEd Maste * @return Handle for the socket on success, NULL on failure. 112c43e99fdSEd Maste * @see evhttp_bind_socket(), evhttp_del_accept_socket() 113c43e99fdSEd Maste */ 114c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 115c43e99fdSEd Maste struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port); 116c43e99fdSEd Maste 117c43e99fdSEd Maste /** 118c43e99fdSEd Maste * Makes an HTTP server accept connections on the specified socket. 119c43e99fdSEd Maste * 120c43e99fdSEd Maste * This may be useful to create a socket and then fork multiple instances 121c43e99fdSEd Maste * of an http server, or when a socket has been communicated via file 122c43e99fdSEd Maste * descriptor passing in situations where an http servers does not have 123c43e99fdSEd Maste * permissions to bind to a low-numbered port. 124c43e99fdSEd Maste * 125c43e99fdSEd Maste * Can be called multiple times to have the http server listen to 126c43e99fdSEd Maste * multiple different sockets. 127c43e99fdSEd Maste * 128c43e99fdSEd Maste * @param http a pointer to an evhttp object 129c43e99fdSEd Maste * @param fd a socket fd that is ready for accepting connections 130c43e99fdSEd Maste * @return 0 on success, -1 on failure. 131c43e99fdSEd Maste * @see evhttp_bind_socket() 132c43e99fdSEd Maste */ 133c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 134c43e99fdSEd Maste int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd); 135c43e99fdSEd Maste 136c43e99fdSEd Maste /** 137c43e99fdSEd Maste * Like evhttp_accept_socket(), but returns a handle for referencing the socket. 138c43e99fdSEd Maste * 139c43e99fdSEd Maste * The returned pointer is not valid after \a http is freed. 140c43e99fdSEd Maste * 141c43e99fdSEd Maste * @param http a pointer to an evhttp object 142c43e99fdSEd Maste * @param fd a socket fd that is ready for accepting connections 143c43e99fdSEd Maste * @return Handle for the socket on success, NULL on failure. 144c43e99fdSEd Maste * @see evhttp_accept_socket(), evhttp_del_accept_socket() 145c43e99fdSEd Maste */ 146c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 147c43e99fdSEd Maste struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd); 148c43e99fdSEd Maste 149c43e99fdSEd Maste /** 150c43e99fdSEd Maste * The most low-level evhttp_bind/accept method: takes an evconnlistener, and 151c43e99fdSEd Maste * returns an evhttp_bound_socket. The listener will be freed when the bound 152c43e99fdSEd Maste * socket is freed. 153c43e99fdSEd Maste */ 154c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 155c43e99fdSEd Maste struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener); 156c43e99fdSEd Maste 157c43e99fdSEd Maste /** 158c43e99fdSEd Maste * Return the listener used to implement a bound socket. 159c43e99fdSEd Maste */ 160c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 161c43e99fdSEd Maste struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound); 162c43e99fdSEd Maste 163c43e99fdSEd Maste typedef void evhttp_bound_socket_foreach_fn(struct evhttp_bound_socket *, void *); 164c43e99fdSEd Maste /** 165c43e99fdSEd Maste * Applies the function specified in the first argument to all 166c43e99fdSEd Maste * evhttp_bound_sockets associated with "http". The user must not 167c43e99fdSEd Maste * attempt to free or remove any connections, sockets or listeners 168c43e99fdSEd Maste * in the callback "function". 169c43e99fdSEd Maste * 170c43e99fdSEd Maste * @param http pointer to an evhttp object 171c43e99fdSEd Maste * @param function function to apply to every bound socket 172c43e99fdSEd Maste * @param argument pointer value passed to function for every socket iterated 173c43e99fdSEd Maste */ 174c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 175c43e99fdSEd Maste void evhttp_foreach_bound_socket(struct evhttp *http, evhttp_bound_socket_foreach_fn *function, void *argument); 176c43e99fdSEd Maste 177c43e99fdSEd Maste /** 178c43e99fdSEd Maste * Makes an HTTP server stop accepting connections on the specified socket 179c43e99fdSEd Maste * 180c43e99fdSEd Maste * This may be useful when a socket has been sent via file descriptor passing 181c43e99fdSEd Maste * and is no longer needed by the current process. 182c43e99fdSEd Maste * 183c43e99fdSEd Maste * If you created this bound socket with evhttp_bind_socket_with_handle or 184c43e99fdSEd Maste * evhttp_accept_socket_with_handle, this function closes the fd you provided. 185c43e99fdSEd Maste * If you created this bound socket with evhttp_bind_listener, this function 186c43e99fdSEd Maste * frees the listener you provided. 187c43e99fdSEd Maste * 188c43e99fdSEd Maste * \a bound_socket is an invalid pointer after this call returns. 189c43e99fdSEd Maste * 190c43e99fdSEd Maste * @param http a pointer to an evhttp object 191c43e99fdSEd Maste * @param bound_socket a handle returned by evhttp_{bind,accept}_socket_with_handle 192c43e99fdSEd Maste * @see evhttp_bind_socket_with_handle(), evhttp_accept_socket_with_handle() 193c43e99fdSEd Maste */ 194c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 195c43e99fdSEd Maste void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound_socket); 196c43e99fdSEd Maste 197c43e99fdSEd Maste /** 198c43e99fdSEd Maste * Get the raw file descriptor referenced by an evhttp_bound_socket. 199c43e99fdSEd Maste * 200c43e99fdSEd Maste * @param bound_socket a handle returned by evhttp_{bind,accept}_socket_with_handle 201c43e99fdSEd Maste * @return the file descriptor used by the bound socket 202c43e99fdSEd Maste * @see evhttp_bind_socket_with_handle(), evhttp_accept_socket_with_handle() 203c43e99fdSEd Maste */ 204c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 205c43e99fdSEd Maste evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_socket); 206c43e99fdSEd Maste 207c43e99fdSEd Maste /** 208c43e99fdSEd Maste * Free the previously created HTTP server. 209c43e99fdSEd Maste * 210c43e99fdSEd Maste * Works only if no requests are currently being served. 211c43e99fdSEd Maste * 212c43e99fdSEd Maste * @param http the evhttp server object to be freed 213c43e99fdSEd Maste * @see evhttp_start() 214c43e99fdSEd Maste */ 215c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 216c43e99fdSEd Maste void evhttp_free(struct evhttp* http); 217c43e99fdSEd Maste 218c43e99fdSEd Maste /** XXX Document. */ 219c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 220c43e99fdSEd Maste void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size); 221c43e99fdSEd Maste /** XXX Document. */ 222c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 223c43e99fdSEd Maste void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size); 224c43e99fdSEd Maste 225c43e99fdSEd Maste /** 226c43e99fdSEd Maste Set the value to use for the Content-Type header when none was provided. If 227c43e99fdSEd Maste the content type string is NULL, the Content-Type header will not be 228c43e99fdSEd Maste automatically added. 229c43e99fdSEd Maste 230c43e99fdSEd Maste @param http the http server on which to set the default content type 231c43e99fdSEd Maste @param content_type the value for the Content-Type header 232c43e99fdSEd Maste */ 233c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 234c43e99fdSEd Maste void evhttp_set_default_content_type(struct evhttp *http, 235c43e99fdSEd Maste const char *content_type); 236c43e99fdSEd Maste 237c43e99fdSEd Maste /** 238c43e99fdSEd Maste Sets the what HTTP methods are supported in requests accepted by this 239c43e99fdSEd Maste server, and passed to user callbacks. 240c43e99fdSEd Maste 241c43e99fdSEd Maste If not supported they will generate a "405 Method not allowed" response. 242c43e99fdSEd Maste 243c43e99fdSEd Maste By default this includes the following methods: GET, POST, HEAD, PUT, DELETE 244c43e99fdSEd Maste 245c43e99fdSEd Maste @param http the http server on which to set the methods 246c43e99fdSEd Maste @param methods bit mask constructed from evhttp_cmd_type values 247c43e99fdSEd Maste */ 248c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 249c43e99fdSEd Maste void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods); 250c43e99fdSEd Maste 251c43e99fdSEd Maste /** 252c43e99fdSEd Maste Set a callback for a specified URI 253c43e99fdSEd Maste 254c43e99fdSEd Maste @param http the http sever on which to set the callback 255c43e99fdSEd Maste @param path the path for which to invoke the callback 256c43e99fdSEd Maste @param cb the callback function that gets invoked on requesting path 257c43e99fdSEd Maste @param cb_arg an additional context argument for the callback 258c43e99fdSEd Maste @return 0 on success, -1 if the callback existed already, -2 on failure 259c43e99fdSEd Maste */ 260c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 261c43e99fdSEd Maste int evhttp_set_cb(struct evhttp *http, const char *path, 262c43e99fdSEd Maste void (*cb)(struct evhttp_request *, void *), void *cb_arg); 263c43e99fdSEd Maste 264c43e99fdSEd Maste /** Removes the callback for a specified URI */ 265c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 266c43e99fdSEd Maste int evhttp_del_cb(struct evhttp *, const char *); 267c43e99fdSEd Maste 268c43e99fdSEd Maste /** 269c43e99fdSEd Maste Set a callback for all requests that are not caught by specific callbacks 270c43e99fdSEd Maste 271c43e99fdSEd Maste Invokes the specified callback for all requests that do not match any of 272c43e99fdSEd Maste the previously specified request paths. This is catchall for requests not 273c43e99fdSEd Maste specifically configured with evhttp_set_cb(). 274c43e99fdSEd Maste 275c43e99fdSEd Maste @param http the evhttp server object for which to set the callback 276c43e99fdSEd Maste @param cb the callback to invoke for any unmatched requests 277c43e99fdSEd Maste @param arg an context argument for the callback 278c43e99fdSEd Maste */ 279c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 280c43e99fdSEd Maste void evhttp_set_gencb(struct evhttp *http, 281c43e99fdSEd Maste void (*cb)(struct evhttp_request *, void *), void *arg); 282c43e99fdSEd Maste 283c43e99fdSEd Maste /** 284c43e99fdSEd Maste Set a callback used to create new bufferevents for connections 285c43e99fdSEd Maste to a given evhttp object. 286c43e99fdSEd Maste 287c43e99fdSEd Maste You can use this to override the default bufferevent type -- for example, 288c43e99fdSEd Maste to make this evhttp object use SSL bufferevents rather than unencrypted 289c43e99fdSEd Maste ones. 290c43e99fdSEd Maste 291c43e99fdSEd Maste New bufferevents must be allocated with no fd set on them. 292c43e99fdSEd Maste 293c43e99fdSEd Maste @param http the evhttp server object for which to set the callback 294c43e99fdSEd Maste @param cb the callback to invoke for incoming connections 295c43e99fdSEd Maste @param arg an context argument for the callback 296c43e99fdSEd Maste */ 297c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 298c43e99fdSEd Maste void evhttp_set_bevcb(struct evhttp *http, 299c43e99fdSEd Maste struct bufferevent *(*cb)(struct event_base *, void *), void *arg); 300c43e99fdSEd Maste 301c43e99fdSEd Maste /** 302c43e99fdSEd Maste Adds a virtual host to the http server. 303c43e99fdSEd Maste 304c43e99fdSEd Maste A virtual host is a newly initialized evhttp object that has request 305c43e99fdSEd Maste callbacks set on it via evhttp_set_cb() or evhttp_set_gencb(). It 306c43e99fdSEd Maste most not have any listing sockets associated with it. 307c43e99fdSEd Maste 308c43e99fdSEd Maste If the virtual host has not been removed by the time that evhttp_free() 309c43e99fdSEd Maste is called on the main http server, it will be automatically freed, too. 310c43e99fdSEd Maste 311c43e99fdSEd Maste It is possible to have hierarchical vhosts. For example: A vhost 312c43e99fdSEd Maste with the pattern *.example.com may have other vhosts with patterns 313c43e99fdSEd Maste foo.example.com and bar.example.com associated with it. 314c43e99fdSEd Maste 315c43e99fdSEd Maste @param http the evhttp object to which to add a virtual host 316c43e99fdSEd Maste @param pattern the glob pattern against which the hostname is matched. 317c43e99fdSEd Maste The match is case insensitive and follows otherwise regular shell 318c43e99fdSEd Maste matching. 319c43e99fdSEd Maste @param vhost the virtual host to add the regular http server. 320c43e99fdSEd Maste @return 0 on success, -1 on failure 321c43e99fdSEd Maste @see evhttp_remove_virtual_host() 322c43e99fdSEd Maste */ 323c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 324c43e99fdSEd Maste int evhttp_add_virtual_host(struct evhttp* http, const char *pattern, 325c43e99fdSEd Maste struct evhttp* vhost); 326c43e99fdSEd Maste 327c43e99fdSEd Maste /** 328c43e99fdSEd Maste Removes a virtual host from the http server. 329c43e99fdSEd Maste 330c43e99fdSEd Maste @param http the evhttp object from which to remove the virtual host 331c43e99fdSEd Maste @param vhost the virtual host to remove from the regular http server. 332c43e99fdSEd Maste @return 0 on success, -1 on failure 333c43e99fdSEd Maste @see evhttp_add_virtual_host() 334c43e99fdSEd Maste */ 335c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 336c43e99fdSEd Maste int evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost); 337c43e99fdSEd Maste 338c43e99fdSEd Maste /** 339c43e99fdSEd Maste Add a server alias to an http object. The http object can be a virtual 340c43e99fdSEd Maste host or the main server. 341c43e99fdSEd Maste 342c43e99fdSEd Maste @param http the evhttp object 343c43e99fdSEd Maste @param alias the alias to add 344c43e99fdSEd Maste @see evhttp_add_remove_alias() 345c43e99fdSEd Maste */ 346c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 347c43e99fdSEd Maste int evhttp_add_server_alias(struct evhttp *http, const char *alias); 348c43e99fdSEd Maste 349c43e99fdSEd Maste /** 350c43e99fdSEd Maste Remove a server alias from an http object. 351c43e99fdSEd Maste 352c43e99fdSEd Maste @param http the evhttp object 353c43e99fdSEd Maste @param alias the alias to remove 354c43e99fdSEd Maste @see evhttp_add_server_alias() 355c43e99fdSEd Maste */ 356c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 357c43e99fdSEd Maste int evhttp_remove_server_alias(struct evhttp *http, const char *alias); 358c43e99fdSEd Maste 359c43e99fdSEd Maste /** 360c43e99fdSEd Maste * Set the timeout for an HTTP request. 361c43e99fdSEd Maste * 362c43e99fdSEd Maste * @param http an evhttp object 363c43e99fdSEd Maste * @param timeout_in_secs the timeout, in seconds 364c43e99fdSEd Maste */ 365c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 366c43e99fdSEd Maste void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs); 367c43e99fdSEd Maste 368c43e99fdSEd Maste /** 369c43e99fdSEd Maste * Set the timeout for an HTTP request. 370c43e99fdSEd Maste * 371c43e99fdSEd Maste * @param http an evhttp object 372c43e99fdSEd Maste * @param tv the timeout, or NULL 373c43e99fdSEd Maste */ 374c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 375c43e99fdSEd Maste void evhttp_set_timeout_tv(struct evhttp *http, const struct timeval* tv); 376c43e99fdSEd Maste 377c43e99fdSEd Maste /* Read all the clients body, and only after this respond with an error if the 378c43e99fdSEd Maste * clients body exceed max_body_size */ 379c43e99fdSEd Maste #define EVHTTP_SERVER_LINGERING_CLOSE 0x0001 380c43e99fdSEd Maste /** 381c43e99fdSEd Maste * Set connection flags for HTTP server. 382c43e99fdSEd Maste * 383c43e99fdSEd Maste * @see EVHTTP_SERVER_* 384c43e99fdSEd Maste * @return 0 on success, otherwise non zero (for example if flag doesn't 385c43e99fdSEd Maste * supported). 386c43e99fdSEd Maste */ 387c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 388c43e99fdSEd Maste int evhttp_set_flags(struct evhttp *http, int flags); 389c43e99fdSEd Maste 390c43e99fdSEd Maste /* Request/Response functionality */ 391c43e99fdSEd Maste 392c43e99fdSEd Maste /** 393c43e99fdSEd Maste * Send an HTML error message to the client. 394c43e99fdSEd Maste * 395c43e99fdSEd Maste * @param req a request object 396c43e99fdSEd Maste * @param error the HTTP error code 397c43e99fdSEd Maste * @param reason a brief explanation of the error. If this is NULL, we'll 398c43e99fdSEd Maste * just use the standard meaning of the error code. 399c43e99fdSEd Maste */ 400c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 401c43e99fdSEd Maste void evhttp_send_error(struct evhttp_request *req, int error, 402c43e99fdSEd Maste const char *reason); 403c43e99fdSEd Maste 404c43e99fdSEd Maste /** 405c43e99fdSEd Maste * Send an HTML reply to the client. 406c43e99fdSEd Maste * 407c43e99fdSEd Maste * The body of the reply consists of the data in databuf. After calling 408c43e99fdSEd Maste * evhttp_send_reply() databuf will be empty, but the buffer is still 409c43e99fdSEd Maste * owned by the caller and needs to be deallocated by the caller if 410c43e99fdSEd Maste * necessary. 411c43e99fdSEd Maste * 412c43e99fdSEd Maste * @param req a request object 413c43e99fdSEd Maste * @param code the HTTP response code to send 414c43e99fdSEd Maste * @param reason a brief message to send with the response code 415c43e99fdSEd Maste * @param databuf the body of the response 416c43e99fdSEd Maste */ 417c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 418c43e99fdSEd Maste void evhttp_send_reply(struct evhttp_request *req, int code, 419c43e99fdSEd Maste const char *reason, struct evbuffer *databuf); 420c43e99fdSEd Maste 421c43e99fdSEd Maste /* Low-level response interface, for streaming/chunked replies */ 422c43e99fdSEd Maste 423c43e99fdSEd Maste /** 424c43e99fdSEd Maste Initiate a reply that uses Transfer-Encoding chunked. 425c43e99fdSEd Maste 426c43e99fdSEd Maste This allows the caller to stream the reply back to the client and is 427c43e99fdSEd Maste useful when either not all of the reply data is immediately available 428c43e99fdSEd Maste or when sending very large replies. 429c43e99fdSEd Maste 430c43e99fdSEd Maste The caller needs to supply data chunks with evhttp_send_reply_chunk() 431c43e99fdSEd Maste and complete the reply by calling evhttp_send_reply_end(). 432c43e99fdSEd Maste 433c43e99fdSEd Maste @param req a request object 434c43e99fdSEd Maste @param code the HTTP response code to send 435c43e99fdSEd Maste @param reason a brief message to send with the response code 436c43e99fdSEd Maste */ 437c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 438c43e99fdSEd Maste void evhttp_send_reply_start(struct evhttp_request *req, int code, 439c43e99fdSEd Maste const char *reason); 440c43e99fdSEd Maste 441c43e99fdSEd Maste /** 442c43e99fdSEd Maste Send another data chunk as part of an ongoing chunked reply. 443c43e99fdSEd Maste 444c43e99fdSEd Maste The reply chunk consists of the data in databuf. After calling 445c43e99fdSEd Maste evhttp_send_reply_chunk() databuf will be empty, but the buffer is 446c43e99fdSEd Maste still owned by the caller and needs to be deallocated by the caller 447c43e99fdSEd Maste if necessary. 448c43e99fdSEd Maste 449c43e99fdSEd Maste @param req a request object 450c43e99fdSEd Maste @param databuf the data chunk to send as part of the reply. 451c43e99fdSEd Maste */ 452c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 453c43e99fdSEd Maste void evhttp_send_reply_chunk(struct evhttp_request *req, 454c43e99fdSEd Maste struct evbuffer *databuf); 455c43e99fdSEd Maste 456c43e99fdSEd Maste /** 457c43e99fdSEd Maste Send another data chunk as part of an ongoing chunked reply. 458c43e99fdSEd Maste 459c43e99fdSEd Maste The reply chunk consists of the data in databuf. After calling 460c43e99fdSEd Maste evhttp_send_reply_chunk() databuf will be empty, but the buffer is 461c43e99fdSEd Maste still owned by the caller and needs to be deallocated by the caller 462c43e99fdSEd Maste if necessary. 463c43e99fdSEd Maste 464c43e99fdSEd Maste @param req a request object 465c43e99fdSEd Maste @param databuf the data chunk to send as part of the reply. 466c43e99fdSEd Maste @param cb callback funcion 467c43e99fdSEd Maste @param call back's argument. 468c43e99fdSEd Maste */ 469c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 470c43e99fdSEd Maste void evhttp_send_reply_chunk_with_cb(struct evhttp_request *, struct evbuffer *, 471c43e99fdSEd Maste void (*cb)(struct evhttp_connection *, void *), void *arg); 472c43e99fdSEd Maste 473c43e99fdSEd Maste /** 474c43e99fdSEd Maste Complete a chunked reply, freeing the request as appropriate. 475c43e99fdSEd Maste 476c43e99fdSEd Maste @param req a request object 477c43e99fdSEd Maste */ 478c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 479c43e99fdSEd Maste void evhttp_send_reply_end(struct evhttp_request *req); 480c43e99fdSEd Maste 481c43e99fdSEd Maste /* 482c43e99fdSEd Maste * Interfaces for making requests 483c43e99fdSEd Maste */ 484c43e99fdSEd Maste 485c43e99fdSEd Maste /** The different request types supported by evhttp. These are as specified 486c43e99fdSEd Maste * in RFC2616, except for PATCH which is specified by RFC5789. 487c43e99fdSEd Maste * 488c43e99fdSEd Maste * By default, only some of these methods are accepted and passed to user 489c43e99fdSEd Maste * callbacks; use evhttp_set_allowed_methods() to change which methods 490c43e99fdSEd Maste * are allowed. 491c43e99fdSEd Maste */ 492c43e99fdSEd Maste enum evhttp_cmd_type { 493c43e99fdSEd Maste EVHTTP_REQ_GET = 1 << 0, 494c43e99fdSEd Maste EVHTTP_REQ_POST = 1 << 1, 495c43e99fdSEd Maste EVHTTP_REQ_HEAD = 1 << 2, 496c43e99fdSEd Maste EVHTTP_REQ_PUT = 1 << 3, 497c43e99fdSEd Maste EVHTTP_REQ_DELETE = 1 << 4, 498c43e99fdSEd Maste EVHTTP_REQ_OPTIONS = 1 << 5, 499c43e99fdSEd Maste EVHTTP_REQ_TRACE = 1 << 6, 500c43e99fdSEd Maste EVHTTP_REQ_CONNECT = 1 << 7, 501c43e99fdSEd Maste EVHTTP_REQ_PATCH = 1 << 8 502c43e99fdSEd Maste }; 503c43e99fdSEd Maste 504c43e99fdSEd Maste /** a request object can represent either a request or a reply */ 505c43e99fdSEd Maste enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE }; 506c43e99fdSEd Maste 507c43e99fdSEd Maste /** 508c43e99fdSEd Maste * Create and return a connection object that can be used to for making HTTP 509c43e99fdSEd Maste * requests. The connection object tries to resolve address and establish the 510c43e99fdSEd Maste * connection when it is given an http request object. 511c43e99fdSEd Maste * 512c43e99fdSEd Maste * @param base the event_base to use for handling the connection 513c43e99fdSEd Maste * @param dnsbase the dns_base to use for resolving host names; if not 514c43e99fdSEd Maste * specified host name resolution will block. 515c43e99fdSEd Maste * @param bev a bufferevent to use for connecting to the server; if NULL, a 516c43e99fdSEd Maste * socket-based bufferevent will be created. This buffrevent will be freed 517c43e99fdSEd Maste * when the connection closes. It must have no fd set on it. 518c43e99fdSEd Maste * @param address the address to which to connect 519c43e99fdSEd Maste * @param port the port to connect to 520*b50261e2SCy Schubert * @return an evhttp_connection object that can be used for making requests or 521*b50261e2SCy Schubert * NULL on error 522c43e99fdSEd Maste */ 523c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 524c43e99fdSEd Maste struct evhttp_connection *evhttp_connection_base_bufferevent_new( 525c43e99fdSEd Maste struct event_base *base, struct evdns_base *dnsbase, struct bufferevent* bev, const char *address, ev_uint16_t port); 526c43e99fdSEd Maste 527c43e99fdSEd Maste /** 528c43e99fdSEd Maste * Return the bufferevent that an evhttp_connection is using. 529c43e99fdSEd Maste */ 530c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 531c43e99fdSEd Maste struct bufferevent* evhttp_connection_get_bufferevent(struct evhttp_connection *evcon); 532c43e99fdSEd Maste 533c43e99fdSEd Maste /** 534c43e99fdSEd Maste * Return the HTTP server associated with this connection, or NULL. 535c43e99fdSEd Maste */ 536c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 537c43e99fdSEd Maste struct evhttp *evhttp_connection_get_server(struct evhttp_connection *evcon); 538c43e99fdSEd Maste 539c43e99fdSEd Maste /** 540c43e99fdSEd Maste * Creates a new request object that needs to be filled in with the request 541c43e99fdSEd Maste * parameters. The callback is executed when the request completed or an 542c43e99fdSEd Maste * error occurred. 543c43e99fdSEd Maste */ 544c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 545c43e99fdSEd Maste struct evhttp_request *evhttp_request_new( 546c43e99fdSEd Maste void (*cb)(struct evhttp_request *, void *), void *arg); 547c43e99fdSEd Maste 548c43e99fdSEd Maste /** 549c43e99fdSEd Maste * Enable delivery of chunks to requestor. 550c43e99fdSEd Maste * @param cb will be called after every read of data with the same argument 551c43e99fdSEd Maste * as the completion callback. Will never be called on an empty 552c43e99fdSEd Maste * response. May drain the input buffer; it will be drained 553c43e99fdSEd Maste * automatically on return. 554c43e99fdSEd Maste */ 555c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 556c43e99fdSEd Maste void evhttp_request_set_chunked_cb(struct evhttp_request *, 557c43e99fdSEd Maste void (*cb)(struct evhttp_request *, void *)); 558c43e99fdSEd Maste 559c43e99fdSEd Maste /** 560c43e99fdSEd Maste * Register callback for additional parsing of request headers. 561c43e99fdSEd Maste * @param cb will be called after receiving and parsing the full header. 562c43e99fdSEd Maste * It allows analyzing the header and possibly closing the connection 563c43e99fdSEd Maste * by returning a value < 0. 564c43e99fdSEd Maste */ 565c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 566c43e99fdSEd Maste void evhttp_request_set_header_cb(struct evhttp_request *, 567c43e99fdSEd Maste int (*cb)(struct evhttp_request *, void *)); 568c43e99fdSEd Maste 569c43e99fdSEd Maste /** 570c43e99fdSEd Maste * The different error types supported by evhttp 571c43e99fdSEd Maste * 572c43e99fdSEd Maste * @see evhttp_request_set_error_cb() 573c43e99fdSEd Maste */ 574c43e99fdSEd Maste enum evhttp_request_error { 575c43e99fdSEd Maste /** 576c43e99fdSEd Maste * Timeout reached, also @see evhttp_connection_set_timeout() 577c43e99fdSEd Maste */ 578c43e99fdSEd Maste EVREQ_HTTP_TIMEOUT, 579c43e99fdSEd Maste /** 580c43e99fdSEd Maste * EOF reached 581c43e99fdSEd Maste */ 582c43e99fdSEd Maste EVREQ_HTTP_EOF, 583c43e99fdSEd Maste /** 584c43e99fdSEd Maste * Error while reading header, or invalid header 585c43e99fdSEd Maste */ 586c43e99fdSEd Maste EVREQ_HTTP_INVALID_HEADER, 587c43e99fdSEd Maste /** 588c43e99fdSEd Maste * Error encountered while reading or writing 589c43e99fdSEd Maste */ 590c43e99fdSEd Maste EVREQ_HTTP_BUFFER_ERROR, 591c43e99fdSEd Maste /** 592c43e99fdSEd Maste * The evhttp_cancel_request() called on this request. 593c43e99fdSEd Maste */ 594c43e99fdSEd Maste EVREQ_HTTP_REQUEST_CANCEL, 595c43e99fdSEd Maste /** 596c43e99fdSEd Maste * Body is greater then evhttp_connection_set_max_body_size() 597c43e99fdSEd Maste */ 598c43e99fdSEd Maste EVREQ_HTTP_DATA_TOO_LONG 599c43e99fdSEd Maste }; 600c43e99fdSEd Maste /** 601c43e99fdSEd Maste * Set a callback for errors 602c43e99fdSEd Maste * @see evhttp_request_error for error types. 603c43e99fdSEd Maste * 604c43e99fdSEd Maste * On error, both the error callback and the regular callback will be called, 605c43e99fdSEd Maste * error callback is called before the regular callback. 606c43e99fdSEd Maste **/ 607c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 608c43e99fdSEd Maste void evhttp_request_set_error_cb(struct evhttp_request *, 609c43e99fdSEd Maste void (*)(enum evhttp_request_error, void *)); 610c43e99fdSEd Maste 611c43e99fdSEd Maste /** 612c43e99fdSEd Maste * Set a callback to be called on request completion of evhttp_send_* function. 613c43e99fdSEd Maste * 614c43e99fdSEd Maste * The callback function will be called on the completion of the request after 615c43e99fdSEd Maste * the output data has been written and before the evhttp_request object 616c43e99fdSEd Maste * is destroyed. This can be useful for tracking resources associated with a 617c43e99fdSEd Maste * request (ex: timing metrics). 618c43e99fdSEd Maste * 619c43e99fdSEd Maste * @param req a request object 620c43e99fdSEd Maste * @param cb callback function that will be called on request completion 621c43e99fdSEd Maste * @param cb_arg an additional context argument for the callback 622c43e99fdSEd Maste */ 623c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 624c43e99fdSEd Maste void evhttp_request_set_on_complete_cb(struct evhttp_request *req, 625c43e99fdSEd Maste void (*cb)(struct evhttp_request *, void *), void *cb_arg); 626c43e99fdSEd Maste 627c43e99fdSEd Maste /** Frees the request object and removes associated events. */ 628c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 629c43e99fdSEd Maste void evhttp_request_free(struct evhttp_request *req); 630c43e99fdSEd Maste 631c43e99fdSEd Maste /** 632c43e99fdSEd Maste * Create and return a connection object that can be used to for making HTTP 633c43e99fdSEd Maste * requests. The connection object tries to resolve address and establish the 634c43e99fdSEd Maste * connection when it is given an http request object. 635c43e99fdSEd Maste * 636c43e99fdSEd Maste * @param base the event_base to use for handling the connection 637c43e99fdSEd Maste * @param dnsbase the dns_base to use for resolving host names; if not 638c43e99fdSEd Maste * specified host name resolution will block. 639c43e99fdSEd Maste * @param address the address to which to connect 640c43e99fdSEd Maste * @param port the port to connect to 641*b50261e2SCy Schubert * @return an evhttp_connection object that can be used for making requests or 642*b50261e2SCy Schubert * NULL on error 643c43e99fdSEd Maste */ 644c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 645c43e99fdSEd Maste struct evhttp_connection *evhttp_connection_base_new( 646c43e99fdSEd Maste struct event_base *base, struct evdns_base *dnsbase, 647c43e99fdSEd Maste const char *address, ev_uint16_t port); 648c43e99fdSEd Maste 649c43e99fdSEd Maste /** 650c43e99fdSEd Maste * Set family hint for DNS requests. 651c43e99fdSEd Maste */ 652c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 653c43e99fdSEd Maste void evhttp_connection_set_family(struct evhttp_connection *evcon, 654c43e99fdSEd Maste int family); 655c43e99fdSEd Maste 656c43e99fdSEd Maste /* reuse connection address on retry */ 657c43e99fdSEd Maste #define EVHTTP_CON_REUSE_CONNECTED_ADDR 0x0008 658c43e99fdSEd Maste /* Try to read error, since server may already send and close 659c43e99fdSEd Maste * connection, but if at that time we have some data to send then we 660c43e99fdSEd Maste * can send get EPIPE and fail, while we can read that HTTP error. */ 661c43e99fdSEd Maste #define EVHTTP_CON_READ_ON_WRITE_ERROR 0x0010 662c43e99fdSEd Maste /* @see EVHTTP_SERVER_LINGERING_CLOSE */ 663c43e99fdSEd Maste #define EVHTTP_CON_LINGERING_CLOSE 0x0020 664c43e99fdSEd Maste /* Padding for public flags, @see EVHTTP_CON_* in http-internal.h */ 665c43e99fdSEd Maste #define EVHTTP_CON_PUBLIC_FLAGS_END 0x100000 666c43e99fdSEd Maste /** 667c43e99fdSEd Maste * Set connection flags. 668c43e99fdSEd Maste * 669c43e99fdSEd Maste * @see EVHTTP_CON_* 670c43e99fdSEd Maste * @return 0 on success, otherwise non zero (for example if flag doesn't 671c43e99fdSEd Maste * supported). 672c43e99fdSEd Maste */ 673c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 674c43e99fdSEd Maste int evhttp_connection_set_flags(struct evhttp_connection *evcon, 675c43e99fdSEd Maste int flags); 676c43e99fdSEd Maste 677c43e99fdSEd Maste /** Takes ownership of the request object 678c43e99fdSEd Maste * 679c43e99fdSEd Maste * Can be used in a request callback to keep onto the request until 680c43e99fdSEd Maste * evhttp_request_free() is explicitly called by the user. 681c43e99fdSEd Maste */ 682c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 683c43e99fdSEd Maste void evhttp_request_own(struct evhttp_request *req); 684c43e99fdSEd Maste 685c43e99fdSEd Maste /** Returns 1 if the request is owned by the user */ 686c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 687c43e99fdSEd Maste int evhttp_request_is_owned(struct evhttp_request *req); 688c43e99fdSEd Maste 689c43e99fdSEd Maste /** 690c43e99fdSEd Maste * Returns the connection object associated with the request or NULL 691c43e99fdSEd Maste * 692c43e99fdSEd Maste * The user needs to either free the request explicitly or call 693c43e99fdSEd Maste * evhttp_send_reply_end(). 694c43e99fdSEd Maste */ 695c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 696c43e99fdSEd Maste struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req); 697c43e99fdSEd Maste 698c43e99fdSEd Maste /** 699c43e99fdSEd Maste * Returns the underlying event_base for this connection 700c43e99fdSEd Maste */ 701c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 702c43e99fdSEd Maste struct event_base *evhttp_connection_get_base(struct evhttp_connection *req); 703c43e99fdSEd Maste 704c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 705c43e99fdSEd Maste void evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon, 706c43e99fdSEd Maste ev_ssize_t new_max_headers_size); 707c43e99fdSEd Maste 708c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 709c43e99fdSEd Maste void evhttp_connection_set_max_body_size(struct evhttp_connection* evcon, 710c43e99fdSEd Maste ev_ssize_t new_max_body_size); 711c43e99fdSEd Maste 712c43e99fdSEd Maste /** Frees an http connection */ 713c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 714c43e99fdSEd Maste void evhttp_connection_free(struct evhttp_connection *evcon); 715c43e99fdSEd Maste 716c43e99fdSEd Maste /** Disowns a given connection object 717c43e99fdSEd Maste * 718c43e99fdSEd Maste * Can be used to tell libevent to free the connection object after 719c43e99fdSEd Maste * the last request has completed or failed. 720c43e99fdSEd Maste */ 721c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 722c43e99fdSEd Maste void evhttp_connection_free_on_completion(struct evhttp_connection *evcon); 723c43e99fdSEd Maste 724c43e99fdSEd Maste /** sets the ip address from which http connections are made */ 725c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 726c43e99fdSEd Maste void evhttp_connection_set_local_address(struct evhttp_connection *evcon, 727c43e99fdSEd Maste const char *address); 728c43e99fdSEd Maste 729c43e99fdSEd Maste /** sets the local port from which http connections are made */ 730c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 731c43e99fdSEd Maste void evhttp_connection_set_local_port(struct evhttp_connection *evcon, 732c43e99fdSEd Maste ev_uint16_t port); 733c43e99fdSEd Maste 734c43e99fdSEd Maste /** Sets the timeout in seconds for events related to this connection */ 735c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 736c43e99fdSEd Maste void evhttp_connection_set_timeout(struct evhttp_connection *evcon, 737c43e99fdSEd Maste int timeout_in_secs); 738c43e99fdSEd Maste 739c43e99fdSEd Maste /** Sets the timeout for events related to this connection. Takes a struct 740c43e99fdSEd Maste * timeval. */ 741c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 742c43e99fdSEd Maste void evhttp_connection_set_timeout_tv(struct evhttp_connection *evcon, 743c43e99fdSEd Maste const struct timeval *tv); 744c43e99fdSEd Maste 745c43e99fdSEd Maste /** Sets the delay before retrying requests on this connection. This is only 746c43e99fdSEd Maste * used if evhttp_connection_set_retries is used to make the number of retries 747c43e99fdSEd Maste * at least one. Each retry after the first is twice as long as the one before 748c43e99fdSEd Maste * it. */ 749c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 750c43e99fdSEd Maste void evhttp_connection_set_initial_retry_tv(struct evhttp_connection *evcon, 751c43e99fdSEd Maste const struct timeval *tv); 752c43e99fdSEd Maste 753c43e99fdSEd Maste /** Sets the retry limit for this connection - -1 repeats indefinitely */ 754c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 755c43e99fdSEd Maste void evhttp_connection_set_retries(struct evhttp_connection *evcon, 756c43e99fdSEd Maste int retry_max); 757c43e99fdSEd Maste 758c43e99fdSEd Maste /** Set a callback for connection close. */ 759c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 760c43e99fdSEd Maste void evhttp_connection_set_closecb(struct evhttp_connection *evcon, 761c43e99fdSEd Maste void (*)(struct evhttp_connection *, void *), void *); 762c43e99fdSEd Maste 763c43e99fdSEd Maste /** Get the remote address and port associated with this connection. */ 764c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 765c43e99fdSEd Maste void evhttp_connection_get_peer(struct evhttp_connection *evcon, 766c43e99fdSEd Maste char **address, ev_uint16_t *port); 767c43e99fdSEd Maste 768c43e99fdSEd Maste /** Get the remote address associated with this connection. 769c43e99fdSEd Maste * extracted from getpeername() OR from nameserver. 770c43e99fdSEd Maste * 771c43e99fdSEd Maste * @return NULL if getpeername() return non success, 772c43e99fdSEd Maste * or connection is not connected, 773c43e99fdSEd Maste * otherwise it return pointer to struct sockaddr_storage */ 774c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 775c43e99fdSEd Maste const struct sockaddr* 776c43e99fdSEd Maste evhttp_connection_get_addr(struct evhttp_connection *evcon); 777c43e99fdSEd Maste 778c43e99fdSEd Maste /** 779c43e99fdSEd Maste Make an HTTP request over the specified connection. 780c43e99fdSEd Maste 781c43e99fdSEd Maste The connection gets ownership of the request. On failure, the 782c43e99fdSEd Maste request object is no longer valid as it has been freed. 783c43e99fdSEd Maste 784c43e99fdSEd Maste @param evcon the evhttp_connection object over which to send the request 785c43e99fdSEd Maste @param req the previously created and configured request object 786c43e99fdSEd Maste @param type the request type EVHTTP_REQ_GET, EVHTTP_REQ_POST, etc. 787c43e99fdSEd Maste @param uri the URI associated with the request 788c43e99fdSEd Maste @return 0 on success, -1 on failure 789c43e99fdSEd Maste @see evhttp_cancel_request() 790c43e99fdSEd Maste */ 791c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 792c43e99fdSEd Maste int evhttp_make_request(struct evhttp_connection *evcon, 793c43e99fdSEd Maste struct evhttp_request *req, 794c43e99fdSEd Maste enum evhttp_cmd_type type, const char *uri); 795c43e99fdSEd Maste 796c43e99fdSEd Maste /** 797c43e99fdSEd Maste Cancels a pending HTTP request. 798c43e99fdSEd Maste 799c43e99fdSEd Maste Cancels an ongoing HTTP request. The callback associated with this request 800c43e99fdSEd Maste is not executed and the request object is freed. If the request is 801c43e99fdSEd Maste currently being processed, e.g. it is ongoing, the corresponding 802c43e99fdSEd Maste evhttp_connection object is going to get reset. 803c43e99fdSEd Maste 804c43e99fdSEd Maste A request cannot be canceled if its callback has executed already. A request 805c43e99fdSEd Maste may be canceled reentrantly from its chunked callback. 806c43e99fdSEd Maste 807c43e99fdSEd Maste @param req the evhttp_request to cancel; req becomes invalid after this call. 808c43e99fdSEd Maste */ 809c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 810c43e99fdSEd Maste void evhttp_cancel_request(struct evhttp_request *req); 811c43e99fdSEd Maste 812c43e99fdSEd Maste /** 813c43e99fdSEd Maste * A structure to hold a parsed URI or Relative-Ref conforming to RFC3986. 814c43e99fdSEd Maste */ 815c43e99fdSEd Maste struct evhttp_uri; 816c43e99fdSEd Maste 817c43e99fdSEd Maste /** Returns the request URI */ 818c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 819c43e99fdSEd Maste const char *evhttp_request_get_uri(const struct evhttp_request *req); 820c43e99fdSEd Maste /** Returns the request URI (parsed) */ 821c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 822c43e99fdSEd Maste const struct evhttp_uri *evhttp_request_get_evhttp_uri(const struct evhttp_request *req); 823c43e99fdSEd Maste /** Returns the request command */ 824c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 825c43e99fdSEd Maste enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req); 826c43e99fdSEd Maste 827c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 828c43e99fdSEd Maste int evhttp_request_get_response_code(const struct evhttp_request *req); 829c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 830c43e99fdSEd Maste const char * evhttp_request_get_response_code_line(const struct evhttp_request *req); 831c43e99fdSEd Maste 832c43e99fdSEd Maste /** Returns the input headers */ 833c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 834c43e99fdSEd Maste struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req); 835c43e99fdSEd Maste /** Returns the output headers */ 836c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 837c43e99fdSEd Maste struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req); 838c43e99fdSEd Maste /** Returns the input buffer */ 839c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 840c43e99fdSEd Maste struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req); 841c43e99fdSEd Maste /** Returns the output buffer */ 842c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 843c43e99fdSEd Maste struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req); 844c43e99fdSEd Maste /** Returns the host associated with the request. If a client sends an absolute 845c43e99fdSEd Maste URI, the host part of that is preferred. Otherwise, the input headers are 846c43e99fdSEd Maste searched for a Host: header. NULL is returned if no absolute URI or Host: 847c43e99fdSEd Maste header is provided. */ 848c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 849c43e99fdSEd Maste const char *evhttp_request_get_host(struct evhttp_request *req); 850c43e99fdSEd Maste 851c43e99fdSEd Maste /* Interfaces for dealing with HTTP headers */ 852c43e99fdSEd Maste 853c43e99fdSEd Maste /** 854c43e99fdSEd Maste Finds the value belonging to a header. 855c43e99fdSEd Maste 856c43e99fdSEd Maste @param headers the evkeyvalq object in which to find the header 857c43e99fdSEd Maste @param key the name of the header to find 858c43e99fdSEd Maste @returns a pointer to the value for the header or NULL if the header 859c43e99fdSEd Maste could not be found. 860c43e99fdSEd Maste @see evhttp_add_header(), evhttp_remove_header() 861c43e99fdSEd Maste */ 862c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 863c43e99fdSEd Maste const char *evhttp_find_header(const struct evkeyvalq *headers, 864c43e99fdSEd Maste const char *key); 865c43e99fdSEd Maste 866c43e99fdSEd Maste /** 867c43e99fdSEd Maste Removes a header from a list of existing headers. 868c43e99fdSEd Maste 869c43e99fdSEd Maste @param headers the evkeyvalq object from which to remove a header 870c43e99fdSEd Maste @param key the name of the header to remove 871c43e99fdSEd Maste @returns 0 if the header was removed, -1 otherwise. 872c43e99fdSEd Maste @see evhttp_find_header(), evhttp_add_header() 873c43e99fdSEd Maste */ 874c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 875c43e99fdSEd Maste int evhttp_remove_header(struct evkeyvalq *headers, const char *key); 876c43e99fdSEd Maste 877c43e99fdSEd Maste /** 878c43e99fdSEd Maste Adds a header to a list of existing headers. 879c43e99fdSEd Maste 880c43e99fdSEd Maste @param headers the evkeyvalq object to which to add a header 881c43e99fdSEd Maste @param key the name of the header 882c43e99fdSEd Maste @param value the value belonging to the header 883c43e99fdSEd Maste @returns 0 on success, -1 otherwise. 884c43e99fdSEd Maste @see evhttp_find_header(), evhttp_clear_headers() 885c43e99fdSEd Maste */ 886c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 887c43e99fdSEd Maste int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value); 888c43e99fdSEd Maste 889c43e99fdSEd Maste /** 890c43e99fdSEd Maste Removes all headers from the header list. 891c43e99fdSEd Maste 892c43e99fdSEd Maste @param headers the evkeyvalq object from which to remove all headers 893c43e99fdSEd Maste */ 894c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 895c43e99fdSEd Maste void evhttp_clear_headers(struct evkeyvalq *headers); 896c43e99fdSEd Maste 897c43e99fdSEd Maste /* Miscellaneous utility functions */ 898c43e99fdSEd Maste 899c43e99fdSEd Maste 900c43e99fdSEd Maste /** 901c43e99fdSEd Maste Helper function to encode a string for inclusion in a URI. All 902c43e99fdSEd Maste characters are replaced by their hex-escaped (%22) equivalents, 903c43e99fdSEd Maste except for characters explicitly unreserved by RFC3986 -- that is, 904c43e99fdSEd Maste ASCII alphanumeric characters, hyphen, dot, underscore, and tilde. 905c43e99fdSEd Maste 906c43e99fdSEd Maste The returned string must be freed by the caller. 907c43e99fdSEd Maste 908c43e99fdSEd Maste @param str an unencoded string 909c43e99fdSEd Maste @return a newly allocated URI-encoded string or NULL on failure 910c43e99fdSEd Maste */ 911c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 912c43e99fdSEd Maste char *evhttp_encode_uri(const char *str); 913c43e99fdSEd Maste 914c43e99fdSEd Maste /** 915c43e99fdSEd Maste As evhttp_encode_uri, but if 'size' is nonnegative, treat the string 916c43e99fdSEd Maste as being 'size' bytes long. This allows you to encode strings that 917c43e99fdSEd Maste may contain 0-valued bytes. 918c43e99fdSEd Maste 919c43e99fdSEd Maste The returned string must be freed by the caller. 920c43e99fdSEd Maste 921c43e99fdSEd Maste @param str an unencoded string 922c43e99fdSEd Maste @param size the length of the string to encode, or -1 if the string 923c43e99fdSEd Maste is NUL-terminated 924c43e99fdSEd Maste @param space_to_plus if true, space characters in 'str' are encoded 925c43e99fdSEd Maste as +, not %20. 926c43e99fdSEd Maste @return a newly allocate URI-encoded string, or NULL on failure. 927c43e99fdSEd Maste */ 928c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 929c43e99fdSEd Maste char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus); 930c43e99fdSEd Maste 931c43e99fdSEd Maste /** 932c43e99fdSEd Maste Helper function to sort of decode a URI-encoded string. Unlike 933*b50261e2SCy Schubert evhttp_uridecode, it decodes all plus characters that appear 934c43e99fdSEd Maste _after_ the first question mark character, but no plusses that occur 935c43e99fdSEd Maste before. This is not a good way to decode URIs in whole or in part. 936c43e99fdSEd Maste 937c43e99fdSEd Maste The returned string must be freed by the caller 938c43e99fdSEd Maste 939c43e99fdSEd Maste @deprecated This function is deprecated; you probably want to use 940*b50261e2SCy Schubert evhttp_uridecode instead. 941c43e99fdSEd Maste 942c43e99fdSEd Maste @param uri an encoded URI 943c43e99fdSEd Maste @return a newly allocated unencoded URI or NULL on failure 944c43e99fdSEd Maste */ 945c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 946c43e99fdSEd Maste char *evhttp_decode_uri(const char *uri); 947c43e99fdSEd Maste 948c43e99fdSEd Maste /** 949c43e99fdSEd Maste Helper function to decode a URI-escaped string or HTTP parameter. 950c43e99fdSEd Maste 951c43e99fdSEd Maste If 'decode_plus' is 1, then we decode the string as an HTTP parameter 952c43e99fdSEd Maste value, and convert all plus ('+') characters to spaces. If 953c43e99fdSEd Maste 'decode_plus' is 0, we leave all plus characters unchanged. 954c43e99fdSEd Maste 955c43e99fdSEd Maste The returned string must be freed by the caller. 956c43e99fdSEd Maste 957c43e99fdSEd Maste @param uri a URI-encode encoded URI 958c43e99fdSEd Maste @param decode_plus determines whether we convert '+' to space. 959c43e99fdSEd Maste @param size_out if size_out is not NULL, *size_out is set to the size of the 960c43e99fdSEd Maste returned string 961c43e99fdSEd Maste @return a newly allocated unencoded URI or NULL on failure 962c43e99fdSEd Maste */ 963c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 964c43e99fdSEd Maste char *evhttp_uridecode(const char *uri, int decode_plus, 965c43e99fdSEd Maste size_t *size_out); 966c43e99fdSEd Maste 967c43e99fdSEd Maste /** 968c43e99fdSEd Maste Helper function to parse out arguments in a query. 969c43e99fdSEd Maste 970c43e99fdSEd Maste Parsing a URI like 971c43e99fdSEd Maste 972c43e99fdSEd Maste http://foo.com/?q=test&s=some+thing 973c43e99fdSEd Maste 974c43e99fdSEd Maste will result in two entries in the key value queue. 975c43e99fdSEd Maste 976c43e99fdSEd Maste The first entry is: key="q", value="test" 977c43e99fdSEd Maste The second entry is: key="s", value="some thing" 978c43e99fdSEd Maste 979c43e99fdSEd Maste @deprecated This function is deprecated as of Libevent 2.0.9. Use 980c43e99fdSEd Maste evhttp_uri_parse and evhttp_parse_query_str instead. 981c43e99fdSEd Maste 982c43e99fdSEd Maste @param uri the request URI 983c43e99fdSEd Maste @param headers the head of the evkeyval queue 984c43e99fdSEd Maste @return 0 on success, -1 on failure 985c43e99fdSEd Maste */ 986c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 987c43e99fdSEd Maste int evhttp_parse_query(const char *uri, struct evkeyvalq *headers); 988c43e99fdSEd Maste 989c43e99fdSEd Maste /** 990c43e99fdSEd Maste Helper function to parse out arguments from the query portion of an 991c43e99fdSEd Maste HTTP URI. 992c43e99fdSEd Maste 993c43e99fdSEd Maste Parsing a query string like 994c43e99fdSEd Maste 995c43e99fdSEd Maste q=test&s=some+thing 996c43e99fdSEd Maste 997c43e99fdSEd Maste will result in two entries in the key value queue. 998c43e99fdSEd Maste 999c43e99fdSEd Maste The first entry is: key="q", value="test" 1000c43e99fdSEd Maste The second entry is: key="s", value="some thing" 1001c43e99fdSEd Maste 1002c43e99fdSEd Maste @param query_parse the query portion of the URI 1003c43e99fdSEd Maste @param headers the head of the evkeyval queue 1004c43e99fdSEd Maste @return 0 on success, -1 on failure 1005c43e99fdSEd Maste */ 1006c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1007c43e99fdSEd Maste int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers); 1008c43e99fdSEd Maste 1009c43e99fdSEd Maste /** 1010c43e99fdSEd Maste * Escape HTML character entities in a string. 1011c43e99fdSEd Maste * 1012c43e99fdSEd Maste * Replaces <, >, ", ' and & with <, >, ", 1013c43e99fdSEd Maste * ' and & correspondingly. 1014c43e99fdSEd Maste * 1015c43e99fdSEd Maste * The returned string needs to be freed by the caller. 1016c43e99fdSEd Maste * 1017c43e99fdSEd Maste * @param html an unescaped HTML string 1018c43e99fdSEd Maste * @return an escaped HTML string or NULL on error 1019c43e99fdSEd Maste */ 1020c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1021c43e99fdSEd Maste char *evhttp_htmlescape(const char *html); 1022c43e99fdSEd Maste 1023c43e99fdSEd Maste /** 1024c43e99fdSEd Maste * Return a new empty evhttp_uri with no fields set. 1025c43e99fdSEd Maste */ 1026c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1027c43e99fdSEd Maste struct evhttp_uri *evhttp_uri_new(void); 1028c43e99fdSEd Maste 1029c43e99fdSEd Maste /** 1030c43e99fdSEd Maste * Changes the flags set on a given URI. See EVHTTP_URI_* for 1031c43e99fdSEd Maste * a list of flags. 1032c43e99fdSEd Maste **/ 1033c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1034c43e99fdSEd Maste void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags); 1035c43e99fdSEd Maste 1036c43e99fdSEd Maste /** Return the scheme of an evhttp_uri, or NULL if there is no scheme has 1037c43e99fdSEd Maste * been set and the evhttp_uri contains a Relative-Ref. */ 1038c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1039c43e99fdSEd Maste const char *evhttp_uri_get_scheme(const struct evhttp_uri *uri); 1040c43e99fdSEd Maste /** 1041c43e99fdSEd Maste * Return the userinfo part of an evhttp_uri, or NULL if it has no userinfo 1042c43e99fdSEd Maste * set. 1043c43e99fdSEd Maste */ 1044c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1045c43e99fdSEd Maste const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri); 1046c43e99fdSEd Maste /** 1047c43e99fdSEd Maste * Return the host part of an evhttp_uri, or NULL if it has no host set. 1048c43e99fdSEd Maste * The host may either be a regular hostname (conforming to the RFC 3986 1049c43e99fdSEd Maste * "regname" production), or an IPv4 address, or the empty string, or a 1050c43e99fdSEd Maste * bracketed IPv6 address, or a bracketed 'IP-Future' address. 1051c43e99fdSEd Maste * 1052c43e99fdSEd Maste * Note that having a NULL host means that the URI has no authority 1053c43e99fdSEd Maste * section, but having an empty-string host means that the URI has an 1054c43e99fdSEd Maste * authority section with no host part. For example, 1055c43e99fdSEd Maste * "mailto:user@example.com" has a host of NULL, but "file:///etc/motd" 1056c43e99fdSEd Maste * has a host of "". 1057c43e99fdSEd Maste */ 1058c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1059c43e99fdSEd Maste const char *evhttp_uri_get_host(const struct evhttp_uri *uri); 1060c43e99fdSEd Maste /** Return the port part of an evhttp_uri, or -1 if there is no port set. */ 1061c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1062c43e99fdSEd Maste int evhttp_uri_get_port(const struct evhttp_uri *uri); 1063c43e99fdSEd Maste /** Return the path part of an evhttp_uri, or NULL if it has no path set */ 1064c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1065c43e99fdSEd Maste const char *evhttp_uri_get_path(const struct evhttp_uri *uri); 1066c43e99fdSEd Maste /** Return the query part of an evhttp_uri (excluding the leading "?"), or 1067c43e99fdSEd Maste * NULL if it has no query set */ 1068c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1069c43e99fdSEd Maste const char *evhttp_uri_get_query(const struct evhttp_uri *uri); 1070c43e99fdSEd Maste /** Return the fragment part of an evhttp_uri (excluding the leading "#"), 1071c43e99fdSEd Maste * or NULL if it has no fragment set */ 1072c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1073c43e99fdSEd Maste const char *evhttp_uri_get_fragment(const struct evhttp_uri *uri); 1074c43e99fdSEd Maste 1075c43e99fdSEd Maste /** Set the scheme of an evhttp_uri, or clear the scheme if scheme==NULL. 1076c43e99fdSEd Maste * Returns 0 on success, -1 if scheme is not well-formed. */ 1077c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1078c43e99fdSEd Maste int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme); 1079c43e99fdSEd Maste /** Set the userinfo of an evhttp_uri, or clear the userinfo if userinfo==NULL. 1080c43e99fdSEd Maste * Returns 0 on success, -1 if userinfo is not well-formed. */ 1081c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1082c43e99fdSEd Maste int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo); 1083c43e99fdSEd Maste /** Set the host of an evhttp_uri, or clear the host if host==NULL. 1084c43e99fdSEd Maste * Returns 0 on success, -1 if host is not well-formed. */ 1085c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1086c43e99fdSEd Maste int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host); 1087c43e99fdSEd Maste /** Set the port of an evhttp_uri, or clear the port if port==-1. 1088c43e99fdSEd Maste * Returns 0 on success, -1 if port is not well-formed. */ 1089c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1090c43e99fdSEd Maste int evhttp_uri_set_port(struct evhttp_uri *uri, int port); 1091c43e99fdSEd Maste /** Set the path of an evhttp_uri, or clear the path if path==NULL. 1092c43e99fdSEd Maste * Returns 0 on success, -1 if path is not well-formed. */ 1093c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1094c43e99fdSEd Maste int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path); 1095c43e99fdSEd Maste /** Set the query of an evhttp_uri, or clear the query if query==NULL. 1096c43e99fdSEd Maste * The query should not include a leading "?". 1097c43e99fdSEd Maste * Returns 0 on success, -1 if query is not well-formed. */ 1098c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1099c43e99fdSEd Maste int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query); 1100c43e99fdSEd Maste /** Set the fragment of an evhttp_uri, or clear the fragment if fragment==NULL. 1101c43e99fdSEd Maste * The fragment should not include a leading "#". 1102c43e99fdSEd Maste * Returns 0 on success, -1 if fragment is not well-formed. */ 1103c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1104c43e99fdSEd Maste int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment); 1105c43e99fdSEd Maste 1106c43e99fdSEd Maste /** 1107c43e99fdSEd Maste * Helper function to parse a URI-Reference as specified by RFC3986. 1108c43e99fdSEd Maste * 1109c43e99fdSEd Maste * This function matches the URI-Reference production from RFC3986, 1110c43e99fdSEd Maste * which includes both URIs like 1111c43e99fdSEd Maste * 1112c43e99fdSEd Maste * scheme://[[userinfo]@]foo.com[:port]]/[path][?query][#fragment] 1113c43e99fdSEd Maste * 1114c43e99fdSEd Maste * and relative-refs like 1115c43e99fdSEd Maste * 1116c43e99fdSEd Maste * [path][?query][#fragment] 1117c43e99fdSEd Maste * 1118c43e99fdSEd Maste * Any optional elements portions not present in the original URI are 1119c43e99fdSEd Maste * left set to NULL in the resulting evhttp_uri. If no port is 1120c43e99fdSEd Maste * specified, the port is set to -1. 1121c43e99fdSEd Maste * 1122c43e99fdSEd Maste * Note that no decoding is performed on percent-escaped characters in 1123c43e99fdSEd Maste * the string; if you want to parse them, use evhttp_uridecode or 1124c43e99fdSEd Maste * evhttp_parse_query_str as appropriate. 1125c43e99fdSEd Maste * 1126c43e99fdSEd Maste * Note also that most URI schemes will have additional constraints that 1127c43e99fdSEd Maste * this function does not know about, and cannot check. For example, 1128c43e99fdSEd Maste * mailto://www.example.com/cgi-bin/fortune.pl is not a reasonable 1129c43e99fdSEd Maste * mailto url, http://www.example.com:99999/ is not a reasonable HTTP 1130c43e99fdSEd Maste * URL, and ftp:username@example.com is not a reasonable FTP URL. 1131c43e99fdSEd Maste * Nevertheless, all of these URLs conform to RFC3986, and this function 1132c43e99fdSEd Maste * accepts all of them as valid. 1133c43e99fdSEd Maste * 1134c43e99fdSEd Maste * @param source_uri the request URI 1135c43e99fdSEd Maste * @param flags Zero or more EVHTTP_URI_* flags to affect the behavior 1136c43e99fdSEd Maste * of the parser. 1137c43e99fdSEd Maste * @return uri container to hold parsed data, or NULL if there is error 1138c43e99fdSEd Maste * @see evhttp_uri_free() 1139c43e99fdSEd Maste */ 1140c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1141c43e99fdSEd Maste struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri, 1142c43e99fdSEd Maste unsigned flags); 1143c43e99fdSEd Maste 1144c43e99fdSEd Maste /** Tolerate URIs that do not conform to RFC3986. 1145c43e99fdSEd Maste * 1146c43e99fdSEd Maste * Unfortunately, some HTTP clients generate URIs that, according to RFC3986, 1147c43e99fdSEd Maste * are not conformant URIs. If you need to support these URIs, you can 1148c43e99fdSEd Maste * do so by passing this flag to evhttp_uri_parse_with_flags. 1149c43e99fdSEd Maste * 1150c43e99fdSEd Maste * Currently, these changes are: 1151c43e99fdSEd Maste * <ul> 1152c43e99fdSEd Maste * <li> Nonconformant URIs are allowed to contain otherwise unreasonable 1153c43e99fdSEd Maste * characters in their path, query, and fragment components. 1154c43e99fdSEd Maste * </ul> 1155c43e99fdSEd Maste */ 1156c43e99fdSEd Maste #define EVHTTP_URI_NONCONFORMANT 0x01 1157c43e99fdSEd Maste 1158c43e99fdSEd Maste /** Alias for evhttp_uri_parse_with_flags(source_uri, 0) */ 1159c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1160c43e99fdSEd Maste struct evhttp_uri *evhttp_uri_parse(const char *source_uri); 1161c43e99fdSEd Maste 1162c43e99fdSEd Maste /** 1163c43e99fdSEd Maste * Free all memory allocated for a parsed uri. Only use this for URIs 1164c43e99fdSEd Maste * generated by evhttp_uri_parse. 1165c43e99fdSEd Maste * 1166c43e99fdSEd Maste * @param uri container with parsed data 1167c43e99fdSEd Maste * @see evhttp_uri_parse() 1168c43e99fdSEd Maste */ 1169c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1170c43e99fdSEd Maste void evhttp_uri_free(struct evhttp_uri *uri); 1171c43e99fdSEd Maste 1172c43e99fdSEd Maste /** 1173c43e99fdSEd Maste * Join together the uri parts from parsed data to form a URI-Reference. 1174c43e99fdSEd Maste * 1175c43e99fdSEd Maste * Note that no escaping of reserved characters is done on the members 1176c43e99fdSEd Maste * of the evhttp_uri, so the generated string might not be a valid URI 1177c43e99fdSEd Maste * unless the members of evhttp_uri are themselves valid. 1178c43e99fdSEd Maste * 1179c43e99fdSEd Maste * @param uri container with parsed data 1180c43e99fdSEd Maste * @param buf destination buffer 1181c43e99fdSEd Maste * @param limit destination buffer size 1182c43e99fdSEd Maste * @return an joined uri as string or NULL on error 1183c43e99fdSEd Maste * @see evhttp_uri_parse() 1184c43e99fdSEd Maste */ 1185c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1186c43e99fdSEd Maste char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit); 1187c43e99fdSEd Maste 1188c43e99fdSEd Maste #ifdef __cplusplus 1189c43e99fdSEd Maste } 1190c43e99fdSEd Maste #endif 1191c43e99fdSEd Maste 1192c43e99fdSEd Maste #endif /* EVENT2_HTTP_H_INCLUDED_ */ 1193