http-internal.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2001 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  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_H_
  10. #define _HTTP_H_
  11. #define HTTP_CONNECT_TIMEOUT 45
  12. #define HTTP_WRITE_TIMEOUT 50
  13. #define HTTP_READ_TIMEOUT 50
  14. #define HTTP_PREFIX "http://"
  15. #define HTTP_DEFAULTPORT 80
  16. enum message_read_status {
  17. ALL_DATA_READ = 1,
  18. MORE_DATA_EXPECTED = 0,
  19. DATA_CORRUPTED = -1,
  20. REQUEST_CANCELED = -2
  21. };
  22. enum evhttp_connection_error {
  23. EVCON_HTTP_TIMEOUT,
  24. EVCON_HTTP_EOF,
  25. EVCON_HTTP_INVALID_HEADER
  26. };
  27. struct evbuffer;
  28. struct evhttp_request;
  29. /* A stupid connection object - maybe make this a bufferevent later */
  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. struct evhttp_connection {
  43. /* we use tailq only if they were created for an http server */
  44. TAILQ_ENTRY(evhttp_connection) (next);
  45. int fd;
  46. struct event ev;
  47. struct event close_ev;
  48. struct evbuffer *input_buffer;
  49. struct evbuffer *output_buffer;
  50. char *bind_address; /* address to use for binding the src */
  51. u_short bind_port; /* local port for binding the src */
  52. char *address; /* address to connect to */
  53. u_short port;
  54. int flags;
  55. #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
  56. #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
  57. #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
  58. int timeout; /* timeout in seconds for events */
  59. int retry_cnt; /* retry count */
  60. int retry_max; /* maximum number of retries */
  61. enum evhttp_connection_state state;
  62. /* for server connections, the http server they are connected with */
  63. struct evhttp *http_server;
  64. TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
  65. void (*cb)(struct evhttp_connection *, void *);
  66. void *cb_arg;
  67. void (*closecb)(struct evhttp_connection *, void *);
  68. void *closecb_arg;
  69. struct event_base *base;
  70. };
  71. struct evhttp_cb {
  72. TAILQ_ENTRY(evhttp_cb) next;
  73. char *what;
  74. void (*cb)(struct evhttp_request *req, void *);
  75. void *cbarg;
  76. };
  77. /* both the http server as well as the rpc system need to queue connections */
  78. TAILQ_HEAD(evconq, evhttp_connection);
  79. /* each bound socket is stored in one of these */
  80. struct evhttp_bound_socket {
  81. TAILQ_ENTRY(evhttp_bound_socket) (next);
  82. struct event bind_ev;
  83. };
  84. struct evhttp {
  85. TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
  86. TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
  87. struct evconq connections;
  88. int timeout;
  89. void (*gencb)(struct evhttp_request *req, void *);
  90. void *gencbarg;
  91. struct event_base *base;
  92. };
  93. /* resets the connection; can be reused for more requests */
  94. void evhttp_connection_reset(struct evhttp_connection *);
  95. /* connects if necessary */
  96. int evhttp_connection_connect(struct evhttp_connection *);
  97. /* notifies the current request that it failed; resets connection */
  98. void evhttp_connection_fail(struct evhttp_connection *,
  99. enum evhttp_connection_error error);
  100. void evhttp_get_request(struct evhttp *, int, struct sockaddr *, socklen_t);
  101. int evhttp_hostportfile(char *, char **, u_short *, char **);
  102. int evhttp_parse_firstline(struct evhttp_request *, struct evbuffer*);
  103. int evhttp_parse_headers(struct evhttp_request *, struct evbuffer*);
  104. void evhttp_start_read(struct evhttp_connection *);
  105. void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
  106. void evhttp_write_buffer(struct evhttp_connection *,
  107. void (*)(struct evhttp_connection *, void *), void *);
  108. /* response sending HTML the data in the buffer */
  109. void evhttp_response_code(struct evhttp_request *, int, const char *);
  110. void evhttp_send_page(struct evhttp_request *, struct evbuffer *);
  111. #endif /* _HTTP_H */