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 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
)
107 if (uh
->uc
.url
->port
)
108 port
= uh
->uc
.url
->port
;
110 fd
= usock(USOCK_TCP
| USOCK_NONBLOCK
, uh
->uc
.url
->host
, port
);
114 ustream_fd_init(&uh
->ufd
, fd
);
116 memset(&uh
->uc
.local_addr
, 0, sizeof(uh
->uc
.local_addr
));
117 memset(&uh
->uc
.remote_addr
, 0, sizeof(uh
->uc
.remote_addr
));
119 sl
= sizeof(uh
->uc
.local_addr
);
120 getsockname(fd
, &uh
->uc
.local_addr
.sa
, &sl
);
121 getpeername(fd
, &uh
->uc
.remote_addr
.sa
, &sl
);
126 static void uclient_http_disconnect(struct uclient_http
*uh
)
132 ustream_free(&uh
->ussl
.stream
);
133 ustream_free(&uh
->ufd
.stream
);
134 close(uh
->ufd
.fd
.fd
);
138 static void uclient_http_free_url_state(struct uclient
*cl
)
140 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
142 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
145 uclient_http_disconnect(uh
);
148 static void uclient_http_error(struct uclient_http
*uh
, int code
)
150 uh
->state
= HTTP_STATE_ERROR
;
152 ustream_state_change(uh
->us
);
153 uclient_backend_set_error(&uh
->uc
, code
);
156 static void uclient_notify_eof(struct uclient_http
*uh
)
158 struct ustream
*us
= uh
->us
;
161 if (!us
->eof
&& !us
->write_error
)
164 if (ustream_pending_data(us
, false))
168 uclient_backend_set_eof(&uh
->uc
);
170 if (uh
->connection_close
)
171 uclient_http_disconnect(uh
);
174 static void uclient_http_reset_state(struct uclient_http
*uh
)
176 uclient_backend_reset_state(&uh
->uc
);
177 uh
->read_chunked
= -1;
178 uh
->content_length
= -1;
180 uh
->connection_close
= false;
181 uh
->state
= HTTP_STATE_INIT
;
183 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
184 uh
->auth_type
= AUTH_TYPE_NONE
;
187 static void uclient_http_init_request(struct uclient_http
*uh
)
189 uclient_http_reset_state(uh
);
190 blob_buf_init(&uh
->meta
, 0);
193 static enum auth_type
194 uclient_http_update_auth_type(struct uclient_http
*uh
)
197 return AUTH_TYPE_NONE
;
199 if (!strncasecmp(uh
->auth_str
, "basic", 5))
200 return AUTH_TYPE_BASIC
;
202 if (!strncasecmp(uh
->auth_str
, "digest", 6))
203 return AUTH_TYPE_DIGEST
;
205 return AUTH_TYPE_NONE
;
208 static void uclient_http_process_headers(struct uclient_http
*uh
)
211 HTTP_HDR_TRANSFER_ENCODING
,
213 HTTP_HDR_CONTENT_LENGTH
,
217 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
218 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
219 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
220 [HTTP_HDR_CONNECTION
] = hdr("connection"),
221 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
222 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
225 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
226 struct blob_attr
*cur
;
228 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
230 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
231 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
232 uh
->read_chunked
= 0;
234 cur
= tb
[HTTP_HDR_CONNECTION
];
235 if (cur
&& strstr(blobmsg_data(cur
), "close"))
236 uh
->connection_close
= true;
238 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
240 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
242 cur
= tb
[HTTP_HDR_AUTH
];
245 uh
->auth_str
= strdup(blobmsg_data(cur
));
248 uh
->auth_type
= uclient_http_update_auth_type(uh
);
252 uclient_http_add_auth_basic(struct uclient_http
*uh
)
254 struct uclient_url
*url
= uh
->uc
.url
;
255 int auth_len
= strlen(url
->auth
);
261 auth_buf
= alloca(base64_len(auth_len
) + 1);
262 base64_encode(url
->auth
, auth_len
, auth_buf
);
263 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
266 static char *digest_unquote_sep(char **str
)
268 char *cur
= *str
+ 1;
300 static bool strmatch(char **str
, const char *prefix
)
302 int len
= strlen(prefix
);
304 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
312 get_cnonce(char *dest
)
317 f
= fopen("/dev/urandom", "r");
319 fread(&val
, sizeof(val
), 1, f
);
323 bin_to_hex(dest
, &val
, sizeof(val
));
326 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
328 int available
= *len
- *ofs
;
336 required
= strlen(name
) + 4 + strlen(val
) * 2;
337 if (required
> available
)
338 *len
+= required
- available
+ 64;
340 *buf
= realloc(*buf
, *len
);
345 cur
+= sprintf(cur
, ", %s=\"", name
);
347 while ((next
= strchr(val
, '"'))) {
349 memcpy(cur
, val
, next
- val
);
353 cur
+= sprintf(cur
, "\\\"");
357 cur
+= sprintf(cur
, "%s\"", val
);
362 uclient_http_add_auth_digest(struct uclient_http
*uh
)
364 struct uclient_url
*url
= uh
->uc
.url
;
365 const char *realm
= NULL
, *opaque
= NULL
;
366 const char *user
, *password
;
375 struct http_digest_data data
= {
377 .cnonce
= cnonce_str
,
381 len
= strlen(uh
->auth_str
) + 1;
386 strcpy(buf
, uh
->auth_str
);
393 const char **dest
= NULL
;
395 while (isspace(*next
))
398 if (strmatch(&next
, "realm"))
400 else if (strmatch(&next
, "qop"))
402 else if (strmatch(&next
, "nonce"))
404 else if (strmatch(&next
, "opaque"))
409 *dest
= digest_unquote_sep(&next
);
412 if (!realm
|| !data
.qop
|| !data
.nonce
)
415 sprintf(nc_str
, "%08x", uh
->nc
++);
416 get_cnonce(cnonce_str
);
419 data
.uri
= url
->location
;
420 data
.method
= request_types
[uh
->req_type
];
422 password
= strchr(url
->auth
, ':');
426 len
= password
- url
->auth
;
430 user_buf
= alloca(len
+ 1);
431 strncpy(user_buf
, url
->auth
, len
);
440 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
441 http_digest_calculate_response(hash
, &data
);
447 add_field(&buf
, &ofs
, &len
, "username", user
);
448 add_field(&buf
, &ofs
, &len
, "realm", realm
);
449 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
450 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
451 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
452 add_field(&buf
, &ofs
, &len
, "response", hash
);
454 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
456 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
461 uclient_http_add_auth_header(struct uclient_http
*uh
)
463 if (!uh
->uc
.url
->auth
)
466 switch (uh
->auth_type
) {
467 case AUTH_TYPE_UNKNOWN
:
470 case AUTH_TYPE_BASIC
:
471 uclient_http_add_auth_basic(uh
);
473 case AUTH_TYPE_DIGEST
:
474 uclient_http_add_auth_digest(uh
);
480 uclient_http_send_headers(struct uclient_http
*uh
)
482 struct uclient_url
*url
= uh
->uc
.url
;
483 struct blob_attr
*cur
;
484 enum request_type req_type
= uh
->req_type
;
487 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
490 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
)
493 ustream_printf(uh
->us
,
496 request_types
[req_type
],
497 url
->location
, url
->host
);
499 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
500 ustream_printf(uh
->us
, "%s: %s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
502 if (uh
->req_type
== REQ_POST
)
503 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
505 uclient_http_add_auth_header(uh
);
507 ustream_printf(uh
->us
, "\r\n");
509 uh
->state
= HTTP_STATE_HEADERS_SENT
;
512 static void uclient_http_headers_complete(struct uclient_http
*uh
)
514 enum auth_type auth_type
= uh
->auth_type
;
516 uh
->state
= HTTP_STATE_RECV_DATA
;
517 uh
->uc
.meta
= uh
->meta
.head
;
518 uclient_http_process_headers(uh
);
520 if (auth_type
== AUTH_TYPE_UNKNOWN
) {
521 uclient_http_init_request(uh
);
522 uclient_http_send_headers(uh
);
523 uh
->state
= HTTP_STATE_REQUEST_DONE
;
527 if (uh
->uc
.cb
->header_done
)
528 uh
->uc
.cb
->header_done(&uh
->uc
);
530 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
532 uclient_notify_eof(uh
);
536 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
541 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
547 code
= strsep(&data
, " ");
551 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
555 uh
->state
= HTTP_STATE_RECV_HEADERS
;
560 uclient_http_headers_complete(uh
);
564 sep
= strchr(data
, ':');
570 for (name
= data
; *name
; name
++)
571 *name
= tolower(*name
);
574 while (isspace(*sep
))
577 blobmsg_add_string(&uh
->meta
, name
, sep
);
581 uh
->uc
.status_code
= 400;
583 uclient_notify_eof(uh
);
586 static void __uclient_notify_read(struct uclient_http
*uh
)
588 struct uclient
*uc
= &uh
->uc
;
592 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
595 data
= ustream_get_read_buf(uh
->us
, &len
);
599 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
604 sep
= strstr(data
, "\r\n");
608 /* Check for multi-line HTTP headers */
613 if (isspace(sep
[2]) && sep
[2] != '\r') {
621 cur_len
= sep
+ 2 - data
;
622 uclient_parse_http_line(uh
, data
);
623 ustream_consume(uh
->us
, cur_len
);
626 data
= ustream_get_read_buf(uh
->us
, &len
);
627 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
633 if (uh
->state
== HTTP_STATE_RECV_DATA
&& uc
->cb
->data_read
)
634 uc
->cb
->data_read(uc
);
637 static void uclient_notify_read(struct ustream
*us
, int bytes
)
639 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
641 __uclient_notify_read(uh
);
644 static void uclient_notify_state(struct ustream
*us
)
646 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
648 uclient_notify_eof(uh
);
651 static int uclient_setup_http(struct uclient_http
*uh
)
653 struct ustream
*us
= &uh
->ufd
.stream
;
659 us
->string_data
= true;
660 us
->notify_state
= uclient_notify_state
;
661 us
->notify_read
= uclient_notify_read
;
663 ret
= uclient_do_connect(uh
, "80");
665 return UCLIENT_ERROR_CONNECT
;
670 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
672 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
674 __uclient_notify_read(uh
);
677 static void uclient_ssl_notify_state(struct ustream
*us
)
679 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
681 uclient_notify_eof(uh
);
684 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
686 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
688 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
691 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
693 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
695 if (!uh
->ssl_require_validation
)
698 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
701 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
703 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
705 if (!uh
->ssl_require_validation
)
708 if (!uh
->ussl
.valid_cn
)
709 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
712 static int uclient_setup_https(struct uclient_http
*uh
)
714 struct ustream
*us
= &uh
->ussl
.stream
;
721 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
723 ret
= uclient_do_connect(uh
, "443");
725 return UCLIENT_ERROR_CONNECT
;
727 us
->string_data
= true;
728 us
->notify_state
= uclient_ssl_notify_state
;
729 us
->notify_read
= uclient_ssl_notify_read
;
730 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
731 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
732 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
733 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
734 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
739 static int uclient_http_connect(struct uclient
*cl
)
741 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
744 uclient_http_init_request(uh
);
749 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
752 ret
= uclient_setup_https(uh
);
754 ret
= uclient_setup_http(uh
);
759 static struct uclient
*uclient_http_alloc(void)
761 struct uclient_http
*uh
;
763 uh
= calloc_a(sizeof(*uh
));
764 blob_buf_init(&uh
->headers
, 0);
769 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
775 static void uclient_http_free(struct uclient
*cl
)
777 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
779 uclient_http_free_url_state(cl
);
780 uclient_http_free_ssl_ctx(uh
);
781 blob_buf_free(&uh
->headers
);
782 blob_buf_free(&uh
->meta
);
787 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
789 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
792 if (cl
->backend
!= &uclient_backend_http
)
795 if (uh
->state
> HTTP_STATE_INIT
)
798 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
799 if (strcmp(request_types
[i
], type
) != 0)
810 uclient_http_reset_headers(struct uclient
*cl
)
812 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
814 blob_buf_init(&uh
->headers
, 0);
820 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
822 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
824 if (cl
->backend
!= &uclient_backend_http
)
827 if (uh
->state
> HTTP_STATE_INIT
)
830 blobmsg_add_string(&uh
->headers
, name
, value
);
835 uclient_http_send_data(struct uclient
*cl
, char *buf
, unsigned int len
)
837 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
839 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
842 uclient_http_send_headers(uh
);
845 ustream_printf(uh
->us
, "%X\r\n", len
);
846 ustream_write(uh
->us
, buf
, len
, false);
847 ustream_printf(uh
->us
, "\r\n");
854 uclient_http_request_done(struct uclient
*cl
)
856 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
858 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
861 uclient_http_send_headers(uh
);
862 if (uh
->req_type
== REQ_POST
)
863 ustream_printf(uh
->us
, "0\r\n\r\n");
864 uh
->state
= HTTP_STATE_REQUEST_DONE
;
870 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
872 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
874 char *data
, *data_end
;
876 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
879 data
= ustream_get_read_buf(uh
->us
, &read_len
);
880 if (!data
|| !read_len
)
883 data_end
= data
+ read_len
;
886 if (uh
->read_chunked
== 0) {
889 if (data
[0] == '\r' && data
[1] == '\n') {
894 sep
= strstr(data
, "\r\n");
899 uh
->read_chunked
= strtoul(data
, NULL
, 16);
901 read_len
+= sep
+ 2 - data
;
904 if (!uh
->read_chunked
)
908 if (len
> data_end
- data
)
909 len
= data_end
- data
;
911 if (uh
->read_chunked
>= 0) {
912 if (len
> uh
->read_chunked
)
913 len
= uh
->read_chunked
;
915 uh
->read_chunked
-= len
;
916 } else if (uh
->content_length
>= 0) {
917 if (len
> uh
->content_length
)
918 len
= uh
->content_length
;
920 uh
->content_length
-= len
;
921 if (!uh
->content_length
)
927 memcpy(buf
, data
, len
);
931 ustream_consume(uh
->us
, read_len
);
933 uclient_notify_eof(uh
);
938 bool uclient_http_redirect(struct uclient
*cl
)
940 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
941 struct blobmsg_policy location
= {
943 .type
= BLOBMSG_TYPE_STRING
,
945 struct uclient_url
*url
= cl
->url
;
946 struct blob_attr
*tb
;
948 if (cl
->backend
!= &uclient_backend_http
)
951 switch (cl
->status_code
) {
960 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
964 url
= uclient_get_url(blobmsg_data(tb
), url
->auth
);
970 uclient_http_connect(cl
);
971 uclient_http_request_done(cl
);
976 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
977 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
979 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
981 if (cl
->backend
!= &uclient_backend_http
)
984 uclient_http_free_url_state(cl
);
986 uclient_http_free_ssl_ctx(uh
);
989 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
994 const struct uclient_backend uclient_backend_http
= {
995 .prefix
= uclient_http_prefix
,
997 .alloc
= uclient_http_alloc
,
998 .free
= uclient_http_free
,
999 .connect
= uclient_http_connect
,
1000 .update_url
= uclient_http_free_url_state
,
1002 .read
= uclient_http_read
,
1003 .write
= uclient_http_send_data
,
1004 .request
= uclient_http_request_done
,