2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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.
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.
24 #include <libubox/ustream.h>
25 #include <libubox/ustream-ssl.h>
26 #include <libubox/usock.h>
27 #include <libubox/blobmsg.h>
30 #include "uclient-utils.h"
31 #include "uclient-backend.h"
51 HTTP_STATE_HEADERS_SENT,
52 HTTP_STATE_REQUEST_DONE,
53 HTTP_STATE_RECV_HEADERS,
58 static const char * const request_types[__REQ_MAX] = {
63 [REQ_DELETE] = "DELETE",
69 const struct ustream_ssl_ops *ssl_ops;
70 struct ustream_ssl_ctx *ssl_ctx;
73 struct ustream_fd ufd;
74 struct ustream_ssl ussl;
76 struct uloop_timeout disconnect_t;
79 bool ssl_require_validation;
82 bool connection_close;
84 enum request_type req_type;
85 enum http_state state;
87 enum auth_type auth_type;
95 struct blob_buf headers;
105 static const char * const uclient_http_prefix[] = {
106 [PREFIX_HTTP] = "http://",
107 [PREFIX_HTTPS] = "https://",
108 [__PREFIX_MAX] = NULL
111 static int uclient_http_connect(struct uclient *cl);
113 static int uclient_do_connect(struct uclient_http *uh, const char *port)
118 if (uh->uc.url->port)
119 port = uh->uc.url->port;
121 memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
123 fd = usock_inet(USOCK_TCP, uh->uc.url->host, port, &uh->uc.remote_addr);
127 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
128 ustream_fd_init(&uh->ufd, fd);
130 sl = sizeof(uh->uc.local_addr);
131 memset(&uh->uc.local_addr, 0, sl);
132 getsockname(fd, &uh->uc.local_addr.sa, &sl);
137 static void uclient_http_disconnect(struct uclient_http *uh)
139 uloop_timeout_cancel(&uh->disconnect_t);
144 ustream_free(&uh->ussl.stream);
145 ustream_free(&uh->ufd.stream);
146 close(uh->ufd.fd.fd);
150 static void uclient_http_free_url_state(struct uclient *cl)
152 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
154 uh->auth_type = AUTH_TYPE_UNKNOWN;
157 uclient_http_disconnect(uh);
160 static void uclient_http_error(struct uclient_http *uh, int code)
162 uh->state = HTTP_STATE_ERROR;
164 ustream_state_change(uh->us);
165 uclient_backend_set_error(&uh->uc, code);
168 static void uclient_http_request_disconnect(struct uclient *cl)
170 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
176 uh->disconnect = true;
177 uloop_timeout_set(&uh->disconnect_t, 1);
180 static void uclient_notify_eof(struct uclient_http *uh)
182 struct ustream *us = uh->us;
188 if (!us->eof && !us->write_error)
191 if (ustream_pending_data(us, false))
195 if (uh->content_length < 0 && uh->read_chunked >= 0)
196 uh->uc.data_eof = true;
198 uclient_backend_set_eof(&uh->uc);
200 if (uh->connection_close)
201 uclient_http_request_disconnect(&uh->uc);
204 static void uclient_http_reset_state(struct uclient_http *uh)
207 uclient_backend_reset_state(&uh->uc);
208 uh->read_chunked = -1;
209 uh->content_length = -1;
211 uh->disconnect = false;
212 uh->connection_close = false;
213 uh->state = HTTP_STATE_INIT;
215 if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
216 uh->auth_type = AUTH_TYPE_NONE;
219 static void uclient_http_init_request(struct uclient_http *uh)
222 uclient_http_reset_state(uh);
223 blob_buf_init(&uh->meta, 0);
226 static enum auth_type
227 uclient_http_update_auth_type(struct uclient_http *uh)
230 return AUTH_TYPE_NONE;
232 if (!strncasecmp(uh->auth_str, "basic", 5))
233 return AUTH_TYPE_BASIC;
235 if (!strncasecmp(uh->auth_str, "digest", 6))
236 return AUTH_TYPE_DIGEST;
238 return AUTH_TYPE_NONE;
241 static void uclient_http_process_headers(struct uclient_http *uh)
244 HTTP_HDR_TRANSFER_ENCODING,
246 HTTP_HDR_CONTENT_LENGTH,
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"),
258 struct blob_attr *tb[__HTTP_HDR_MAX];
259 struct blob_attr *cur;
261 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
263 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
264 if (cur && strstr(blobmsg_data(cur), "chunked"))
265 uh->read_chunked = 0;
267 cur = tb[HTTP_HDR_CONNECTION];
268 if (cur && strstr(blobmsg_data(cur), "close"))
269 uh->connection_close = true;
271 cur = tb[HTTP_HDR_CONTENT_LENGTH];
273 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
275 cur = tb[HTTP_HDR_AUTH];
278 uh->auth_str = strdup(blobmsg_data(cur));
281 uh->auth_type = uclient_http_update_auth_type(uh);
285 uclient_http_add_auth_basic(struct uclient_http *uh)
287 struct uclient_url *url = uh->uc.url;
288 int auth_len = strlen(url->auth);
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);
299 static char *digest_unquote_sep(char **str)
301 char *cur = *str + 1;
333 static char *digest_sep(char **str)
338 next = strchr(*str, ',');
343 *str += strlen(*str);
349 static bool strmatch(char **str, const char *prefix)
351 int len = strlen(prefix);
353 if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
361 get_cnonce(char *dest)
366 f = fopen("/dev/urandom", "r");
368 fread(&val, sizeof(val), 1, f);
372 bin_to_hex(dest, &val, sizeof(val));
375 static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
377 int available = *len - *ofs;
385 required = strlen(name) + 4 + strlen(val) * 2;
386 if (required > available)
387 *len += required - available + 64;
389 *buf = realloc(*buf, *len);
394 cur += sprintf(cur, ", %s=\"", name);
396 while ((next = strchr(val, '"'))) {
398 memcpy(cur, val, next - val);
402 cur += sprintf(cur, "\\\"");
406 cur += sprintf(cur, "%s\"", val);
411 uclient_http_add_auth_digest(struct uclient_http *uh)
413 struct uclient_url *url = uh->uc.url;
414 const char *realm = NULL, *opaque = NULL;
415 const char *user, *password;
424 struct http_digest_data data = {
426 .cnonce = cnonce_str,
430 len = strlen(uh->auth_str) + 1;
435 strcpy(buf, uh->auth_str);
442 const char **dest = NULL;
445 while (*next && isspace(*next))
448 if (strmatch(&next, "realm"))
450 else if (strmatch(&next, "qop"))
452 else if (strmatch(&next, "nonce"))
454 else if (strmatch(&next, "opaque"))
456 else if (strmatch(&next, "stale") ||
457 strmatch(&next, "algorithm") ||
458 strmatch(&next, "auth-param")) {
461 } else if (strmatch(&next, "domain") ||
462 strmatch(&next, "qop-options"))
469 *dest = digest_unquote_sep(&next);
472 if (!realm || !data.qop || !data.nonce)
475 sprintf(nc_str, "%08x", uh->nc++);
476 get_cnonce(cnonce_str);
479 data.uri = url->location;
480 data.method = request_types[uh->req_type];
482 password = strchr(url->auth, ':');
486 len = password - url->auth;
490 user_buf = alloca(len + 1);
491 strncpy(user_buf, url->auth, len);
500 http_digest_calculate_auth_hash(ahash, user, realm, password);
501 http_digest_calculate_response(hash, &data);
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);
514 add_field(&buf, &ofs, &len, "opaque", opaque);
516 ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
521 uclient_http_add_auth_header(struct uclient_http *uh)
523 if (!uh->uc.url->auth)
526 switch (uh->auth_type) {
527 case AUTH_TYPE_UNKNOWN:
530 case AUTH_TYPE_BASIC:
531 uclient_http_add_auth_basic(uh);
533 case AUTH_TYPE_DIGEST:
534 uclient_http_add_auth_digest(uh);
540 uclient_http_send_headers(struct uclient_http *uh)
542 struct uclient_url *url = uh->uc.url;
543 struct blob_attr *cur;
544 enum request_type req_type = uh->req_type;
547 if (uh->state >= HTTP_STATE_HEADERS_SENT)
550 if (uh->uc.proxy_url)
551 url = uh->uc.proxy_url;
553 ustream_printf(uh->us,
556 request_types[req_type],
557 url->location, url->host);
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));
562 if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
563 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
565 uclient_http_add_auth_header(uh);
567 ustream_printf(uh->us, "\r\n");
569 uh->state = HTTP_STATE_HEADERS_SENT;
572 static void uclient_http_headers_complete(struct uclient_http *uh)
574 enum auth_type auth_type = uh->auth_type;
575 int seq = uh->uc.seq;
577 uh->state = HTTP_STATE_RECV_DATA;
578 uh->uc.meta = uh->meta.head;
579 uclient_http_process_headers(uh);
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;
589 if (uh->uc.cb->header_done)
590 uh->uc.cb->header_done(&uh->uc);
592 if (uh->eof || seq != uh->uc.seq)
595 if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
597 uclient_notify_eof(uh);
601 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
606 if (uh->state == HTTP_STATE_REQUEST_DONE) {
615 code = strsep(&data, " ");
619 uh->uc.status_code = strtoul(code, &sep, 10);
623 uh->state = HTTP_STATE_RECV_HEADERS;
628 uclient_http_headers_complete(uh);
632 sep = strchr(data, ':');
638 for (name = data; *name; name++)
639 *name = tolower(*name);
642 while (isspace(*sep))
645 blobmsg_add_string(&uh->meta, name, sep);
649 uh->uc.status_code = 400;
651 uclient_notify_eof(uh);
654 static void __uclient_notify_read(struct uclient_http *uh)
656 struct uclient *uc = &uh->uc;
657 unsigned int seq = uh->seq;
661 if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
664 data = ustream_get_read_buf(uh->us, &len);
668 if (uh->state < HTTP_STATE_RECV_DATA) {
673 sep = strchr(data, '\n');
678 if (sep > data && sep[-1] == '\r')
681 /* Check for multi-line HTTP headers */
686 if (isspace(*next) && *next != '\r' && *next != '\n') {
695 cur_len = next - data;
696 uclient_parse_http_line(uh, data);
700 ustream_consume(uh->us, cur_len);
706 data = ustream_get_read_buf(uh->us, &len);
707 } while (data && uh->state < HTTP_STATE_RECV_DATA);
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);
720 if (uc->cb->data_read)
721 uc->cb->data_read(uc);
725 static void __uclient_notify_write(struct uclient_http *uh)
727 struct uclient *uc = &uh->uc;
729 if (uc->cb->data_sent)
730 uc->cb->data_sent(uc);
733 static void uclient_notify_read(struct ustream *us, int bytes)
735 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
737 __uclient_notify_read(uh);
740 static void uclient_notify_write(struct ustream *us, int bytes)
742 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
744 __uclient_notify_write(uh);
747 static void uclient_notify_state(struct ustream *us)
749 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
751 if (uh->ufd.stream.write_error) {
752 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
755 uclient_notify_eof(uh);
758 static int uclient_setup_http(struct uclient_http *uh)
760 struct ustream *us = &uh->ufd.stream;
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;
771 ret = uclient_do_connect(uh, "80");
773 return UCLIENT_ERROR_CONNECT;
778 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
780 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
782 __uclient_notify_read(uh);
785 static void uclient_ssl_notify_write(struct ustream *us, int bytes)
787 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
789 __uclient_notify_write(uh);
792 static void uclient_ssl_notify_state(struct ustream *us)
794 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
796 uclient_notify_eof(uh);
799 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
801 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
803 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
806 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
808 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
810 if (!uh->ssl_require_validation)
813 uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
816 static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
818 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
820 if (!uh->ssl_require_validation)
823 if (!uh->ussl.valid_cn)
824 uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
827 static int uclient_setup_https(struct uclient_http *uh)
829 struct ustream *us = &uh->ussl.stream;
836 return UCLIENT_ERROR_MISSING_SSL_CONTEXT;
838 ret = uclient_do_connect(uh, "443");
840 return UCLIENT_ERROR_CONNECT;
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);
856 static int uclient_http_connect(struct uclient *cl)
858 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
861 if (!cl->eof || uh->disconnect || uh->connection_close)
862 uclient_http_disconnect(uh);
864 uclient_http_init_request(uh);
869 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
872 ret = uclient_setup_https(uh);
874 ret = uclient_setup_http(uh);
879 static void uclient_http_disconnect_cb(struct uloop_timeout *timeout)
881 struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t);
883 uclient_http_disconnect(uh);
886 static struct uclient *uclient_http_alloc(void)
888 struct uclient_http *uh;
890 uh = calloc_a(sizeof(*uh));
891 uh->disconnect_t.cb = uclient_http_disconnect_cb;
892 blob_buf_init(&uh->headers, 0);
897 static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
903 static void uclient_http_free(struct uclient *cl)
905 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
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);
915 uclient_http_set_request_type(struct uclient *cl, const char *type)
917 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
920 if (cl->backend != &uclient_backend_http)
923 if (uh->state > HTTP_STATE_INIT)
926 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
927 if (strcmp(request_types[i], type) != 0)
938 uclient_http_reset_headers(struct uclient *cl)
940 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
942 blob_buf_init(&uh->headers, 0);
948 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
950 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
952 if (cl->backend != &uclient_backend_http)
955 if (uh->state > HTTP_STATE_INIT)
958 blobmsg_add_string(&uh->headers, name, value);
963 uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len)
965 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
967 if (uh->state >= HTTP_STATE_REQUEST_DONE)
970 uclient_http_send_headers(uh);
973 ustream_printf(uh->us, "%X\r\n", len);
974 ustream_write(uh->us, buf, len, false);
975 ustream_printf(uh->us, "\r\n");
982 uclient_http_request_done(struct uclient *cl)
984 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
986 if (uh->state >= HTTP_STATE_REQUEST_DONE)
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;
998 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
1000 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1002 char *data, *data_end;
1004 if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
1007 data = ustream_get_read_buf(uh->us, &read_len);
1008 if (!data || !read_len)
1011 data_end = data + read_len;
1014 if (uh->read_chunked == 0) {
1017 if (data[0] == '\r' && data[1] == '\n') {
1022 sep = strstr(data, "\r\n");
1027 uh->read_chunked = strtoul(data, NULL, 16);
1029 read_len += sep + 2 - data;
1032 if (!uh->read_chunked) {
1034 uh->uc.data_eof = true;
1038 if (len > data_end - data)
1039 len = data_end - data;
1041 if (uh->read_chunked >= 0) {
1042 if (len > uh->read_chunked)
1043 len = uh->read_chunked;
1045 uh->read_chunked -= len;
1046 } else if (uh->content_length >= 0) {
1047 if (len > uh->content_length)
1048 len = uh->content_length;
1050 uh->content_length -= len;
1051 if (!uh->content_length) {
1053 uh->uc.data_eof = true;
1059 memcpy(buf, data, len);
1063 ustream_consume(uh->us, read_len);
1065 uclient_notify_eof(uh);
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);
1074 int uclient_http_redirect(struct uclient *cl)
1076 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1077 struct blobmsg_policy location = {
1079 .type = BLOBMSG_TYPE_STRING,
1081 struct uclient_url *url = cl->url;
1082 struct blob_attr *tb;
1084 if (cl->backend != &uclient_backend_http)
1087 switch (cl->status_code) {
1096 blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
1100 url = uclient_get_url(blobmsg_data(tb), url->auth);
1106 if (uclient_http_connect(cl))
1109 uclient_http_request_done(cl);
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)
1117 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1119 if (cl->backend != &uclient_backend_http)
1122 uclient_http_free_url_state(cl);
1124 uclient_http_free_ssl_ctx(uh);
1127 uh->ssl_require_validation = !!ctx && require_validation;
1132 const struct uclient_backend uclient_backend_http = {
1133 .prefix = uclient_http_prefix,
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,
1142 .read = uclient_http_read,
1143 .write = uclient_http_send_data,
1144 .request = uclient_http_request_done,