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