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
)
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
;
657 us
->string_data
= true;
658 us
->notify_state
= uclient_notify_state
;
659 us
->notify_read
= uclient_notify_read
;
661 ret
= uclient_do_connect(uh
, "80");
668 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
670 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
672 __uclient_notify_read(uh
);
675 static void uclient_ssl_notify_state(struct ustream
*us
)
677 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
679 uclient_notify_eof(uh
);
682 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
684 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
686 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
689 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
691 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
693 if (!uh
->ssl_require_validation
)
696 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
699 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
701 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
703 if (!uh
->ssl_require_validation
)
706 if (!uh
->ussl
.valid_cn
)
707 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
710 static int uclient_setup_https(struct uclient_http
*uh
)
712 struct ustream
*us
= &uh
->ussl
.stream
;
718 ret
= uclient_do_connect(uh
, "443");
723 uh
->ssl_ctx
= ustream_ssl_context_new(false);
725 us
->string_data
= true;
726 us
->notify_state
= uclient_ssl_notify_state
;
727 us
->notify_read
= uclient_ssl_notify_read
;
728 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
729 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
730 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
731 ustream_ssl_init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
732 ustream_ssl_set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
737 static int uclient_http_connect(struct uclient
*cl
)
739 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
742 uclient_http_init_request(uh
);
747 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
750 ret
= uclient_setup_https(uh
);
752 ret
= uclient_setup_http(uh
);
755 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
760 static struct uclient
*uclient_http_alloc(void)
762 struct uclient_http
*uh
;
764 uh
= calloc_a(sizeof(*uh
));
765 blob_buf_init(&uh
->headers
, 0);
770 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
772 if (uh
->ssl_ctx
&& !uh
->ssl_ctx_ext
)
773 ustream_ssl_context_free(uh
->ssl_ctx
);
775 uh
->ssl_ctx_ext
= false;
778 static void uclient_http_free(struct uclient
*cl
)
780 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
782 uclient_http_free_ssl_ctx(uh
);
783 uclient_http_free_url_state(cl
);
784 blob_buf_free(&uh
->headers
);
785 blob_buf_free(&uh
->meta
);
790 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
792 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
795 if (cl
->backend
!= &uclient_backend_http
)
798 if (uh
->state
> HTTP_STATE_INIT
)
801 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
802 if (strcmp(request_types
[i
], type
) != 0)
813 uclient_http_reset_headers(struct uclient
*cl
)
815 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
817 blob_buf_init(&uh
->headers
, 0);
823 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
825 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
827 if (cl
->backend
!= &uclient_backend_http
)
830 if (uh
->state
> HTTP_STATE_INIT
)
833 blobmsg_add_string(&uh
->headers
, name
, value
);
838 uclient_http_send_data(struct uclient
*cl
, char *buf
, unsigned int len
)
840 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
842 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
845 uclient_http_send_headers(uh
);
848 ustream_printf(uh
->us
, "%X\r\n", len
);
849 ustream_write(uh
->us
, buf
, len
, false);
850 ustream_printf(uh
->us
, "\r\n");
857 uclient_http_request_done(struct uclient
*cl
)
859 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
861 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
864 uclient_http_send_headers(uh
);
865 if (uh
->req_type
== REQ_POST
)
866 ustream_printf(uh
->us
, "0\r\n\r\n");
867 uh
->state
= HTTP_STATE_REQUEST_DONE
;
873 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
875 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
877 char *data
, *data_end
;
879 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
882 data
= ustream_get_read_buf(uh
->us
, &read_len
);
883 if (!data
|| !read_len
)
886 data_end
= data
+ read_len
;
889 if (uh
->read_chunked
== 0) {
892 if (data
[0] == '\r' && data
[1] == '\n') {
897 sep
= strstr(data
, "\r\n");
902 uh
->read_chunked
= strtoul(data
, NULL
, 16);
904 read_len
+= sep
+ 2 - data
;
907 if (!uh
->read_chunked
)
911 if (len
> data_end
- data
)
912 len
= data_end
- data
;
914 if (uh
->read_chunked
>= 0) {
915 if (len
> uh
->read_chunked
)
916 len
= uh
->read_chunked
;
918 uh
->read_chunked
-= len
;
919 } else if (uh
->content_length
>= 0) {
920 if (len
> uh
->content_length
)
921 len
= uh
->content_length
;
923 uh
->content_length
-= len
;
924 if (!uh
->content_length
)
930 memcpy(buf
, data
, len
);
934 ustream_consume(uh
->us
, read_len
);
936 uclient_notify_eof(uh
);
941 bool uclient_http_redirect(struct uclient
*cl
)
943 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
944 struct blobmsg_policy location
= {
946 .type
= BLOBMSG_TYPE_STRING
,
948 struct uclient_url
*url
= cl
->url
;
949 struct blob_attr
*tb
;
951 if (cl
->backend
!= &uclient_backend_http
)
954 switch (cl
->status_code
) {
963 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
967 url
= uclient_get_url(blobmsg_data(tb
), url
->auth
);
973 uclient_http_connect(cl
);
974 uclient_http_request_done(cl
);
979 int uclient_http_set_ssl_ctx(struct uclient
*cl
, struct ustream_ssl_ctx
*ctx
, bool require_validation
)
981 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
983 if (cl
->backend
!= &uclient_backend_http
)
986 uclient_http_free_url_state(cl
);
988 uclient_http_free_ssl_ctx(uh
);
990 uh
->ssl_ctx_ext
= !!ctx
;
991 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
996 const struct uclient_backend uclient_backend_http
= {
997 .prefix
= uclient_http_prefix
,
999 .alloc
= uclient_http_alloc
,
1000 .free
= uclient_http_free
,
1001 .connect
= uclient_http_connect
,
1002 .update_url
= uclient_http_free_url_state
,
1004 .read
= uclient_http_read
,
1005 .write
= uclient_http_send_data
,
1006 .request
= uclient_http_request_done
,