http: add support for specifying ipv4/ipv6 preference
[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 <sys/socket.h>
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <unistd.h>
22 #include <stdint.h>
23 #include <fcntl.h>
24
25 #include <libubox/ustream.h>
26 #include <libubox/ustream-ssl.h>
27 #include <libubox/usock.h>
28 #include <libubox/blobmsg.h>
29
30 #include "uclient.h"
31 #include "uclient-utils.h"
32 #include "uclient-backend.h"
33
34 enum auth_type {
35 AUTH_TYPE_UNKNOWN,
36 AUTH_TYPE_NONE,
37 AUTH_TYPE_BASIC,
38 AUTH_TYPE_DIGEST,
39 };
40
41 enum request_type {
42 REQ_GET,
43 REQ_HEAD,
44 REQ_POST,
45 REQ_PUT,
46 REQ_DELETE,
47 __REQ_MAX
48 };
49
50 enum http_state {
51 HTTP_STATE_INIT,
52 HTTP_STATE_HEADERS_SENT,
53 HTTP_STATE_REQUEST_DONE,
54 HTTP_STATE_RECV_HEADERS,
55 HTTP_STATE_RECV_DATA,
56 HTTP_STATE_ERROR,
57 };
58
59 static const char * const request_types[__REQ_MAX] = {
60 [REQ_GET] = "GET",
61 [REQ_HEAD] = "HEAD",
62 [REQ_POST] = "POST",
63 [REQ_PUT] = "PUT",
64 [REQ_DELETE] = "DELETE",
65 };
66
67 struct uclient_http {
68 struct uclient uc;
69
70 const struct ustream_ssl_ops *ssl_ops;
71 struct ustream_ssl_ctx *ssl_ctx;
72 struct ustream *us;
73
74 struct ustream_fd ufd;
75 struct ustream_ssl ussl;
76
77 struct uloop_timeout disconnect_t;
78 unsigned int seq;
79
80 bool ssl_require_validation;
81 bool ssl;
82 bool eof;
83 bool connection_close;
84 bool disconnect;
85 enum request_type req_type;
86 enum http_state state;
87
88 enum auth_type auth_type;
89 char *auth_str;
90
91 long read_chunked;
92 long content_length;
93
94 int usock_flags;
95
96 uint32_t nc;
97
98 struct blob_buf headers;
99 struct blob_buf meta;
100 };
101
102 enum {
103 PREFIX_HTTP,
104 PREFIX_HTTPS,
105 __PREFIX_MAX,
106 };
107
108 static const char * const uclient_http_prefix[] = {
109 [PREFIX_HTTP] = "http://",
110 [PREFIX_HTTPS] = "https://",
111 [__PREFIX_MAX] = NULL
112 };
113
114 static int uclient_http_connect(struct uclient *cl);
115
116 static int uclient_do_connect(struct uclient_http *uh, const char *port)
117 {
118 socklen_t sl;
119 int fd;
120
121 if (uh->uc.url->port)
122 port = uh->uc.url->port;
123
124 memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
125
126 fd = usock_inet(USOCK_TCP | uh->usock_flags, uh->uc.url->host, port, &uh->uc.remote_addr);
127 if (fd < 0)
128 return -1;
129
130 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
131 ustream_fd_init(&uh->ufd, fd);
132
133 sl = sizeof(uh->uc.local_addr);
134 memset(&uh->uc.local_addr, 0, sl);
135 getsockname(fd, &uh->uc.local_addr.sa, &sl);
136
137 return 0;
138 }
139
140 static void uclient_http_disconnect(struct uclient_http *uh)
141 {
142 uloop_timeout_cancel(&uh->disconnect_t);
143 if (!uh->us)
144 return;
145
146 if (uh->ssl)
147 ustream_free(&uh->ussl.stream);
148 ustream_free(&uh->ufd.stream);
149 close(uh->ufd.fd.fd);
150 uh->us = NULL;
151 }
152
153 static void uclient_http_free_url_state(struct uclient *cl)
154 {
155 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
156
157 uh->auth_type = AUTH_TYPE_UNKNOWN;
158 free(uh->auth_str);
159 uh->auth_str = NULL;
160 uclient_http_disconnect(uh);
161 }
162
163 static void uclient_http_error(struct uclient_http *uh, int code)
164 {
165 uh->state = HTTP_STATE_ERROR;
166 uh->us->eof = true;
167 ustream_state_change(uh->us);
168 uclient_backend_set_error(&uh->uc, code);
169 }
170
171 static void uclient_http_request_disconnect(struct uclient *cl)
172 {
173 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
174
175 if (!uh->us)
176 return;
177
178 uh->eof = true;
179 uh->disconnect = true;
180 uloop_timeout_set(&uh->disconnect_t, 1);
181 }
182
183 static void uclient_notify_eof(struct uclient_http *uh)
184 {
185 struct ustream *us = uh->us;
186
187 if (uh->disconnect)
188 return;
189
190 if (!uh->eof) {
191 if (!us->eof && !us->write_error)
192 return;
193
194 if (ustream_pending_data(us, false))
195 return;
196 }
197
198 if (uh->content_length < 0 && uh->read_chunked >= 0)
199 uh->uc.data_eof = true;
200
201 uclient_backend_set_eof(&uh->uc);
202
203 if (uh->connection_close)
204 uclient_http_request_disconnect(&uh->uc);
205 }
206
207 static void uclient_http_reset_state(struct uclient_http *uh)
208 {
209 uh->seq++;
210 uclient_backend_reset_state(&uh->uc);
211 uh->read_chunked = -1;
212 uh->content_length = -1;
213 uh->eof = false;
214 uh->disconnect = false;
215 uh->connection_close = false;
216 uh->state = HTTP_STATE_INIT;
217
218 if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
219 uh->auth_type = AUTH_TYPE_NONE;
220 }
221
222 static void uclient_http_init_request(struct uclient_http *uh)
223 {
224 uh->seq++;
225 uclient_http_reset_state(uh);
226 blob_buf_init(&uh->meta, 0);
227 }
228
229 static enum auth_type
230 uclient_http_update_auth_type(struct uclient_http *uh)
231 {
232 if (!uh->auth_str)
233 return AUTH_TYPE_NONE;
234
235 if (!strncasecmp(uh->auth_str, "basic", 5))
236 return AUTH_TYPE_BASIC;
237
238 if (!strncasecmp(uh->auth_str, "digest", 6))
239 return AUTH_TYPE_DIGEST;
240
241 return AUTH_TYPE_NONE;
242 }
243
244 static void uclient_http_process_headers(struct uclient_http *uh)
245 {
246 enum {
247 HTTP_HDR_TRANSFER_ENCODING,
248 HTTP_HDR_CONNECTION,
249 HTTP_HDR_CONTENT_LENGTH,
250 HTTP_HDR_AUTH,
251 __HTTP_HDR_MAX,
252 };
253 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
254 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
255 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
256 [HTTP_HDR_CONNECTION] = hdr("connection"),
257 [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
258 [HTTP_HDR_AUTH] = hdr("www-authenticate"),
259 #undef hdr
260 };
261 struct blob_attr *tb[__HTTP_HDR_MAX];
262 struct blob_attr *cur;
263
264 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
265
266 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
267 if (cur && strstr(blobmsg_data(cur), "chunked"))
268 uh->read_chunked = 0;
269
270 cur = tb[HTTP_HDR_CONNECTION];
271 if (cur && strstr(blobmsg_data(cur), "close"))
272 uh->connection_close = true;
273
274 cur = tb[HTTP_HDR_CONTENT_LENGTH];
275 if (cur)
276 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
277
278 cur = tb[HTTP_HDR_AUTH];
279 if (cur) {
280 free(uh->auth_str);
281 uh->auth_str = strdup(blobmsg_data(cur));
282 }
283
284 uh->auth_type = uclient_http_update_auth_type(uh);
285 }
286
287 static void
288 uclient_http_add_auth_basic(struct uclient_http *uh)
289 {
290 struct uclient_url *url = uh->uc.url;
291 int auth_len = strlen(url->auth);
292 char *auth_buf;
293
294 if (auth_len > 512)
295 return;
296
297 auth_buf = alloca(base64_len(auth_len) + 1);
298 base64_encode(url->auth, auth_len, auth_buf);
299 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
300 }
301
302 static char *digest_unquote_sep(char **str)
303 {
304 char *cur = *str + 1;
305 char *start = cur;
306 char *out;
307
308 if (**str != '"')
309 return NULL;
310
311 out = cur;
312 while (1) {
313 if (!*cur)
314 return NULL;
315
316 if (*cur == '"') {
317 cur++;
318 break;
319 }
320
321 if (*cur == '\\')
322 cur++;
323
324 *(out++) = *(cur++);
325 }
326
327 if (*cur == ',')
328 cur++;
329
330 *out = 0;
331 *str = cur;
332
333 return start;
334 }
335
336 static char *digest_sep(char **str)
337 {
338 char *cur, *next;
339
340 cur = *str;
341 next = strchr(*str, ',');
342 if (next) {
343 *str = next + 1;
344 *next = 0;
345 } else {
346 *str += strlen(*str);
347 }
348
349 return cur;
350 }
351
352 static bool strmatch(char **str, const char *prefix)
353 {
354 int len = strlen(prefix);
355
356 if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
357 return false;
358
359 *str += len + 1;
360 return true;
361 }
362
363 static void
364 get_cnonce(char *dest)
365 {
366 uint32_t val = 0;
367 FILE *f;
368
369 f = fopen("/dev/urandom", "r");
370 if (f) {
371 fread(&val, sizeof(val), 1, f);
372 fclose(f);
373 }
374
375 bin_to_hex(dest, &val, sizeof(val));
376 }
377
378 static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
379 {
380 int available = *len - *ofs;
381 int required;
382 const char *next;
383 char *cur;
384
385 if (*len && !*buf)
386 return;
387
388 required = strlen(name) + 4 + strlen(val) * 2;
389 if (required > available)
390 *len += required - available + 64;
391
392 *buf = realloc(*buf, *len);
393 if (!*buf)
394 return;
395
396 cur = *buf + *ofs;
397 cur += sprintf(cur, ", %s=\"", name);
398
399 while ((next = strchr(val, '"'))) {
400 if (next > val) {
401 memcpy(cur, val, next - val);
402 cur += next - val;
403 }
404
405 cur += sprintf(cur, "\\\"");
406 val = next + 1;
407 }
408
409 cur += sprintf(cur, "%s\"", val);
410 *ofs = cur - *buf;
411 }
412
413 static void
414 uclient_http_add_auth_digest(struct uclient_http *uh)
415 {
416 struct uclient_url *url = uh->uc.url;
417 const char *realm = NULL, *opaque = NULL;
418 const char *user, *password;
419 char *buf, *next;
420 int len, ofs;
421
422 char cnonce_str[9];
423 char nc_str[9];
424 char ahash[33];
425 char hash[33];
426
427 struct http_digest_data data = {
428 .nc = nc_str,
429 .cnonce = cnonce_str,
430 .auth_hash = ahash,
431 };
432
433 len = strlen(uh->auth_str) + 1;
434 if (len > 512)
435 return;
436
437 buf = alloca(len);
438 strcpy(buf, uh->auth_str);
439
440 /* skip auth type */
441 strsep(&buf, " ");
442
443 next = buf;
444 while (*next) {
445 const char **dest = NULL;
446 const char *tmp;
447
448 while (*next && isspace(*next))
449 next++;
450
451 if (strmatch(&next, "realm"))
452 dest = &realm;
453 else if (strmatch(&next, "qop"))
454 dest = &data.qop;
455 else if (strmatch(&next, "nonce"))
456 dest = &data.nonce;
457 else if (strmatch(&next, "opaque"))
458 dest = &opaque;
459 else if (strmatch(&next, "stale") ||
460 strmatch(&next, "algorithm") ||
461 strmatch(&next, "auth-param")) {
462 digest_sep(&next);
463 continue;
464 } else if (strmatch(&next, "domain") ||
465 strmatch(&next, "qop-options"))
466 dest = &tmp;
467 else {
468 digest_sep(&next);
469 continue;
470 }
471
472 *dest = digest_unquote_sep(&next);
473 }
474
475 if (!realm || !data.qop || !data.nonce)
476 return;
477
478 sprintf(nc_str, "%08x", uh->nc++);
479 get_cnonce(cnonce_str);
480
481 data.qop = "auth";
482 data.uri = url->location;
483 data.method = request_types[uh->req_type];
484
485 password = strchr(url->auth, ':');
486 if (password) {
487 char *user_buf;
488
489 len = password - url->auth;
490 if (len > 256)
491 return;
492
493 user_buf = alloca(len + 1);
494 strncpy(user_buf, url->auth, len);
495 user_buf[len] = 0;
496 user = user_buf;
497 password++;
498 } else {
499 user = url->auth;
500 password = "";
501 }
502
503 http_digest_calculate_auth_hash(ahash, user, realm, password);
504 http_digest_calculate_response(hash, &data);
505
506 buf = NULL;
507 len = 0;
508 ofs = 0;
509
510 add_field(&buf, &ofs, &len, "username", user);
511 add_field(&buf, &ofs, &len, "realm", realm);
512 add_field(&buf, &ofs, &len, "nonce", data.nonce);
513 add_field(&buf, &ofs, &len, "uri", data.uri);
514 add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
515 add_field(&buf, &ofs, &len, "response", hash);
516 if (opaque)
517 add_field(&buf, &ofs, &len, "opaque", opaque);
518
519 ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
520 free(buf);
521 }
522
523 static void
524 uclient_http_add_auth_header(struct uclient_http *uh)
525 {
526 if (!uh->uc.url->auth)
527 return;
528
529 switch (uh->auth_type) {
530 case AUTH_TYPE_UNKNOWN:
531 case AUTH_TYPE_NONE:
532 break;
533 case AUTH_TYPE_BASIC:
534 uclient_http_add_auth_basic(uh);
535 break;
536 case AUTH_TYPE_DIGEST:
537 uclient_http_add_auth_digest(uh);
538 break;
539 }
540 }
541
542 static void
543 uclient_http_send_headers(struct uclient_http *uh)
544 {
545 struct uclient_url *url = uh->uc.url;
546 struct blob_attr *cur;
547 enum request_type req_type = uh->req_type;
548 int rem;
549
550 if (uh->state >= HTTP_STATE_HEADERS_SENT)
551 return;
552
553 if (uh->uc.proxy_url)
554 url = uh->uc.proxy_url;
555
556 ustream_printf(uh->us,
557 "%s %s HTTP/1.1\r\n"
558 "Host: %s\r\n",
559 request_types[req_type],
560 url->location, url->host);
561
562 blobmsg_for_each_attr(cur, uh->headers.head, rem)
563 ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
564
565 if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
566 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
567
568 uclient_http_add_auth_header(uh);
569
570 ustream_printf(uh->us, "\r\n");
571
572 uh->state = HTTP_STATE_HEADERS_SENT;
573 }
574
575 static void uclient_http_headers_complete(struct uclient_http *uh)
576 {
577 enum auth_type auth_type = uh->auth_type;
578 int seq = uh->uc.seq;
579
580 uh->state = HTTP_STATE_RECV_DATA;
581 uh->uc.meta = uh->meta.head;
582 uclient_http_process_headers(uh);
583
584 if (auth_type == AUTH_TYPE_UNKNOWN && uh->uc.status_code == 401 &&
585 (uh->req_type == REQ_HEAD || uh->req_type == REQ_GET)) {
586 uclient_http_connect(&uh->uc);
587 uclient_http_send_headers(uh);
588 uh->state = HTTP_STATE_REQUEST_DONE;
589 return;
590 }
591
592 if (uh->uc.cb->header_done)
593 uh->uc.cb->header_done(&uh->uc);
594
595 if (uh->eof || seq != uh->uc.seq)
596 return;
597
598 if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
599 uh->eof = true;
600 uclient_notify_eof(uh);
601 }
602 }
603
604 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
605 {
606 char *name;
607 char *sep;
608
609 if (uh->state == HTTP_STATE_REQUEST_DONE) {
610 char *code;
611
612 if (!strlen(data))
613 return;
614
615 /* HTTP/1.1 */
616 strsep(&data, " ");
617
618 code = strsep(&data, " ");
619 if (!code)
620 goto error;
621
622 uh->uc.status_code = strtoul(code, &sep, 10);
623 if (sep && *sep)
624 goto error;
625
626 uh->state = HTTP_STATE_RECV_HEADERS;
627 return;
628 }
629
630 if (!*data) {
631 uclient_http_headers_complete(uh);
632 return;
633 }
634
635 sep = strchr(data, ':');
636 if (!sep)
637 return;
638
639 *(sep++) = 0;
640
641 for (name = data; *name; name++)
642 *name = tolower(*name);
643
644 name = data;
645 while (isspace(*sep))
646 sep++;
647
648 blobmsg_add_string(&uh->meta, name, sep);
649 return;
650
651 error:
652 uh->uc.status_code = 400;
653 uh->eof = true;
654 uclient_notify_eof(uh);
655 }
656
657 static void __uclient_notify_read(struct uclient_http *uh)
658 {
659 struct uclient *uc = &uh->uc;
660 unsigned int seq = uh->seq;
661 char *data;
662 int len;
663
664 if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
665 return;
666
667 data = ustream_get_read_buf(uh->us, &len);
668 if (!data || !len)
669 return;
670
671 if (uh->state < HTTP_STATE_RECV_DATA) {
672 char *sep, *next;
673 int cur_len;
674
675 do {
676 sep = strchr(data, '\n');
677 if (!sep)
678 break;
679
680 next = sep + 1;
681 if (sep > data && sep[-1] == '\r')
682 sep--;
683
684 /* Check for multi-line HTTP headers */
685 if (sep > data) {
686 if (!*next)
687 return;
688
689 if (isspace(*next) && *next != '\r' && *next != '\n') {
690 sep[0] = ' ';
691 if (sep + 1 < next)
692 sep[1] = ' ';
693 continue;
694 }
695 }
696
697 *sep = 0;
698 cur_len = next - data;
699 uclient_parse_http_line(uh, data);
700 if (seq != uh->seq)
701 return;
702
703 ustream_consume(uh->us, cur_len);
704 len -= cur_len;
705
706 if (uh->eof)
707 return;
708
709 data = ustream_get_read_buf(uh->us, &len);
710 } while (data && uh->state < HTTP_STATE_RECV_DATA);
711
712 if (!len)
713 return;
714 }
715
716 if (uh->eof)
717 return;
718
719 if (uh->state == HTTP_STATE_RECV_DATA) {
720 /* Now it's uclient user turn to read some data */
721 uloop_timeout_cancel(&uc->connection_timeout);
722
723 if (uc->cb->data_read)
724 uc->cb->data_read(uc);
725 }
726 }
727
728 static void __uclient_notify_write(struct uclient_http *uh)
729 {
730 struct uclient *uc = &uh->uc;
731
732 if (uc->cb->data_sent)
733 uc->cb->data_sent(uc);
734 }
735
736 static void uclient_notify_read(struct ustream *us, int bytes)
737 {
738 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
739
740 __uclient_notify_read(uh);
741 }
742
743 static void uclient_notify_write(struct ustream *us, int bytes)
744 {
745 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
746
747 __uclient_notify_write(uh);
748 }
749
750 static void uclient_notify_state(struct ustream *us)
751 {
752 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
753
754 if (uh->ufd.stream.write_error) {
755 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
756 return;
757 }
758 uclient_notify_eof(uh);
759 }
760
761 static int uclient_setup_http(struct uclient_http *uh)
762 {
763 struct ustream *us = &uh->ufd.stream;
764 int ret;
765
766 uh->us = us;
767 uh->ssl = false;
768
769 us->string_data = true;
770 us->notify_state = uclient_notify_state;
771 us->notify_read = uclient_notify_read;
772 us->notify_write = uclient_notify_write;
773
774 ret = uclient_do_connect(uh, "80");
775 if (ret)
776 return UCLIENT_ERROR_CONNECT;
777
778 return 0;
779 }
780
781 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
782 {
783 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
784
785 __uclient_notify_read(uh);
786 }
787
788 static void uclient_ssl_notify_write(struct ustream *us, int bytes)
789 {
790 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
791
792 __uclient_notify_write(uh);
793 }
794
795 static void uclient_ssl_notify_state(struct ustream *us)
796 {
797 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
798
799 uclient_notify_eof(uh);
800 }
801
802 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
803 {
804 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
805
806 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
807 }
808
809 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
810 {
811 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
812
813 if (!uh->ssl_require_validation)
814 return;
815
816 uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
817 }
818
819 static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
820 {
821 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
822
823 if (!uh->ssl_require_validation)
824 return;
825
826 if (!uh->ussl.valid_cn)
827 uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
828 }
829
830 static int uclient_setup_https(struct uclient_http *uh)
831 {
832 struct ustream *us = &uh->ussl.stream;
833 int ret;
834
835 uh->ssl = true;
836 uh->us = us;
837
838 if (!uh->ssl_ctx)
839 return UCLIENT_ERROR_MISSING_SSL_CONTEXT;
840
841 ret = uclient_do_connect(uh, "443");
842 if (ret)
843 return UCLIENT_ERROR_CONNECT;
844
845 us->string_data = true;
846 us->notify_state = uclient_ssl_notify_state;
847 us->notify_read = uclient_ssl_notify_read;
848 us->notify_write = uclient_ssl_notify_write;
849 uh->ussl.notify_error = uclient_ssl_notify_error;
850 uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
851 uh->ussl.notify_connected = uclient_ssl_notify_connected;
852 uh->ussl.server_name = uh->uc.url->host;
853 uh->ssl_ops->init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
854 uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host);
855
856 return 0;
857 }
858
859 static int uclient_http_connect(struct uclient *cl)
860 {
861 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
862 int ret;
863
864 if (!cl->eof || uh->disconnect || uh->connection_close)
865 uclient_http_disconnect(uh);
866
867 uclient_http_init_request(uh);
868
869 if (uh->us)
870 return 0;
871
872 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
873
874 if (uh->ssl)
875 ret = uclient_setup_https(uh);
876 else
877 ret = uclient_setup_http(uh);
878
879 return ret;
880 }
881
882 static void uclient_http_disconnect_cb(struct uloop_timeout *timeout)
883 {
884 struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t);
885
886 uclient_http_disconnect(uh);
887 }
888
889 static struct uclient *uclient_http_alloc(void)
890 {
891 struct uclient_http *uh;
892
893 uh = calloc_a(sizeof(*uh));
894 uh->disconnect_t.cb = uclient_http_disconnect_cb;
895 blob_buf_init(&uh->headers, 0);
896
897 return &uh->uc;
898 }
899
900 static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
901 {
902 uh->ssl_ops = NULL;
903 uh->ssl_ctx = NULL;
904 }
905
906 static void uclient_http_free(struct uclient *cl)
907 {
908 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
909
910 uclient_http_free_url_state(cl);
911 uclient_http_free_ssl_ctx(uh);
912 blob_buf_free(&uh->headers);
913 blob_buf_free(&uh->meta);
914 free(uh);
915 }
916
917 int
918 uclient_http_set_request_type(struct uclient *cl, const char *type)
919 {
920 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
921 int i;
922
923 if (cl->backend != &uclient_backend_http)
924 return -1;
925
926 if (uh->state > HTTP_STATE_INIT)
927 return -1;
928
929 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
930 if (strcmp(request_types[i], type) != 0)
931 continue;
932
933 uh->req_type = i;
934 return 0;
935 }
936
937 return -1;
938 }
939
940 int
941 uclient_http_reset_headers(struct uclient *cl)
942 {
943 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
944
945 blob_buf_init(&uh->headers, 0);
946
947 return 0;
948 }
949
950 int
951 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
952 {
953 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
954
955 if (cl->backend != &uclient_backend_http)
956 return -1;
957
958 if (uh->state > HTTP_STATE_INIT)
959 return -1;
960
961 blobmsg_add_string(&uh->headers, name, value);
962 return 0;
963 }
964
965 static int
966 uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len)
967 {
968 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
969
970 if (uh->state >= HTTP_STATE_REQUEST_DONE)
971 return -1;
972
973 uclient_http_send_headers(uh);
974
975 if (len > 0) {
976 ustream_printf(uh->us, "%X\r\n", len);
977 ustream_write(uh->us, buf, len, false);
978 ustream_printf(uh->us, "\r\n");
979 }
980
981 return len;
982 }
983
984 static int
985 uclient_http_request_done(struct uclient *cl)
986 {
987 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
988
989 if (uh->state >= HTTP_STATE_REQUEST_DONE)
990 return -1;
991
992 uclient_http_send_headers(uh);
993 if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
994 ustream_printf(uh->us, "0\r\n\r\n");
995 uh->state = HTTP_STATE_REQUEST_DONE;
996
997 return 0;
998 }
999
1000 static int
1001 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
1002 {
1003 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1004 int read_len = 0;
1005 char *data, *data_end;
1006
1007 if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
1008 return 0;
1009
1010 data = ustream_get_read_buf(uh->us, &read_len);
1011 if (!data || !read_len)
1012 return 0;
1013
1014 data_end = data + read_len;
1015 read_len = 0;
1016
1017 if (uh->read_chunked == 0) {
1018 char *sep;
1019
1020 if (data[0] == '\r' && data[1] == '\n') {
1021 data += 2;
1022 read_len += 2;
1023 }
1024
1025 sep = strstr(data, "\r\n");
1026 if (!sep)
1027 return 0;
1028
1029 *sep = 0;
1030 uh->read_chunked = strtoul(data, NULL, 16);
1031
1032 read_len += sep + 2 - data;
1033 data = sep + 2;
1034
1035 if (!uh->read_chunked) {
1036 uh->eof = true;
1037 uh->uc.data_eof = true;
1038 }
1039 }
1040
1041 if (len > data_end - data)
1042 len = data_end - data;
1043
1044 if (uh->read_chunked >= 0) {
1045 if (len > uh->read_chunked)
1046 len = uh->read_chunked;
1047
1048 uh->read_chunked -= len;
1049 } else if (uh->content_length >= 0) {
1050 if (len > uh->content_length)
1051 len = uh->content_length;
1052
1053 uh->content_length -= len;
1054 if (!uh->content_length) {
1055 uh->eof = true;
1056 uh->uc.data_eof = true;
1057 }
1058 }
1059
1060 if (len > 0) {
1061 read_len += len;
1062 memcpy(buf, data, len);
1063 }
1064
1065 if (read_len > 0)
1066 ustream_consume(uh->us, read_len);
1067
1068 uclient_notify_eof(uh);
1069
1070 /* Now that we consumed something and if this isn't EOF, start timer again */
1071 if (!uh->uc.eof && !cl->connection_timeout.pending)
1072 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
1073
1074 return len;
1075 }
1076
1077 int uclient_http_redirect(struct uclient *cl)
1078 {
1079 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1080 struct blobmsg_policy location = {
1081 .name = "location",
1082 .type = BLOBMSG_TYPE_STRING,
1083 };
1084 struct uclient_url *url = cl->url;
1085 struct blob_attr *tb;
1086
1087 if (cl->backend != &uclient_backend_http)
1088 return false;
1089
1090 switch (cl->status_code) {
1091 case 301:
1092 case 302:
1093 case 307:
1094 break;
1095 default:
1096 return false;
1097 }
1098
1099 blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
1100 if (!tb)
1101 return false;
1102
1103 url = uclient_get_url(blobmsg_data(tb), url->auth);
1104 if (!url)
1105 return false;
1106
1107 free(cl->url);
1108 cl->url = url;
1109 if (uclient_http_connect(cl))
1110 return -1;
1111
1112 uclient_http_request_done(cl);
1113
1114 return true;
1115 }
1116
1117 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
1118 struct ustream_ssl_ctx *ctx, bool require_validation)
1119 {
1120 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1121
1122 if (cl->backend != &uclient_backend_http)
1123 return -1;
1124
1125 uclient_http_free_url_state(cl);
1126
1127 uclient_http_free_ssl_ctx(uh);
1128 uh->ssl_ops = ops;
1129 uh->ssl_ctx = ctx;
1130 uh->ssl_require_validation = !!ctx && require_validation;
1131
1132 return 0;
1133 }
1134
1135 int uclient_http_set_address_family(struct uclient *cl, int af)
1136 {
1137 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1138
1139 if (cl->backend != &uclient_backend_http)
1140 return -1;
1141
1142 switch (af) {
1143 case AF_INET:
1144 uh->usock_flags = USOCK_IPV4ONLY;
1145 break;
1146 case AF_INET6:
1147 uh->usock_flags = USOCK_IPV6ONLY;
1148 break;
1149 default:
1150 uh->usock_flags = 0;
1151 break;
1152 }
1153
1154 return 0;
1155 }
1156
1157 const struct uclient_backend uclient_backend_http = {
1158 .prefix = uclient_http_prefix,
1159
1160 .alloc = uclient_http_alloc,
1161 .free = uclient_http_free,
1162 .connect = uclient_http_connect,
1163 .disconnect = uclient_http_request_disconnect,
1164 .update_url = uclient_http_free_url_state,
1165 .update_proxy_url = uclient_http_free_url_state,
1166
1167 .read = uclient_http_read,
1168 .write = uclient_http_send_data,
1169 .request = uclient_http_request_done,
1170 };