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