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