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