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 #ifndef PROXY_HTTPCLIENT_H
30 #define PROXY_HTTPCLIENT_H
31
32 #include "../public/nsapi.h"
33 #include "../daemon/httpparser.h"
34 #include "../util/io.h"
35
36 #include <sys/socket.h>
37 #include <cx/string.h>
38 #include <cx/mempool.h>
39 #include <cx/test.h>
40
41 #include <inttypes.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #define HTTP_CLIENT_BUFFER_SIZE 4096
48
49 #define HTTP_CLIENT_CALLBACK_WOULD_BLOCK -1
50 #define HTTP_CLIENT_CALLBACK_ERROR -2
51
52 typedef struct HttpClient HttpClient;
53 struct HttpClient {
54 EventHandler *ev;
55 CxMempool *mp;
56 char *method;
57 char *uri;
58
59 struct sockaddr *addr;
60 size_t addrlen;
61
62 int socketfd;
63 HttpStream *stream;
64
65 HeaderArray *request_headers;
66 HeaderArray *response_headers;
67
68
69
70
71
72
73
74 int64_t req_content_length;
75
76 int error;
77 int statuscode;
78
79
80
81
82
83
84
85
86
87 ssize_t (*request_body_read)(HttpClient *,
void *,
size_t,
void *);
88 void *request_body_read_userdata;
89
90
91
92
93
94
95 int (*response_start)(HttpClient *,
int,
char *,
void *);
96 void *response_start_userdata;
97
98
99
100
101
102
103
104
105
106 ssize_t (*response_body_write)(HttpClient *,
void *,
size_t,
void *);
107 void *response_body_write_userdata;
108
109
110
111
112
113
114
115
116
117 void (*response_finished)(HttpClient *,
void *);
118 void *response_finished_userdata;
119
120
121
122 HttpParser *parser;
123 netbuf buffer;
124
125 char *transfer_buffer;
126 size_t transfer_buffer_alloc;
127 size_t transfer_buffer_len;
128 size_t transfer_buffer_pos;
129
130 size_t req_contentlength_pos;
131
132 int request_body_complete;
133 int request_body_terminated;
134 int response_header_complete;
135
136 Event readev;
137 Event writeev;
138 };
139
140 HttpClient* http_client_new(EventHandler *ev);
141
142 void http_client_free(HttpClient *client);
143
144 int http_client_set_addr(HttpClient *client,
const struct sockaddr *addr,
socklen_t addrlen);
145
146 int http_client_set_method(HttpClient *client,
const char *method);
147
148 int http_client_set_uri(HttpClient *client,
const char *uri);
149
150 int http_client_set_method_len(HttpClient *client,
const char *method,
size_t len);
151
152 int http_client_set_uri_len(HttpClient *client,
const char *uri,
size_t len);
153
154
155
156
157
158
159
160 int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value);
161
162
163
164
165
166
167
168 int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value);
169
170
171
172
173 int http_client_set_content_length(HttpClient *client,
int64_t contentlength);
174
175
176
177
178 int http_client_enable_chunked_transfer_encoding(HttpClient *client);
179
180 int http_client_start(HttpClient *client);
181
182
183 void http_client_add_tests(CxTestSuite *suite);
184
185 #ifdef __cplusplus
186 }
187 #endif
188
189 #endif
190
191