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.
18 #include <sys/socket.h>
26 #include <libubox/ustream.h>
27 #include <libubox/ustream-ssl.h>
28 #include <libubox/usock.h>
29 #include <libubox/blobmsg.h>
32 #include "uclient-utils.h"
33 #include "uclient-backend.h"
53 HTTP_STATE_HEADERS_SENT
,
54 HTTP_STATE_REQUEST_DONE
,
55 HTTP_STATE_RECV_HEADERS
,
60 static const char * const request_types
[__REQ_MAX
] = {
65 [REQ_DELETE
] = "DELETE",
71 const struct ustream_ssl_ops
*ssl_ops
;
72 struct ustream_ssl_ctx
*ssl_ctx
;
75 struct ustream_fd ufd
;
76 struct ustream_ssl ussl
;
78 struct uloop_timeout disconnect_t
;
81 bool ssl_require_validation
;
84 bool connection_close
;
86 enum request_type req_type
;
87 enum http_state state
;
89 enum auth_type auth_type
;
99 struct blob_buf headers
;
100 struct blob_buf meta
;
109 static const char * const uclient_http_prefix
[] = {
110 [PREFIX_HTTP
] = "http://",
111 [PREFIX_HTTPS
] = "https://",
112 [__PREFIX_MAX
] = NULL
115 static int uclient_http_connect(struct uclient
*cl
);
117 static int uclient_do_connect(struct uclient_http
*uh
, const char *port
)
122 if (uh
->uc
.url
->port
)
123 port
= uh
->uc
.url
->port
;
125 memset(&uh
->uc
.remote_addr
, 0, sizeof(uh
->uc
.remote_addr
));
127 fd
= usock_inet_timeout(USOCK_TCP
| USOCK_NONBLOCK
| uh
->usock_flags
,
128 uh
->uc
.url
->host
, port
, &uh
->uc
.remote_addr
,
129 uh
->uc
.timeout_msecs
);
133 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
134 ustream_fd_init(&uh
->ufd
, fd
);
136 sl
= sizeof(uh
->uc
.local_addr
);
137 memset(&uh
->uc
.local_addr
, 0, sl
);
138 getsockname(fd
, &uh
->uc
.local_addr
.sa
, &sl
);
143 static void uclient_http_disconnect(struct uclient_http
*uh
)
145 uloop_timeout_cancel(&uh
->disconnect_t
);
150 ustream_free(&uh
->ussl
.stream
);
151 ustream_free(&uh
->ufd
.stream
);
152 close(uh
->ufd
.fd
.fd
);
156 static void uclient_http_free_url_state(struct uclient
*cl
)
158 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
160 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
163 uclient_http_disconnect(uh
);
166 static void uclient_http_error(struct uclient_http
*uh
, int code
)
168 uh
->state
= HTTP_STATE_ERROR
;
170 ustream_state_change(uh
->us
);
171 uclient_backend_set_error(&uh
->uc
, code
);
174 static void uclient_http_request_disconnect(struct uclient
*cl
)
176 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
182 uh
->disconnect
= true;
183 uloop_timeout_set(&uh
->disconnect_t
, 1);
186 static void uclient_notify_eof(struct uclient_http
*uh
)
188 struct ustream
*us
= uh
->us
;
194 if (!us
->eof
&& !us
->write_error
)
197 if (ustream_pending_data(us
, false))
201 if (uh
->content_length
< 0 && uh
->read_chunked
>= 0)
202 uh
->uc
.data_eof
= true;
204 uclient_backend_set_eof(&uh
->uc
);
206 if (uh
->connection_close
)
207 uclient_http_request_disconnect(&uh
->uc
);
210 static void uclient_http_reset_state(struct uclient_http
*uh
)
213 uclient_backend_reset_state(&uh
->uc
);
214 uh
->read_chunked
= -1;
215 uh
->content_length
= -1;
217 uh
->disconnect
= false;
218 uh
->connection_close
= false;
219 uh
->state
= HTTP_STATE_INIT
;
221 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
222 uh
->auth_type
= AUTH_TYPE_NONE
;
225 static void uclient_http_init_request(struct uclient_http
*uh
)
228 uclient_http_reset_state(uh
);
229 blob_buf_init(&uh
->meta
, 0);
232 static enum auth_type
233 uclient_http_update_auth_type(struct uclient_http
*uh
)
236 return AUTH_TYPE_NONE
;
238 if (!strncasecmp(uh
->auth_str
, "basic", 5))
239 return AUTH_TYPE_BASIC
;
241 if (!strncasecmp(uh
->auth_str
, "digest", 6))
242 return AUTH_TYPE_DIGEST
;
244 return AUTH_TYPE_NONE
;
247 static void uclient_http_process_headers(struct uclient_http
*uh
)
250 HTTP_HDR_TRANSFER_ENCODING
,
252 HTTP_HDR_CONTENT_LENGTH
,
256 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
257 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
258 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
259 [HTTP_HDR_CONNECTION
] = hdr("connection"),
260 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
261 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
264 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
265 struct blob_attr
*cur
;
267 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
269 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
270 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
271 uh
->read_chunked
= 0;
273 cur
= tb
[HTTP_HDR_CONNECTION
];
274 if (cur
&& strstr(blobmsg_data(cur
), "close"))
275 uh
->connection_close
= true;
277 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
279 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
281 cur
= tb
[HTTP_HDR_AUTH
];
284 uh
->auth_str
= strdup(blobmsg_data(cur
));
287 uh
->auth_type
= uclient_http_update_auth_type(uh
);
290 static bool uclient_request_supports_body(enum request_type req_type
)
303 uclient_http_add_auth_basic(struct uclient_http
*uh
)
305 struct uclient_url
*url
= uh
->uc
.url
;
306 int auth_len
= strlen(url
->auth
);
312 auth_buf
= alloca(base64_len(auth_len
) + 1);
316 base64_encode(url
->auth
, auth_len
, auth_buf
);
317 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
322 static char *digest_unquote_sep(char **str
)
324 char *cur
= *str
+ 1;
356 static char *digest_sep(char **str
)
361 next
= strchr(*str
, ',');
366 *str
+= strlen(*str
);
372 static bool strmatch(char **str
, const char *prefix
)
374 int len
= strlen(prefix
);
376 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
384 get_cnonce(char *dest
)
390 f
= fopen("/dev/urandom", "r");
392 n
= fread(&val
, sizeof(val
), 1, f
);
398 bin_to_hex(dest
, &val
, sizeof(val
));
401 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
403 int available
= *len
- *ofs
;
411 required
= strlen(name
) + 4 + strlen(val
) * 2;
412 if (required
> available
)
413 *len
+= required
- available
+ 64;
415 *buf
= realloc(*buf
, *len
);
420 cur
+= sprintf(cur
, ", %s=\"", name
);
422 while ((next
= strchr(val
, '"'))) {
424 memcpy(cur
, val
, next
- val
);
428 cur
+= sprintf(cur
, "\\\"");
432 cur
+= sprintf(cur
, "%s\"", val
);
437 uclient_http_add_auth_digest(struct uclient_http
*uh
)
440 struct uclient_url
*url
= uh
->uc
.url
;
441 const char *realm
= NULL
, *opaque
= NULL
;
442 const char *user
, *password
;
443 char *buf
, *next
, *buf_orig
;
451 struct http_digest_data data
= {
453 .cnonce
= cnonce_str
,
457 len
= strlen(uh
->auth_str
) + 1;
469 strcpy(buf
, uh
->auth_str
);
472 buf_orig
= strsep(&buf
, " ");
476 const char **dest
= NULL
;
479 while (*next
&& isspace(*next
))
482 if (strmatch(&next
, "realm"))
484 else if (strmatch(&next
, "qop"))
486 else if (strmatch(&next
, "nonce"))
488 else if (strmatch(&next
, "opaque"))
490 else if (strmatch(&next
, "stale") ||
491 strmatch(&next
, "algorithm") ||
492 strmatch(&next
, "auth-param")) {
495 } else if (strmatch(&next
, "domain") ||
496 strmatch(&next
, "qop-options"))
503 *dest
= digest_unquote_sep(&next
);
506 if (!realm
|| !data
.qop
|| !data
.nonce
) {
511 sprintf(nc_str
, "%08x", uh
->nc
++);
512 get_cnonce(cnonce_str
);
515 data
.uri
= url
->location
;
516 data
.method
= request_types
[uh
->req_type
];
518 password
= strchr(url
->auth
, ':');
522 len
= password
- url
->auth
;
528 user_buf
= alloca(len
+ 1);
534 strncpy(user_buf
, url
->auth
, len
);
543 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
544 http_digest_calculate_response(hash
, &data
);
550 add_field(&buf
, &ofs
, &len
, "username", user
);
551 add_field(&buf
, &ofs
, &len
, "realm", realm
);
552 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
553 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
554 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
555 add_field(&buf
, &ofs
, &len
, "response", hash
);
557 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
559 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
572 uclient_http_add_auth_header(struct uclient_http
*uh
)
574 if (!uh
->uc
.url
->auth
)
577 switch (uh
->auth_type
) {
578 case AUTH_TYPE_UNKNOWN
:
581 case AUTH_TYPE_BASIC
:
582 uclient_http_add_auth_basic(uh
);
584 case AUTH_TYPE_DIGEST
:
585 uclient_http_add_auth_digest(uh
);
591 uclient_http_send_headers(struct uclient_http
*uh
)
593 struct uclient_url
*url
= uh
->uc
.url
;
594 struct blob_attr
*cur
;
595 enum request_type req_type
= uh
->req_type
;
599 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
602 if (uh
->uc
.proxy_url
)
603 url
= uh
->uc
.proxy_url
;
605 literal_ipv6
= strchr(url
->host
, ':');
607 ustream_printf(uh
->us
,
609 "Host: %s%s%s%s%s\r\n",
610 request_types
[req_type
], url
->location
,
611 literal_ipv6
? "[" : "",
613 literal_ipv6
? "]" : "",
614 url
->port
? ":" : "",
615 url
->port
? url
->port
: "");
617 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
618 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
620 if (uclient_request_supports_body(uh
->req_type
))
621 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
623 uclient_http_add_auth_header(uh
);
625 ustream_printf(uh
->us
, "\r\n");
627 uh
->state
= HTTP_STATE_HEADERS_SENT
;
630 static void uclient_http_headers_complete(struct uclient_http
*uh
)
632 enum auth_type auth_type
= uh
->auth_type
;
633 int seq
= uh
->uc
.seq
;
635 uh
->state
= HTTP_STATE_RECV_DATA
;
636 uh
->uc
.meta
= uh
->meta
.head
;
637 uclient_http_process_headers(uh
);
639 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
640 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
641 uclient_http_connect(&uh
->uc
);
642 uclient_http_send_headers(uh
);
643 uh
->state
= HTTP_STATE_REQUEST_DONE
;
647 if (uh
->uc
.cb
->header_done
)
648 uh
->uc
.cb
->header_done(&uh
->uc
);
650 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
653 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
655 uclient_notify_eof(uh
);
659 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
664 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
673 code
= strsep(&data
, " ");
677 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
681 uh
->state
= HTTP_STATE_RECV_HEADERS
;
686 uclient_http_headers_complete(uh
);
690 sep
= strchr(data
, ':');
696 for (name
= data
; *name
; name
++)
697 *name
= tolower(*name
);
700 while (isspace(*sep
))
703 blobmsg_add_string(&uh
->meta
, name
, sep
);
707 uh
->uc
.status_code
= 400;
709 uclient_notify_eof(uh
);
712 static void __uclient_notify_read(struct uclient_http
*uh
)
714 struct uclient
*uc
= &uh
->uc
;
715 unsigned int seq
= uh
->seq
;
719 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
722 data
= ustream_get_read_buf(uh
->us
, &len
);
726 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
731 sep
= strchr(data
, '\n');
736 if (sep
> data
&& sep
[-1] == '\r')
739 /* Check for multi-line HTTP headers */
744 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
753 cur_len
= next
- data
;
754 uclient_parse_http_line(uh
, data
);
758 ustream_consume(uh
->us
, cur_len
);
764 data
= ustream_get_read_buf(uh
->us
, &len
);
765 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
774 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
775 /* Now it's uclient user turn to read some data */
776 uloop_timeout_cancel(&uc
->connection_timeout
);
778 if (uc
->cb
->data_read
)
779 uc
->cb
->data_read(uc
);
783 static void __uclient_notify_write(struct uclient_http
*uh
)
785 struct uclient
*uc
= &uh
->uc
;
787 if (uc
->cb
->data_sent
)
788 uc
->cb
->data_sent(uc
);
791 static void uclient_notify_read(struct ustream
*us
, int bytes
)
793 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
795 __uclient_notify_read(uh
);
798 static void uclient_notify_write(struct ustream
*us
, int bytes
)
800 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
802 __uclient_notify_write(uh
);
805 static void uclient_notify_state(struct ustream
*us
)
807 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
809 if (uh
->ufd
.stream
.write_error
) {
810 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
813 uclient_notify_eof(uh
);
816 static int uclient_setup_http(struct uclient_http
*uh
)
818 struct ustream
*us
= &uh
->ufd
.stream
;
824 us
->string_data
= true;
825 us
->notify_state
= uclient_notify_state
;
826 us
->notify_read
= uclient_notify_read
;
827 us
->notify_write
= uclient_notify_write
;
829 ret
= uclient_do_connect(uh
, "80");
831 return UCLIENT_ERROR_CONNECT
;
836 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
838 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
840 __uclient_notify_read(uh
);
843 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
845 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
847 __uclient_notify_write(uh
);
850 static void uclient_ssl_notify_state(struct ustream
*us
)
852 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
854 uclient_notify_eof(uh
);
857 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
859 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
861 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
864 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
866 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
868 if (!uh
->ssl_require_validation
)
871 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
874 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
876 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
878 if (!uh
->ssl_require_validation
)
881 if (!uh
->ussl
.valid_cn
)
882 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
885 static int uclient_setup_https(struct uclient_http
*uh
)
887 struct ustream
*us
= &uh
->ussl
.stream
;
894 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
896 ret
= uclient_do_connect(uh
, "443");
898 return UCLIENT_ERROR_CONNECT
;
900 us
->string_data
= true;
901 us
->notify_state
= uclient_ssl_notify_state
;
902 us
->notify_read
= uclient_ssl_notify_read
;
903 us
->notify_write
= uclient_ssl_notify_write
;
904 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
905 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
906 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
907 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
908 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
909 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
914 static int uclient_http_connect(struct uclient
*cl
)
916 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
919 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
920 uclient_http_disconnect(uh
);
922 uclient_http_init_request(uh
);
927 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
930 ret
= uclient_setup_https(uh
);
932 ret
= uclient_setup_http(uh
);
937 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
939 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
941 uclient_http_disconnect(uh
);
944 static struct uclient
*uclient_http_alloc(void)
946 struct uclient_http
*uh
;
948 uh
= calloc_a(sizeof(*uh
));
952 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
953 blob_buf_init(&uh
->headers
, 0);
958 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
964 static void uclient_http_free(struct uclient
*cl
)
966 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
968 uclient_http_free_url_state(cl
);
969 uclient_http_free_ssl_ctx(uh
);
970 blob_buf_free(&uh
->headers
);
971 blob_buf_free(&uh
->meta
);
976 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
978 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
981 if (cl
->backend
!= &uclient_backend_http
)
984 if (uh
->state
> HTTP_STATE_INIT
)
987 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
988 if (strcmp(request_types
[i
], type
) != 0)
999 uclient_http_reset_headers(struct uclient
*cl
)
1001 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1003 blob_buf_init(&uh
->headers
, 0);
1009 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
1011 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1013 if (cl
->backend
!= &uclient_backend_http
)
1016 if (uh
->state
> HTTP_STATE_INIT
)
1019 blobmsg_add_string(&uh
->headers
, name
, value
);
1024 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
1026 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1028 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1031 uclient_http_send_headers(uh
);
1034 ustream_printf(uh
->us
, "%X\r\n", len
);
1035 ustream_write(uh
->us
, buf
, len
, false);
1036 ustream_printf(uh
->us
, "\r\n");
1043 uclient_http_request_done(struct uclient
*cl
)
1045 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1047 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1050 uclient_http_send_headers(uh
);
1051 if (uclient_request_supports_body(uh
->req_type
))
1052 ustream_printf(uh
->us
, "0\r\n\r\n");
1053 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1059 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1061 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1063 char *data
, *data_end
;
1065 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1068 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1069 if (!data
|| !read_len
)
1072 data_end
= data
+ read_len
;
1075 if (uh
->read_chunked
== 0) {
1078 if (data
[0] == '\r' && data
[1] == '\n') {
1083 sep
= strstr(data
, "\r\n");
1088 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1090 read_len
+= sep
+ 2 - data
;
1093 if (!uh
->read_chunked
) {
1095 uh
->uc
.data_eof
= true;
1099 if (len
> data_end
- data
)
1100 len
= data_end
- data
;
1102 if (uh
->read_chunked
>= 0) {
1103 if (len
> uh
->read_chunked
)
1104 len
= uh
->read_chunked
;
1106 uh
->read_chunked
-= len
;
1107 } else if (uh
->content_length
>= 0) {
1108 if (len
> uh
->content_length
)
1109 len
= uh
->content_length
;
1111 uh
->content_length
-= len
;
1112 if (!uh
->content_length
) {
1114 uh
->uc
.data_eof
= true;
1120 memcpy(buf
, data
, len
);
1124 ustream_consume(uh
->us
, read_len
);
1126 uclient_notify_eof(uh
);
1128 /* Now that we consumed something and if this isn't EOF, start timer again */
1129 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1130 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1135 int uclient_http_redirect(struct uclient
*cl
)
1137 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1138 struct blobmsg_policy location
= {
1140 .type
= BLOBMSG_TYPE_STRING
,
1142 struct uclient_url
*url
= cl
->url
;
1143 struct blob_attr
*tb
;
1145 if (cl
->backend
!= &uclient_backend_http
)
1148 switch (cl
->status_code
) {
1157 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1161 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1167 if (uclient_http_connect(cl
))
1170 uclient_http_request_done(cl
);
1175 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1176 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1178 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1180 if (cl
->backend
!= &uclient_backend_http
)
1183 uclient_http_free_url_state(cl
);
1185 uclient_http_free_ssl_ctx(uh
);
1188 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1193 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1195 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1197 if (cl
->backend
!= &uclient_backend_http
)
1202 uh
->usock_flags
= USOCK_IPV4ONLY
;
1205 uh
->usock_flags
= USOCK_IPV6ONLY
;
1208 uh
->usock_flags
= 0;
1215 const struct uclient_backend uclient_backend_http
= {
1216 .prefix
= uclient_http_prefix
,
1218 .alloc
= uclient_http_alloc
,
1219 .free
= uclient_http_free
,
1220 .connect
= uclient_http_connect
,
1221 .disconnect
= uclient_http_request_disconnect
,
1222 .update_url
= uclient_http_free_url_state
,
1223 .update_proxy_url
= uclient_http_free_url_state
,
1225 .read
= uclient_http_read
,
1226 .write
= uclient_http_send_data
,
1227 .request
= uclient_http_request_done
,