remove debug code
[project/uclient.git] / uclient-http.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <unistd.h>
4
5 #include <libubox/ustream.h>
6 #include <libubox/ustream-ssl.h>
7 #include <libubox/usock.h>
8 #include <libubox/blobmsg.h>
9
10 #include "uclient.h"
11 #include "uclient-utils.h"
12 #include "uclient-backend.h"
13
14 static struct ustream_ssl_ctx *ssl_ctx;
15
16 enum request_type {
17 REQ_GET,
18 REQ_HEAD,
19 REQ_POST,
20 __REQ_MAX
21 };
22
23 enum http_state {
24 HTTP_STATE_INIT,
25 HTTP_STATE_HEADERS_SENT,
26 HTTP_STATE_REQUEST_DONE,
27 HTTP_STATE_RECV_HEADERS,
28 HTTP_STATE_RECV_DATA,
29 HTTP_STATE_ERROR,
30 };
31
32 static const char * const request_types[__REQ_MAX] = {
33 [REQ_GET] = "GET",
34 [REQ_HEAD] = "HEAD",
35 [REQ_POST] = "POST",
36 };
37
38 struct uclient_http {
39 struct uclient uc;
40
41 struct ustream *us;
42
43 struct ustream_fd ufd;
44 struct ustream_ssl ussl;
45
46 bool ssl;
47 bool eof;
48 enum request_type req_type;
49 enum http_state state;
50
51 long read_chunked;
52
53 struct blob_buf headers;
54 struct blob_buf meta;
55 };
56
57 enum {
58 PREFIX_HTTP,
59 PREFIX_HTTPS,
60 __PREFIX_MAX,
61 };
62
63 static const char * const uclient_http_prefix[] = {
64 [PREFIX_HTTP] = "http://",
65 [PREFIX_HTTPS] = "https://",
66 [__PREFIX_MAX] = NULL
67 };
68
69 static int uclient_do_connect(struct uclient_http *uh, const char *port)
70 {
71 int fd;
72
73 if (uh->uc.url->port)
74 port = uh->uc.url->port;
75
76 fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
77 if (fd < 0)
78 return -1;
79
80 ustream_fd_init(&uh->ufd, fd);
81 return 0;
82 }
83
84 static void uclient_notify_eof(struct uclient_http *uh)
85 {
86 struct ustream *us = uh->us;
87
88 if (!uh->eof) {
89 if (!us->eof && !us->write_error)
90 return;
91
92 if (ustream_pending_data(us, false))
93 return;
94 }
95
96 uclient_backend_set_eof(&uh->uc);
97 }
98
99 static void uclient_http_process_headers(struct uclient_http *uh)
100 {
101 enum {
102 HTTP_HDR_TRANSFER_ENCODING,
103 __HTTP_HDR_MAX,
104 };
105 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
106 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
107 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
108 #undef hdr
109 };
110 struct blob_attr *tb[__HTTP_HDR_MAX];
111 struct blob_attr *cur;
112
113 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
114
115 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
116 if (cur && strstr(blobmsg_data(cur), "chunked"))
117 uh->read_chunked = 0;
118 }
119
120 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
121 {
122 char *name;
123 char *sep;
124
125 if (uh->state == HTTP_STATE_REQUEST_DONE) {
126 uh->state = HTTP_STATE_RECV_HEADERS;
127 return;
128 }
129
130 if (!*data) {
131 uh->state = HTTP_STATE_RECV_DATA;
132 uh->uc.meta = uh->meta.head;
133 uclient_http_process_headers(uh);
134 if (uh->uc.cb->header_done)
135 uh->uc.cb->header_done(&uh->uc);
136 return;
137 }
138
139 sep = strchr(data, ':');
140 if (!sep)
141 return;
142
143 *(sep++) = 0;
144
145 for (name = data; *name; name++)
146 *name = tolower(*name);
147
148 name = data;
149 while (isspace(*sep))
150 sep++;
151
152 blobmsg_add_string(&uh->meta, name, sep);
153 }
154
155 static void __uclient_notify_read(struct uclient_http *uh)
156 {
157 struct uclient *uc = &uh->uc;
158 char *data;
159 int len;
160
161 if (uh->state < HTTP_STATE_REQUEST_DONE)
162 return;
163
164 data = ustream_get_read_buf(uh->us, &len);
165 if (!data || !len)
166 return;
167
168 if (uh->state < HTTP_STATE_RECV_DATA) {
169 char *sep;
170 int cur_len;
171
172 do {
173 sep = strstr(data, "\r\n");
174 if (!sep)
175 break;
176
177 /* Check for multi-line HTTP headers */
178 if (sep > data) {
179 if (!sep[2])
180 return;
181
182 if (isspace(sep[2]) && sep[2] != '\r') {
183 sep[0] = ' ';
184 sep[1] = ' ';
185 continue;
186 }
187 }
188
189 *sep = 0;
190 cur_len = sep + 2 - data;
191 uclient_parse_http_line(uh, data);
192 ustream_consume(uh->us, cur_len);
193 len -= cur_len;
194
195 data = ustream_get_read_buf(uh->us, &len);
196 } while (uh->state < HTTP_STATE_RECV_DATA);
197
198 if (!len)
199 return;
200 }
201
202 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
203 uc->cb->data_read(uc);
204 }
205
206 static void uclient_notify_read(struct ustream *us, int bytes)
207 {
208 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
209
210 __uclient_notify_read(uh);
211 }
212
213 static void uclient_notify_state(struct ustream *us)
214 {
215 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
216
217 uclient_notify_eof(uh);
218 }
219
220 static int uclient_setup_http(struct uclient_http *uh)
221 {
222 struct ustream *us = &uh->ufd.stream;
223 int ret;
224
225 uh->us = us;
226 us->string_data = true;
227 us->notify_state = uclient_notify_state;
228 us->notify_read = uclient_notify_read;
229
230 ret = uclient_do_connect(uh, "80");
231 if (ret)
232 return ret;
233
234 return 0;
235 }
236
237 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
238 {
239 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
240
241 __uclient_notify_read(uh);
242 }
243
244 static void uclient_ssl_notify_state(struct ustream *us)
245 {
246 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
247
248 uclient_notify_eof(uh);
249 }
250
251 static int uclient_setup_https(struct uclient_http *uh)
252 {
253 struct ustream *us = &uh->ussl.stream;
254 int ret;
255
256 uh->ssl = true;
257 uh->us = us;
258
259 ret = uclient_do_connect(uh, "443");
260 if (ret)
261 return ret;
262
263 if (!ssl_ctx)
264 ssl_ctx = ustream_ssl_context_new(false);
265
266 us->string_data = true;
267 us->notify_state = uclient_ssl_notify_state;
268 us->notify_read = uclient_ssl_notify_read;
269 ustream_ssl_init(&uh->ussl, &uh->ufd.stream, ssl_ctx, false);
270
271 return 0;
272 }
273
274 static void uclient_http_disconnect(struct uclient_http *uh)
275 {
276 uclient_backend_reset_state(&uh->uc);
277 uh->read_chunked = -1;
278 uh->eof = false;
279
280 if (!uh->us)
281 return;
282
283 if (uh->ssl)
284 ustream_free(&uh->ussl.stream);
285 ustream_free(&uh->ufd.stream);
286 close(uh->ufd.fd.fd);
287 uh->us = NULL;
288 }
289
290 static int uclient_http_connect(struct uclient *cl)
291 {
292 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
293
294 uclient_http_disconnect(uh);
295 blob_buf_init(&uh->meta, 0);
296
297 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
298 uh->state = HTTP_STATE_INIT;
299
300 if (uh->ssl)
301 return uclient_setup_https(uh);
302 else
303 return uclient_setup_http(uh);
304 }
305
306 static struct uclient *uclient_http_alloc(void)
307 {
308 struct uclient_http *uh;
309
310 uh = calloc_a(sizeof(*uh));
311 blob_buf_init(&uh->headers, 0);
312
313 return &uh->uc;
314 }
315
316 static void uclient_http_free(struct uclient *cl)
317 {
318 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
319
320 uclient_http_disconnect(uh);
321 blob_buf_free(&uh->headers);
322 blob_buf_free(&uh->meta);
323 free(uh);
324 }
325
326 int
327 uclient_http_set_request_type(struct uclient *cl, const char *type)
328 {
329 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
330 int i;
331
332 if (cl->backend != &uclient_backend_http)
333 return -1;
334
335 if (uh->state > HTTP_STATE_INIT)
336 return -1;
337
338 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
339 if (strcmp(request_types[i], type) != 0)
340 continue;
341
342 uh->req_type = i;
343 return 0;
344 }
345
346 return -1;
347 }
348
349 int
350 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
351 {
352 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
353
354 blob_buf_init(&uh->headers, 0);
355
356 return 0;
357 }
358
359 int
360 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
361 {
362 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
363
364 if (cl->backend != &uclient_backend_http)
365 return -1;
366
367 if (uh->state > HTTP_STATE_INIT)
368 return -1;
369
370 blobmsg_add_string(&uh->headers, name, value);
371 return 0;
372 }
373
374 static void
375 uclient_http_send_headers(struct uclient_http *uh)
376 {
377 struct uclient_url *url = uh->uc.url;
378 struct blob_attr *cur;
379 int rem;
380
381 if (uh->state >= HTTP_STATE_HEADERS_SENT)
382 return;
383
384 ustream_printf(uh->us,
385 "%s /%s HTTP/1.1\r\n"
386 "Host: %s\r\n"
387 "Connection: close\r\n",
388 request_types[uh->req_type],
389 url->location, url->host);
390
391 blobmsg_for_each_attr(cur, uh->headers.head, rem)
392 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
393
394 if (url->auth) {
395 int auth_len = strlen(url->auth);
396 char *auth_buf;
397
398 if (auth_len > 512)
399 return;
400
401 auth_buf = alloca(base64_len(auth_len) + 1);
402 base64_encode(url->auth, auth_len, auth_buf);
403 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
404 }
405
406 if (uh->req_type == REQ_POST)
407 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
408
409 ustream_printf(uh->us, "\r\n");
410 }
411
412 static int
413 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
414 {
415 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
416
417 if (uh->state >= HTTP_STATE_REQUEST_DONE)
418 return -1;
419
420 uclient_http_send_headers(uh);
421
422 ustream_printf(uh->us, "%X\r\n", len);
423 ustream_write(uh->us, buf, len, false);
424 ustream_printf(uh->us, "\r\n");
425
426 return len;
427 }
428
429 static int
430 uclient_http_request_done(struct uclient *cl)
431 {
432 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
433
434 if (uh->state >= HTTP_STATE_REQUEST_DONE)
435 return -1;
436
437 uclient_http_send_headers(uh);
438 uh->state = HTTP_STATE_REQUEST_DONE;
439
440 return 0;
441 }
442
443 static int
444 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
445 {
446 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
447 int read_len = 0;
448 char *data, *data_end;
449
450 if (uh->state < HTTP_STATE_RECV_DATA)
451 return 0;
452
453 data = ustream_get_read_buf(uh->us, &read_len);
454 if (!data || !read_len)
455 return 0;
456
457 data_end = data + read_len;
458 read_len = 0;
459
460 if (uh->read_chunked == 0) {
461 char *sep;
462
463 if (data[0] == '\r' && data[1] == '\n') {
464 data += 2;
465 read_len += 2;
466 }
467
468 sep = strstr(data, "\r\n");
469 if (!sep)
470 return 0;
471
472 *sep = 0;
473 uh->read_chunked = strtoul(data, NULL, 16);
474
475 read_len += sep + 2 - data;
476 data = sep + 2;
477
478 if (!uh->read_chunked)
479 uh->eof = true;
480 }
481
482 if (len > data_end - data)
483 len = data_end - data;
484
485 if (uh->read_chunked >= 0) {
486 if (len > uh->read_chunked)
487 len = uh->read_chunked;
488
489 uh->read_chunked -= len;
490 }
491
492 if (len > 0) {
493 read_len += len;
494 memcpy(buf, data, len);
495 }
496
497 if (read_len > 0)
498 ustream_consume(uh->us, read_len);
499
500 uclient_notify_eof(uh);
501
502 return len;
503 }
504
505 const struct uclient_backend uclient_backend_http __hidden = {
506 .prefix = uclient_http_prefix,
507
508 .alloc = uclient_http_alloc,
509 .free = uclient_http_free,
510 .connect = uclient_http_connect,
511
512 .read = uclient_http_read,
513 .write = uclient_http_send_data,
514 .request = uclient_http_request_done,
515 };