http-internal.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
  3. * Copyright 2007-2012 Niels Provos and Nick Mathewson
  4. *
  5. * This header file contains definitions for dealing with HTTP requests
  6. * that are internal to libevent. As user of the library, you should not
  7. * need to know about these.
  8. */
  9. #ifndef HTTP_INTERNAL_H_INCLUDED_
  10. #define HTTP_INTERNAL_H_INCLUDED_
  11. #include "event2/event_struct.h"
  12. #include "util-internal.h"
  13. #include "defer-internal.h"
  14. #include "event2/http.h"
  15. #define HTTP_CONNECT_TIMEOUT 45
  16. #define HTTP_WRITE_TIMEOUT 50
  17. #define HTTP_READ_TIMEOUT 50
  18. enum message_read_status {
  19. ALL_DATA_READ = 1,
  20. MORE_DATA_EXPECTED = 0,
  21. DATA_CORRUPTED = -1,
  22. REQUEST_CANCELED = -2,
  23. DATA_TOO_LONG = -3
  24. };
  25. struct evbuffer;
  26. struct addrinfo;
  27. struct evhttp_request;
  28. /* Indicates an unknown request method. */
  29. #define EVHTTP_REQ_UNKNOWN_ (1<<15)
  30. enum evhttp_connection_state {
  31. EVCON_DISCONNECTED, /**< not currently connected not trying either*/
  32. EVCON_CONNECTING, /**< tries to currently connect */
  33. EVCON_IDLE, /**< connection is established */
  34. EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
  35. **< Status-Line (outgoing conn) */
  36. EVCON_READING_HEADERS, /**< reading request/response headers */
  37. EVCON_READING_BODY, /**< reading request/response body */
  38. EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
  39. EVCON_WRITING /**< writing request/response headers/body */
  40. };
  41. struct event_base;
  42. /* A client or server connection. */
  43. struct evhttp_connection {
  44. /* we use this tailq only if this connection was created for an http
  45. * server */
  46. TAILQ_ENTRY(evhttp_connection) next;
  47. evutil_socket_t fd;
  48. struct bufferevent *bufev;
  49. bev_factory_cb bufcb;
  50. void *bufcb_arg;
  51. struct event retry_ev; /* for retrying connects */
  52. char *bind_address; /* address to use for binding the src */
  53. ev_uint16_t bind_port; /* local port for binding the src */
  54. char *address; /* address to connect to */
  55. ev_uint16_t port;
  56. size_t max_headers_size;
  57. ev_uint64_t max_body_size;
  58. int flags;
  59. #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
  60. #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
  61. #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
  62. /* set when we want to auto free the connection */
  63. #define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
  64. /* Installed when attempt to read HTTP error after write failed, see
  65. * EVHTTP_CON_READ_ON_WRITE_ERROR */
  66. #define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
  67. struct timeval timeout; /* timeout for events */
  68. int retry_cnt; /* retry count */
  69. int retry_max; /* maximum number of retries */
  70. struct timeval initial_retry_timeout; /* Timeout for low long to wait
  71. * after first failing attempt
  72. * before retry */
  73. enum evhttp_connection_state state;
  74. /* for server connections, the http server they are connected with */
  75. struct evhttp *http_server;
  76. TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
  77. void (*cb)(struct evhttp_connection *, void *);
  78. void *cb_arg;
  79. void (*closecb)(struct evhttp_connection *, void *);
  80. void *closecb_arg;
  81. struct event_callback read_more_deferred_cb;
  82. struct event_base *base;
  83. struct evdns_base *dns_base;
  84. int ai_family;
  85. };
  86. /* A callback for an http server */
  87. struct evhttp_cb {
  88. TAILQ_ENTRY(evhttp_cb) next;
  89. char *what;
  90. int chunked;
  91. void (*cb)(struct evhttp_request *req, void *);
  92. void *cbarg;
  93. };
  94. /* both the http server as well as the rpc system need to queue connections */
  95. TAILQ_HEAD(evconq, evhttp_connection);
  96. /* each bound socket is stored in one of these */
  97. struct evhttp_bound_socket {
  98. TAILQ_ENTRY(evhttp_bound_socket) next;
  99. struct evconnlistener *listener;
  100. };
  101. /* server alias list item. */
  102. struct evhttp_server_alias {
  103. TAILQ_ENTRY(evhttp_server_alias) next;
  104. char *alias; /* the server alias. */
  105. };
  106. struct evhttp {
  107. /* Next vhost, if this is a vhost. */
  108. TAILQ_ENTRY(evhttp) next_vhost;
  109. /* All listeners for this host */
  110. TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
  111. TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
  112. /* All live connections on this host. */
  113. struct evconq connections;
  114. TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
  115. TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
  116. /* NULL if this server is not a vhost */
  117. char *vhost_pattern;
  118. struct timeval timeout;
  119. size_t default_max_headers_size;
  120. ev_uint64_t default_max_body_size;
  121. int flags;
  122. const char *default_content_type;
  123. /* Bitmask of all HTTP methods that we accept and pass to user
  124. * callbacks. */
  125. ev_uint16_t allowed_methods;
  126. /* Fallback callback if all the other callbacks for this connection
  127. don't match. */
  128. void (*gencb)(struct evhttp_request *req, void *);
  129. void *gencbarg;
  130. struct bufferevent* (*bevcb)(struct event_base *, void *);
  131. void *bevcbarg;
  132. struct event_base *base;
  133. };
  134. /* XXX most of these functions could be static. */
  135. /* resets the connection; can be reused for more requests */
  136. void evhttp_connection_reset_(struct evhttp_connection *);
  137. /* connects if necessary */
  138. int evhttp_connection_connect_(struct evhttp_connection *);
  139. enum evhttp_request_error;
  140. /* notifies the current request that it failed; resets connection */
  141. EVENT2_EXPORT_SYMBOL
  142. void evhttp_connection_fail_(struct evhttp_connection *,
  143. enum evhttp_request_error error);
  144. enum message_read_status;
  145. EVENT2_EXPORT_SYMBOL
  146. enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
  147. EVENT2_EXPORT_SYMBOL
  148. enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
  149. void evhttp_start_read_(struct evhttp_connection *);
  150. void evhttp_start_write_(struct evhttp_connection *);
  151. /* response sending HTML the data in the buffer */
  152. void evhttp_response_code_(struct evhttp_request *, int, const char *);
  153. void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
  154. EVENT2_EXPORT_SYMBOL
  155. int evhttp_decode_uri_internal(const char *uri, size_t length,
  156. char *ret, int decode_plus);
  157. #endif /* HTTP_INTERNAL_H_INCLUDED_ */