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 struct ustream_ssl_ctx
*ssl_ctx
;
67 struct ustream_fd ufd
;
68 struct ustream_ssl ussl
;
70 bool ssl_require_validation
;
74 bool connection_close
;
75 enum request_type req_type
;
76 enum http_state state
;
78 enum auth_type auth_type
;
86 struct blob_buf headers
;
96 static const char * const uclient_http_prefix
[] = {
97 [PREFIX_HTTP
] = "http://",
98 [PREFIX_HTTPS
] = "https://",
102 static int uclient_do_connect(struct uclient_http
*uh
, const char *port
)
106 if (uh
->uc
.url
->port
)
107 port
= uh
->uc
.url
->port
;
109 fd
= usock(USOCK_TCP
| USOCK_NONBLOCK
, uh
->uc
.url
->host
, port
);
113 ustream_fd_init(&uh
->ufd
, fd
);
117 static void uclient_http_disconnect(struct uclient_http
*uh
)
123 ustream_free(&uh
->ussl
.stream
);
124 ustream_free(&uh
->ufd
.stream
);
125 close(uh
->ufd
.fd
.fd
);
129 static void uclient_http_free_url_state(struct uclient
*cl
)
131 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
133 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
136 uclient_http_disconnect(uh
);
139 static void uclient_http_error(struct uclient_http
*uh
, int code
)
141 uh
->state
= HTTP_STATE_ERROR
;
143 ustream_state_change(uh
->us
);
144 uclient_backend_set_error(&uh
->uc
, code
);
147 static void uclient_notify_eof(struct uclient_http
*uh
)
149 struct ustream
*us
= uh
->us
;
152 if (!us
->eof
&& !us
->write_error
)
155 if (ustream_pending_data(us
, false))
159 uclient_backend_set_eof(&uh
->uc
);
161 if (uh
->connection_close
)
162 uclient_http_disconnect(uh
);
165 static void uclient_http_reset_state(struct uclient_http
*uh
)
167 uclient_backend_reset_state(&uh
->uc
);
168 uh
->read_chunked
= -1;
169 uh
->content_length
= -1;
171 uh
->connection_close
= false;
172 uh
->state
= HTTP_STATE_INIT
;
174 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
175 uh
->auth_type
= AUTH_TYPE_NONE
;
178 static void uclient_http_init_request(struct uclient_http
*uh
)
180 uclient_http_reset_state(uh
);
181 blob_buf_init(&uh
->meta
, 0);
184 static enum auth_type
185 uclient_http_update_auth_type(struct uclient_http
*uh
)
188 return AUTH_TYPE_NONE
;
190 if (!strncasecmp(uh
->auth_str
, "basic", 5))
191 return AUTH_TYPE_BASIC
;
193 if (!strncasecmp(uh
->auth_str
, "digest", 6))
194 return AUTH_TYPE_DIGEST
;
196 return AUTH_TYPE_NONE
;
199 static void uclient_http_process_headers(struct uclient_http
*uh
)
202 HTTP_HDR_TRANSFER_ENCODING
,
204 HTTP_HDR_CONTENT_LENGTH
,
208 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
209 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
210 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
211 [HTTP_HDR_CONNECTION
] = hdr("connection"),
212 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
213 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
216 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
217 struct blob_attr
*cur
;
219 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
221 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
222 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
223 uh
->read_chunked
= 0;
225 cur
= tb
[HTTP_HDR_CONNECTION
];
226 if (cur
&& strstr(blobmsg_data(cur
), "close"))
227 uh
->connection_close
= true;
229 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
231 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
233 cur
= tb
[HTTP_HDR_AUTH
];
236 uh
->auth_str
= strdup(blobmsg_data(cur
));
239 uh
->auth_type
= uclient_http_update_auth_type(uh
);
243 uclient_http_add_auth_basic(struct uclient_http
*uh
)
245 struct uclient_url
*url
= uh
->uc
.url
;
246 int auth_len
= strlen(url
->auth
);
252 auth_buf
= alloca(base64_len(auth_len
) + 1);
253 base64_encode(url
->auth
, auth_len
, auth_buf
);
254 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
257 static char *digest_unquote_sep(char **str
)
259 char *cur
= *str
+ 1;
291 static bool strmatch(char **str
, const char *prefix
)
293 int len
= strlen(prefix
);
295 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
303 get_cnonce(char *dest
)
308 f
= fopen("/dev/urandom", "r");
310 fread(&val
, sizeof(val
), 1, f
);
314 bin_to_hex(dest
, &val
, sizeof(val
));
317 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
319 int available
= *len
- *ofs
;
327 required
= strlen(name
) + 4 + strlen(val
) * 2;
328 if (required
> available
)
329 *len
+= required
- available
+ 64;
331 *buf
= realloc(*buf
, *len
);
336 cur
+= sprintf(cur
, ", %s=\"", name
);
338 while ((next
= strchr(val
, '"'))) {
340 memcpy(cur
, val
, next
- val
);
344 cur
+= sprintf(cur
, "\\\"");
348 cur
+= sprintf(cur
, "%s\"", val
);
353 uclient_http_add_auth_digest(struct uclient_http
*uh
)
355 struct uclient_url
*url
= uh
->uc
.url
;
356 const char *realm
= NULL
, *opaque
= NULL
;
357 const char *user
, *password
;
366 struct http_digest_data data
= {
368 .cnonce
= cnonce_str
,
372 len
= strlen(uh
->auth_str
) + 1;
377 strcpy(buf
, uh
->auth_str
);
384 const char **dest
= NULL
;
386 while (isspace(*next
))
389 if (strmatch(&next
, "realm"))
391 else if (strmatch(&next
, "qop"))
393 else if (strmatch(&next
, "nonce"))
395 else if (strmatch(&next
, "opaque"))
400 *dest
= digest_unquote_sep(&next
);
403 if (!realm
|| !data
.qop
|| !data
.nonce
)
406 sprintf(nc_str
, "%08x", uh
->nc
++);
407 get_cnonce(cnonce_str
);
410 data
.uri
= url
->location
;
411 data
.method
= request_types
[uh
->req_type
];
413 password
= strchr(url
->auth
, ':');
417 len
= password
- url
->auth
;
421 user_buf
= alloca(len
+ 1);
422 strncpy(user_buf
, url
->auth
, len
);
431 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
432 http_digest_calculate_response(hash
, &data
);
438 add_field(&buf
, &ofs
, &len
, "username", user
);
439 add_field(&buf
, &ofs
, &len
, "realm", realm
);
440 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
441 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
442 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
443 add_field(&buf
, &ofs
, &len
, "response", hash
);
445 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
447 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
452 uclient_http_add_auth_header(struct uclient_http
*uh
)
454 if (!uh
->uc
.url
->auth
)
457 switch (uh
->auth_type
) {
458 case AUTH_TYPE_UNKNOWN
:
461 case AUTH_TYPE_BASIC
:
462 uclient_http_add_auth_basic(uh
);
464 case AUTH_TYPE_DIGEST
:
465 uclient_http_add_auth_digest(uh
);
471 uclient_http_send_headers(struct uclient_http
*uh
)
473 struct uclient_url
*url
= uh
->uc
.url
;
474 struct blob_attr
*cur
;
475 enum request_type req_type
= uh
->req_type
;
478 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
481 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
)
484 ustream_printf(uh
->us
,
487 request_types
[req_type
],
488 url
->location
, url
->host
);
490 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
491 ustream_printf(uh
->us
, "%s: %s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
493 if (uh
->req_type
== REQ_POST
)
494 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
496 uclient_http_add_auth_header(uh
);
498 ustream_printf(uh
->us
, "\r\n");
500 uh
->state
= HTTP_STATE_HEADERS_SENT
;
503 static void uclient_http_headers_complete(struct uclient_http
*uh
)
505 enum auth_type auth_type
= uh
->auth_type
;
507 uh
->state
= HTTP_STATE_RECV_DATA
;
508 uh
->uc
.meta
= uh
->meta
.head
;
509 uclient_http_process_headers(uh
);
511 if (auth_type
== AUTH_TYPE_UNKNOWN
) {
512 uclient_http_init_request(uh
);
513 uclient_http_send_headers(uh
);
514 uh
->state
= HTTP_STATE_REQUEST_DONE
;
518 if (uh
->uc
.cb
->header_done
)
519 uh
->uc
.cb
->header_done(&uh
->uc
);
521 if (uh
->req_type
== REQ_HEAD
) {
523 uclient_notify_eof(uh
);
527 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
532 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
538 code
= strsep(&data
, " ");
542 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
546 uh
->state
= HTTP_STATE_RECV_HEADERS
;
551 uclient_http_headers_complete(uh
);
555 sep
= strchr(data
, ':');
561 for (name
= data
; *name
; name
++)
562 *name
= tolower(*name
);
565 while (isspace(*sep
))
568 blobmsg_add_string(&uh
->meta
, name
, sep
);
572 uh
->uc
.status_code
= 400;
574 uclient_notify_eof(uh
);
577 static void __uclient_notify_read(struct uclient_http
*uh
)
579 struct uclient
*uc
= &uh
->uc
;
583 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
586 data
= ustream_get_read_buf(uh
->us
, &len
);
590 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
595 sep
= strstr(data
, "\r\n");
599 /* Check for multi-line HTTP headers */
604 if (isspace(sep
[2]) && sep
[2] != '\r') {
612 cur_len
= sep
+ 2 - data
;
613 uclient_parse_http_line(uh
, data
);
614 ustream_consume(uh
->us
, cur_len
);
617 data
= ustream_get_read_buf(uh
->us
, &len
);
618 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
624 if (uh
->state
== HTTP_STATE_RECV_DATA
&& uc
->cb
->data_read
)
625 uc
->cb
->data_read(uc
);
628 static void uclient_notify_read(struct ustream
*us
, int bytes
)
630 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
632 __uclient_notify_read(uh
);
635 static void uclient_notify_state(struct ustream
*us
)
637 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
639 uclient_notify_eof(uh
);
642 static int uclient_setup_http(struct uclient_http
*uh
)
644 struct ustream
*us
= &uh
->ufd
.stream
;
648 us
->string_data
= true;
649 us
->notify_state
= uclient_notify_state
;
650 us
->notify_read
= uclient_notify_read
;
652 ret
= uclient_do_connect(uh
, "80");
659 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
661 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
663 __uclient_notify_read(uh
);
666 static void uclient_ssl_notify_state(struct ustream
*us
)
668 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
670 uclient_notify_eof(uh
);
673 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
675 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
677 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
680 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
682 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
684 if (!uh
->ssl_require_validation
)
687 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
690 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
692 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
694 if (!uh
->ssl_require_validation
)
697 if (!uh
->ussl
.valid_cn
)
698 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
701 static int uclient_setup_https(struct uclient_http
*uh
)
703 struct ustream
*us
= &uh
->ussl
.stream
;
709 ret
= uclient_do_connect(uh
, "443");
714 uh
->ssl_ctx
= ustream_ssl_context_new(false);
716 us
->string_data
= true;
717 us
->notify_state
= uclient_ssl_notify_state
;
718 us
->notify_read
= uclient_ssl_notify_read
;
719 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
720 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
721 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
722 ustream_ssl_init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
723 ustream_ssl_set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
728 static int uclient_http_connect(struct uclient
*cl
)
730 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
733 uclient_http_init_request(uh
);
738 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
741 ret
= uclient_setup_https(uh
);
743 ret
= uclient_setup_http(uh
);
746 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
751 static struct uclient
*uclient_http_alloc(void)
753 struct uclient_http
*uh
;
755 uh
= calloc_a(sizeof(*uh
));
756 blob_buf_init(&uh
->headers
, 0);
761 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
763 if (uh
->ssl_ctx
&& !uh
->ssl_ctx_ext
)
764 ustream_ssl_context_free(uh
->ssl_ctx
);
766 uh
->ssl_ctx_ext
= false;
769 static void uclient_http_free(struct uclient
*cl
)
771 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
773 uclient_http_free_ssl_ctx(uh
);
774 uclient_http_free_url_state(cl
);
775 blob_buf_free(&uh
->headers
);
776 blob_buf_free(&uh
->meta
);
781 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
783 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
786 if (cl
->backend
!= &uclient_backend_http
)
789 if (uh
->state
> HTTP_STATE_INIT
)
792 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
793 if (strcmp(request_types
[i
], type
) != 0)
804 uclient_http_reset_headers(struct uclient
*cl
)
806 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
808 blob_buf_init(&uh
->headers
, 0);
814 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
816 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
818 if (cl
->backend
!= &uclient_backend_http
)
821 if (uh
->state
> HTTP_STATE_INIT
)
824 blobmsg_add_string(&uh
->headers
, name
, value
);
829 uclient_http_send_data(struct uclient
*cl
, char *buf
, unsigned int len
)
831 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
833 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
836 uclient_http_send_headers(uh
);
838 ustream_printf(uh
->us
, "%X\r\n", len
);
840 ustream_write(uh
->us
, buf
, len
, false);
841 ustream_printf(uh
->us
, "\r\n");
847 uclient_http_request_done(struct uclient
*cl
)
849 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
851 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
854 uclient_http_send_headers(uh
);
855 if (uh
->req_type
== REQ_POST
)
856 ustream_printf(uh
->us
, "0\r\n\r\n");
857 uh
->state
= HTTP_STATE_REQUEST_DONE
;
863 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
865 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
867 char *data
, *data_end
;
869 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
872 data
= ustream_get_read_buf(uh
->us
, &read_len
);
873 if (!data
|| !read_len
)
876 data_end
= data
+ read_len
;
879 if (uh
->read_chunked
== 0) {
882 if (data
[0] == '\r' && data
[1] == '\n') {
887 sep
= strstr(data
, "\r\n");
892 uh
->read_chunked
= strtoul(data
, NULL
, 16);
894 read_len
+= sep
+ 2 - data
;
897 if (!uh
->read_chunked
)
901 if (len
> data_end
- data
)
902 len
= data_end
- data
;
904 if (uh
->read_chunked
>= 0) {
905 if (len
> uh
->read_chunked
)
906 len
= uh
->read_chunked
;
908 uh
->read_chunked
-= len
;
909 } else if (uh
->content_length
>= 0) {
910 if (len
> uh
->content_length
)
911 len
= uh
->content_length
;
913 uh
->content_length
-= len
;
914 if (!uh
->content_length
)
920 memcpy(buf
, data
, len
);
924 ustream_consume(uh
->us
, read_len
);
926 uclient_notify_eof(uh
);
931 bool uclient_http_redirect(struct uclient
*cl
)
933 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
934 struct blobmsg_policy location
= {
936 .type
= BLOBMSG_TYPE_STRING
,
938 struct uclient_url
*url
= cl
->url
;
939 struct blob_attr
*tb
;
941 if (cl
->backend
!= &uclient_backend_http
)
944 switch (cl
->status_code
) {
953 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
957 url
= uclient_get_url(blobmsg_data(tb
), url
->auth
);
963 uclient_http_connect(cl
);
964 uclient_http_request_done(cl
);
969 int uclient_http_set_ssl_ctx(struct uclient
*cl
, struct ustream_ssl_ctx
*ctx
, bool require_validation
)
971 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
973 if (cl
->backend
!= &uclient_backend_http
)
976 uclient_http_free_url_state(cl
);
978 uclient_http_free_ssl_ctx(uh
);
980 uh
->ssl_ctx_ext
= !!ctx
;
981 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
986 const struct uclient_backend uclient_backend_http
= {
987 .prefix
= uclient_http_prefix
,
989 .alloc
= uclient_http_alloc
,
990 .free
= uclient_http_free
,
991 .connect
= uclient_http_connect
,
992 .update_url
= uclient_http_free_url_state
,
994 .read
= uclient_http_read
,
995 .write
= uclient_http_send_data
,
996 .request
= uclient_http_request_done
,