uclient-fetch: fix inconsistencies in help text (thx, Hannu Nyman)
[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 <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 void
290 uclient_http_add_auth_basic(struct uclient_http *uh)
291 {
292         struct uclient_url *url = uh->uc.url;
293         int auth_len = strlen(url->auth);
294         char *auth_buf;
295
296         if (auth_len > 512)
297                 return;
298
299         auth_buf = alloca(base64_len(auth_len) + 1);
300         base64_encode(url->auth, auth_len, auth_buf);
301         ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
302 }
303
304 static char *digest_unquote_sep(char **str)
305 {
306         char *cur = *str + 1;
307         char *start = cur;
308         char *out;
309
310         if (**str != '"')
311                 return NULL;
312
313         out = cur;
314         while (1) {
315                 if (!*cur)
316                         return NULL;
317
318                 if (*cur == '"') {
319                         cur++;
320                         break;
321                 }
322
323                 if (*cur == '\\')
324                         cur++;
325
326                 *(out++) = *(cur++);
327         }
328
329         if (*cur == ',')
330                 cur++;
331
332         *out = 0;
333         *str = cur;
334
335         return start;
336 }
337
338 static char *digest_sep(char **str)
339 {
340         char *cur, *next;
341
342         cur = *str;
343         next = strchr(*str, ',');
344         if (next) {
345             *str = next + 1;
346             *next = 0;
347         } else {
348             *str += strlen(*str);
349         }
350
351         return cur;
352 }
353
354 static bool strmatch(char **str, const char *prefix)
355 {
356         int len = strlen(prefix);
357
358         if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
359                 return false;
360
361         *str += len + 1;
362         return true;
363 }
364
365 static void
366 get_cnonce(char *dest)
367 {
368         uint32_t val = 0;
369         FILE *f;
370
371         f = fopen("/dev/urandom", "r");
372         if (f) {
373                 fread(&val, sizeof(val), 1, f);
374                 fclose(f);
375         }
376
377         bin_to_hex(dest, &val, sizeof(val));
378 }
379
380 static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
381 {
382         int available = *len - *ofs;
383         int required;
384         const char *next;
385         char *cur;
386
387         if (*len && !*buf)
388                 return;
389
390         required = strlen(name) + 4 + strlen(val) * 2;
391         if (required > available)
392                 *len += required - available + 64;
393
394         *buf = realloc(*buf, *len);
395         if (!*buf)
396                 return;
397
398         cur = *buf + *ofs;
399         cur += sprintf(cur, ", %s=\"", name);
400
401         while ((next = strchr(val, '"'))) {
402                 if (next > val) {
403                         memcpy(cur, val, next - val);
404                         cur += next - val;
405                 }
406
407                 cur += sprintf(cur, "\\\"");
408                 val = next + 1;
409         }
410
411         cur += sprintf(cur, "%s\"", val);
412         *ofs = cur - *buf;
413 }
414
415 static void
416 uclient_http_add_auth_digest(struct uclient_http *uh)
417 {
418         struct uclient_url *url = uh->uc.url;
419         const char *realm = NULL, *opaque = NULL;
420         const char *user, *password;
421         char *buf, *next;
422         int len, ofs;
423
424         char cnonce_str[9];
425         char nc_str[9];
426         char ahash[33];
427         char hash[33];
428
429         struct http_digest_data data = {
430                 .nc = nc_str,
431                 .cnonce = cnonce_str,
432                 .auth_hash = ahash,
433         };
434
435         len = strlen(uh->auth_str) + 1;
436         if (len > 512)
437                 return;
438
439         buf = alloca(len);
440         strcpy(buf, uh->auth_str);
441
442         /* skip auth type */
443         strsep(&buf, " ");
444
445         next = buf;
446         while (*next) {
447                 const char **dest = NULL;
448                 const char *tmp;
449
450                 while (*next && isspace(*next))
451                         next++;
452
453                 if (strmatch(&next, "realm"))
454                         dest = &realm;
455                 else if (strmatch(&next, "qop"))
456                         dest = &data.qop;
457                 else if (strmatch(&next, "nonce"))
458                         dest = &data.nonce;
459                 else if (strmatch(&next, "opaque"))
460                         dest = &opaque;
461                 else if (strmatch(&next, "stale") ||
462                          strmatch(&next, "algorithm") ||
463                          strmatch(&next, "auth-param")) {
464                         digest_sep(&next);
465                         continue;
466                 } else if (strmatch(&next, "domain") ||
467                          strmatch(&next, "qop-options"))
468                         dest = &tmp;
469                 else {
470                         digest_sep(&next);
471                         continue;
472                 }
473
474                 *dest = digest_unquote_sep(&next);
475         }
476
477         if (!realm || !data.qop || !data.nonce)
478                 return;
479
480         sprintf(nc_str, "%08x", uh->nc++);
481         get_cnonce(cnonce_str);
482
483         data.qop = "auth";
484         data.uri = url->location;
485         data.method = request_types[uh->req_type];
486
487         password = strchr(url->auth, ':');
488         if (password) {
489                 char *user_buf;
490
491                 len = password - url->auth;
492                 if (len > 256)
493                         return;
494
495                 user_buf = alloca(len + 1);
496                 strncpy(user_buf, url->auth, len);
497                 user_buf[len] = 0;
498                 user = user_buf;
499                 password++;
500         } else {
501                 user = url->auth;
502                 password = "";
503         }
504
505         http_digest_calculate_auth_hash(ahash, user, realm, password);
506         http_digest_calculate_response(hash, &data);
507
508         buf = NULL;
509         len = 0;
510         ofs = 0;
511
512         add_field(&buf, &ofs, &len, "username", user);
513         add_field(&buf, &ofs, &len, "realm", realm);
514         add_field(&buf, &ofs, &len, "nonce", data.nonce);
515         add_field(&buf, &ofs, &len, "uri", data.uri);
516         add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
517         add_field(&buf, &ofs, &len, "response", hash);
518         if (opaque)
519                 add_field(&buf, &ofs, &len, "opaque", opaque);
520
521         ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
522         free(buf);
523 }
524
525 static void
526 uclient_http_add_auth_header(struct uclient_http *uh)
527 {
528         if (!uh->uc.url->auth)
529                 return;
530
531         switch (uh->auth_type) {
532         case AUTH_TYPE_UNKNOWN:
533         case AUTH_TYPE_NONE:
534                 break;
535         case AUTH_TYPE_BASIC:
536                 uclient_http_add_auth_basic(uh);
537                 break;
538         case AUTH_TYPE_DIGEST:
539                 uclient_http_add_auth_digest(uh);
540                 break;
541         }
542 }
543
544 static void
545 uclient_http_send_headers(struct uclient_http *uh)
546 {
547         struct uclient_url *url = uh->uc.url;
548         struct blob_attr *cur;
549         enum request_type req_type = uh->req_type;
550         int rem;
551
552         if (uh->state >= HTTP_STATE_HEADERS_SENT)
553                 return;
554
555         if (uh->uc.proxy_url)
556                 url = uh->uc.proxy_url;
557
558         ustream_printf(uh->us,
559                 "%s %s HTTP/1.1\r\n"
560                 "Host: %s\r\n",
561                 request_types[req_type],
562                 url->location, url->host);
563
564         blobmsg_for_each_attr(cur, uh->headers.head, rem)
565                 ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
566
567         if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
568                 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
569
570         uclient_http_add_auth_header(uh);
571
572         ustream_printf(uh->us, "\r\n");
573
574         uh->state = HTTP_STATE_HEADERS_SENT;
575 }
576
577 static void uclient_http_headers_complete(struct uclient_http *uh)
578 {
579         enum auth_type auth_type = uh->auth_type;
580         int seq = uh->uc.seq;
581
582         uh->state = HTTP_STATE_RECV_DATA;
583         uh->uc.meta = uh->meta.head;
584         uclient_http_process_headers(uh);
585
586         if (auth_type == AUTH_TYPE_UNKNOWN && uh->uc.status_code == 401 &&
587             (uh->req_type == REQ_HEAD || uh->req_type == REQ_GET)) {
588                 uclient_http_connect(&uh->uc);
589                 uclient_http_send_headers(uh);
590                 uh->state = HTTP_STATE_REQUEST_DONE;
591                 return;
592         }
593
594         if (uh->uc.cb->header_done)
595                 uh->uc.cb->header_done(&uh->uc);
596
597         if (uh->eof || seq != uh->uc.seq)
598                 return;
599
600         if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
601                 uh->eof = true;
602                 uclient_notify_eof(uh);
603         }
604 }
605
606 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
607 {
608         char *name;
609         char *sep;
610
611         if (uh->state == HTTP_STATE_REQUEST_DONE) {
612                 char *code;
613
614                 if (!strlen(data))
615                         return;
616
617                 /* HTTP/1.1 */
618                 strsep(&data, " ");
619
620                 code = strsep(&data, " ");
621                 if (!code)
622                         goto error;
623
624                 uh->uc.status_code = strtoul(code, &sep, 10);
625                 if (sep && *sep)
626                         goto error;
627
628                 uh->state = HTTP_STATE_RECV_HEADERS;
629                 return;
630         }
631
632         if (!*data) {
633                 uclient_http_headers_complete(uh);
634                 return;
635         }
636
637         sep = strchr(data, ':');
638         if (!sep)
639                 return;
640
641         *(sep++) = 0;
642
643         for (name = data; *name; name++)
644                 *name = tolower(*name);
645
646         name = data;
647         while (isspace(*sep))
648                 sep++;
649
650         blobmsg_add_string(&uh->meta, name, sep);
651         return;
652
653 error:
654         uh->uc.status_code = 400;
655         uh->eof = true;
656         uclient_notify_eof(uh);
657 }
658
659 static void __uclient_notify_read(struct uclient_http *uh)
660 {
661         struct uclient *uc = &uh->uc;
662         unsigned int seq = uh->seq;
663         char *data;
664         int len;
665
666         if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
667                 return;
668
669         data = ustream_get_read_buf(uh->us, &len);
670         if (!data || !len)
671                 return;
672
673         if (uh->state < HTTP_STATE_RECV_DATA) {
674                 char *sep, *next;
675                 int cur_len;
676
677                 do {
678                         sep = strchr(data, '\n');
679                         if (!sep)
680                                 break;
681
682                         next = sep + 1;
683                         if (sep > data && sep[-1] == '\r')
684                                 sep--;
685
686                         /* Check for multi-line HTTP headers */
687                         if (sep > data) {
688                                 if (!*next)
689                                         return;
690
691                                 if (isspace(*next) && *next != '\r' && *next != '\n') {
692                                         sep[0] = ' ';
693                                         if (sep + 1 < next)
694                                                 sep[1] = ' ';
695                                         continue;
696                                 }
697                         }
698
699                         *sep = 0;
700                         cur_len = next - data;
701                         uclient_parse_http_line(uh, data);
702                         if (seq != uh->seq)
703                                 return;
704
705                         ustream_consume(uh->us, cur_len);
706                         len -= cur_len;
707
708                         if (uh->eof)
709                                 return;
710
711                         data = ustream_get_read_buf(uh->us, &len);
712                 } while (data && uh->state < HTTP_STATE_RECV_DATA);
713
714                 if (!len)
715                         return;
716         }
717
718         if (uh->eof)
719                 return;
720
721         if (uh->state == HTTP_STATE_RECV_DATA) {
722                 /* Now it's uclient user turn to read some data */
723                 uloop_timeout_cancel(&uc->connection_timeout);
724
725                 if (uc->cb->data_read)
726                         uc->cb->data_read(uc);
727         }
728 }
729
730 static void __uclient_notify_write(struct uclient_http *uh)
731 {
732         struct uclient *uc = &uh->uc;
733
734         if (uc->cb->data_sent)
735                 uc->cb->data_sent(uc);
736 }
737
738 static void uclient_notify_read(struct ustream *us, int bytes)
739 {
740         struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
741
742         __uclient_notify_read(uh);
743 }
744
745 static void uclient_notify_write(struct ustream *us, int bytes)
746 {
747         struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
748
749         __uclient_notify_write(uh);
750 }
751
752 static void uclient_notify_state(struct ustream *us)
753 {
754         struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
755
756         if (uh->ufd.stream.write_error) {
757                 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
758                 return;
759         }
760         uclient_notify_eof(uh);
761 }
762
763 static int uclient_setup_http(struct uclient_http *uh)
764 {
765         struct ustream *us = &uh->ufd.stream;
766         int ret;
767
768         uh->us = us;
769         uh->ssl = false;
770
771         us->string_data = true;
772         us->notify_state = uclient_notify_state;
773         us->notify_read = uclient_notify_read;
774         us->notify_write = uclient_notify_write;
775
776         ret = uclient_do_connect(uh, "80");
777         if (ret)
778                 return UCLIENT_ERROR_CONNECT;
779
780         return 0;
781 }
782
783 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
784 {
785         struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
786
787         __uclient_notify_read(uh);
788 }
789
790 static void uclient_ssl_notify_write(struct ustream *us, int bytes)
791 {
792         struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
793
794         __uclient_notify_write(uh);
795 }
796
797 static void uclient_ssl_notify_state(struct ustream *us)
798 {
799         struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
800
801         uclient_notify_eof(uh);
802 }
803
804 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
805 {
806         struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
807
808         uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
809 }
810
811 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
812 {
813         struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
814
815         if (!uh->ssl_require_validation)
816                 return;
817
818         uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
819 }
820
821 static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
822 {
823         struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
824
825         if (!uh->ssl_require_validation)
826                 return;
827
828         if (!uh->ussl.valid_cn)
829                 uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
830 }
831
832 static int uclient_setup_https(struct uclient_http *uh)
833 {
834         struct ustream *us = &uh->ussl.stream;
835         int ret;
836
837         uh->ssl = true;
838         uh->us = us;
839
840         if (!uh->ssl_ctx)
841                 return UCLIENT_ERROR_MISSING_SSL_CONTEXT;
842
843         ret = uclient_do_connect(uh, "443");
844         if (ret)
845                 return UCLIENT_ERROR_CONNECT;
846
847         us->string_data = true;
848         us->notify_state = uclient_ssl_notify_state;
849         us->notify_read = uclient_ssl_notify_read;
850         us->notify_write = uclient_ssl_notify_write;
851         uh->ussl.notify_error = uclient_ssl_notify_error;
852         uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
853         uh->ussl.notify_connected = uclient_ssl_notify_connected;
854         uh->ussl.server_name = uh->uc.url->host;
855         uh->ssl_ops->init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
856         uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host);
857
858         return 0;
859 }
860
861 static int uclient_http_connect(struct uclient *cl)
862 {
863         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
864         int ret;
865
866         if (!cl->eof || uh->disconnect || uh->connection_close)
867                 uclient_http_disconnect(uh);
868
869         uclient_http_init_request(uh);
870
871         if (uh->us)
872                 return 0;
873
874         uh->ssl = cl->url->prefix == PREFIX_HTTPS;
875
876         if (uh->ssl)
877                 ret = uclient_setup_https(uh);
878         else
879                 ret = uclient_setup_http(uh);
880
881         return ret;
882 }
883
884 static void uclient_http_disconnect_cb(struct uloop_timeout *timeout)
885 {
886         struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t);
887
888         uclient_http_disconnect(uh);
889 }
890
891 static struct uclient *uclient_http_alloc(void)
892 {
893         struct uclient_http *uh;
894
895         uh = calloc_a(sizeof(*uh));
896         uh->disconnect_t.cb = uclient_http_disconnect_cb;
897         blob_buf_init(&uh->headers, 0);
898
899         return &uh->uc;
900 }
901
902 static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
903 {
904         uh->ssl_ops = NULL;
905         uh->ssl_ctx = NULL;
906 }
907
908 static void uclient_http_free(struct uclient *cl)
909 {
910         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
911
912         uclient_http_free_url_state(cl);
913         uclient_http_free_ssl_ctx(uh);
914         blob_buf_free(&uh->headers);
915         blob_buf_free(&uh->meta);
916         free(uh);
917 }
918
919 int
920 uclient_http_set_request_type(struct uclient *cl, const char *type)
921 {
922         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
923         int i;
924
925         if (cl->backend != &uclient_backend_http)
926                 return -1;
927
928         if (uh->state > HTTP_STATE_INIT)
929                 return -1;
930
931         for (i = 0; i < ARRAY_SIZE(request_types); i++) {
932                 if (strcmp(request_types[i], type) != 0)
933                         continue;
934
935                 uh->req_type = i;
936                 return 0;
937         }
938
939         return -1;
940 }
941
942 int
943 uclient_http_reset_headers(struct uclient *cl)
944 {
945         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
946
947         blob_buf_init(&uh->headers, 0);
948
949         return 0;
950 }
951
952 int
953 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
954 {
955         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
956
957         if (cl->backend != &uclient_backend_http)
958                 return -1;
959
960         if (uh->state > HTTP_STATE_INIT)
961                 return -1;
962
963         blobmsg_add_string(&uh->headers, name, value);
964         return 0;
965 }
966
967 static int
968 uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len)
969 {
970         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
971
972         if (uh->state >= HTTP_STATE_REQUEST_DONE)
973                 return -1;
974
975         uclient_http_send_headers(uh);
976
977         if (len > 0) {
978                 ustream_printf(uh->us, "%X\r\n", len);
979                 ustream_write(uh->us, buf, len, false);
980                 ustream_printf(uh->us, "\r\n");
981         }
982
983         return len;
984 }
985
986 static int
987 uclient_http_request_done(struct uclient *cl)
988 {
989         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
990
991         if (uh->state >= HTTP_STATE_REQUEST_DONE)
992                 return -1;
993
994         uclient_http_send_headers(uh);
995         if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
996                 ustream_printf(uh->us, "0\r\n\r\n");
997         uh->state = HTTP_STATE_REQUEST_DONE;
998
999         return 0;
1000 }
1001
1002 static int
1003 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
1004 {
1005         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1006         int read_len = 0;
1007         char *data, *data_end;
1008
1009         if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
1010                 return 0;
1011
1012         data = ustream_get_read_buf(uh->us, &read_len);
1013         if (!data || !read_len)
1014                 return 0;
1015
1016         data_end = data + read_len;
1017         read_len = 0;
1018
1019         if (uh->read_chunked == 0) {
1020                 char *sep;
1021
1022                 if (data[0] == '\r' && data[1] == '\n') {
1023                         data += 2;
1024                         read_len += 2;
1025                 }
1026
1027                 sep = strstr(data, "\r\n");
1028                 if (!sep)
1029                         return 0;
1030
1031                 *sep = 0;
1032                 uh->read_chunked = strtoul(data, NULL, 16);
1033
1034                 read_len += sep + 2 - data;
1035                 data = sep + 2;
1036
1037                 if (!uh->read_chunked) {
1038                         uh->eof = true;
1039                         uh->uc.data_eof = true;
1040                 }
1041         }
1042
1043         if (len > data_end - data)
1044                 len = data_end - data;
1045
1046         if (uh->read_chunked >= 0) {
1047                 if (len > uh->read_chunked)
1048                         len = uh->read_chunked;
1049
1050                 uh->read_chunked -= len;
1051         } else if (uh->content_length >= 0) {
1052                 if (len > uh->content_length)
1053                         len = uh->content_length;
1054
1055                 uh->content_length -= len;
1056                 if (!uh->content_length) {
1057                         uh->eof = true;
1058                         uh->uc.data_eof = true;
1059                 }
1060         }
1061
1062         if (len > 0) {
1063                 read_len += len;
1064                 memcpy(buf, data, len);
1065         }
1066
1067         if (read_len > 0)
1068                 ustream_consume(uh->us, read_len);
1069
1070         uclient_notify_eof(uh);
1071
1072         /* Now that we consumed something and if this isn't EOF, start timer again */
1073         if (!uh->uc.eof && !cl->connection_timeout.pending)
1074                 uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
1075
1076         return len;
1077 }
1078
1079 int uclient_http_redirect(struct uclient *cl)
1080 {
1081         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1082         struct blobmsg_policy location = {
1083                 .name = "location",
1084                 .type = BLOBMSG_TYPE_STRING,
1085         };
1086         struct uclient_url *url = cl->url;
1087         struct blob_attr *tb;
1088
1089         if (cl->backend != &uclient_backend_http)
1090                 return false;
1091
1092         switch (cl->status_code) {
1093         case 301:
1094         case 302:
1095         case 307:
1096                 break;
1097         default:
1098                 return false;
1099         }
1100
1101         blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
1102         if (!tb)
1103                 return false;
1104
1105         url = uclient_get_url(blobmsg_data(tb), url->auth);
1106         if (!url)
1107                 return false;
1108
1109         free(cl->url);
1110         cl->url = url;
1111         if (uclient_http_connect(cl))
1112                 return -1;
1113
1114         uclient_http_request_done(cl);
1115
1116         return true;
1117 }
1118
1119 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
1120                              struct ustream_ssl_ctx *ctx, bool require_validation)
1121 {
1122         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1123
1124         if (cl->backend != &uclient_backend_http)
1125                 return -1;
1126
1127         uclient_http_free_url_state(cl);
1128
1129         uclient_http_free_ssl_ctx(uh);
1130         uh->ssl_ops = ops;
1131         uh->ssl_ctx = ctx;
1132         uh->ssl_require_validation = !!ctx && require_validation;
1133
1134         return 0;
1135 }
1136
1137 int uclient_http_set_address_family(struct uclient *cl, int af)
1138 {
1139         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1140
1141         if (cl->backend != &uclient_backend_http)
1142                 return -1;
1143
1144         switch (af) {
1145         case AF_INET:
1146                 uh->usock_flags = USOCK_IPV4ONLY;
1147                 break;
1148         case AF_INET6:
1149                 uh->usock_flags = USOCK_IPV6ONLY;
1150                 break;
1151         default:
1152                 uh->usock_flags = 0;
1153                 break;
1154         }
1155
1156         return 0;
1157 }
1158
1159 const struct uclient_backend uclient_backend_http = {
1160         .prefix = uclient_http_prefix,
1161
1162         .alloc = uclient_http_alloc,
1163         .free = uclient_http_free,
1164         .connect = uclient_http_connect,
1165         .disconnect = uclient_http_request_disconnect,
1166         .update_url = uclient_http_free_url_state,
1167         .update_proxy_url = uclient_http_free_url_state,
1168
1169         .read = uclient_http_read,
1170         .write = uclient_http_send_data,
1171         .request = uclient_http_request_done,
1172 };