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.
23 #include <libubox/ustream.h>
24 #include <libubox/ustream-ssl.h>
25 #include <libubox/usock.h>
26 #include <libubox/blobmsg.h>
29 #include "uclient-utils.h"
30 #include "uclient-backend.h"
48 HTTP_STATE_HEADERS_SENT,
49 HTTP_STATE_REQUEST_DONE,
50 HTTP_STATE_RECV_HEADERS,
55 static const char * const request_types[__REQ_MAX] = {
64 const struct ustream_ssl_ops *ssl_ops;
65 struct ustream_ssl_ctx *ssl_ctx;
68 struct ustream_fd ufd;
69 struct ustream_ssl ussl;
71 struct uloop_timeout disconnect_t;
73 bool ssl_require_validation;
76 bool connection_close;
78 enum request_type req_type;
79 enum http_state state;
81 enum auth_type auth_type;
89 struct blob_buf headers;
99 static const char * const uclient_http_prefix[] = {
100 [PREFIX_HTTP] = "http://",
101 [PREFIX_HTTPS] = "https://",
102 [__PREFIX_MAX] = NULL
105 static int uclient_do_connect(struct uclient_http *uh, const char *port)
110 if (uh->uc.url->port)
111 port = uh->uc.url->port;
113 fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
117 ustream_fd_init(&uh->ufd, fd);
119 memset(&uh->uc.local_addr, 0, sizeof(uh->uc.local_addr));
120 memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
122 sl = sizeof(uh->uc.local_addr);
123 getsockname(fd, &uh->uc.local_addr.sa, &sl);
124 getpeername(fd, &uh->uc.remote_addr.sa, &sl);
129 static void uclient_http_disconnect(struct uclient_http *uh)
131 uloop_timeout_cancel(&uh->disconnect_t);
136 ustream_free(&uh->ussl.stream);
137 ustream_free(&uh->ufd.stream);
138 close(uh->ufd.fd.fd);
142 static void uclient_http_free_url_state(struct uclient *cl)
144 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
146 uh->auth_type = AUTH_TYPE_UNKNOWN;
149 uclient_http_disconnect(uh);
152 static void uclient_http_error(struct uclient_http *uh, int code)
154 uh->state = HTTP_STATE_ERROR;
156 ustream_state_change(uh->us);
157 uclient_backend_set_error(&uh->uc, code);
160 static void uclient_notify_eof(struct uclient_http *uh)
162 struct ustream *us = uh->us;
168 if (!us->eof && !us->write_error)
171 if (ustream_pending_data(us, false))
175 uclient_backend_set_eof(&uh->uc);
177 if (uh->connection_close)
178 uclient_http_disconnect(uh);
181 static void uclient_http_reset_state(struct uclient_http *uh)
183 uclient_backend_reset_state(&uh->uc);
184 uh->read_chunked = -1;
185 uh->content_length = -1;
187 uh->disconnect = false;
188 uh->connection_close = false;
189 uh->state = HTTP_STATE_INIT;
191 if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
192 uh->auth_type = AUTH_TYPE_NONE;
195 static void uclient_http_init_request(struct uclient_http *uh)
197 uclient_http_reset_state(uh);
198 blob_buf_init(&uh->meta, 0);
201 static enum auth_type
202 uclient_http_update_auth_type(struct uclient_http *uh)
205 return AUTH_TYPE_NONE;
207 if (!strncasecmp(uh->auth_str, "basic", 5))
208 return AUTH_TYPE_BASIC;
210 if (!strncasecmp(uh->auth_str, "digest", 6))
211 return AUTH_TYPE_DIGEST;
213 return AUTH_TYPE_NONE;
216 static void uclient_http_process_headers(struct uclient_http *uh)
219 HTTP_HDR_TRANSFER_ENCODING,
221 HTTP_HDR_CONTENT_LENGTH,
225 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
226 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
227 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
228 [HTTP_HDR_CONNECTION] = hdr("connection"),
229 [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
230 [HTTP_HDR_AUTH] = hdr("www-authenticate"),
233 struct blob_attr *tb[__HTTP_HDR_MAX];
234 struct blob_attr *cur;
236 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
238 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
239 if (cur && strstr(blobmsg_data(cur), "chunked"))
240 uh->read_chunked = 0;
242 cur = tb[HTTP_HDR_CONNECTION];
243 if (cur && strstr(blobmsg_data(cur), "close"))
244 uh->connection_close = true;
246 cur = tb[HTTP_HDR_CONTENT_LENGTH];
248 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
250 cur = tb[HTTP_HDR_AUTH];
253 uh->auth_str = strdup(blobmsg_data(cur));
256 uh->auth_type = uclient_http_update_auth_type(uh);
260 uclient_http_add_auth_basic(struct uclient_http *uh)
262 struct uclient_url *url = uh->uc.url;
263 int auth_len = strlen(url->auth);
269 auth_buf = alloca(base64_len(auth_len) + 1);
270 base64_encode(url->auth, auth_len, auth_buf);
271 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
274 static char *digest_unquote_sep(char **str)
276 char *cur = *str + 1;
308 static bool strmatch(char **str, const char *prefix)
310 int len = strlen(prefix);
312 if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
320 get_cnonce(char *dest)
325 f = fopen("/dev/urandom", "r");
327 fread(&val, sizeof(val), 1, f);
331 bin_to_hex(dest, &val, sizeof(val));
334 static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
336 int available = *len - *ofs;
344 required = strlen(name) + 4 + strlen(val) * 2;
345 if (required > available)
346 *len += required - available + 64;
348 *buf = realloc(*buf, *len);
353 cur += sprintf(cur, ", %s=\"", name);
355 while ((next = strchr(val, '"'))) {
357 memcpy(cur, val, next - val);
361 cur += sprintf(cur, "\\\"");
365 cur += sprintf(cur, "%s\"", val);
370 uclient_http_add_auth_digest(struct uclient_http *uh)
372 struct uclient_url *url = uh->uc.url;
373 const char *realm = NULL, *opaque = NULL;
374 const char *user, *password;
383 struct http_digest_data data = {
385 .cnonce = cnonce_str,
389 len = strlen(uh->auth_str) + 1;
394 strcpy(buf, uh->auth_str);
401 const char **dest = NULL;
403 while (isspace(*next))
406 if (strmatch(&next, "realm"))
408 else if (strmatch(&next, "qop"))
410 else if (strmatch(&next, "nonce"))
412 else if (strmatch(&next, "opaque"))
417 *dest = digest_unquote_sep(&next);
420 if (!realm || !data.qop || !data.nonce)
423 sprintf(nc_str, "%08x", uh->nc++);
424 get_cnonce(cnonce_str);
427 data.uri = url->location;
428 data.method = request_types[uh->req_type];
430 password = strchr(url->auth, ':');
434 len = password - url->auth;
438 user_buf = alloca(len + 1);
439 strncpy(user_buf, url->auth, len);
448 http_digest_calculate_auth_hash(ahash, user, realm, password);
449 http_digest_calculate_response(hash, &data);
455 add_field(&buf, &ofs, &len, "username", user);
456 add_field(&buf, &ofs, &len, "realm", realm);
457 add_field(&buf, &ofs, &len, "nonce", data.nonce);
458 add_field(&buf, &ofs, &len, "uri", data.uri);
459 add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
460 add_field(&buf, &ofs, &len, "response", hash);
462 add_field(&buf, &ofs, &len, "opaque", opaque);
464 ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
469 uclient_http_add_auth_header(struct uclient_http *uh)
471 if (!uh->uc.url->auth)
474 switch (uh->auth_type) {
475 case AUTH_TYPE_UNKNOWN:
478 case AUTH_TYPE_BASIC:
479 uclient_http_add_auth_basic(uh);
481 case AUTH_TYPE_DIGEST:
482 uclient_http_add_auth_digest(uh);
488 uclient_http_send_headers(struct uclient_http *uh)
490 struct uclient_url *url = uh->uc.url;
491 struct blob_attr *cur;
492 enum request_type req_type = uh->req_type;
495 if (uh->state >= HTTP_STATE_HEADERS_SENT)
498 if (uh->auth_type == AUTH_TYPE_UNKNOWN)
501 ustream_printf(uh->us,
504 request_types[req_type],
505 url->location, url->host);
507 blobmsg_for_each_attr(cur, uh->headers.head, rem)
508 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
510 if (uh->req_type == REQ_POST)
511 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
513 uclient_http_add_auth_header(uh);
515 ustream_printf(uh->us, "\r\n");
517 uh->state = HTTP_STATE_HEADERS_SENT;
520 static void uclient_http_headers_complete(struct uclient_http *uh)
522 enum auth_type auth_type = uh->auth_type;
524 uh->state = HTTP_STATE_RECV_DATA;
525 uh->uc.meta = uh->meta.head;
526 uclient_http_process_headers(uh);
528 if (auth_type == AUTH_TYPE_UNKNOWN) {
529 uclient_http_init_request(uh);
530 uclient_http_send_headers(uh);
531 uh->state = HTTP_STATE_REQUEST_DONE;
535 if (uh->uc.cb->header_done)
536 uh->uc.cb->header_done(&uh->uc);
541 if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
543 uclient_notify_eof(uh);
547 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
552 if (uh->state == HTTP_STATE_REQUEST_DONE) {
558 code = strsep(&data, " ");
562 uh->uc.status_code = strtoul(code, &sep, 10);
566 uh->state = HTTP_STATE_RECV_HEADERS;
571 uclient_http_headers_complete(uh);
575 sep = strchr(data, ':');
581 for (name = data; *name; name++)
582 *name = tolower(*name);
585 while (isspace(*sep))
588 blobmsg_add_string(&uh->meta, name, sep);
592 uh->uc.status_code = 400;
594 uclient_notify_eof(uh);
597 static void __uclient_notify_read(struct uclient_http *uh)
599 struct uclient *uc = &uh->uc;
603 if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
606 data = ustream_get_read_buf(uh->us, &len);
610 if (uh->state < HTTP_STATE_RECV_DATA) {
615 sep = strstr(data, "\r\n");
619 /* Check for multi-line HTTP headers */
624 if (isspace(sep[2]) && sep[2] != '\r') {
632 cur_len = sep + 2 - data;
633 uclient_parse_http_line(uh, data);
634 ustream_consume(uh->us, cur_len);
640 data = ustream_get_read_buf(uh->us, &len);
641 } while (data && uh->state < HTTP_STATE_RECV_DATA);
650 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
651 uc->cb->data_read(uc);
654 static void uclient_notify_read(struct ustream *us, int bytes)
656 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
658 __uclient_notify_read(uh);
661 static void uclient_notify_state(struct ustream *us)
663 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
665 uclient_notify_eof(uh);
668 static int uclient_setup_http(struct uclient_http *uh)
670 struct ustream *us = &uh->ufd.stream;
676 us->string_data = true;
677 us->notify_state = uclient_notify_state;
678 us->notify_read = uclient_notify_read;
680 ret = uclient_do_connect(uh, "80");
682 return UCLIENT_ERROR_CONNECT;
687 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
689 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
691 __uclient_notify_read(uh);
694 static void uclient_ssl_notify_state(struct ustream *us)
696 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
698 uclient_notify_eof(uh);
701 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
703 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
705 uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
708 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
710 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
712 if (!uh->ssl_require_validation)
715 uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
718 static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
720 struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
722 if (!uh->ssl_require_validation)
725 if (!uh->ussl.valid_cn)
726 uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
729 static int uclient_setup_https(struct uclient_http *uh)
731 struct ustream *us = &uh->ussl.stream;
738 return UCLIENT_ERROR_MISSING_SSL_CONTEXT;
740 ret = uclient_do_connect(uh, "443");
742 return UCLIENT_ERROR_CONNECT;
744 us->string_data = true;
745 us->notify_state = uclient_ssl_notify_state;
746 us->notify_read = uclient_ssl_notify_read;
747 uh->ussl.notify_error = uclient_ssl_notify_error;
748 uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
749 uh->ussl.notify_connected = uclient_ssl_notify_connected;
750 uh->ssl_ops->init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
751 uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host);
756 static int uclient_http_connect(struct uclient *cl)
758 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
761 uclient_http_init_request(uh);
766 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
769 ret = uclient_setup_https(uh);
771 ret = uclient_setup_http(uh);
776 static void uclient_http_disconnect_cb(struct uloop_timeout *timeout)
778 struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t);
780 uclient_http_disconnect(uh);
783 static struct uclient *uclient_http_alloc(void)
785 struct uclient_http *uh;
787 uh = calloc_a(sizeof(*uh));
788 uh->disconnect_t.cb = uclient_http_disconnect_cb;
789 blob_buf_init(&uh->headers, 0);
794 static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
800 static void uclient_http_free(struct uclient *cl)
802 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
804 uclient_http_free_url_state(cl);
805 uclient_http_free_ssl_ctx(uh);
806 blob_buf_free(&uh->headers);
807 blob_buf_free(&uh->meta);
812 uclient_http_set_request_type(struct uclient *cl, const char *type)
814 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
817 if (cl->backend != &uclient_backend_http)
820 if (uh->state > HTTP_STATE_INIT)
823 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
824 if (strcmp(request_types[i], type) != 0)
835 uclient_http_reset_headers(struct uclient *cl)
837 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
839 blob_buf_init(&uh->headers, 0);
845 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
847 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
849 if (cl->backend != &uclient_backend_http)
852 if (uh->state > HTTP_STATE_INIT)
855 blobmsg_add_string(&uh->headers, name, value);
860 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
862 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
864 if (uh->state >= HTTP_STATE_REQUEST_DONE)
867 uclient_http_send_headers(uh);
870 ustream_printf(uh->us, "%X\r\n", len);
871 ustream_write(uh->us, buf, len, false);
872 ustream_printf(uh->us, "\r\n");
879 uclient_http_request_done(struct uclient *cl)
881 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
883 if (uh->state >= HTTP_STATE_REQUEST_DONE)
886 uclient_http_send_headers(uh);
887 if (uh->req_type == REQ_POST)
888 ustream_printf(uh->us, "0\r\n\r\n");
889 uh->state = HTTP_STATE_REQUEST_DONE;
895 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
897 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
899 char *data, *data_end;
901 if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
904 data = ustream_get_read_buf(uh->us, &read_len);
905 if (!data || !read_len)
908 data_end = data + read_len;
911 if (uh->read_chunked == 0) {
914 if (data[0] == '\r' && data[1] == '\n') {
919 sep = strstr(data, "\r\n");
924 uh->read_chunked = strtoul(data, NULL, 16);
926 read_len += sep + 2 - data;
929 if (!uh->read_chunked)
933 if (len > data_end - data)
934 len = data_end - data;
936 if (uh->read_chunked >= 0) {
937 if (len > uh->read_chunked)
938 len = uh->read_chunked;
940 uh->read_chunked -= len;
941 } else if (uh->content_length >= 0) {
942 if (len > uh->content_length)
943 len = uh->content_length;
945 uh->content_length -= len;
946 if (!uh->content_length)
952 memcpy(buf, data, len);
956 ustream_consume(uh->us, read_len);
958 uclient_notify_eof(uh);
963 bool uclient_http_redirect(struct uclient *cl)
965 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
966 struct blobmsg_policy location = {
968 .type = BLOBMSG_TYPE_STRING,
970 struct uclient_url *url = cl->url;
971 struct blob_attr *tb;
973 if (cl->backend != &uclient_backend_http)
976 switch (cl->status_code) {
985 blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
989 url = uclient_get_url(blobmsg_data(tb), url->auth);
995 uclient_http_connect(cl);
996 uclient_http_request_done(cl);
1001 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
1002 struct ustream_ssl_ctx *ctx, bool require_validation)
1004 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1006 if (cl->backend != &uclient_backend_http)
1009 uclient_http_free_url_state(cl);
1011 uclient_http_free_ssl_ctx(uh);
1014 uh->ssl_require_validation = !!ctx && require_validation;
1019 static void uclient_http_request_disconnect(struct uclient *cl)
1021 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
1027 uh->disconnect = true;
1028 uloop_timeout_set(&uh->disconnect_t, 1);
1031 const struct uclient_backend uclient_backend_http = {
1032 .prefix = uclient_http_prefix,
1034 .alloc = uclient_http_alloc,
1035 .free = uclient_http_free,
1036 .connect = uclient_http_connect,
1037 .disconnect = uclient_http_request_disconnect,
1038 .update_url = uclient_http_free_url_state,
1040 .read = uclient_http_read,
1041 .write = uclient_http_send_data,
1042 .request = uclient_http_request_done,