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
->uc
.data_eof
= true;
205 uclient_backend_set_eof(&uh
->uc
);
207 if (uh
->connection_close
)
208 uclient_http_request_disconnect(&uh
->uc
);
211 static void uclient_http_reset_state(struct uclient_http
*uh
)
214 uclient_backend_reset_state(&uh
->uc
);
215 uh
->read_chunked
= -1;
216 uh
->content_length
= -1;
218 uh
->disconnect
= false;
219 uh
->connection_close
= false;
220 uh
->state
= HTTP_STATE_INIT
;
222 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
223 uh
->auth_type
= AUTH_TYPE_NONE
;
226 static void uclient_http_init_request(struct uclient_http
*uh
)
229 uclient_http_reset_state(uh
);
230 blob_buf_init(&uh
->meta
, 0);
233 static enum auth_type
234 uclient_http_update_auth_type(struct uclient_http
*uh
)
237 return AUTH_TYPE_NONE
;
239 if (!strncasecmp(uh
->auth_str
, "basic", 5))
240 return AUTH_TYPE_BASIC
;
242 if (!strncasecmp(uh
->auth_str
, "digest", 6))
243 return AUTH_TYPE_DIGEST
;
245 return AUTH_TYPE_NONE
;
248 static void uclient_http_process_headers(struct uclient_http
*uh
)
251 HTTP_HDR_TRANSFER_ENCODING
,
253 HTTP_HDR_CONTENT_LENGTH
,
257 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
258 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
259 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
260 [HTTP_HDR_CONNECTION
] = hdr("connection"),
261 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
262 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
265 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
266 struct blob_attr
*cur
;
268 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
270 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
271 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
272 uh
->read_chunked
= 0;
274 cur
= tb
[HTTP_HDR_CONNECTION
];
275 if (cur
&& strstr(blobmsg_data(cur
), "close"))
276 uh
->connection_close
= true;
278 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
280 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
282 cur
= tb
[HTTP_HDR_AUTH
];
285 uh
->auth_str
= strdup(blobmsg_data(cur
));
288 uh
->auth_type
= uclient_http_update_auth_type(uh
);
291 static bool uclient_request_supports_body(enum request_type req_type
)
304 uclient_http_add_auth_basic(struct uclient_http
*uh
)
306 struct uclient_url
*url
= uh
->uc
.url
;
307 int auth_len
= strlen(url
->auth
);
313 auth_buf
= alloca(base64_len(auth_len
) + 1);
317 base64_encode(url
->auth
, auth_len
, auth_buf
);
318 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
323 static char *digest_unquote_sep(char **str
)
325 char *cur
= *str
+ 1;
357 static char *digest_sep(char **str
)
362 next
= strchr(*str
, ',');
367 *str
+= strlen(*str
);
373 static bool strmatch(char **str
, const char *prefix
)
375 int len
= strlen(prefix
);
377 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
385 get_cnonce(char *dest
)
391 f
= fopen("/dev/urandom", "r");
393 n
= fread(&val
, sizeof(val
), 1, f
);
399 bin_to_hex(dest
, &val
, sizeof(val
));
402 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
404 int available
= *len
- *ofs
;
412 required
= strlen(name
) + 4 + strlen(val
) * 2;
413 if (required
> available
)
414 *len
+= required
- available
+ 64;
416 *buf
= realloc(*buf
, *len
);
421 cur
+= sprintf(cur
, ", %s=\"", name
);
423 while ((next
= strchr(val
, '"'))) {
425 memcpy(cur
, val
, next
- val
);
429 cur
+= sprintf(cur
, "\\\"");
433 cur
+= sprintf(cur
, "%s\"", val
);
438 uclient_http_add_auth_digest(struct uclient_http
*uh
)
440 struct uclient_url
*url
= uh
->uc
.url
;
441 const char *realm
= NULL
, *opaque
= NULL
;
442 const char *user
, *password
;
443 char *buf
, *next
, *buf_orig
;
452 struct http_digest_data data
= {
454 .cnonce
= cnonce_str
,
458 len
= strlen(uh
->auth_str
) + 1;
470 strcpy(buf
, uh
->auth_str
);
473 buf_orig
= strsep(&buf
, " ");
477 const char **dest
= NULL
;
480 while (*next
&& isspace(*next
))
483 if (strmatch(&next
, "realm"))
485 else if (strmatch(&next
, "qop"))
487 else if (strmatch(&next
, "nonce"))
489 else if (strmatch(&next
, "opaque"))
491 else if (strmatch(&next
, "stale") ||
492 strmatch(&next
, "algorithm") ||
493 strmatch(&next
, "auth-param")) {
496 } else if (strmatch(&next
, "domain") ||
497 strmatch(&next
, "qop-options"))
504 *dest
= digest_unquote_sep(&next
);
507 if (!realm
|| !data
.qop
|| !data
.nonce
) {
512 sprintf(nc_str
, "%08x", uh
->nc
++);
513 get_cnonce(cnonce_str
);
516 data
.uri
= url
->location
;
517 data
.method
= request_types
[uh
->req_type
];
519 password
= strchr(url
->auth
, ':');
523 len
= password
- url
->auth
;
529 user_buf
= alloca(len
+ 1);
535 strncpy(user_buf
, url
->auth
, len
);
544 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
545 http_digest_calculate_response(hash
, &data
);
551 add_field(&buf
, &ofs
, &len
, "username", user
);
552 add_field(&buf
, &ofs
, &len
, "realm", realm
);
553 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
554 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
555 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
556 add_field(&buf
, &ofs
, &len
, "response", hash
);
558 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
560 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
573 uclient_http_add_auth_header(struct uclient_http
*uh
)
575 if (!uh
->uc
.url
->auth
)
578 switch (uh
->auth_type
) {
579 case AUTH_TYPE_UNKNOWN
:
582 case AUTH_TYPE_BASIC
:
583 return uclient_http_add_auth_basic(uh
);
584 case AUTH_TYPE_DIGEST
:
585 return uclient_http_add_auth_digest(uh
);
592 uclient_http_send_headers(struct uclient_http
*uh
)
594 struct uclient_url
*url
= uh
->uc
.url
;
595 struct blob_attr
*cur
;
596 enum request_type req_type
= uh
->req_type
;
600 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
603 if (uh
->uc
.proxy_url
)
604 url
= uh
->uc
.proxy_url
;
606 literal_ipv6
= strchr(url
->host
, ':');
608 ustream_printf(uh
->us
,
610 "Host: %s%s%s%s%s\r\n",
611 request_types
[req_type
], url
->location
,
612 literal_ipv6
? "[" : "",
614 literal_ipv6
? "]" : "",
615 url
->port
? ":" : "",
616 url
->port
? url
->port
: "");
618 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
619 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
621 if (uclient_request_supports_body(uh
->req_type
))
622 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
624 err
= uclient_http_add_auth_header(uh
);
628 ustream_printf(uh
->us
, "\r\n");
630 uh
->state
= HTTP_STATE_HEADERS_SENT
;
635 static void uclient_http_headers_complete(struct uclient_http
*uh
)
637 enum auth_type auth_type
= uh
->auth_type
;
638 int seq
= uh
->uc
.seq
;
640 uh
->state
= HTTP_STATE_RECV_DATA
;
641 uh
->uc
.meta
= uh
->meta
.head
;
642 uclient_http_process_headers(uh
);
644 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
645 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
646 uclient_http_connect(&uh
->uc
);
647 uclient_http_send_headers(uh
);
648 uh
->state
= HTTP_STATE_REQUEST_DONE
;
652 if (uh
->uc
.cb
->header_done
)
653 uh
->uc
.cb
->header_done(&uh
->uc
);
655 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
658 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
660 uclient_notify_eof(uh
);
664 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
669 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
678 code
= strsep(&data
, " ");
682 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
686 uh
->state
= HTTP_STATE_RECV_HEADERS
;
691 uclient_http_headers_complete(uh
);
695 sep
= strchr(data
, ':');
701 for (name
= data
; *name
; name
++)
702 *name
= tolower(*name
);
705 while (isspace(*sep
))
708 blobmsg_add_string(&uh
->meta
, name
, sep
);
712 uh
->uc
.status_code
= 400;
714 uclient_notify_eof(uh
);
717 static void __uclient_notify_read(struct uclient_http
*uh
)
719 struct uclient
*uc
= &uh
->uc
;
720 unsigned int seq
= uh
->seq
;
724 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
727 data
= ustream_get_read_buf(uh
->us
, &len
);
731 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
736 sep
= strchr(data
, '\n');
741 if (sep
> data
&& sep
[-1] == '\r')
744 /* Check for multi-line HTTP headers */
749 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
758 cur_len
= next
- data
;
759 uclient_parse_http_line(uh
, data
);
763 ustream_consume(uh
->us
, cur_len
);
769 data
= ustream_get_read_buf(uh
->us
, &len
);
770 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
779 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
780 /* Now it's uclient user turn to read some data */
781 uloop_timeout_cancel(&uc
->connection_timeout
);
783 if (uc
->cb
->data_read
)
784 uc
->cb
->data_read(uc
);
788 static void __uclient_notify_write(struct uclient_http
*uh
)
790 struct uclient
*uc
= &uh
->uc
;
792 if (uc
->cb
->data_sent
)
793 uc
->cb
->data_sent(uc
);
796 static void uclient_notify_read(struct ustream
*us
, int bytes
)
798 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
800 __uclient_notify_read(uh
);
803 static void uclient_notify_write(struct ustream
*us
, int bytes
)
805 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
807 __uclient_notify_write(uh
);
810 static void uclient_notify_state(struct ustream
*us
)
812 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
814 if (uh
->ufd
.stream
.write_error
) {
815 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
818 uclient_notify_eof(uh
);
821 static int uclient_setup_http(struct uclient_http
*uh
)
823 struct ustream
*us
= &uh
->ufd
.stream
;
829 us
->string_data
= true;
830 us
->notify_state
= uclient_notify_state
;
831 us
->notify_read
= uclient_notify_read
;
832 us
->notify_write
= uclient_notify_write
;
834 ret
= uclient_do_connect(uh
, "80");
836 return UCLIENT_ERROR_CONNECT
;
841 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
843 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
845 __uclient_notify_read(uh
);
848 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
850 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
852 __uclient_notify_write(uh
);
855 static void uclient_ssl_notify_state(struct ustream
*us
)
857 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
859 uclient_notify_eof(uh
);
862 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
864 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
866 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
869 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
871 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
873 if (!uh
->ssl_require_validation
)
876 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
879 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
881 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
883 if (!uh
->ssl_require_validation
)
886 if (!uh
->ussl
.valid_cn
)
887 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
890 static int uclient_setup_https(struct uclient_http
*uh
)
892 struct ustream
*us
= &uh
->ussl
.stream
;
899 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
901 ret
= uclient_do_connect(uh
, "443");
903 return UCLIENT_ERROR_CONNECT
;
905 us
->string_data
= true;
906 us
->notify_state
= uclient_ssl_notify_state
;
907 us
->notify_read
= uclient_ssl_notify_read
;
908 us
->notify_write
= uclient_ssl_notify_write
;
909 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
910 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
911 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
912 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
913 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
914 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
919 static int uclient_http_connect(struct uclient
*cl
)
921 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
924 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
925 uclient_http_disconnect(uh
);
927 uclient_http_init_request(uh
);
932 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
935 ret
= uclient_setup_https(uh
);
937 ret
= uclient_setup_http(uh
);
942 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
944 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
946 uclient_http_disconnect(uh
);
949 static struct uclient
*uclient_http_alloc(void)
951 struct uclient_http
*uh
;
953 uh
= calloc_a(sizeof(*uh
));
957 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
958 blob_buf_init(&uh
->headers
, 0);
963 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
969 static void uclient_http_free(struct uclient
*cl
)
971 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
973 uclient_http_free_url_state(cl
);
974 uclient_http_free_ssl_ctx(uh
);
975 blob_buf_free(&uh
->headers
);
976 blob_buf_free(&uh
->meta
);
981 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
983 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
986 if (cl
->backend
!= &uclient_backend_http
)
989 if (uh
->state
> HTTP_STATE_INIT
)
992 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
993 if (strcmp(request_types
[i
], type
) != 0)
1004 uclient_http_reset_headers(struct uclient
*cl
)
1006 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1008 blob_buf_init(&uh
->headers
, 0);
1014 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
1016 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1018 if (cl
->backend
!= &uclient_backend_http
)
1021 if (uh
->state
> HTTP_STATE_INIT
)
1024 blobmsg_add_string(&uh
->headers
, name
, value
);
1029 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
1031 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1034 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1037 err
= uclient_http_send_headers(uh
);
1042 ustream_printf(uh
->us
, "%X\r\n", len
);
1043 ustream_write(uh
->us
, buf
, len
, false);
1044 ustream_printf(uh
->us
, "\r\n");
1051 uclient_http_request_done(struct uclient
*cl
)
1053 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1056 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
1059 err
= uclient_http_send_headers(uh
);
1063 if (uclient_request_supports_body(uh
->req_type
))
1064 ustream_printf(uh
->us
, "0\r\n\r\n");
1065 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1071 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1073 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1075 char *data
, *data_end
;
1077 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1080 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1081 if (!data
|| !read_len
)
1084 data_end
= data
+ read_len
;
1087 if (uh
->read_chunked
== 0) {
1090 if (data
[0] == '\r' && data
[1] == '\n') {
1095 sep
= strstr(data
, "\r\n");
1100 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1102 read_len
+= sep
+ 2 - data
;
1105 if (!uh
->read_chunked
) {
1107 uh
->uc
.data_eof
= true;
1111 if (len
> data_end
- data
)
1112 len
= data_end
- data
;
1114 if (uh
->read_chunked
>= 0) {
1115 if (len
> uh
->read_chunked
)
1116 len
= uh
->read_chunked
;
1118 uh
->read_chunked
-= len
;
1119 } else if (uh
->content_length
>= 0) {
1120 if (len
> uh
->content_length
)
1121 len
= uh
->content_length
;
1123 uh
->content_length
-= len
;
1124 if (!uh
->content_length
) {
1126 uh
->uc
.data_eof
= true;
1132 memcpy(buf
, data
, len
);
1136 ustream_consume(uh
->us
, read_len
);
1138 uclient_notify_eof(uh
);
1140 /* Now that we consumed something and if this isn't EOF, start timer again */
1141 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1142 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1147 int uclient_http_redirect(struct uclient
*cl
)
1149 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1150 struct blobmsg_policy location
= {
1152 .type
= BLOBMSG_TYPE_STRING
,
1154 struct uclient_url
*url
= cl
->url
;
1155 struct blob_attr
*tb
;
1157 if (cl
->backend
!= &uclient_backend_http
)
1160 switch (cl
->status_code
) {
1169 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1173 url
= uclient_get_url_location(url
, blobmsg_data(tb
));
1179 if (uclient_http_connect(cl
))
1182 uclient_http_request_done(cl
);
1187 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1188 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1190 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1192 if (cl
->backend
!= &uclient_backend_http
)
1195 uclient_http_free_url_state(cl
);
1197 uclient_http_free_ssl_ctx(uh
);
1200 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1205 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1207 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1209 if (cl
->backend
!= &uclient_backend_http
)
1214 uh
->usock_flags
= USOCK_IPV4ONLY
;
1217 uh
->usock_flags
= USOCK_IPV6ONLY
;
1220 uh
->usock_flags
= 0;
1227 const struct uclient_backend uclient_backend_http
= {
1228 .prefix
= uclient_http_prefix
,
1230 .alloc
= uclient_http_alloc
,
1231 .free
= uclient_http_free
,
1232 .connect
= uclient_http_connect
,
1233 .disconnect
= uclient_http_request_disconnect
,
1234 .update_url
= uclient_http_free_url_state
,
1235 .update_proxy_url
= uclient_http_free_url_state
,
1237 .read
= uclient_http_read
,
1238 .write
= uclient_http_send_data
,
1239 .request
= uclient_http_request_done
,