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