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