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>
25 #include <libubox/ustream.h>
26 #include <libubox/ustream-ssl.h>
27 #include <libubox/usock.h>
28 #include <libubox/blobmsg.h>
31 #include "uclient-utils.h"
32 #include "uclient-backend.h"
52 HTTP_STATE_HEADERS_SENT
,
53 HTTP_STATE_REQUEST_DONE
,
54 HTTP_STATE_RECV_HEADERS
,
59 static const char * const request_types
[__REQ_MAX
] = {
64 [REQ_DELETE
] = "DELETE",
70 const struct ustream_ssl_ops
*ssl_ops
;
71 struct ustream_ssl_ctx
*ssl_ctx
;
74 struct ustream_fd ufd
;
75 struct ustream_ssl ussl
;
77 struct uloop_timeout disconnect_t
;
80 bool ssl_require_validation
;
83 bool connection_close
;
85 enum request_type req_type
;
86 enum http_state state
;
88 enum auth_type auth_type
;
98 struct blob_buf headers
;
108 static const char * const uclient_http_prefix
[] = {
109 [PREFIX_HTTP
] = "http://",
110 [PREFIX_HTTPS
] = "https://",
111 [__PREFIX_MAX
] = NULL
114 static int uclient_http_connect(struct uclient
*cl
);
116 static int uclient_do_connect(struct uclient_http
*uh
, const char *port
)
121 if (uh
->uc
.url
->port
)
122 port
= uh
->uc
.url
->port
;
124 memset(&uh
->uc
.remote_addr
, 0, sizeof(uh
->uc
.remote_addr
));
126 fd
= usock_inet_timeout(USOCK_TCP
| USOCK_NONBLOCK
| uh
->usock_flags
,
127 uh
->uc
.url
->host
, port
, &uh
->uc
.remote_addr
,
128 uh
->uc
.timeout_msecs
);
132 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
133 ustream_fd_init(&uh
->ufd
, fd
);
135 sl
= sizeof(uh
->uc
.local_addr
);
136 memset(&uh
->uc
.local_addr
, 0, sl
);
137 getsockname(fd
, &uh
->uc
.local_addr
.sa
, &sl
);
142 static void uclient_http_disconnect(struct uclient_http
*uh
)
144 uloop_timeout_cancel(&uh
->disconnect_t
);
149 ustream_free(&uh
->ussl
.stream
);
150 ustream_free(&uh
->ufd
.stream
);
151 close(uh
->ufd
.fd
.fd
);
155 static void uclient_http_free_url_state(struct uclient
*cl
)
157 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
159 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
162 uclient_http_disconnect(uh
);
165 static void uclient_http_error(struct uclient_http
*uh
, int code
)
167 uh
->state
= HTTP_STATE_ERROR
;
169 ustream_state_change(uh
->us
);
170 uclient_backend_set_error(&uh
->uc
, code
);
173 static void uclient_http_request_disconnect(struct uclient
*cl
)
175 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
181 uh
->disconnect
= true;
182 uloop_timeout_set(&uh
->disconnect_t
, 1);
185 static void uclient_notify_eof(struct uclient_http
*uh
)
187 struct ustream
*us
= uh
->us
;
193 if (!us
->eof
&& !us
->write_error
)
196 if (ustream_pending_data(us
, false))
200 if (uh
->content_length
< 0 && uh
->read_chunked
>= 0)
201 uh
->uc
.data_eof
= true;
203 uclient_backend_set_eof(&uh
->uc
);
205 if (uh
->connection_close
)
206 uclient_http_request_disconnect(&uh
->uc
);
209 static void uclient_http_reset_state(struct uclient_http
*uh
)
212 uclient_backend_reset_state(&uh
->uc
);
213 uh
->read_chunked
= -1;
214 uh
->content_length
= -1;
216 uh
->disconnect
= false;
217 uh
->connection_close
= false;
218 uh
->state
= HTTP_STATE_INIT
;
220 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
221 uh
->auth_type
= AUTH_TYPE_NONE
;
224 static void uclient_http_init_request(struct uclient_http
*uh
)
227 uclient_http_reset_state(uh
);
228 blob_buf_init(&uh
->meta
, 0);
231 static enum auth_type
232 uclient_http_update_auth_type(struct uclient_http
*uh
)
235 return AUTH_TYPE_NONE
;
237 if (!strncasecmp(uh
->auth_str
, "basic", 5))
238 return AUTH_TYPE_BASIC
;
240 if (!strncasecmp(uh
->auth_str
, "digest", 6))
241 return AUTH_TYPE_DIGEST
;
243 return AUTH_TYPE_NONE
;
246 static void uclient_http_process_headers(struct uclient_http
*uh
)
249 HTTP_HDR_TRANSFER_ENCODING
,
251 HTTP_HDR_CONTENT_LENGTH
,
255 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
256 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
257 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
258 [HTTP_HDR_CONNECTION
] = hdr("connection"),
259 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
260 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
263 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
264 struct blob_attr
*cur
;
266 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
268 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
269 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
270 uh
->read_chunked
= 0;
272 cur
= tb
[HTTP_HDR_CONNECTION
];
273 if (cur
&& strstr(blobmsg_data(cur
), "close"))
274 uh
->connection_close
= true;
276 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
278 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
280 cur
= tb
[HTTP_HDR_AUTH
];
283 uh
->auth_str
= strdup(blobmsg_data(cur
));
286 uh
->auth_type
= uclient_http_update_auth_type(uh
);
289 static bool uclient_request_supports_body(enum request_type req_type
)
302 uclient_http_add_auth_basic(struct uclient_http
*uh
)
304 struct uclient_url
*url
= uh
->uc
.url
;
305 int auth_len
= strlen(url
->auth
);
311 auth_buf
= alloca(base64_len(auth_len
) + 1);
312 base64_encode(url
->auth
, auth_len
, auth_buf
);
313 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
316 static char *digest_unquote_sep(char **str
)
318 char *cur
= *str
+ 1;
350 static char *digest_sep(char **str
)
355 next
= strchr(*str
, ',');
360 *str
+= strlen(*str
);
366 static bool strmatch(char **str
, const char *prefix
)
368 int len
= strlen(prefix
);
370 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
378 get_cnonce(char *dest
)
383 f
= fopen("/dev/urandom", "r");
385 fread(&val
, sizeof(val
), 1, f
);
389 bin_to_hex(dest
, &val
, sizeof(val
));
392 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
394 int available
= *len
- *ofs
;
402 required
= strlen(name
) + 4 + strlen(val
) * 2;
403 if (required
> available
)
404 *len
+= required
- available
+ 64;
406 *buf
= realloc(*buf
, *len
);
411 cur
+= sprintf(cur
, ", %s=\"", name
);
413 while ((next
= strchr(val
, '"'))) {
415 memcpy(cur
, val
, next
- val
);
419 cur
+= sprintf(cur
, "\\\"");
423 cur
+= sprintf(cur
, "%s\"", val
);
428 uclient_http_add_auth_digest(struct uclient_http
*uh
)
430 struct uclient_url
*url
= uh
->uc
.url
;
431 const char *realm
= NULL
, *opaque
= NULL
;
432 const char *user
, *password
;
441 struct http_digest_data data
= {
443 .cnonce
= cnonce_str
,
447 len
= strlen(uh
->auth_str
) + 1;
452 strcpy(buf
, uh
->auth_str
);
459 const char **dest
= NULL
;
462 while (*next
&& isspace(*next
))
465 if (strmatch(&next
, "realm"))
467 else if (strmatch(&next
, "qop"))
469 else if (strmatch(&next
, "nonce"))
471 else if (strmatch(&next
, "opaque"))
473 else if (strmatch(&next
, "stale") ||
474 strmatch(&next
, "algorithm") ||
475 strmatch(&next
, "auth-param")) {
478 } else if (strmatch(&next
, "domain") ||
479 strmatch(&next
, "qop-options"))
486 *dest
= digest_unquote_sep(&next
);
489 if (!realm
|| !data
.qop
|| !data
.nonce
)
492 sprintf(nc_str
, "%08x", uh
->nc
++);
493 get_cnonce(cnonce_str
);
496 data
.uri
= url
->location
;
497 data
.method
= request_types
[uh
->req_type
];
499 password
= strchr(url
->auth
, ':');
503 len
= password
- url
->auth
;
507 user_buf
= alloca(len
+ 1);
508 strncpy(user_buf
, url
->auth
, len
);
517 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
518 http_digest_calculate_response(hash
, &data
);
524 add_field(&buf
, &ofs
, &len
, "username", user
);
525 add_field(&buf
, &ofs
, &len
, "realm", realm
);
526 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
527 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
528 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
529 add_field(&buf
, &ofs
, &len
, "response", hash
);
531 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
533 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
538 uclient_http_add_auth_header(struct uclient_http
*uh
)
540 if (!uh
->uc
.url
->auth
)
543 switch (uh
->auth_type
) {
544 case AUTH_TYPE_UNKNOWN
:
547 case AUTH_TYPE_BASIC
:
548 uclient_http_add_auth_basic(uh
);
550 case AUTH_TYPE_DIGEST
:
551 uclient_http_add_auth_digest(uh
);
557 uclient_http_send_headers(struct uclient_http
*uh
)
559 struct uclient_url
*url
= uh
->uc
.url
;
560 struct blob_attr
*cur
;
561 enum request_type req_type
= uh
->req_type
;
564 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
567 if (uh
->uc
.proxy_url
)
568 url
= uh
->uc
.proxy_url
;
570 ustream_printf(uh
->us
,
573 request_types
[req_type
],
574 url
->location
, url
->host
,
575 url
->port
? ":" : "",
576 url
->port
? url
->port
: "");
578 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
579 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
581 if (uclient_request_supports_body(uh
->req_type
))
582 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
584 uclient_http_add_auth_header(uh
);
586 ustream_printf(uh
->us
, "\r\n");
588 uh
->state
= HTTP_STATE_HEADERS_SENT
;
591 static void uclient_http_headers_complete(struct uclient_http
*uh
)
593 enum auth_type auth_type
= uh
->auth_type
;
594 int seq
= uh
->uc
.seq
;
596 uh
->state
= HTTP_STATE_RECV_DATA
;
597 uh
->uc
.meta
= uh
->meta
.head
;
598 uclient_http_process_headers(uh
);
600 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
601 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
602 uclient_http_connect(&uh
->uc
);
603 uclient_http_send_headers(uh
);
604 uh
->state
= HTTP_STATE_REQUEST_DONE
;
608 if (uh
->uc
.cb
->header_done
)
609 uh
->uc
.cb
->header_done(&uh
->uc
);
611 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
614 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
616 uclient_notify_eof(uh
);
620 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
625 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
634 code
= strsep(&data
, " ");
638 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
642 uh
->state
= HTTP_STATE_RECV_HEADERS
;
647 uclient_http_headers_complete(uh
);
651 sep
= strchr(data
, ':');
657 for (name
= data
; *name
; name
++)
658 *name
= tolower(*name
);
661 while (isspace(*sep
))
664 blobmsg_add_string(&uh
->meta
, name
, sep
);
668 uh
->uc
.status_code
= 400;
670 uclient_notify_eof(uh
);
673 static void __uclient_notify_read(struct uclient_http
*uh
)
675 struct uclient
*uc
= &uh
->uc
;
676 unsigned int seq
= uh
->seq
;
680 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
683 data
= ustream_get_read_buf(uh
->us
, &len
);
687 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
692 sep
= strchr(data
, '\n');
697 if (sep
> data
&& sep
[-1] == '\r')
700 /* Check for multi-line HTTP headers */
705 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
714 cur_len
= next
- data
;
715 uclient_parse_http_line(uh
, data
);
719 ustream_consume(uh
->us
, cur_len
);
725 data
= ustream_get_read_buf(uh
->us
, &len
);
726 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
735 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
736 /* Now it's uclient user turn to read some data */
737 uloop_timeout_cancel(&uc
->connection_timeout
);
739 if (uc
->cb
->data_read
)
740 uc
->cb
->data_read(uc
);
744 static void __uclient_notify_write(struct uclient_http
*uh
)
746 struct uclient
*uc
= &uh
->uc
;
748 if (uc
->cb
->data_sent
)
749 uc
->cb
->data_sent(uc
);
752 static void uclient_notify_read(struct ustream
*us
, int bytes
)
754 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
756 __uclient_notify_read(uh
);
759 static void uclient_notify_write(struct ustream
*us
, int bytes
)
761 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
763 __uclient_notify_write(uh
);
766 static void uclient_notify_state(struct ustream
*us
)
768 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
770 if (uh
->ufd
.stream
.write_error
) {
771 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
774 uclient_notify_eof(uh
);
777 static int uclient_setup_http(struct uclient_http
*uh
)
779 struct ustream
*us
= &uh
->ufd
.stream
;
785 us
->string_data
= true;
786 us
->notify_state
= uclient_notify_state
;
787 us
->notify_read
= uclient_notify_read
;
788 us
->notify_write
= uclient_notify_write
;
790 ret
= uclient_do_connect(uh
, "80");
792 return UCLIENT_ERROR_CONNECT
;
797 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
799 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
801 __uclient_notify_read(uh
);
804 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
806 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
808 __uclient_notify_write(uh
);
811 static void uclient_ssl_notify_state(struct ustream
*us
)
813 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
815 uclient_notify_eof(uh
);
818 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
820 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
822 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
825 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
827 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
829 if (!uh
->ssl_require_validation
)
832 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
835 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
837 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
839 if (!uh
->ssl_require_validation
)
842 if (!uh
->ussl
.valid_cn
)
843 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
846 static int uclient_setup_https(struct uclient_http
*uh
)
848 struct ustream
*us
= &uh
->ussl
.stream
;
855 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
857 ret
= uclient_do_connect(uh
, "443");
859 return UCLIENT_ERROR_CONNECT
;
861 us
->string_data
= true;
862 us
->notify_state
= uclient_ssl_notify_state
;
863 us
->notify_read
= uclient_ssl_notify_read
;
864 us
->notify_write
= uclient_ssl_notify_write
;
865 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
866 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
867 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
868 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
869 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
870 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
875 static int uclient_http_connect(struct uclient
*cl
)
877 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
880 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
881 uclient_http_disconnect(uh
);
883 uclient_http_init_request(uh
);
888 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
891 ret
= uclient_setup_https(uh
);
893 ret
= uclient_setup_http(uh
);
898 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
900 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
902 uclient_http_disconnect(uh
);
905 static struct uclient
*uclient_http_alloc(void)
907 struct uclient_http
*uh
;
909 uh
= calloc_a(sizeof(*uh
));
910 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
911 blob_buf_init(&uh
->headers
, 0);
916 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
922 static void uclient_http_free(struct uclient
*cl
)
924 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
926 uclient_http_free_url_state(cl
);
927 uclient_http_free_ssl_ctx(uh
);
928 blob_buf_free(&uh
->headers
);
929 blob_buf_free(&uh
->meta
);
934 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
936 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
939 if (cl
->backend
!= &uclient_backend_http
)
942 if (uh
->state
> HTTP_STATE_INIT
)
945 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
946 if (strcmp(request_types
[i
], type
) != 0)
957 uclient_http_reset_headers(struct uclient
*cl
)
959 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
961 blob_buf_init(&uh
->headers
, 0);
967 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
969 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
971 if (cl
->backend
!= &uclient_backend_http
)
974 if (uh
->state
> HTTP_STATE_INIT
)
977 blobmsg_add_string(&uh
->headers
, name
, value
);
982 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
984 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
986 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
989 uclient_http_send_headers(uh
);
992 ustream_printf(uh
->us
, "%X\r\n", len
);
993 ustream_write(uh
->us
, buf
, len
, false);
994 ustream_printf(uh
->us
, "\r\n");
1001 uclient_http_request_done(struct uclient
*cl
)
1003 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1005 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1008 uclient_http_send_headers(uh
);
1009 if (uclient_request_supports_body(uh
->req_type
))
1010 ustream_printf(uh
->us
, "0\r\n\r\n");
1011 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1017 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1019 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1021 char *data
, *data_end
;
1023 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1026 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1027 if (!data
|| !read_len
)
1030 data_end
= data
+ read_len
;
1033 if (uh
->read_chunked
== 0) {
1036 if (data
[0] == '\r' && data
[1] == '\n') {
1041 sep
= strstr(data
, "\r\n");
1046 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1048 read_len
+= sep
+ 2 - data
;
1051 if (!uh
->read_chunked
) {
1053 uh
->uc
.data_eof
= true;
1057 if (len
> data_end
- data
)
1058 len
= data_end
- data
;
1060 if (uh
->read_chunked
>= 0) {
1061 if (len
> uh
->read_chunked
)
1062 len
= uh
->read_chunked
;
1064 uh
->read_chunked
-= len
;
1065 } else if (uh
->content_length
>= 0) {
1066 if (len
> uh
->content_length
)
1067 len
= uh
->content_length
;
1069 uh
->content_length
-= len
;
1070 if (!uh
->content_length
) {
1072 uh
->uc
.data_eof
= true;
1078 memcpy(buf
, data
, len
);
1082 ustream_consume(uh
->us
, read_len
);
1084 uclient_notify_eof(uh
);
1086 /* Now that we consumed something and if this isn't EOF, start timer again */
1087 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1088 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1093 int uclient_http_redirect(struct uclient
*cl
)
1095 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1096 struct blobmsg_policy location
= {
1098 .type
= BLOBMSG_TYPE_STRING
,
1100 struct uclient_url
*url
= cl
->url
;
1101 struct blob_attr
*tb
;
1103 if (cl
->backend
!= &uclient_backend_http
)
1106 switch (cl
->status_code
) {
1115 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1119 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1125 if (uclient_http_connect(cl
))
1128 uclient_http_request_done(cl
);
1133 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1134 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1136 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1138 if (cl
->backend
!= &uclient_backend_http
)
1141 uclient_http_free_url_state(cl
);
1143 uclient_http_free_ssl_ctx(uh
);
1146 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1151 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1153 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1155 if (cl
->backend
!= &uclient_backend_http
)
1160 uh
->usock_flags
= USOCK_IPV4ONLY
;
1163 uh
->usock_flags
= USOCK_IPV6ONLY
;
1166 uh
->usock_flags
= 0;
1173 const struct uclient_backend uclient_backend_http
= {
1174 .prefix
= uclient_http_prefix
,
1176 .alloc
= uclient_http_alloc
,
1177 .free
= uclient_http_free
,
1178 .connect
= uclient_http_connect
,
1179 .disconnect
= uclient_http_request_disconnect
,
1180 .update_url
= uclient_http_free_url_state
,
1181 .update_proxy_url
= uclient_http_free_url_state
,
1183 .read
= uclient_http_read
,
1184 .write
= uclient_http_send_data
,
1185 .request
= uclient_http_request_done
,