avoid sending duplicate headers
[project/uclient.git] / uclient-http.c
1 /*
2 * uclient - ustream based protocol client library
3 *
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <unistd.h>
21 #include <stdint.h>
22
23 #include <libubox/ustream.h>
24 #include <libubox/ustream-ssl.h>
25 #include <libubox/usock.h>
26 #include <libubox/blobmsg.h>
27
28 #include "uclient.h"
29 #include "uclient-utils.h"
30 #include "uclient-backend.h"
31
32 enum auth_type {
33 AUTH_TYPE_UNKNOWN,
34 AUTH_TYPE_NONE,
35 AUTH_TYPE_BASIC,
36 AUTH_TYPE_DIGEST,
37 };
38
39 enum request_type {
40 REQ_GET,
41 REQ_HEAD,
42 REQ_POST,
43 __REQ_MAX
44 };
45
46 enum http_state {
47 HTTP_STATE_INIT,
48 HTTP_STATE_HEADERS_SENT,
49 HTTP_STATE_REQUEST_DONE,
50 HTTP_STATE_RECV_HEADERS,
51 HTTP_STATE_RECV_DATA,
52 HTTP_STATE_ERROR,
53 };
54
55 static const char * const request_types[__REQ_MAX] = {
56 [REQ_GET] = "GET",
57 [REQ_HEAD] = "HEAD",
58 [REQ_POST] = "POST",
59 };
60
61 struct uclient_http {
62 struct uclient uc;
63
64 struct ustream_ssl_ctx *ssl_ctx;
65 struct ustream *us;
66
67 struct ustream_fd ufd;
68 struct ustream_ssl ussl;
69
70 bool ssl_require_validation;
71 bool ssl_ctx_ext;
72 bool ssl;
73 bool eof;
74 bool connection_close;
75 enum request_type req_type;
76 enum http_state state;
77
78 enum auth_type auth_type;
79 char *auth_str;
80
81 long read_chunked;
82 long content_length;
83
84 uint32_t nc;
85
86 struct blob_buf headers;
87 struct blob_buf meta;
88 };
89
90 enum {
91 PREFIX_HTTP,
92 PREFIX_HTTPS,
93 __PREFIX_MAX,
94 };
95
96 static const char * const uclient_http_prefix[] = {
97 [PREFIX_HTTP] = "http://",
98 [PREFIX_HTTPS] = "https://",
99 [__PREFIX_MAX] = NULL
100 };
101
102 static int uclient_do_connect(struct uclient_http *uh, const char *port)
103 {
104 int fd;
105
106 if (uh->uc.url->port)
107 port = uh->uc.url->port;
108
109 fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
110 if (fd < 0)
111 return -1;
112
113 ustream_fd_init(&uh->ufd, fd);
114 return 0;
115 }
116
117 static void uclient_http_disconnect(struct uclient_http *uh)
118 {
119 if (!uh->us)
120 return;
121
122 if (uh->ssl)
123 ustream_free(&uh->ussl.stream);
124 ustream_free(&uh->ufd.stream);
125 close(uh->ufd.fd.fd);
126 uh->us = NULL;
127 }
128
129 static void uclient_http_free_url_state(struct uclient *cl)
130 {
131 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
132
133 uh->auth_type = AUTH_TYPE_UNKNOWN;
134 free(uh->auth_str);
135 uh->auth_str = NULL;
136 uclient_http_disconnect(uh);
137 }
138
139 static void uclient_http_error(struct uclient_http *uh, int code)
140 {
141 uh->state = HTTP_STATE_ERROR;
142 uh->us->eof = true;
143 ustream_state_change(uh->us);
144 uclient_backend_set_error(&uh->uc, code);
145 }
146
147 static void uclient_notify_eof(struct uclient_http *uh)
148 {
149 struct ustream *us = uh->us;
150
151 if (!uh->eof) {
152 if (!us->eof && !us->write_error)
153 return;
154
155 if (ustream_pending_data(us, false))
156 return;
157 }
158
159 uclient_backend_set_eof(&uh->uc);
160
161 if (uh->connection_close)
162 uclient_http_disconnect(uh);
163 }
164
165 static void uclient_http_reset_state(struct uclient_http *uh)
166 {
167 uclient_backend_reset_state(&uh->uc);
168 uh->read_chunked = -1;
169 uh->content_length = -1;
170 uh->eof = false;
171 uh->connection_close = false;
172 uh->state = HTTP_STATE_INIT;
173
174 if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
175 uh->auth_type = AUTH_TYPE_NONE;
176 }
177
178 static void uclient_http_init_request(struct uclient_http *uh)
179 {
180 uclient_http_reset_state(uh);
181 blob_buf_init(&uh->meta, 0);
182 }
183
184 static enum auth_type
185 uclient_http_update_auth_type(struct uclient_http *uh)
186 {
187 if (!uh->auth_str)
188 return AUTH_TYPE_NONE;
189
190 if (!strncasecmp(uh->auth_str, "basic", 5))
191 return AUTH_TYPE_BASIC;
192
193 if (!strncasecmp(uh->auth_str, "digest", 6))
194 return AUTH_TYPE_DIGEST;
195
196 return AUTH_TYPE_NONE;
197 }
198
199 static void uclient_http_process_headers(struct uclient_http *uh)
200 {
201 enum {
202 HTTP_HDR_TRANSFER_ENCODING,
203 HTTP_HDR_CONNECTION,
204 HTTP_HDR_CONTENT_LENGTH,
205 HTTP_HDR_AUTH,
206 __HTTP_HDR_MAX,
207 };
208 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
209 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
210 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
211 [HTTP_HDR_CONNECTION] = hdr("connection"),
212 [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
213 [HTTP_HDR_AUTH] = hdr("www-authenticate"),
214 #undef hdr
215 };
216 struct blob_attr *tb[__HTTP_HDR_MAX];
217 struct blob_attr *cur;
218
219 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
220
221 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
222 if (cur && strstr(blobmsg_data(cur), "chunked"))
223 uh->read_chunked = 0;
224
225 cur = tb[HTTP_HDR_CONNECTION];
226 if (cur && strstr(blobmsg_data(cur), "close"))
227 uh->connection_close = true;
228
229 cur = tb[HTTP_HDR_CONTENT_LENGTH];
230 if (cur)
231 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
232
233 cur = tb[HTTP_HDR_AUTH];
234 if (cur) {
235 free(uh->auth_str);
236 uh->auth_str = strdup(blobmsg_data(cur));
237 }
238
239 uh->auth_type = uclient_http_update_auth_type(uh);
240 }
241
242 static void
243 uclient_http_add_auth_basic(struct uclient_http *uh)
244 {
245 struct uclient_url *url = uh->uc.url;
246 int auth_len = strlen(url->auth);
247 char *auth_buf;
248
249 if (auth_len > 512)
250 return;
251
252 auth_buf = alloca(base64_len(auth_len) + 1);
253 base64_encode(url->auth, auth_len, auth_buf);
254 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
255 }
256
257 static char *digest_unquote_sep(char **str)
258 {
259 char *cur = *str + 1;
260 char *start = cur;
261 char *out;
262
263 if (**str != '"')
264 return NULL;
265
266 out = cur;
267 while (1) {
268 if (!*cur)
269 return NULL;
270
271 if (*cur == '"') {
272 cur++;
273 break;
274 }
275
276 if (*cur == '\\')
277 cur++;
278
279 *(out++) = *(cur++);
280 }
281
282 if (*cur == ',')
283 cur++;
284
285 *out = 0;
286 *str = cur;
287
288 return start;
289 }
290
291 static bool strmatch(char **str, const char *prefix)
292 {
293 int len = strlen(prefix);
294
295 if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
296 return false;
297
298 *str += len + 1;
299 return true;
300 }
301
302 static void
303 get_cnonce(char *dest)
304 {
305 uint32_t val = 0;
306 FILE *f;
307
308 f = fopen("/dev/urandom", "r");
309 if (f) {
310 fread(&val, sizeof(val), 1, f);
311 fclose(f);
312 }
313
314 bin_to_hex(dest, &val, sizeof(val));
315 }
316
317 static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
318 {
319 int available = *len - *ofs;
320 int required;
321 const char *next;
322 char *cur;
323
324 if (*len && !*buf)
325 return;
326
327 required = strlen(name) + 4 + strlen(val) * 2;
328 if (required > available)
329 *len += required - available + 64;
330
331 *buf = realloc(*buf, *len);
332 if (!*buf)
333 return;
334
335 cur = *buf + *ofs;
336 cur += sprintf(cur, ", %s=\"", name);
337
338 while ((next = strchr(val, '"'))) {
339 if (next > val) {
340 memcpy(cur, val, next - val);
341 cur += next - val;
342 }
343
344 cur += sprintf(cur, "\\\"");
345 val = next + 1;
346 }
347
348 cur += sprintf(cur, "%s\"", val);
349 *ofs = cur - *buf;
350 }
351
352 static void
353 uclient_http_add_auth_digest(struct uclient_http *uh)
354 {
355 struct uclient_url *url = uh->uc.url;
356 const char *realm = NULL, *opaque = NULL;
357 const char *user, *password;
358 char *buf, *next;
359 int len, ofs;
360
361 char cnonce_str[9];
362 char nc_str[9];
363 char ahash[33];
364 char hash[33];
365
366 struct http_digest_data data = {
367 .nc = nc_str,
368 .cnonce = cnonce_str,
369 .auth_hash = ahash,
370 };
371
372 len = strlen(uh->auth_str) + 1;
373 if (len > 512)
374 return;
375
376 buf = alloca(len);
377 strcpy(buf, uh->auth_str);
378
379 /* skip auth type */
380 strsep(&buf, " ");
381
382 next = buf;
383 while (*next) {
384 const char **dest = NULL;
385
386 while (isspace(*next))
387 next++;
388
389 if (strmatch(&next, "realm"))
390 dest = &realm;
391 else if (strmatch(&next, "qop"))
392 dest = &data.qop;
393 else if (strmatch(&next, "nonce"))
394 dest = &data.nonce;
395 else if (strmatch(&next, "opaque"))
396 dest = &opaque;
397 else
398 return;
399
400 *dest = digest_unquote_sep(&next);
401 }
402
403 if (!realm || !data.qop || !data.nonce)
404 return;
405
406 sprintf(nc_str, "%08x", uh->nc++);
407 get_cnonce(cnonce_str);
408
409 data.qop = "auth";
410 data.uri = url->location;
411 data.method = request_types[uh->req_type];
412
413 password = strchr(url->auth, ':');
414 if (password) {
415 char *user_buf;
416
417 len = password - url->auth;
418 if (len > 256)
419 return;
420
421 user_buf = alloca(len + 1);
422 strncpy(user_buf, url->auth, len);
423 user_buf[len] = 0;
424 user = user_buf;
425 password++;
426 } else {
427 user = url->auth;
428 password = "";
429 }
430
431 http_digest_calculate_auth_hash(ahash, user, realm, password);
432 http_digest_calculate_response(hash, &data);
433
434 buf = NULL;
435 len = 0;
436 ofs = 0;
437
438 add_field(&buf, &ofs, &len, "username", user);
439 add_field(&buf, &ofs, &len, "realm", realm);
440 add_field(&buf, &ofs, &len, "nonce", data.nonce);
441 add_field(&buf, &ofs, &len, "uri", data.uri);
442 add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
443 add_field(&buf, &ofs, &len, "response", hash);
444 if (opaque)
445 add_field(&buf, &ofs, &len, "opaque", opaque);
446
447 ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
448 free(buf);
449 }
450
451 static void
452 uclient_http_add_auth_header(struct uclient_http *uh)
453 {
454 if (!uh->uc.url->auth)
455 return;
456
457 switch (uh->auth_type) {
458 case AUTH_TYPE_UNKNOWN:
459 case AUTH_TYPE_NONE:
460 break;
461 case AUTH_TYPE_BASIC:
462 uclient_http_add_auth_basic(uh);
463 break;
464 case AUTH_TYPE_DIGEST:
465 uclient_http_add_auth_digest(uh);
466 break;
467 }
468 }
469
470 static void
471 uclient_http_send_headers(struct uclient_http *uh)
472 {
473 struct uclient_url *url = uh->uc.url;
474 struct blob_attr *cur;
475 enum request_type req_type = uh->req_type;
476 int rem;
477
478 if (uh->state >= HTTP_STATE_HEADERS_SENT)
479 return;
480
481 if (uh->auth_type == AUTH_TYPE_UNKNOWN)
482 req_type = REQ_HEAD;
483
484 ustream_printf(uh->us,
485 "%s %s HTTP/1.1\r\n"
486 "Host: %s\r\n",
487 request_types[req_type],
488 url->location, url->host);
489
490 blobmsg_for_each_attr(cur, uh->headers.head, rem)
491 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
492
493 if (uh->req_type == REQ_POST)
494 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
495
496 uclient_http_add_auth_header(uh);
497
498 ustream_printf(uh->us, "\r\n");
499
500 uh->state = HTTP_STATE_HEADERS_SENT;
501 }
502
503 static void uclient_http_headers_complete(struct uclient_http *uh)
504 {
505 enum auth_type auth_type = uh->auth_type;
506
507 uh->state = HTTP_STATE_RECV_DATA;
508 uh->uc.meta = uh->meta.head;
509 uclient_http_process_headers(uh);
510
511 if (auth_type == AUTH_TYPE_UNKNOWN) {
512 uclient_http_init_request(uh);
513 uclient_http_send_headers(uh);
514 uh->state = HTTP_STATE_REQUEST_DONE;
515 return;
516 }
517
518 if (uh->uc.cb->header_done)
519 uh->uc.cb->header_done(&uh->uc);
520
521 if (uh->req_type == REQ_HEAD) {
522 uh->eof = true;
523 uclient_notify_eof(uh);
524 }
525 }
526
527 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
528 {
529 char *name;
530 char *sep;
531
532 if (uh->state == HTTP_STATE_REQUEST_DONE) {
533 char *code;
534
535 /* HTTP/1.1 */
536 strsep(&data, " ");
537
538 code = strsep(&data, " ");
539 if (!code)
540 goto error;
541
542 uh->uc.status_code = strtoul(code, &sep, 10);
543 if (sep && *sep)
544 goto error;
545
546 uh->state = HTTP_STATE_RECV_HEADERS;
547 return;
548 }
549
550 if (!*data) {
551 uclient_http_headers_complete(uh);
552 return;
553 }
554
555 sep = strchr(data, ':');
556 if (!sep)
557 return;
558
559 *(sep++) = 0;
560
561 for (name = data; *name; name++)
562 *name = tolower(*name);
563
564 name = data;
565 while (isspace(*sep))
566 sep++;
567
568 blobmsg_add_string(&uh->meta, name, sep);
569 return;
570
571 error:
572 uh->uc.status_code = 400;
573 uh->eof = true;
574 uclient_notify_eof(uh);
575 }
576
577 static void __uclient_notify_read(struct uclient_http *uh)
578 {
579 struct uclient *uc = &uh->uc;
580 char *data;
581 int len;
582
583 if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
584 return;
585
586 data = ustream_get_read_buf(uh->us, &len);
587 if (!data || !len)
588 return;
589
590 if (uh->state < HTTP_STATE_RECV_DATA) {
591 char *sep;
592 int cur_len;
593
594 do {
595 sep = strstr(data, "\r\n");
596 if (!sep)
597 break;
598
599 /* Check for multi-line HTTP headers */
600 if (sep > data) {
601 if (!sep[2])
602 return;
603
604 if (isspace(sep[2]) && sep[2] != '\r') {
605 sep[0] = ' ';
606 sep[1] = ' ';
607 continue;
608 }
609 }
610
611 *sep = 0;
612 cur_len = sep + 2 - data;
613 uclient_parse_http_line(uh, data);
614 ustream_consume(uh->us, cur_len);
615 len -= cur_len;
616
617 data = ustream_get_read_buf(uh->us, &len);
618 } while (data && uh->state < HTTP_STATE_RECV_DATA);
619
620 if (!len)
621 return;
622 }
623
624 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
625 uc->cb->data_read(uc);
626 }
627
628 static void uclient_notify_read(struct ustream *us, int bytes)
629 {
630 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
631
632 __uclient_notify_read(uh);
633 }
634
635 static void uclient_notify_state(struct ustream *us)
636 {
637 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
638
639 uclient_notify_eof(uh);
640 }
641
642 static int uclient_setup_http(struct uclient_http *uh)
643 {
644 struct ustream *us = &uh->ufd.stream;
645 int ret;
646
647 uh->us = us;
648 us->string_data = true;
649 us->notify_state = uclient_notify_state;
650 us->notify_read = uclient_notify_read;
651
652 ret = uclient_do_connect(uh, "80");
653 if (ret)
654 return ret;
655
656 return 0;
657 }
658
659 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
660 {
661 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
662
663 __uclient_notify_read(uh);
664 }
665
666 static void uclient_ssl_notify_state(struct ustream *us)
667 {
668 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
669
670 uclient_notify_eof(uh);
671 }
672
673 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
674 {
675 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
676
677 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
678 }
679
680 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
681 {
682 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
683
684 if (!uh->ssl_require_validation)
685 return;
686
687 uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
688 }
689
690 static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
691 {
692 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
693
694 if (!uh->ssl_require_validation)
695 return;
696
697 if (!uh->ussl.valid_cn)
698 uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
699 }
700
701 static int uclient_setup_https(struct uclient_http *uh)
702 {
703 struct ustream *us = &uh->ussl.stream;
704 int ret;
705
706 uh->ssl = true;
707 uh->us = us;
708
709 ret = uclient_do_connect(uh, "443");
710 if (ret)
711 return ret;
712
713 if (!uh->ssl_ctx)
714 uh->ssl_ctx = ustream_ssl_context_new(false);
715
716 us->string_data = true;
717 us->notify_state = uclient_ssl_notify_state;
718 us->notify_read = uclient_ssl_notify_read;
719 uh->ussl.notify_error = uclient_ssl_notify_error;
720 uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
721 uh->ussl.notify_connected = uclient_ssl_notify_connected;
722 ustream_ssl_init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
723 ustream_ssl_set_peer_cn(&uh->ussl, uh->uc.url->host);
724
725 return 0;
726 }
727
728 static int uclient_http_connect(struct uclient *cl)
729 {
730 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
731 int ret;
732
733 uclient_http_init_request(uh);
734
735 if (uh->us)
736 return 0;
737
738 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
739
740 if (uh->ssl)
741 ret = uclient_setup_https(uh);
742 else
743 ret = uclient_setup_http(uh);
744
745 if (ret)
746 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
747
748 return ret;
749 }
750
751 static struct uclient *uclient_http_alloc(void)
752 {
753 struct uclient_http *uh;
754
755 uh = calloc_a(sizeof(*uh));
756 blob_buf_init(&uh->headers, 0);
757
758 return &uh->uc;
759 }
760
761 static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
762 {
763 if (uh->ssl_ctx && !uh->ssl_ctx_ext)
764 ustream_ssl_context_free(uh->ssl_ctx);
765
766 uh->ssl_ctx_ext = false;
767 }
768
769 static void uclient_http_free(struct uclient *cl)
770 {
771 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
772
773 uclient_http_free_ssl_ctx(uh);
774 uclient_http_free_url_state(cl);
775 blob_buf_free(&uh->headers);
776 blob_buf_free(&uh->meta);
777 free(uh);
778 }
779
780 int
781 uclient_http_set_request_type(struct uclient *cl, const char *type)
782 {
783 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
784 int i;
785
786 if (cl->backend != &uclient_backend_http)
787 return -1;
788
789 if (uh->state > HTTP_STATE_INIT)
790 return -1;
791
792 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
793 if (strcmp(request_types[i], type) != 0)
794 continue;
795
796 uh->req_type = i;
797 return 0;
798 }
799
800 return -1;
801 }
802
803 int
804 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
805 {
806 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
807
808 blob_buf_init(&uh->headers, 0);
809
810 return 0;
811 }
812
813 int
814 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
815 {
816 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
817
818 if (cl->backend != &uclient_backend_http)
819 return -1;
820
821 if (uh->state > HTTP_STATE_INIT)
822 return -1;
823
824 blobmsg_add_string(&uh->headers, name, value);
825 return 0;
826 }
827
828 static int
829 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
830 {
831 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
832
833 if (uh->state >= HTTP_STATE_REQUEST_DONE)
834 return -1;
835
836 uclient_http_send_headers(uh);
837
838 ustream_printf(uh->us, "%X\r\n", len);
839 if (len > 0)
840 ustream_write(uh->us, buf, len, false);
841 ustream_printf(uh->us, "\r\n");
842
843 return len;
844 }
845
846 static int
847 uclient_http_request_done(struct uclient *cl)
848 {
849 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
850
851 if (uh->state >= HTTP_STATE_REQUEST_DONE)
852 return -1;
853
854 uclient_http_send_headers(uh);
855 if (uh->req_type == REQ_POST)
856 ustream_printf(uh->us, "0\r\n\r\n");
857 uh->state = HTTP_STATE_REQUEST_DONE;
858
859 return 0;
860 }
861
862 static int
863 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
864 {
865 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
866 int read_len = 0;
867 char *data, *data_end;
868
869 if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
870 return 0;
871
872 data = ustream_get_read_buf(uh->us, &read_len);
873 if (!data || !read_len)
874 return 0;
875
876 data_end = data + read_len;
877 read_len = 0;
878
879 if (uh->read_chunked == 0) {
880 char *sep;
881
882 if (data[0] == '\r' && data[1] == '\n') {
883 data += 2;
884 read_len += 2;
885 }
886
887 sep = strstr(data, "\r\n");
888 if (!sep)
889 return 0;
890
891 *sep = 0;
892 uh->read_chunked = strtoul(data, NULL, 16);
893
894 read_len += sep + 2 - data;
895 data = sep + 2;
896
897 if (!uh->read_chunked)
898 uh->eof = true;
899 }
900
901 if (len > data_end - data)
902 len = data_end - data;
903
904 if (uh->read_chunked >= 0) {
905 if (len > uh->read_chunked)
906 len = uh->read_chunked;
907
908 uh->read_chunked -= len;
909 } else if (uh->content_length >= 0) {
910 if (len > uh->content_length)
911 len = uh->content_length;
912
913 uh->content_length -= len;
914 if (!uh->content_length)
915 uh->eof = true;
916 }
917
918 if (len > 0) {
919 read_len += len;
920 memcpy(buf, data, len);
921 }
922
923 if (read_len > 0)
924 ustream_consume(uh->us, read_len);
925
926 uclient_notify_eof(uh);
927
928 return len;
929 }
930
931 bool uclient_http_redirect(struct uclient *cl)
932 {
933 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
934 struct blobmsg_policy location = {
935 .name = "location",
936 .type = BLOBMSG_TYPE_STRING,
937 };
938 struct uclient_url *url = cl->url;
939 struct blob_attr *tb;
940
941 if (cl->backend != &uclient_backend_http)
942 return false;
943
944 switch (cl->status_code) {
945 case 301:
946 case 302:
947 case 307:
948 break;
949 default:
950 return false;
951 }
952
953 blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
954 if (!tb)
955 return false;
956
957 url = uclient_get_url(blobmsg_data(tb), url->auth);
958 if (!url)
959 return false;
960
961 free(cl->url);
962 cl->url = url;
963 uclient_http_connect(cl);
964 uclient_http_request_done(cl);
965
966 return true;
967 }
968
969 int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation)
970 {
971 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
972
973 if (cl->backend != &uclient_backend_http)
974 return -1;
975
976 uclient_http_free_url_state(cl);
977
978 uclient_http_free_ssl_ctx(uh);
979 uh->ssl_ctx = ctx;
980 uh->ssl_ctx_ext = !!ctx;
981 uh->ssl_require_validation = !!ctx && require_validation;
982
983 return 0;
984 }
985
986 const struct uclient_backend uclient_backend_http = {
987 .prefix = uclient_http_prefix,
988
989 .alloc = uclient_http_alloc,
990 .free = uclient_http_free,
991 .connect = uclient_http_connect,
992 .update_url = uclient_http_free_url_state,
993
994 .read = uclient_http_read,
995 .write = uclient_http_send_data,
996 .request = uclient_http_request_done,
997 };