evhttp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  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 _EVHTTP_H_
  28. #define _EVHTTP_H_
  29. #include "event.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #ifdef WIN32
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <winsock2.h>
  36. #include <windows.h>
  37. #undef WIN32_LEAN_AND_MEAN
  38. #endif
  39. /** @file evhttp.h
  40. *
  41. * Basic support for HTTP serving.
  42. *
  43. * As libevent is a library for dealing with event notification and most
  44. * interesting applications are networked today, I have often found the
  45. * need to write HTTP code. The following prototypes and definitions provide
  46. * an application with a minimal interface for making HTTP requests and for
  47. * creating a very simple HTTP server.
  48. */
  49. /* Response codes */
  50. #define HTTP_OK 200
  51. #define HTTP_NOCONTENT 204
  52. #define HTTP_MOVEPERM 301
  53. #define HTTP_MOVETEMP 302
  54. #define HTTP_NOTMODIFIED 304
  55. #define HTTP_BADREQUEST 400
  56. #define HTTP_NOTFOUND 404
  57. #define HTTP_SERVUNAVAIL 503
  58. struct evhttp;
  59. struct evhttp_request;
  60. struct evkeyvalq;
  61. /** Create a new HTTP server
  62. *
  63. * @param base (optional) the event base to receive the HTTP events
  64. * @return a pointer to a newly initialized evhttp server structure
  65. */
  66. struct evhttp *evhttp_new(struct event_base *base);
  67. /**
  68. * Binds an HTTP server on the specified address and port.
  69. *
  70. * Can be called multiple times to bind the same http server
  71. * to multiple different ports.
  72. *
  73. * @param http a pointer to an evhttp object
  74. * @param address a string containing the IP address to listen(2) on
  75. * @param port the port number to listen on
  76. * @return 0 on success, -1 on failure
  77. * @see evhttp_free()
  78. */
  79. int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port);
  80. /**
  81. * Makes an HTTP server accept connections on the specified socket
  82. *
  83. * This may be useful to create a socket and then fork multiple instances
  84. * of an http server, or when a socket has been communicated via file
  85. * descriptor passing in situations where an http servers does not have
  86. * permissions to bind to a low-numbered port.
  87. *
  88. * Can be called multiple times to have the http server listen to
  89. * multiple different sockets.
  90. *
  91. * @param http a pointer to an evhttp object
  92. * @param fd a socket fd that is ready for accepting connections
  93. * @return 0 on success, -1 on failure.
  94. * @see evhttp_free(), evhttp_bind_socket()
  95. */
  96. int evhttp_accept_socket(struct evhttp *http, int fd);
  97. /**
  98. * Free the previously created HTTP server.
  99. *
  100. * Works only if no requests are currently being served.
  101. *
  102. * @param http the evhttp server object to be freed
  103. * @see evhttp_start()
  104. */
  105. void evhttp_free(struct evhttp* http);
  106. /** Set a callback for a specified URI */
  107. void evhttp_set_cb(struct evhttp *, const char *,
  108. void (*)(struct evhttp_request *, void *), void *);
  109. /** Removes the callback for a specified URI */
  110. int evhttp_del_cb(struct evhttp *, const char *);
  111. /** Set a callback for all requests that are not caught by specific callbacks
  112. */
  113. void evhttp_set_gencb(struct evhttp *,
  114. void (*)(struct evhttp_request *, void *), void *);
  115. /**
  116. * Set the timeout for an HTTP request.
  117. *
  118. * @param http an evhttp object
  119. * @param timeout_in_secs the timeout, in seconds
  120. */
  121. void evhttp_set_timeout(struct evhttp *, int timeout_in_secs);
  122. /* Request/Response functionality */
  123. /**
  124. * Send an HTML error message to the client.
  125. *
  126. * @param req a request object
  127. * @param error the HTTP error code
  128. * @param reason a brief explanation of the error
  129. */
  130. void evhttp_send_error(struct evhttp_request *req, int error,
  131. const char *reason);
  132. /**
  133. * Send an HTML reply to the client.
  134. *
  135. * @param req a request object
  136. * @param code the HTTP response code to send
  137. * @param reason a brief message to send with the response code
  138. * @param databuf the body of the response
  139. */
  140. void evhttp_send_reply(struct evhttp_request *req, int code,
  141. const char *reason, struct evbuffer *databuf);
  142. /* Low-level response interface, for streaming/chunked replies */
  143. void evhttp_send_reply_start(struct evhttp_request *, int, const char *);
  144. void evhttp_send_reply_chunk(struct evhttp_request *, struct evbuffer *);
  145. void evhttp_send_reply_end(struct evhttp_request *);
  146. /**
  147. * Start an HTTP server on the specified address and port
  148. *
  149. * DEPRECATED: it does not allow an event base to be specified
  150. *
  151. * @param address the address to which the HTTP server should be bound
  152. * @param port the port number on which the HTTP server should listen
  153. * @return an struct evhttp object
  154. */
  155. struct evhttp *evhttp_start(const char *address, u_short port);
  156. /*
  157. * Interfaces for making requests
  158. */
  159. enum evhttp_cmd_type { EVHTTP_REQ_GET, EVHTTP_REQ_POST, EVHTTP_REQ_HEAD };
  160. enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
  161. /**
  162. * the request structure that a server receives.
  163. * WARNING: expect this structure to change. I will try to provide
  164. * reasonable accessors.
  165. */
  166. struct evhttp_request {
  167. #if defined(TAILQ_ENTRY)
  168. TAILQ_ENTRY(evhttp_request) next;
  169. #else
  170. struct {
  171. struct evhttp_request *tqe_next;
  172. struct evhttp_request **tqe_prev;
  173. } next;
  174. #endif
  175. /* the connection object that this request belongs to */
  176. struct evhttp_connection *evcon;
  177. int flags;
  178. #define EVHTTP_REQ_OWN_CONNECTION 0x0001
  179. #define EVHTTP_PROXY_REQUEST 0x0002
  180. struct evkeyvalq *input_headers;
  181. struct evkeyvalq *output_headers;
  182. /* address of the remote host and the port connection came from */
  183. char *remote_host;
  184. u_short remote_port;
  185. enum evhttp_request_kind kind;
  186. enum evhttp_cmd_type type;
  187. char *uri; /* uri after HTTP request was parsed */
  188. char major; /* HTTP Major number */
  189. char minor; /* HTTP Minor number */
  190. int response_code; /* HTTP Response code */
  191. char *response_code_line; /* Readable response */
  192. struct evbuffer *input_buffer; /* read data */
  193. ev_int64_t ntoread;
  194. int chunked:1, /* a chunked request */
  195. userdone:1; /* the user has sent all data */
  196. struct evbuffer *output_buffer; /* outgoing post or data */
  197. /* Callback */
  198. void (*cb)(struct evhttp_request *, void *);
  199. void *cb_arg;
  200. /*
  201. * Chunked data callback - call for each completed chunk if
  202. * specified. If not specified, all the data is delivered via
  203. * the regular callback.
  204. */
  205. void (*chunk_cb)(struct evhttp_request *, void *);
  206. };
  207. /**
  208. * Creates a new request object that needs to be filled in with the request
  209. * parameters. The callback is executed when the request completed or an
  210. * error occurred.
  211. */
  212. struct evhttp_request *evhttp_request_new(
  213. void (*cb)(struct evhttp_request *, void *), void *arg);
  214. /** enable delivery of chunks to requestor */
  215. void evhttp_request_set_chunked_cb(struct evhttp_request *,
  216. void (*cb)(struct evhttp_request *, void *));
  217. /** Frees the request object and removes associated events. */
  218. void evhttp_request_free(struct evhttp_request *req);
  219. /** Returns the connection object associated with the request or NULL */
  220. struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req);
  221. /**
  222. * A connection object that can be used to for making HTTP requests. The
  223. * connection object tries to establish the connection when it is given an
  224. * http request object.
  225. */
  226. struct evhttp_connection *evhttp_connection_new(
  227. const char *address, unsigned short port);
  228. /** Frees an http connection */
  229. void evhttp_connection_free(struct evhttp_connection *evcon);
  230. /** sets the ip address from which http connections are made */
  231. void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
  232. const char *address);
  233. /** sets the local port from which http connections are made */
  234. void evhttp_connection_set_local_port(struct evhttp_connection *evcon,
  235. unsigned short port);
  236. /** Sets the timeout for events related to this connection */
  237. void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
  238. int timeout_in_secs);
  239. /** Sets the retry limit for this connection - -1 repeats indefnitely */
  240. void evhttp_connection_set_retries(struct evhttp_connection *evcon,
  241. int retry_max);
  242. /** Set a callback for connection close. */
  243. void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
  244. void (*)(struct evhttp_connection *, void *), void *);
  245. /**
  246. * Associates an event base with the connection - can only be called
  247. * on a freshly created connection object that has not been used yet.
  248. */
  249. void evhttp_connection_set_base(struct evhttp_connection *evcon,
  250. struct event_base *base);
  251. /** Get the remote address and port associated with this connection. */
  252. void evhttp_connection_get_peer(struct evhttp_connection *evcon,
  253. char **address, u_short *port);
  254. /** The connection gets ownership of the request */
  255. int evhttp_make_request(struct evhttp_connection *evcon,
  256. struct evhttp_request *req,
  257. enum evhttp_cmd_type type, const char *uri);
  258. const char *evhttp_request_uri(struct evhttp_request *req);
  259. /* Interfaces for dealing with HTTP headers */
  260. const char *evhttp_find_header(const struct evkeyvalq *, const char *);
  261. int evhttp_remove_header(struct evkeyvalq *, const char *);
  262. int evhttp_add_header(struct evkeyvalq *, const char *, const char *);
  263. void evhttp_clear_headers(struct evkeyvalq *);
  264. /* Miscellaneous utility functions */
  265. /**
  266. Helper function to encode a URI.
  267. The returned string must be freed by the caller.
  268. @param uri an unencoded URI
  269. @return a newly allocated URI-encoded string
  270. */
  271. char *evhttp_encode_uri(const char *uri);
  272. /**
  273. Helper function to decode a URI.
  274. The returned string must be freed by the caller.
  275. @param uri an encoded URI
  276. @return a newly allocated unencoded URI
  277. */
  278. char *evhttp_decode_uri(const char *uri);
  279. /**
  280. * Helper function to parse out arguments in a query.
  281. *
  282. * Parsing a uri like
  283. *
  284. * http://foo.com/?q=test&s=some+thing
  285. *
  286. * will result in two entries in the key value queue.
  287. * The first entry is: key="q", value="test"
  288. * The second entry is: key="s", value="some thing"
  289. *
  290. * @param uri the request URI
  291. * @param headers the head of the evkeyval queue
  292. */
  293. void evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
  294. /**
  295. * Escape HTML character entities in a string.
  296. *
  297. * Replaces <, >, ", ' and & with &lt;, &gt;, &quot;,
  298. * &#039; and &amp; correspondingly.
  299. *
  300. * The returned string needs to be freed by the caller.
  301. *
  302. * @param html an unescaped HTML string
  303. * @return an escaped HTML string
  304. */
  305. char *evhttp_htmlescape(const char *html);
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif /* _EVHTTP_H_ */