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
;
76 struct ustream_fd ufd
;
77 struct ustream_ssl ussl
;
80 struct uloop_timeout disconnect_t
;
84 bool ssl_require_validation
;
87 bool connection_close
;
89 enum request_type req_type
;
90 enum http_state state
;
92 enum auth_type auth_type
;
102 struct blob_buf headers
;
103 struct blob_buf meta
;
112 static const char * const uclient_http_prefix
[] = {
113 [PREFIX_HTTP
] = "http://",
114 [PREFIX_HTTPS
] = "https://",
115 [__PREFIX_MAX
] = NULL
118 static int uclient_http_connect(struct uclient
*cl
);
120 static int uclient_do_connect(struct uclient_http
*uh
, const char *port
)
125 if (uh
->uc
.url
->port
)
126 port
= uh
->uc
.url
->port
;
128 memset(&uh
->uc
.remote_addr
, 0, sizeof(uh
->uc
.remote_addr
));
130 fd
= usock_inet_timeout(USOCK_TCP
| USOCK_NONBLOCK
| uh
->usock_flags
,
131 uh
->uc
.url
->host
, port
, &uh
->uc
.remote_addr
,
132 uh
->uc
.timeout_msecs
);
136 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
139 sl
= sizeof(uh
->uc
.local_addr
);
140 memset(&uh
->uc
.local_addr
, 0, sl
);
141 getsockname(fd
, &uh
->uc
.local_addr
.sa
, &sl
);
146 static void uclient_http_disconnect(struct uclient_http
*uh
)
148 uloop_timeout_cancel(&uh
->disconnect_t
);
153 ustream_free(&uh
->ussl
.stream
);
155 ustream_free(&uh
->ufd
.stream
);
161 static void uclient_http_free_url_state(struct uclient
*cl
)
163 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
165 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
168 uclient_http_disconnect(uh
);
171 static void uclient_http_error(struct uclient_http
*uh
, int code
)
173 uh
->state
= HTTP_STATE_ERROR
;
175 ustream_state_change(uh
->us
);
176 uclient_backend_set_error(&uh
->uc
, code
);
179 static void uclient_http_request_disconnect(struct uclient
*cl
)
181 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
187 uh
->disconnect
= true;
188 uloop_timeout_set(&uh
->disconnect_t
, 1);
191 static void uclient_notify_eof(struct uclient_http
*uh
)
193 struct ustream
*us
= uh
->us
;
199 if (!us
->eof
&& !us
->write_error
)
202 if (ustream_pending_data(us
, false))
206 if ((uh
->content_length
< 0 && uh
->read_chunked
>= 0) ||
207 uh
->content_length
== 0)
208 uh
->uc
.data_eof
= true;
210 uclient_backend_set_eof(&uh
->uc
);
212 if (uh
->connection_close
)
213 uclient_http_request_disconnect(&uh
->uc
);
216 static void uclient_http_reset_state(struct uclient_http
*uh
)
219 uclient_backend_reset_state(&uh
->uc
);
220 uh
->read_chunked
= -1;
221 uh
->content_length
= -1;
223 uh
->disconnect
= false;
224 uh
->connection_close
= false;
225 uh
->state
= HTTP_STATE_INIT
;
227 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
228 uh
->auth_type
= AUTH_TYPE_NONE
;
231 static void uclient_http_init_request(struct uclient_http
*uh
)
234 uclient_http_reset_state(uh
);
235 blob_buf_init(&uh
->meta
, 0);
238 static enum auth_type
239 uclient_http_update_auth_type(struct uclient_http
*uh
)
242 return AUTH_TYPE_NONE
;
244 if (!strncasecmp(uh
->auth_str
, "basic", 5))
245 return AUTH_TYPE_BASIC
;
247 if (!strncasecmp(uh
->auth_str
, "digest", 6))
248 return AUTH_TYPE_DIGEST
;
250 return AUTH_TYPE_NONE
;
253 static void uclient_http_process_headers(struct uclient_http
*uh
)
256 HTTP_HDR_TRANSFER_ENCODING
,
258 HTTP_HDR_CONTENT_LENGTH
,
262 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
263 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
264 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
265 [HTTP_HDR_CONNECTION
] = hdr("connection"),
266 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
267 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
270 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
271 struct blob_attr
*cur
;
273 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
275 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
276 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
277 uh
->read_chunked
= 0;
279 cur
= tb
[HTTP_HDR_CONNECTION
];
280 if (cur
&& strstr(blobmsg_data(cur
), "close"))
281 uh
->connection_close
= true;
283 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
285 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
287 cur
= tb
[HTTP_HDR_AUTH
];
290 uh
->auth_str
= strdup(blobmsg_data(cur
));
293 uh
->auth_type
= uclient_http_update_auth_type(uh
);
296 static bool uclient_request_supports_body(enum request_type req_type
)
309 uclient_http_add_auth_basic(struct uclient_http
*uh
)
311 struct uclient_url
*url
= uh
->uc
.url
;
312 int auth_len
= strlen(url
->auth
);
318 auth_buf
= alloca(base64_len(auth_len
) + 1);
322 base64_encode(url
->auth
, auth_len
, auth_buf
);
323 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
328 static char *digest_unquote_sep(char **str
)
330 char *cur
= *str
+ 1;
362 static char *digest_sep(char **str
)
367 next
= strchr(*str
, ',');
372 *str
+= strlen(*str
);
378 static bool strmatch(char **str
, const char *prefix
)
380 int len
= strlen(prefix
);
382 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
390 get_cnonce(char *dest
)
396 f
= fopen("/dev/urandom", "r");
398 n
= fread(&val
, sizeof(val
), 1, f
);
404 bin_to_hex(dest
, &val
, sizeof(val
));
407 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
409 int available
= *len
- *ofs
;
417 required
= strlen(name
) + 4 + strlen(val
) * 2;
418 if (required
> available
)
419 *len
+= required
- available
+ 64;
421 *buf
= realloc(*buf
, *len
);
426 cur
+= sprintf(cur
, ", %s=\"", name
);
428 while ((next
= strchr(val
, '"'))) {
430 memcpy(cur
, val
, next
- val
);
434 cur
+= sprintf(cur
, "\\\"");
438 cur
+= sprintf(cur
, "%s\"", val
);
443 uclient_http_add_auth_digest(struct uclient_http
*uh
)
445 struct uclient_url
*url
= uh
->uc
.url
;
446 const char *realm
= NULL
, *opaque
= NULL
;
447 const char *user
, *password
;
457 struct http_digest_data data
= {
459 .cnonce
= cnonce_str
,
463 len
= strlen(uh
->auth_str
) + 1;
475 strcpy(buf
, uh
->auth_str
);
482 const char **dest
= NULL
;
485 while (*next
&& isspace(*next
))
488 if (strmatch(&next
, "realm"))
490 else if (strmatch(&next
, "qop"))
492 else if (strmatch(&next
, "nonce"))
494 else if (strmatch(&next
, "opaque"))
496 else if (strmatch(&next
, "stale") ||
497 strmatch(&next
, "algorithm") ||
498 strmatch(&next
, "auth-param")) {
501 } else if (strmatch(&next
, "domain") ||
502 strmatch(&next
, "qop-options"))
509 *dest
= digest_unquote_sep(&next
);
512 if (!realm
|| !data
.qop
|| !data
.nonce
) {
517 sprintf(nc_str
, "%08x", uh
->nc
++);
518 get_cnonce(cnonce_str
);
521 data
.uri
= url
->location
;
522 data
.method
= request_types
[uh
->req_type
];
524 password
= strchr(url
->auth
, ':');
528 len
= password
- url
->auth
;
534 user_buf
= alloca(len
+ 1);
540 strncpy(user_buf
, url
->auth
, len
);
549 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
550 http_digest_calculate_response(hash
, &data
);
556 add_field(&buf
, &ofs
, &len
, "username", user
);
557 add_field(&buf
, &ofs
, &len
, "realm", realm
);
558 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
559 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
560 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
561 add_field(&buf
, &ofs
, &len
, "response", hash
);
563 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
565 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
576 uclient_http_add_auth_header(struct uclient_http
*uh
)
578 if (!uh
->uc
.url
->auth
)
581 switch (uh
->auth_type
) {
582 case AUTH_TYPE_UNKNOWN
:
585 case AUTH_TYPE_BASIC
:
586 return uclient_http_add_auth_basic(uh
);
587 case AUTH_TYPE_DIGEST
:
588 return uclient_http_add_auth_digest(uh
);
595 uclient_http_send_headers(struct uclient_http
*uh
)
597 struct uclient_url
*url
= uh
->uc
.url
;
598 struct blob_attr
*cur
;
599 enum request_type req_type
= uh
->req_type
;
604 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
607 if (uh
->uc
.proxy_url
)
608 url
= uh
->uc
.proxy_url
;
610 literal_ipv6
= strchr(url
->host
, ':');
612 ustream_printf(uh
->us
,
614 "Host: %s%s%s%s%s\r\n",
615 request_types
[req_type
], url
->location
,
616 literal_ipv6
? "[" : "",
618 literal_ipv6
? "]" : "",
619 url
->port
? ":" : "",
620 url
->port
? url
->port
: "");
622 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
623 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
625 if (uclient_request_supports_body(uh
->req_type
))
626 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
628 err
= uclient_http_add_auth_header(uh
);
632 ustream_printf(uh
->us
, "\r\n");
634 uh
->state
= HTTP_STATE_HEADERS_SENT
;
639 static void uclient_http_headers_complete(struct uclient_http
*uh
)
641 enum auth_type auth_type
= uh
->auth_type
;
642 int seq
= uh
->uc
.seq
;
644 uh
->state
= HTTP_STATE_RECV_DATA
;
645 uh
->uc
.meta
= uh
->meta
.head
;
646 uclient_http_process_headers(uh
);
648 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
649 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
650 uclient_http_connect(&uh
->uc
);
651 uclient_http_send_headers(uh
);
652 uh
->state
= HTTP_STATE_REQUEST_DONE
;
656 if (uh
->uc
.cb
->header_done
)
657 uh
->uc
.cb
->header_done(&uh
->uc
);
659 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
662 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204 ||
663 uh
->content_length
== 0) {
665 uclient_notify_eof(uh
);
669 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
674 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
683 code
= strsep(&data
, " ");
687 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
691 uh
->state
= HTTP_STATE_RECV_HEADERS
;
696 uclient_http_headers_complete(uh
);
700 sep
= strchr(data
, ':');
706 for (name
= data
; *name
; name
++)
707 *name
= tolower(*name
);
710 while (isspace(*sep
))
713 blobmsg_add_string(&uh
->meta
, name
, sep
);
717 uh
->uc
.status_code
= 400;
719 uclient_notify_eof(uh
);
722 static void __uclient_notify_read(struct uclient_http
*uh
)
724 struct uclient
*uc
= &uh
->uc
;
725 unsigned int seq
= uh
->seq
;
729 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
732 data
= ustream_get_read_buf(uh
->us
, &len
);
736 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
741 sep
= strchr(data
, '\n');
746 if (sep
> data
&& sep
[-1] == '\r')
749 /* Check for multi-line HTTP headers */
754 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
763 cur_len
= next
- data
;
764 uclient_parse_http_line(uh
, data
);
768 ustream_consume(uh
->us
, cur_len
);
774 data
= ustream_get_read_buf(uh
->us
, &len
);
775 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
784 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
785 /* Now it's uclient user turn to read some data */
786 uloop_timeout_cancel(&uc
->connection_timeout
);
787 uclient_backend_read_notify(uc
);
791 static void __uclient_notify_write(struct uclient_http
*uh
)
793 struct uclient
*uc
= &uh
->uc
;
795 if (uc
->cb
->data_sent
)
796 uc
->cb
->data_sent(uc
);
799 static void uclient_notify_read(struct ustream
*us
, int bytes
)
801 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
803 __uclient_notify_read(uh
);
806 static void uclient_notify_write(struct ustream
*us
, int bytes
)
808 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
810 __uclient_notify_write(uh
);
813 static void uclient_notify_state(struct ustream
*us
)
815 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
817 if (uh
->ufd
.stream
.write_error
) {
818 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
821 uclient_notify_eof(uh
);
824 static int uclient_setup_http(struct uclient_http
*uh
)
826 struct ustream
*us
= &uh
->ufd
.stream
;
829 memset(&uh
->ufd
, 0, sizeof(uh
->ufd
));
833 us
->string_data
= true;
834 us
->notify_state
= uclient_notify_state
;
835 us
->notify_read
= uclient_notify_read
;
836 us
->notify_write
= uclient_notify_write
;
838 ret
= uclient_do_connect(uh
, "80");
840 return UCLIENT_ERROR_CONNECT
;
842 ustream_fd_init(&uh
->ufd
, uh
->fd
);
847 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
849 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
851 __uclient_notify_read(uh
);
854 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
856 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
858 __uclient_notify_write(uh
);
861 static void uclient_ssl_notify_state(struct ustream
*us
)
863 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
865 uclient_notify_eof(uh
);
868 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
870 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
871 struct uclient
*uc
= &uh
->uc
;
874 uc
->cb
->log_msg(uc
, UCLIENT_LOG_SSL_ERROR
, str
);
875 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
878 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
880 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
881 struct uclient
*uc
= &uh
->uc
;
883 if (!uh
->ssl_require_validation
)
887 uc
->cb
->log_msg(uc
, UCLIENT_LOG_SSL_VERIFY_ERROR
, str
);
888 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
891 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
893 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
895 if (!uh
->ssl_require_validation
)
898 if (!uh
->ussl
.valid_cn
)
899 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
902 static int uclient_setup_https(struct uclient_http
*uh
)
904 struct ustream
*us
= &uh
->ussl
.stream
;
907 memset(&uh
->ussl
, 0, sizeof(uh
->ussl
));
912 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
914 ret
= uclient_do_connect(uh
, "443");
916 return UCLIENT_ERROR_CONNECT
;
918 us
->string_data
= true;
919 us
->notify_state
= uclient_ssl_notify_state
;
920 us
->notify_read
= uclient_ssl_notify_read
;
921 us
->notify_write
= uclient_ssl_notify_write
;
922 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
923 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
924 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
925 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
926 uh
->ssl_ops
->init_fd(&uh
->ussl
, uh
->fd
, uh
->ssl_ctx
, false);
927 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
932 static int uclient_http_connect(struct uclient
*cl
)
934 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
937 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
938 uclient_http_disconnect(uh
);
940 uclient_http_init_request(uh
);
945 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
948 ret
= uclient_setup_https(uh
);
950 ret
= uclient_setup_http(uh
);
955 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
957 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
959 uclient_http_disconnect(uh
);
962 static struct uclient
*uclient_http_alloc(void)
964 struct uclient_http
*uh
;
966 uh
= calloc_a(sizeof(*uh
));
970 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
971 blob_buf_init(&uh
->headers
, 0);
976 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
982 static void uclient_http_free(struct uclient
*cl
)
984 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
986 uclient_http_free_url_state(cl
);
987 uclient_http_free_ssl_ctx(uh
);
988 blob_buf_free(&uh
->headers
);
989 blob_buf_free(&uh
->meta
);
994 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
996 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
999 if (cl
->backend
!= &uclient_backend_http
)
1002 if (uh
->state
> HTTP_STATE_INIT
)
1005 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
1006 if (strcmp(request_types
[i
], type
) != 0)
1017 uclient_http_reset_headers(struct uclient
*cl
)
1019 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1021 blob_buf_init(&uh
->headers
, 0);
1027 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
1029 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1031 if (cl
->backend
!= &uclient_backend_http
)
1034 if (uh
->state
> HTTP_STATE_INIT
)
1037 blobmsg_add_string(&uh
->headers
, name
, value
);
1042 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
1044 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1047 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1050 err
= uclient_http_send_headers(uh
);
1055 ustream_printf(uh
->us
, "%X\r\n", len
);
1056 ustream_write(uh
->us
, buf
, len
, false);
1057 ustream_printf(uh
->us
, "\r\n");
1064 uclient_http_request_done(struct uclient
*cl
)
1066 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1069 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1072 err
= uclient_http_send_headers(uh
);
1076 if (uclient_request_supports_body(uh
->req_type
))
1077 ustream_printf(uh
->us
, "0\r\n\r\n");
1078 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1084 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1086 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1088 char *data
, *data_end
;
1090 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1093 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1094 if (!data
|| !read_len
) {
1095 ustream_poll(uh
->us
);
1096 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1097 if (!data
|| !read_len
)
1101 data_end
= data
+ read_len
;
1104 if (uh
->read_chunked
== 0) {
1107 if (data
[0] == '\r' && data
[1] == '\n') {
1112 sep
= strstr(data
, "\r\n");
1117 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1119 read_len
+= sep
+ 2 - data
;
1122 if (!uh
->read_chunked
) {
1124 uh
->uc
.data_eof
= true;
1128 unsigned int diff
= data_end
- data
;
1132 if (uh
->read_chunked
>= 0) {
1133 if (len
> (unsigned long) uh
->read_chunked
)
1134 len
= uh
->read_chunked
;
1136 uh
->read_chunked
-= len
;
1137 } else if (uh
->content_length
>= 0) {
1138 if (len
> (unsigned long) uh
->content_length
)
1139 len
= uh
->content_length
;
1141 uh
->content_length
-= len
;
1142 if (!uh
->content_length
) {
1144 uh
->uc
.data_eof
= true;
1150 memcpy(buf
, data
, len
);
1154 ustream_consume(uh
->us
, read_len
);
1156 uclient_notify_eof(uh
);
1158 /* Now that we consumed something and if this isn't EOF, start timer again */
1159 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1160 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1165 int uclient_http_redirect(struct uclient
*cl
)
1167 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1168 struct blobmsg_policy location
= {
1170 .type
= BLOBMSG_TYPE_STRING
,
1172 struct uclient_url
*url
= cl
->url
;
1173 struct blob_attr
*tb
;
1175 if (cl
->backend
!= &uclient_backend_http
)
1178 if (!uclient_http_status_redirect(cl
))
1181 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1185 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1189 if (cl
->proxy_url
) {
1190 free(cl
->proxy_url
);
1191 cl
->proxy_url
= url
;
1198 if (uclient_http_connect(cl
))
1201 uclient_http_request_done(cl
);
1206 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1207 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1209 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1211 if (cl
->backend
!= &uclient_backend_http
)
1214 uclient_http_free_url_state(cl
);
1216 uclient_http_free_ssl_ctx(uh
);
1219 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1224 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1226 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1228 if (cl
->backend
!= &uclient_backend_http
)
1233 uh
->usock_flags
= USOCK_IPV4ONLY
;
1236 uh
->usock_flags
= USOCK_IPV6ONLY
;
1239 uh
->usock_flags
= 0;
1247 uclient_http_pending_bytes(struct uclient
*cl
, bool write
)
1249 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1251 return ustream_pending_data(uh
->us
, write
);
1254 const struct uclient_backend uclient_backend_http
= {
1255 .prefix
= uclient_http_prefix
,
1257 .alloc
= uclient_http_alloc
,
1258 .free
= uclient_http_free
,
1259 .connect
= uclient_http_connect
,
1260 .disconnect
= uclient_http_request_disconnect
,
1261 .update_url
= uclient_http_free_url_state
,
1262 .update_proxy_url
= uclient_http_free_url_state
,
1264 .read
= uclient_http_read
,
1265 .write
= uclient_http_send_data
,
1266 .request
= uclient_http_request_done
,
1267 .pending_bytes
= uclient_http_pending_bytes
,