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
);
153 close(uh
->ufd
.fd
.fd
);
157 static void uclient_http_free_url_state(struct uclient
*cl
)
159 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
161 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
164 uclient_http_disconnect(uh
);
167 static void uclient_http_error(struct uclient_http
*uh
, int code
)
169 uh
->state
= HTTP_STATE_ERROR
;
171 ustream_state_change(uh
->us
);
172 uclient_backend_set_error(&uh
->uc
, code
);
175 static void uclient_http_request_disconnect(struct uclient
*cl
)
177 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
183 uh
->disconnect
= true;
184 uloop_timeout_set(&uh
->disconnect_t
, 1);
187 static void uclient_notify_eof(struct uclient_http
*uh
)
189 struct ustream
*us
= uh
->us
;
195 if (!us
->eof
&& !us
->write_error
)
198 if (ustream_pending_data(us
, false))
202 if ((uh
->content_length
< 0 && uh
->read_chunked
>= 0) ||
203 uh
->content_length
== 0)
204 uh
->uc
.data_eof
= true;
206 uclient_backend_set_eof(&uh
->uc
);
208 if (uh
->connection_close
)
209 uclient_http_request_disconnect(&uh
->uc
);
212 static void uclient_http_reset_state(struct uclient_http
*uh
)
215 uclient_backend_reset_state(&uh
->uc
);
216 uh
->read_chunked
= -1;
217 uh
->content_length
= -1;
219 uh
->disconnect
= false;
220 uh
->connection_close
= false;
221 uh
->state
= HTTP_STATE_INIT
;
223 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
224 uh
->auth_type
= AUTH_TYPE_NONE
;
227 static void uclient_http_init_request(struct uclient_http
*uh
)
230 uclient_http_reset_state(uh
);
231 blob_buf_init(&uh
->meta
, 0);
234 static enum auth_type
235 uclient_http_update_auth_type(struct uclient_http
*uh
)
238 return AUTH_TYPE_NONE
;
240 if (!strncasecmp(uh
->auth_str
, "basic", 5))
241 return AUTH_TYPE_BASIC
;
243 if (!strncasecmp(uh
->auth_str
, "digest", 6))
244 return AUTH_TYPE_DIGEST
;
246 return AUTH_TYPE_NONE
;
249 static void uclient_http_process_headers(struct uclient_http
*uh
)
252 HTTP_HDR_TRANSFER_ENCODING
,
254 HTTP_HDR_CONTENT_LENGTH
,
258 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
259 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
260 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
261 [HTTP_HDR_CONNECTION
] = hdr("connection"),
262 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
263 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
266 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
267 struct blob_attr
*cur
;
269 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
271 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
272 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
273 uh
->read_chunked
= 0;
275 cur
= tb
[HTTP_HDR_CONNECTION
];
276 if (cur
&& strstr(blobmsg_data(cur
), "close"))
277 uh
->connection_close
= true;
279 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
281 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
283 cur
= tb
[HTTP_HDR_AUTH
];
286 uh
->auth_str
= strdup(blobmsg_data(cur
));
289 uh
->auth_type
= uclient_http_update_auth_type(uh
);
292 static bool uclient_request_supports_body(enum request_type req_type
)
305 uclient_http_add_auth_basic(struct uclient_http
*uh
)
307 struct uclient_url
*url
= uh
->uc
.url
;
308 int auth_len
= strlen(url
->auth
);
314 auth_buf
= alloca(base64_len(auth_len
) + 1);
318 base64_encode(url
->auth
, auth_len
, auth_buf
);
319 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
324 static char *digest_unquote_sep(char **str
)
326 char *cur
= *str
+ 1;
358 static char *digest_sep(char **str
)
363 next
= strchr(*str
, ',');
368 *str
+= strlen(*str
);
374 static bool strmatch(char **str
, const char *prefix
)
376 int len
= strlen(prefix
);
378 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
386 get_cnonce(char *dest
)
392 f
= fopen("/dev/urandom", "r");
394 n
= fread(&val
, sizeof(val
), 1, f
);
400 bin_to_hex(dest
, &val
, sizeof(val
));
403 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
405 int available
= *len
- *ofs
;
413 required
= strlen(name
) + 4 + strlen(val
) * 2;
414 if (required
> available
)
415 *len
+= required
- available
+ 64;
417 *buf
= realloc(*buf
, *len
);
422 cur
+= sprintf(cur
, ", %s=\"", name
);
424 while ((next
= strchr(val
, '"'))) {
426 memcpy(cur
, val
, next
- val
);
430 cur
+= sprintf(cur
, "\\\"");
434 cur
+= sprintf(cur
, "%s\"", val
);
439 uclient_http_add_auth_digest(struct uclient_http
*uh
)
441 struct uclient_url
*url
= uh
->uc
.url
;
442 const char *realm
= NULL
, *opaque
= NULL
;
443 const char *user
, *password
;
444 char *buf
, *next
, *buf_orig
;
453 struct http_digest_data data
= {
455 .cnonce
= cnonce_str
,
459 len
= strlen(uh
->auth_str
) + 1;
471 strcpy(buf
, uh
->auth_str
);
474 buf_orig
= strsep(&buf
, " ");
478 const char **dest
= NULL
;
481 while (*next
&& isspace(*next
))
484 if (strmatch(&next
, "realm"))
486 else if (strmatch(&next
, "qop"))
488 else if (strmatch(&next
, "nonce"))
490 else if (strmatch(&next
, "opaque"))
492 else if (strmatch(&next
, "stale") ||
493 strmatch(&next
, "algorithm") ||
494 strmatch(&next
, "auth-param")) {
497 } else if (strmatch(&next
, "domain") ||
498 strmatch(&next
, "qop-options"))
505 *dest
= digest_unquote_sep(&next
);
508 if (!realm
|| !data
.qop
|| !data
.nonce
) {
513 sprintf(nc_str
, "%08x", uh
->nc
++);
514 get_cnonce(cnonce_str
);
517 data
.uri
= url
->location
;
518 data
.method
= request_types
[uh
->req_type
];
520 password
= strchr(url
->auth
, ':');
524 len
= password
- url
->auth
;
530 user_buf
= alloca(len
+ 1);
536 strncpy(user_buf
, url
->auth
, len
);
545 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
546 http_digest_calculate_response(hash
, &data
);
552 add_field(&buf
, &ofs
, &len
, "username", user
);
553 add_field(&buf
, &ofs
, &len
, "realm", realm
);
554 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
555 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
556 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
557 add_field(&buf
, &ofs
, &len
, "response", hash
);
559 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
561 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
574 uclient_http_add_auth_header(struct uclient_http
*uh
)
576 if (!uh
->uc
.url
->auth
)
579 switch (uh
->auth_type
) {
580 case AUTH_TYPE_UNKNOWN
:
583 case AUTH_TYPE_BASIC
:
584 return uclient_http_add_auth_basic(uh
);
585 case AUTH_TYPE_DIGEST
:
586 return uclient_http_add_auth_digest(uh
);
593 uclient_http_send_headers(struct uclient_http
*uh
)
595 struct uclient_url
*url
= uh
->uc
.url
;
596 struct blob_attr
*cur
;
597 enum request_type req_type
= uh
->req_type
;
602 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
605 if (uh
->uc
.proxy_url
)
606 url
= uh
->uc
.proxy_url
;
608 literal_ipv6
= strchr(url
->host
, ':');
610 ustream_printf(uh
->us
,
612 "Host: %s%s%s%s%s\r\n",
613 request_types
[req_type
], url
->location
,
614 literal_ipv6
? "[" : "",
616 literal_ipv6
? "]" : "",
617 url
->port
? ":" : "",
618 url
->port
? url
->port
: "");
620 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
621 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
623 if (uclient_request_supports_body(uh
->req_type
))
624 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
626 err
= uclient_http_add_auth_header(uh
);
630 ustream_printf(uh
->us
, "\r\n");
632 uh
->state
= HTTP_STATE_HEADERS_SENT
;
637 static void uclient_http_headers_complete(struct uclient_http
*uh
)
639 enum auth_type auth_type
= uh
->auth_type
;
640 int seq
= uh
->uc
.seq
;
642 uh
->state
= HTTP_STATE_RECV_DATA
;
643 uh
->uc
.meta
= uh
->meta
.head
;
644 uclient_http_process_headers(uh
);
646 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
647 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
648 uclient_http_connect(&uh
->uc
);
649 uclient_http_send_headers(uh
);
650 uh
->state
= HTTP_STATE_REQUEST_DONE
;
654 if (uh
->uc
.cb
->header_done
)
655 uh
->uc
.cb
->header_done(&uh
->uc
);
657 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
660 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
662 uclient_notify_eof(uh
);
666 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
671 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
680 code
= strsep(&data
, " ");
684 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
688 uh
->state
= HTTP_STATE_RECV_HEADERS
;
693 uclient_http_headers_complete(uh
);
697 sep
= strchr(data
, ':');
703 for (name
= data
; *name
; name
++)
704 *name
= tolower(*name
);
707 while (isspace(*sep
))
710 blobmsg_add_string(&uh
->meta
, name
, sep
);
714 uh
->uc
.status_code
= 400;
716 uclient_notify_eof(uh
);
719 static void __uclient_notify_read(struct uclient_http
*uh
)
721 struct uclient
*uc
= &uh
->uc
;
722 unsigned int seq
= uh
->seq
;
726 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
729 data
= ustream_get_read_buf(uh
->us
, &len
);
733 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
738 sep
= strchr(data
, '\n');
743 if (sep
> data
&& sep
[-1] == '\r')
746 /* Check for multi-line HTTP headers */
751 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
760 cur_len
= next
- data
;
761 uclient_parse_http_line(uh
, data
);
765 ustream_consume(uh
->us
, cur_len
);
771 data
= ustream_get_read_buf(uh
->us
, &len
);
772 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
781 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
782 /* Now it's uclient user turn to read some data */
783 uloop_timeout_cancel(&uc
->connection_timeout
);
785 if (uc
->cb
->data_read
)
786 uc
->cb
->data_read(uc
);
790 static void __uclient_notify_write(struct uclient_http
*uh
)
792 struct uclient
*uc
= &uh
->uc
;
794 if (uc
->cb
->data_sent
)
795 uc
->cb
->data_sent(uc
);
798 static void uclient_notify_read(struct ustream
*us
, int bytes
)
800 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
802 __uclient_notify_read(uh
);
805 static void uclient_notify_write(struct ustream
*us
, int bytes
)
807 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
809 __uclient_notify_write(uh
);
812 static void uclient_notify_state(struct ustream
*us
)
814 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
816 if (uh
->ufd
.stream
.write_error
) {
817 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
820 uclient_notify_eof(uh
);
823 static int uclient_setup_http(struct uclient_http
*uh
)
825 struct ustream
*us
= &uh
->ufd
.stream
;
831 us
->string_data
= true;
832 us
->notify_state
= uclient_notify_state
;
833 us
->notify_read
= uclient_notify_read
;
834 us
->notify_write
= uclient_notify_write
;
836 ret
= uclient_do_connect(uh
, "80");
838 return UCLIENT_ERROR_CONNECT
;
843 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
845 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
847 __uclient_notify_read(uh
);
850 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
852 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
854 __uclient_notify_write(uh
);
857 static void uclient_ssl_notify_state(struct ustream
*us
)
859 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
861 uclient_notify_eof(uh
);
864 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
866 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
868 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
871 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
873 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
875 if (!uh
->ssl_require_validation
)
878 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
881 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
883 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
885 if (!uh
->ssl_require_validation
)
888 if (!uh
->ussl
.valid_cn
)
889 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
892 static int uclient_setup_https(struct uclient_http
*uh
)
894 struct ustream
*us
= &uh
->ussl
.stream
;
901 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
903 ret
= uclient_do_connect(uh
, "443");
905 return UCLIENT_ERROR_CONNECT
;
907 us
->string_data
= true;
908 us
->notify_state
= uclient_ssl_notify_state
;
909 us
->notify_read
= uclient_ssl_notify_read
;
910 us
->notify_write
= uclient_ssl_notify_write
;
911 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
912 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
913 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
914 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
915 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
916 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
921 static int uclient_http_connect(struct uclient
*cl
)
923 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
926 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
927 uclient_http_disconnect(uh
);
929 uclient_http_init_request(uh
);
934 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
937 ret
= uclient_setup_https(uh
);
939 ret
= uclient_setup_http(uh
);
944 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
946 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
948 uclient_http_disconnect(uh
);
951 static struct uclient
*uclient_http_alloc(void)
953 struct uclient_http
*uh
;
955 uh
= calloc_a(sizeof(*uh
));
959 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
960 blob_buf_init(&uh
->headers
, 0);
965 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
971 static void uclient_http_free(struct uclient
*cl
)
973 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
975 uclient_http_free_url_state(cl
);
976 uclient_http_free_ssl_ctx(uh
);
977 blob_buf_free(&uh
->headers
);
978 blob_buf_free(&uh
->meta
);
983 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
985 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
988 if (cl
->backend
!= &uclient_backend_http
)
991 if (uh
->state
> HTTP_STATE_INIT
)
994 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
995 if (strcmp(request_types
[i
], type
) != 0)
1006 uclient_http_reset_headers(struct uclient
*cl
)
1008 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1010 blob_buf_init(&uh
->headers
, 0);
1016 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
1018 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1020 if (cl
->backend
!= &uclient_backend_http
)
1023 if (uh
->state
> HTTP_STATE_INIT
)
1026 blobmsg_add_string(&uh
->headers
, name
, value
);
1031 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
1033 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1036 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1039 err
= uclient_http_send_headers(uh
);
1044 ustream_printf(uh
->us
, "%X\r\n", len
);
1045 ustream_write(uh
->us
, buf
, len
, false);
1046 ustream_printf(uh
->us
, "\r\n");
1053 uclient_http_request_done(struct uclient
*cl
)
1055 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1058 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1061 err
= uclient_http_send_headers(uh
);
1065 if (uclient_request_supports_body(uh
->req_type
))
1066 ustream_printf(uh
->us
, "0\r\n\r\n");
1067 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1073 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1075 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1077 char *data
, *data_end
;
1079 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1082 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1083 if (!data
|| !read_len
)
1086 data_end
= data
+ read_len
;
1089 if (uh
->read_chunked
== 0) {
1092 if (data
[0] == '\r' && data
[1] == '\n') {
1097 sep
= strstr(data
, "\r\n");
1102 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1104 read_len
+= sep
+ 2 - data
;
1107 if (!uh
->read_chunked
) {
1109 uh
->uc
.data_eof
= true;
1113 if (len
> data_end
- data
)
1114 len
= data_end
- data
;
1116 if (uh
->read_chunked
>= 0) {
1117 if (len
> uh
->read_chunked
)
1118 len
= uh
->read_chunked
;
1120 uh
->read_chunked
-= len
;
1121 } else if (uh
->content_length
>= 0) {
1122 if (len
> uh
->content_length
)
1123 len
= uh
->content_length
;
1125 uh
->content_length
-= len
;
1126 if (!uh
->content_length
) {
1128 uh
->uc
.data_eof
= true;
1134 memcpy(buf
, data
, len
);
1138 ustream_consume(uh
->us
, read_len
);
1140 uclient_notify_eof(uh
);
1142 /* Now that we consumed something and if this isn't EOF, start timer again */
1143 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1144 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1149 int uclient_http_redirect(struct uclient
*cl
)
1151 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1152 struct blobmsg_policy location
= {
1154 .type
= BLOBMSG_TYPE_STRING
,
1156 struct uclient_url
*url
= cl
->url
;
1157 struct blob_attr
*tb
;
1159 if (cl
->backend
!= &uclient_backend_http
)
1162 switch (cl
->status_code
) {
1171 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1175 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1179 if (cl
->proxy_url
) {
1180 free(cl
->proxy_url
);
1181 cl
->proxy_url
= url
;
1188 if (uclient_http_connect(cl
))
1191 uclient_http_request_done(cl
);
1196 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1197 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1199 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1201 if (cl
->backend
!= &uclient_backend_http
)
1204 uclient_http_free_url_state(cl
);
1206 uclient_http_free_ssl_ctx(uh
);
1209 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1214 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1216 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1218 if (cl
->backend
!= &uclient_backend_http
)
1223 uh
->usock_flags
= USOCK_IPV4ONLY
;
1226 uh
->usock_flags
= USOCK_IPV6ONLY
;
1229 uh
->usock_flags
= 0;
1236 const struct uclient_backend uclient_backend_http
= {
1237 .prefix
= uclient_http_prefix
,
1239 .alloc
= uclient_http_alloc
,
1240 .free
= uclient_http_free
,
1241 .connect
= uclient_http_connect
,
1242 .disconnect
= uclient_http_request_disconnect
,
1243 .update_url
= uclient_http_free_url_state
,
1244 .update_proxy_url
= uclient_http_free_url_state
,
1246 .read
= uclient_http_read
,
1247 .write
= uclient_http_send_data
,
1248 .request
= uclient_http_request_done
,