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
));
830 ustream_fd_init(&uh
->ufd
, uh
->fd
);
834 us
->string_data
= true;
835 us
->notify_state
= uclient_notify_state
;
836 us
->notify_read
= uclient_notify_read
;
837 us
->notify_write
= uclient_notify_write
;
839 ret
= uclient_do_connect(uh
, "80");
841 return UCLIENT_ERROR_CONNECT
;
846 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
848 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
850 __uclient_notify_read(uh
);
853 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
855 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
857 __uclient_notify_write(uh
);
860 static void uclient_ssl_notify_state(struct ustream
*us
)
862 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
864 uclient_notify_eof(uh
);
867 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
869 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
870 struct uclient
*uc
= &uh
->uc
;
873 uc
->cb
->log_msg(uc
, UCLIENT_LOG_SSL_ERROR
, str
);
874 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
877 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
879 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
880 struct uclient
*uc
= &uh
->uc
;
882 if (!uh
->ssl_require_validation
)
886 uc
->cb
->log_msg(uc
, UCLIENT_LOG_SSL_VERIFY_ERROR
, str
);
887 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
890 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
892 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
894 if (!uh
->ssl_require_validation
)
897 if (!uh
->ussl
.valid_cn
)
898 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
901 static int uclient_setup_https(struct uclient_http
*uh
)
903 struct ustream
*us
= &uh
->ussl
.stream
;
906 memset(&uh
->ussl
, 0, sizeof(uh
->ussl
));
911 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
913 ret
= uclient_do_connect(uh
, "443");
915 return UCLIENT_ERROR_CONNECT
;
917 us
->string_data
= true;
918 us
->notify_state
= uclient_ssl_notify_state
;
919 us
->notify_read
= uclient_ssl_notify_read
;
920 us
->notify_write
= uclient_ssl_notify_write
;
921 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
922 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
923 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
924 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
925 uh
->ssl_ops
->init_fd(&uh
->ussl
, uh
->fd
, uh
->ssl_ctx
, false);
926 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
931 static int uclient_http_connect(struct uclient
*cl
)
933 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
936 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
937 uclient_http_disconnect(uh
);
939 uclient_http_init_request(uh
);
944 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
947 ret
= uclient_setup_https(uh
);
949 ret
= uclient_setup_http(uh
);
954 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
956 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
958 uclient_http_disconnect(uh
);
961 static struct uclient
*uclient_http_alloc(void)
963 struct uclient_http
*uh
;
965 uh
= calloc_a(sizeof(*uh
));
969 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
970 blob_buf_init(&uh
->headers
, 0);
975 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
981 static void uclient_http_free(struct uclient
*cl
)
983 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
985 uclient_http_free_url_state(cl
);
986 uclient_http_free_ssl_ctx(uh
);
987 blob_buf_free(&uh
->headers
);
988 blob_buf_free(&uh
->meta
);
993 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
995 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
998 if (cl
->backend
!= &uclient_backend_http
)
1001 if (uh
->state
> HTTP_STATE_INIT
)
1004 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
1005 if (strcmp(request_types
[i
], type
) != 0)
1016 uclient_http_reset_headers(struct uclient
*cl
)
1018 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1020 blob_buf_init(&uh
->headers
, 0);
1026 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
1028 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1030 if (cl
->backend
!= &uclient_backend_http
)
1033 if (uh
->state
> HTTP_STATE_INIT
)
1036 blobmsg_add_string(&uh
->headers
, name
, value
);
1041 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
1043 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1046 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1049 err
= uclient_http_send_headers(uh
);
1054 ustream_printf(uh
->us
, "%X\r\n", len
);
1055 ustream_write(uh
->us
, buf
, len
, false);
1056 ustream_printf(uh
->us
, "\r\n");
1063 uclient_http_request_done(struct uclient
*cl
)
1065 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1068 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1071 err
= uclient_http_send_headers(uh
);
1075 if (uclient_request_supports_body(uh
->req_type
))
1076 ustream_printf(uh
->us
, "0\r\n\r\n");
1077 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1083 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1085 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1087 char *data
, *data_end
;
1089 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1092 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1093 if (!data
|| !read_len
) {
1094 ustream_poll(uh
->us
);
1095 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1096 if (!data
|| !read_len
)
1100 data_end
= data
+ read_len
;
1103 if (uh
->read_chunked
== 0) {
1106 if (data
[0] == '\r' && data
[1] == '\n') {
1111 sep
= strstr(data
, "\r\n");
1116 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1118 read_len
+= sep
+ 2 - data
;
1121 if (!uh
->read_chunked
) {
1123 uh
->uc
.data_eof
= true;
1127 unsigned int diff
= data_end
- data
;
1131 if (uh
->read_chunked
>= 0) {
1132 if (len
> (unsigned long) uh
->read_chunked
)
1133 len
= uh
->read_chunked
;
1135 uh
->read_chunked
-= len
;
1136 } else if (uh
->content_length
>= 0) {
1137 if (len
> (unsigned long) uh
->content_length
)
1138 len
= uh
->content_length
;
1140 uh
->content_length
-= len
;
1141 if (!uh
->content_length
) {
1143 uh
->uc
.data_eof
= true;
1149 memcpy(buf
, data
, len
);
1153 ustream_consume(uh
->us
, read_len
);
1155 uclient_notify_eof(uh
);
1157 /* Now that we consumed something and if this isn't EOF, start timer again */
1158 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1159 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1164 int uclient_http_redirect(struct uclient
*cl
)
1166 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1167 struct blobmsg_policy location
= {
1169 .type
= BLOBMSG_TYPE_STRING
,
1171 struct uclient_url
*url
= cl
->url
;
1172 struct blob_attr
*tb
;
1174 if (cl
->backend
!= &uclient_backend_http
)
1177 if (!uclient_http_status_redirect(cl
))
1180 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1184 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1188 if (cl
->proxy_url
) {
1189 free(cl
->proxy_url
);
1190 cl
->proxy_url
= url
;
1197 if (uclient_http_connect(cl
))
1200 uclient_http_request_done(cl
);
1205 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1206 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1208 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1210 if (cl
->backend
!= &uclient_backend_http
)
1213 uclient_http_free_url_state(cl
);
1215 uclient_http_free_ssl_ctx(uh
);
1218 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1223 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1225 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1227 if (cl
->backend
!= &uclient_backend_http
)
1232 uh
->usock_flags
= USOCK_IPV4ONLY
;
1235 uh
->usock_flags
= USOCK_IPV6ONLY
;
1238 uh
->usock_flags
= 0;
1246 uclient_http_pending_bytes(struct uclient
*cl
, bool write
)
1248 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1250 return ustream_pending_data(uh
->us
, write
);
1253 const struct uclient_backend uclient_backend_http
= {
1254 .prefix
= uclient_http_prefix
,
1256 .alloc
= uclient_http_alloc
,
1257 .free
= uclient_http_free
,
1258 .connect
= uclient_http_connect
,
1259 .disconnect
= uclient_http_request_disconnect
,
1260 .update_url
= uclient_http_free_url_state
,
1261 .update_proxy_url
= uclient_http_free_url_state
,
1263 .read
= uclient_http_read
,
1264 .write
= uclient_http_send_data
,
1265 .request
= uclient_http_request_done
,
1266 .pending_bytes
= uclient_http_pending_bytes
,