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