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);
313 base64_encode(url
->auth
, auth_len
, auth_buf
);
314 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
317 static char *digest_unquote_sep(char **str
)
319 char *cur
= *str
+ 1;
351 static char *digest_sep(char **str
)
356 next
= strchr(*str
, ',');
361 *str
+= strlen(*str
);
367 static bool strmatch(char **str
, const char *prefix
)
369 int len
= strlen(prefix
);
371 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
379 get_cnonce(char *dest
)
385 f
= fopen("/dev/urandom", "r");
387 n
= fread(&val
, sizeof(val
), 1, f
);
393 bin_to_hex(dest
, &val
, sizeof(val
));
396 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
398 int available
= *len
- *ofs
;
406 required
= strlen(name
) + 4 + strlen(val
) * 2;
407 if (required
> available
)
408 *len
+= required
- available
+ 64;
410 *buf
= realloc(*buf
, *len
);
415 cur
+= sprintf(cur
, ", %s=\"", name
);
417 while ((next
= strchr(val
, '"'))) {
419 memcpy(cur
, val
, next
- val
);
423 cur
+= sprintf(cur
, "\\\"");
427 cur
+= sprintf(cur
, "%s\"", val
);
432 uclient_http_add_auth_digest(struct uclient_http
*uh
)
434 struct uclient_url
*url
= uh
->uc
.url
;
435 const char *realm
= NULL
, *opaque
= NULL
;
436 const char *user
, *password
;
445 struct http_digest_data data
= {
447 .cnonce
= cnonce_str
,
451 len
= strlen(uh
->auth_str
) + 1;
456 strcpy(buf
, uh
->auth_str
);
463 const char **dest
= NULL
;
466 while (*next
&& isspace(*next
))
469 if (strmatch(&next
, "realm"))
471 else if (strmatch(&next
, "qop"))
473 else if (strmatch(&next
, "nonce"))
475 else if (strmatch(&next
, "opaque"))
477 else if (strmatch(&next
, "stale") ||
478 strmatch(&next
, "algorithm") ||
479 strmatch(&next
, "auth-param")) {
482 } else if (strmatch(&next
, "domain") ||
483 strmatch(&next
, "qop-options"))
490 *dest
= digest_unquote_sep(&next
);
493 if (!realm
|| !data
.qop
|| !data
.nonce
)
496 sprintf(nc_str
, "%08x", uh
->nc
++);
497 get_cnonce(cnonce_str
);
500 data
.uri
= url
->location
;
501 data
.method
= request_types
[uh
->req_type
];
503 password
= strchr(url
->auth
, ':');
507 len
= password
- url
->auth
;
511 user_buf
= alloca(len
+ 1);
512 strncpy(user_buf
, url
->auth
, len
);
521 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
522 http_digest_calculate_response(hash
, &data
);
528 add_field(&buf
, &ofs
, &len
, "username", user
);
529 add_field(&buf
, &ofs
, &len
, "realm", realm
);
530 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
531 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
532 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
533 add_field(&buf
, &ofs
, &len
, "response", hash
);
535 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
537 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
542 uclient_http_add_auth_header(struct uclient_http
*uh
)
544 if (!uh
->uc
.url
->auth
)
547 switch (uh
->auth_type
) {
548 case AUTH_TYPE_UNKNOWN
:
551 case AUTH_TYPE_BASIC
:
552 uclient_http_add_auth_basic(uh
);
554 case AUTH_TYPE_DIGEST
:
555 uclient_http_add_auth_digest(uh
);
561 uclient_http_send_headers(struct uclient_http
*uh
)
563 struct uclient_url
*url
= uh
->uc
.url
;
564 struct blob_attr
*cur
;
565 enum request_type req_type
= uh
->req_type
;
569 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
572 if (uh
->uc
.proxy_url
)
573 url
= uh
->uc
.proxy_url
;
575 literal_ipv6
= strchr(url
->host
, ':');
577 ustream_printf(uh
->us
,
579 "Host: %s%s%s%s%s\r\n",
580 request_types
[req_type
], url
->location
,
581 literal_ipv6
? "[" : "",
583 literal_ipv6
? "]" : "",
584 url
->port
? ":" : "",
585 url
->port
? url
->port
: "");
587 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
588 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
590 if (uclient_request_supports_body(uh
->req_type
))
591 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
593 uclient_http_add_auth_header(uh
);
595 ustream_printf(uh
->us
, "\r\n");
597 uh
->state
= HTTP_STATE_HEADERS_SENT
;
600 static void uclient_http_headers_complete(struct uclient_http
*uh
)
602 enum auth_type auth_type
= uh
->auth_type
;
603 int seq
= uh
->uc
.seq
;
605 uh
->state
= HTTP_STATE_RECV_DATA
;
606 uh
->uc
.meta
= uh
->meta
.head
;
607 uclient_http_process_headers(uh
);
609 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
610 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
611 uclient_http_connect(&uh
->uc
);
612 uclient_http_send_headers(uh
);
613 uh
->state
= HTTP_STATE_REQUEST_DONE
;
617 if (uh
->uc
.cb
->header_done
)
618 uh
->uc
.cb
->header_done(&uh
->uc
);
620 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
623 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
625 uclient_notify_eof(uh
);
629 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
634 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
643 code
= strsep(&data
, " ");
647 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
651 uh
->state
= HTTP_STATE_RECV_HEADERS
;
656 uclient_http_headers_complete(uh
);
660 sep
= strchr(data
, ':');
666 for (name
= data
; *name
; name
++)
667 *name
= tolower(*name
);
670 while (isspace(*sep
))
673 blobmsg_add_string(&uh
->meta
, name
, sep
);
677 uh
->uc
.status_code
= 400;
679 uclient_notify_eof(uh
);
682 static void __uclient_notify_read(struct uclient_http
*uh
)
684 struct uclient
*uc
= &uh
->uc
;
685 unsigned int seq
= uh
->seq
;
689 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
692 data
= ustream_get_read_buf(uh
->us
, &len
);
696 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
701 sep
= strchr(data
, '\n');
706 if (sep
> data
&& sep
[-1] == '\r')
709 /* Check for multi-line HTTP headers */
714 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
723 cur_len
= next
- data
;
724 uclient_parse_http_line(uh
, data
);
728 ustream_consume(uh
->us
, cur_len
);
734 data
= ustream_get_read_buf(uh
->us
, &len
);
735 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
744 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
745 /* Now it's uclient user turn to read some data */
746 uloop_timeout_cancel(&uc
->connection_timeout
);
748 if (uc
->cb
->data_read
)
749 uc
->cb
->data_read(uc
);
753 static void __uclient_notify_write(struct uclient_http
*uh
)
755 struct uclient
*uc
= &uh
->uc
;
757 if (uc
->cb
->data_sent
)
758 uc
->cb
->data_sent(uc
);
761 static void uclient_notify_read(struct ustream
*us
, int bytes
)
763 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
765 __uclient_notify_read(uh
);
768 static void uclient_notify_write(struct ustream
*us
, int bytes
)
770 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
772 __uclient_notify_write(uh
);
775 static void uclient_notify_state(struct ustream
*us
)
777 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
779 if (uh
->ufd
.stream
.write_error
) {
780 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
783 uclient_notify_eof(uh
);
786 static int uclient_setup_http(struct uclient_http
*uh
)
788 struct ustream
*us
= &uh
->ufd
.stream
;
794 us
->string_data
= true;
795 us
->notify_state
= uclient_notify_state
;
796 us
->notify_read
= uclient_notify_read
;
797 us
->notify_write
= uclient_notify_write
;
799 ret
= uclient_do_connect(uh
, "80");
801 return UCLIENT_ERROR_CONNECT
;
806 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
808 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
810 __uclient_notify_read(uh
);
813 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
815 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
817 __uclient_notify_write(uh
);
820 static void uclient_ssl_notify_state(struct ustream
*us
)
822 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
824 uclient_notify_eof(uh
);
827 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
829 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
831 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
834 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
836 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
838 if (!uh
->ssl_require_validation
)
841 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
844 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
846 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
848 if (!uh
->ssl_require_validation
)
851 if (!uh
->ussl
.valid_cn
)
852 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
855 static int uclient_setup_https(struct uclient_http
*uh
)
857 struct ustream
*us
= &uh
->ussl
.stream
;
864 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
866 ret
= uclient_do_connect(uh
, "443");
868 return UCLIENT_ERROR_CONNECT
;
870 us
->string_data
= true;
871 us
->notify_state
= uclient_ssl_notify_state
;
872 us
->notify_read
= uclient_ssl_notify_read
;
873 us
->notify_write
= uclient_ssl_notify_write
;
874 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
875 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
876 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
877 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
878 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
879 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
884 static int uclient_http_connect(struct uclient
*cl
)
886 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
889 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
890 uclient_http_disconnect(uh
);
892 uclient_http_init_request(uh
);
897 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
900 ret
= uclient_setup_https(uh
);
902 ret
= uclient_setup_http(uh
);
907 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
909 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
911 uclient_http_disconnect(uh
);
914 static struct uclient
*uclient_http_alloc(void)
916 struct uclient_http
*uh
;
918 uh
= calloc_a(sizeof(*uh
));
919 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
920 blob_buf_init(&uh
->headers
, 0);
925 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
931 static void uclient_http_free(struct uclient
*cl
)
933 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
935 uclient_http_free_url_state(cl
);
936 uclient_http_free_ssl_ctx(uh
);
937 blob_buf_free(&uh
->headers
);
938 blob_buf_free(&uh
->meta
);
943 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
945 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
948 if (cl
->backend
!= &uclient_backend_http
)
951 if (uh
->state
> HTTP_STATE_INIT
)
954 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
955 if (strcmp(request_types
[i
], type
) != 0)
966 uclient_http_reset_headers(struct uclient
*cl
)
968 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
970 blob_buf_init(&uh
->headers
, 0);
976 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
978 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
980 if (cl
->backend
!= &uclient_backend_http
)
983 if (uh
->state
> HTTP_STATE_INIT
)
986 blobmsg_add_string(&uh
->headers
, name
, value
);
991 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
993 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
995 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
998 uclient_http_send_headers(uh
);
1001 ustream_printf(uh
->us
, "%X\r\n", len
);
1002 ustream_write(uh
->us
, buf
, len
, false);
1003 ustream_printf(uh
->us
, "\r\n");
1010 uclient_http_request_done(struct uclient
*cl
)
1012 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1014 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1017 uclient_http_send_headers(uh
);
1018 if (uclient_request_supports_body(uh
->req_type
))
1019 ustream_printf(uh
->us
, "0\r\n\r\n");
1020 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1026 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1028 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1030 char *data
, *data_end
;
1032 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1035 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1036 if (!data
|| !read_len
)
1039 data_end
= data
+ read_len
;
1042 if (uh
->read_chunked
== 0) {
1045 if (data
[0] == '\r' && data
[1] == '\n') {
1050 sep
= strstr(data
, "\r\n");
1055 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1057 read_len
+= sep
+ 2 - data
;
1060 if (!uh
->read_chunked
) {
1062 uh
->uc
.data_eof
= true;
1066 if (len
> data_end
- data
)
1067 len
= data_end
- data
;
1069 if (uh
->read_chunked
>= 0) {
1070 if (len
> uh
->read_chunked
)
1071 len
= uh
->read_chunked
;
1073 uh
->read_chunked
-= len
;
1074 } else if (uh
->content_length
>= 0) {
1075 if (len
> uh
->content_length
)
1076 len
= uh
->content_length
;
1078 uh
->content_length
-= len
;
1079 if (!uh
->content_length
) {
1081 uh
->uc
.data_eof
= true;
1087 memcpy(buf
, data
, len
);
1091 ustream_consume(uh
->us
, read_len
);
1093 uclient_notify_eof(uh
);
1095 /* Now that we consumed something and if this isn't EOF, start timer again */
1096 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1097 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1102 int uclient_http_redirect(struct uclient
*cl
)
1104 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1105 struct blobmsg_policy location
= {
1107 .type
= BLOBMSG_TYPE_STRING
,
1109 struct uclient_url
*url
= cl
->url
;
1110 struct blob_attr
*tb
;
1112 if (cl
->backend
!= &uclient_backend_http
)
1115 switch (cl
->status_code
) {
1124 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1128 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1134 if (uclient_http_connect(cl
))
1137 uclient_http_request_done(cl
);
1142 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1143 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1145 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1147 if (cl
->backend
!= &uclient_backend_http
)
1150 uclient_http_free_url_state(cl
);
1152 uclient_http_free_ssl_ctx(uh
);
1155 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1160 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1162 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1164 if (cl
->backend
!= &uclient_backend_http
)
1169 uh
->usock_flags
= USOCK_IPV4ONLY
;
1172 uh
->usock_flags
= USOCK_IPV6ONLY
;
1175 uh
->usock_flags
= 0;
1182 const struct uclient_backend uclient_backend_http
= {
1183 .prefix
= uclient_http_prefix
,
1185 .alloc
= uclient_http_alloc
,
1186 .free
= uclient_http_free
,
1187 .connect
= uclient_http_connect
,
1188 .disconnect
= uclient_http_request_disconnect
,
1189 .update_url
= uclient_http_free_url_state
,
1190 .update_proxy_url
= uclient_http_free_url_state
,
1192 .read
= uclient_http_read
,
1193 .write
= uclient_http_send_data
,
1194 .request
= uclient_http_request_done
,