Avoid recursion when processing residual inbuf data.
[shim.git] / httpconn.h
blob800cb3b3d8d0e3f362240abd95c5ce577ff4d61c
1 #ifndef _HTTPCONN_H_
2 #define _HTTPCONN_H_
4 enum http_version {
5 HTTP_UNKNOWN,
6 HTTP_10,
7 HTTP_11
8 };
10 enum http_state {
11 HTTP_STATE_IDLE,
12 HTTP_STATE_CONNECTING,
13 HTTP_STATE_READ_FIRSTLINE,
14 HTTP_STATE_READ_HEADERS,
15 HTTP_STATE_READ_BODY,
16 HTTP_STATE_MANGLED,
17 HTTP_STATE_TUNNEL_CONNECTING,
18 HTTP_STATE_TUNNEL_OPEN,
19 HTTP_STATE_TUNNEL_FLUSHING
22 enum http_type {
23 HTTP_CLIENT,
24 HTTP_SERVER
27 enum http_method {
28 METH_GET,
29 METH_HEAD,
30 METH_POST,
31 METH_PUT,
32 METH_CONNECT
35 enum http_te {
36 TE_IDENTITY,
37 TE_CHUNKED
40 enum http_conn_error {
41 ERROR_NONE,
42 ERROR_CONNECT_FAILED,
43 ERROR_IDLE_CONN_TIMEDOUT,
44 ERROR_CLIENT_EXPECTATION_FAILED,
45 ERROR_CLIENT_POST_WITHOUT_LENGTH,
46 ERROR_INCOMPLETE_HEADERS,
47 ERROR_INCOMPLETE_BODY,
48 ERROR_HEADER_PARSE_FAILED,
49 ERROR_CHUNK_PARSE_FAILED,
50 ERROR_WRITE_FAILED,
51 ERROR_TUNNEL_CONNECT_FAILED,
52 ERROR_TUNNEL_CLOSED
55 #define HTTP_ERROR_RESPONSE(c) (c >= 400 && c <= 599)
57 struct evbuffer;
58 struct event_base;
59 struct evdns_base;
60 struct http_conn;
61 struct header_list;
62 struct url;
64 struct http_request {
65 TAILQ_ENTRY(http_request) next;
66 enum http_method meth;
67 struct url *url;
68 enum http_version vers;
69 struct header_list *headers;
71 TAILQ_HEAD(http_request_list, http_request);
73 struct http_response {
74 enum http_version vers;
75 int code;
76 char *reason;
77 struct header_list *headers;
80 struct http_cbs {
81 void (*on_connect)(struct http_conn *, void *);
82 void (*on_error)(struct http_conn *, enum http_conn_error, void *);
83 void (*on_client_request)(struct http_conn *, struct http_request *, void *);
84 void (*on_server_continuation)(struct http_conn *, void *);
85 void (*on_server_response)(struct http_conn *, struct http_response *, void *);
86 void (*on_read_body)(struct http_conn *, struct evbuffer *, void *);
87 void (*on_msg_complete)(struct http_conn *, void *);
89 /* called when it is ok to write more data after choaking */
90 void (*on_write_more)(struct http_conn *, void *);
92 void (*on_flush)(struct http_conn *, void *);
95 struct http_conn *http_conn_new(struct event_base *base, evutil_socket_t sock,
96 enum http_type type, const struct http_cbs *cbs,
97 void *cbarg);
99 int http_conn_connect(struct http_conn *conn, struct evdns_base *dns,
100 int family, const char *host, int port);
102 void http_conn_free(struct http_conn *conn);
104 void http_conn_write_request(struct http_conn *conn, struct http_request *req);
105 int http_conn_expect_continue(struct http_conn *conn);
106 void http_conn_write_continue(struct http_conn *conn);
107 void http_conn_write_response(struct http_conn *conn, struct http_response *resp);
109 /* return: 0 on choaked, 1 on queued. */
110 int http_conn_write_buf(struct http_conn *conn, struct evbuffer *buf);
111 void http_conn_write_finished(struct http_conn *conn);
113 int http_conn_current_message_has_body(struct http_conn *conn);
114 void http_conn_set_current_message_bodyless(struct http_conn *conn);
116 enum http_te http_conn_get_current_message_body_encoding(struct http_conn *conn);
117 ev_int64_t http_conn_get_current_message_body_length(struct http_conn *conn);
118 void http_conn_set_output_encoding(struct http_conn *conn, enum http_te te);
119 int http_conn_is_persistent(struct http_conn *conn);
120 void http_conn_disable_persistence(struct http_conn *conn);
122 /* turn read off/on; useful for when the other end is choking */
123 void http_conn_stop_reading(struct http_conn *conn);
124 void http_conn_start_reading(struct http_conn *conn);
126 void http_conn_flush(struct http_conn *conn);
128 void http_conn_send_error(struct http_conn *conn, int code,
129 const char *fmt, ...);
131 int http_conn_start_tunnel(struct http_conn *conn, struct evdns_base *dns,
132 int family, const char *host, int port);
134 const char *http_conn_error_to_string(enum http_conn_error err);
135 const char *http_method_to_string(enum http_method m);
136 const char *http_version_to_string(enum http_version v);
138 void http_request_free(struct http_request *req);
139 void http_response_free(struct http_response *req);
141 #endif