http.h 44 KB

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