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>
25 #include <libubox/ustream.h>
26 #include <libubox/ustream-ssl.h>
27 #include <libubox/usock.h>
28 #include <libubox/blobmsg.h>
31 #include "uclient-utils.h"
32 #include "uclient-backend.h"
52 HTTP_STATE_HEADERS_SENT
,
53 HTTP_STATE_REQUEST_DONE
,
54 HTTP_STATE_RECV_HEADERS
,
59 static const char * const request_types
[__REQ_MAX
] = {
64 [REQ_DELETE
] = "DELETE",
70 const struct ustream_ssl_ops
*ssl_ops
;
71 struct ustream_ssl_ctx
*ssl_ctx
;
74 struct ustream_fd ufd
;
75 struct ustream_ssl ussl
;
77 struct uloop_timeout disconnect_t
;
80 bool ssl_require_validation
;
83 bool connection_close
;
85 enum request_type req_type
;
86 enum http_state state
;
88 enum auth_type auth_type
;
98 struct blob_buf headers
;
108 static const char * const uclient_http_prefix
[] = {
109 [PREFIX_HTTP
] = "http://",
110 [PREFIX_HTTPS
] = "https://",
111 [__PREFIX_MAX
] = NULL
114 static int uclient_http_connect(struct uclient
*cl
);
116 static int uclient_do_connect(struct uclient_http
*uh
, const char *port
)
121 if (uh
->uc
.url
->port
)
122 port
= uh
->uc
.url
->port
;
124 memset(&uh
->uc
.remote_addr
, 0, sizeof(uh
->uc
.remote_addr
));
126 fd
= usock_inet(USOCK_TCP
| uh
->usock_flags
, uh
->uc
.url
->host
, port
, &uh
->uc
.remote_addr
);
130 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
131 ustream_fd_init(&uh
->ufd
, fd
);
133 sl
= sizeof(uh
->uc
.local_addr
);
134 memset(&uh
->uc
.local_addr
, 0, sl
);
135 getsockname(fd
, &uh
->uc
.local_addr
.sa
, &sl
);
140 static void uclient_http_disconnect(struct uclient_http
*uh
)
142 uloop_timeout_cancel(&uh
->disconnect_t
);
147 ustream_free(&uh
->ussl
.stream
);
148 ustream_free(&uh
->ufd
.stream
);
149 close(uh
->ufd
.fd
.fd
);
153 static void uclient_http_free_url_state(struct uclient
*cl
)
155 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
157 uh
->auth_type
= AUTH_TYPE_UNKNOWN
;
160 uclient_http_disconnect(uh
);
163 static void uclient_http_error(struct uclient_http
*uh
, int code
)
165 uh
->state
= HTTP_STATE_ERROR
;
167 ustream_state_change(uh
->us
);
168 uclient_backend_set_error(&uh
->uc
, code
);
171 static void uclient_http_request_disconnect(struct uclient
*cl
)
173 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
179 uh
->disconnect
= true;
180 uloop_timeout_set(&uh
->disconnect_t
, 1);
183 static void uclient_notify_eof(struct uclient_http
*uh
)
185 struct ustream
*us
= uh
->us
;
191 if (!us
->eof
&& !us
->write_error
)
194 if (ustream_pending_data(us
, false))
198 if (uh
->content_length
< 0 && uh
->read_chunked
>= 0)
199 uh
->uc
.data_eof
= true;
201 uclient_backend_set_eof(&uh
->uc
);
203 if (uh
->connection_close
)
204 uclient_http_request_disconnect(&uh
->uc
);
207 static void uclient_http_reset_state(struct uclient_http
*uh
)
210 uclient_backend_reset_state(&uh
->uc
);
211 uh
->read_chunked
= -1;
212 uh
->content_length
= -1;
214 uh
->disconnect
= false;
215 uh
->connection_close
= false;
216 uh
->state
= HTTP_STATE_INIT
;
218 if (uh
->auth_type
== AUTH_TYPE_UNKNOWN
&& !uh
->uc
.url
->auth
)
219 uh
->auth_type
= AUTH_TYPE_NONE
;
222 static void uclient_http_init_request(struct uclient_http
*uh
)
225 uclient_http_reset_state(uh
);
226 blob_buf_init(&uh
->meta
, 0);
229 static enum auth_type
230 uclient_http_update_auth_type(struct uclient_http
*uh
)
233 return AUTH_TYPE_NONE
;
235 if (!strncasecmp(uh
->auth_str
, "basic", 5))
236 return AUTH_TYPE_BASIC
;
238 if (!strncasecmp(uh
->auth_str
, "digest", 6))
239 return AUTH_TYPE_DIGEST
;
241 return AUTH_TYPE_NONE
;
244 static void uclient_http_process_headers(struct uclient_http
*uh
)
247 HTTP_HDR_TRANSFER_ENCODING
,
249 HTTP_HDR_CONTENT_LENGTH
,
253 static const struct blobmsg_policy hdr_policy
[__HTTP_HDR_MAX
] = {
254 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
255 [HTTP_HDR_TRANSFER_ENCODING
] = hdr("transfer-encoding"),
256 [HTTP_HDR_CONNECTION
] = hdr("connection"),
257 [HTTP_HDR_CONTENT_LENGTH
] = hdr("content-length"),
258 [HTTP_HDR_AUTH
] = hdr("www-authenticate"),
261 struct blob_attr
*tb
[__HTTP_HDR_MAX
];
262 struct blob_attr
*cur
;
264 blobmsg_parse(hdr_policy
, __HTTP_HDR_MAX
, tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
266 cur
= tb
[HTTP_HDR_TRANSFER_ENCODING
];
267 if (cur
&& strstr(blobmsg_data(cur
), "chunked"))
268 uh
->read_chunked
= 0;
270 cur
= tb
[HTTP_HDR_CONNECTION
];
271 if (cur
&& strstr(blobmsg_data(cur
), "close"))
272 uh
->connection_close
= true;
274 cur
= tb
[HTTP_HDR_CONTENT_LENGTH
];
276 uh
->content_length
= strtoul(blobmsg_data(cur
), NULL
, 10);
278 cur
= tb
[HTTP_HDR_AUTH
];
281 uh
->auth_str
= strdup(blobmsg_data(cur
));
284 uh
->auth_type
= uclient_http_update_auth_type(uh
);
288 uclient_http_add_auth_basic(struct uclient_http
*uh
)
290 struct uclient_url
*url
= uh
->uc
.url
;
291 int auth_len
= strlen(url
->auth
);
297 auth_buf
= alloca(base64_len(auth_len
) + 1);
298 base64_encode(url
->auth
, auth_len
, auth_buf
);
299 ustream_printf(uh
->us
, "Authorization: Basic %s\r\n", auth_buf
);
302 static char *digest_unquote_sep(char **str
)
304 char *cur
= *str
+ 1;
336 static char *digest_sep(char **str
)
341 next
= strchr(*str
, ',');
346 *str
+= strlen(*str
);
352 static bool strmatch(char **str
, const char *prefix
)
354 int len
= strlen(prefix
);
356 if (strncmp(*str
, prefix
, len
) != 0 || (*str
)[len
] != '=')
364 get_cnonce(char *dest
)
369 f
= fopen("/dev/urandom", "r");
371 fread(&val
, sizeof(val
), 1, f
);
375 bin_to_hex(dest
, &val
, sizeof(val
));
378 static void add_field(char **buf
, int *ofs
, int *len
, const char *name
, const char *val
)
380 int available
= *len
- *ofs
;
388 required
= strlen(name
) + 4 + strlen(val
) * 2;
389 if (required
> available
)
390 *len
+= required
- available
+ 64;
392 *buf
= realloc(*buf
, *len
);
397 cur
+= sprintf(cur
, ", %s=\"", name
);
399 while ((next
= strchr(val
, '"'))) {
401 memcpy(cur
, val
, next
- val
);
405 cur
+= sprintf(cur
, "\\\"");
409 cur
+= sprintf(cur
, "%s\"", val
);
414 uclient_http_add_auth_digest(struct uclient_http
*uh
)
416 struct uclient_url
*url
= uh
->uc
.url
;
417 const char *realm
= NULL
, *opaque
= NULL
;
418 const char *user
, *password
;
427 struct http_digest_data data
= {
429 .cnonce
= cnonce_str
,
433 len
= strlen(uh
->auth_str
) + 1;
438 strcpy(buf
, uh
->auth_str
);
445 const char **dest
= NULL
;
448 while (*next
&& isspace(*next
))
451 if (strmatch(&next
, "realm"))
453 else if (strmatch(&next
, "qop"))
455 else if (strmatch(&next
, "nonce"))
457 else if (strmatch(&next
, "opaque"))
459 else if (strmatch(&next
, "stale") ||
460 strmatch(&next
, "algorithm") ||
461 strmatch(&next
, "auth-param")) {
464 } else if (strmatch(&next
, "domain") ||
465 strmatch(&next
, "qop-options"))
472 *dest
= digest_unquote_sep(&next
);
475 if (!realm
|| !data
.qop
|| !data
.nonce
)
478 sprintf(nc_str
, "%08x", uh
->nc
++);
479 get_cnonce(cnonce_str
);
482 data
.uri
= url
->location
;
483 data
.method
= request_types
[uh
->req_type
];
485 password
= strchr(url
->auth
, ':');
489 len
= password
- url
->auth
;
493 user_buf
= alloca(len
+ 1);
494 strncpy(user_buf
, url
->auth
, len
);
503 http_digest_calculate_auth_hash(ahash
, user
, realm
, password
);
504 http_digest_calculate_response(hash
, &data
);
510 add_field(&buf
, &ofs
, &len
, "username", user
);
511 add_field(&buf
, &ofs
, &len
, "realm", realm
);
512 add_field(&buf
, &ofs
, &len
, "nonce", data
.nonce
);
513 add_field(&buf
, &ofs
, &len
, "uri", data
.uri
);
514 add_field(&buf
, &ofs
, &len
, "cnonce", data
.cnonce
);
515 add_field(&buf
, &ofs
, &len
, "response", hash
);
517 add_field(&buf
, &ofs
, &len
, "opaque", opaque
);
519 ustream_printf(uh
->us
, "Authorization: Digest nc=%s, qop=%s%s\r\n", data
.nc
, data
.qop
, buf
);
524 uclient_http_add_auth_header(struct uclient_http
*uh
)
526 if (!uh
->uc
.url
->auth
)
529 switch (uh
->auth_type
) {
530 case AUTH_TYPE_UNKNOWN
:
533 case AUTH_TYPE_BASIC
:
534 uclient_http_add_auth_basic(uh
);
536 case AUTH_TYPE_DIGEST
:
537 uclient_http_add_auth_digest(uh
);
543 uclient_http_send_headers(struct uclient_http
*uh
)
545 struct uclient_url
*url
= uh
->uc
.url
;
546 struct blob_attr
*cur
;
547 enum request_type req_type
= uh
->req_type
;
550 if (uh
->state
>= HTTP_STATE_HEADERS_SENT
)
553 if (uh
->uc
.proxy_url
)
554 url
= uh
->uc
.proxy_url
;
556 ustream_printf(uh
->us
,
559 request_types
[req_type
],
560 url
->location
, url
->host
);
562 blobmsg_for_each_attr(cur
, uh
->headers
.head
, rem
)
563 ustream_printf(uh
->us
, "%s: %s\r\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
565 if (uh
->req_type
== REQ_POST
|| uh
->req_type
== REQ_PUT
)
566 ustream_printf(uh
->us
, "Transfer-Encoding: chunked\r\n");
568 uclient_http_add_auth_header(uh
);
570 ustream_printf(uh
->us
, "\r\n");
572 uh
->state
= HTTP_STATE_HEADERS_SENT
;
575 static void uclient_http_headers_complete(struct uclient_http
*uh
)
577 enum auth_type auth_type
= uh
->auth_type
;
578 int seq
= uh
->uc
.seq
;
580 uh
->state
= HTTP_STATE_RECV_DATA
;
581 uh
->uc
.meta
= uh
->meta
.head
;
582 uclient_http_process_headers(uh
);
584 if (auth_type
== AUTH_TYPE_UNKNOWN
&& uh
->uc
.status_code
== 401 &&
585 (uh
->req_type
== REQ_HEAD
|| uh
->req_type
== REQ_GET
)) {
586 uclient_http_connect(&uh
->uc
);
587 uclient_http_send_headers(uh
);
588 uh
->state
= HTTP_STATE_REQUEST_DONE
;
592 if (uh
->uc
.cb
->header_done
)
593 uh
->uc
.cb
->header_done(&uh
->uc
);
595 if (uh
->eof
|| seq
!= uh
->uc
.seq
)
598 if (uh
->req_type
== REQ_HEAD
|| uh
->uc
.status_code
== 204) {
600 uclient_notify_eof(uh
);
604 static void uclient_parse_http_line(struct uclient_http
*uh
, char *data
)
609 if (uh
->state
== HTTP_STATE_REQUEST_DONE
) {
618 code
= strsep(&data
, " ");
622 uh
->uc
.status_code
= strtoul(code
, &sep
, 10);
626 uh
->state
= HTTP_STATE_RECV_HEADERS
;
631 uclient_http_headers_complete(uh
);
635 sep
= strchr(data
, ':');
641 for (name
= data
; *name
; name
++)
642 *name
= tolower(*name
);
645 while (isspace(*sep
))
648 blobmsg_add_string(&uh
->meta
, name
, sep
);
652 uh
->uc
.status_code
= 400;
654 uclient_notify_eof(uh
);
657 static void __uclient_notify_read(struct uclient_http
*uh
)
659 struct uclient
*uc
= &uh
->uc
;
660 unsigned int seq
= uh
->seq
;
664 if (uh
->state
< HTTP_STATE_REQUEST_DONE
|| uh
->state
== HTTP_STATE_ERROR
)
667 data
= ustream_get_read_buf(uh
->us
, &len
);
671 if (uh
->state
< HTTP_STATE_RECV_DATA
) {
676 sep
= strchr(data
, '\n');
681 if (sep
> data
&& sep
[-1] == '\r')
684 /* Check for multi-line HTTP headers */
689 if (isspace(*next
) && *next
!= '\r' && *next
!= '\n') {
698 cur_len
= next
- data
;
699 uclient_parse_http_line(uh
, data
);
703 ustream_consume(uh
->us
, cur_len
);
709 data
= ustream_get_read_buf(uh
->us
, &len
);
710 } while (data
&& uh
->state
< HTTP_STATE_RECV_DATA
);
719 if (uh
->state
== HTTP_STATE_RECV_DATA
) {
720 /* Now it's uclient user turn to read some data */
721 uloop_timeout_cancel(&uc
->connection_timeout
);
723 if (uc
->cb
->data_read
)
724 uc
->cb
->data_read(uc
);
728 static void __uclient_notify_write(struct uclient_http
*uh
)
730 struct uclient
*uc
= &uh
->uc
;
732 if (uc
->cb
->data_sent
)
733 uc
->cb
->data_sent(uc
);
736 static void uclient_notify_read(struct ustream
*us
, int bytes
)
738 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
740 __uclient_notify_read(uh
);
743 static void uclient_notify_write(struct ustream
*us
, int bytes
)
745 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
747 __uclient_notify_write(uh
);
750 static void uclient_notify_state(struct ustream
*us
)
752 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ufd
.stream
);
754 if (uh
->ufd
.stream
.write_error
) {
755 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
758 uclient_notify_eof(uh
);
761 static int uclient_setup_http(struct uclient_http
*uh
)
763 struct ustream
*us
= &uh
->ufd
.stream
;
769 us
->string_data
= true;
770 us
->notify_state
= uclient_notify_state
;
771 us
->notify_read
= uclient_notify_read
;
772 us
->notify_write
= uclient_notify_write
;
774 ret
= uclient_do_connect(uh
, "80");
776 return UCLIENT_ERROR_CONNECT
;
781 static void uclient_ssl_notify_read(struct ustream
*us
, int bytes
)
783 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
785 __uclient_notify_read(uh
);
788 static void uclient_ssl_notify_write(struct ustream
*us
, int bytes
)
790 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
792 __uclient_notify_write(uh
);
795 static void uclient_ssl_notify_state(struct ustream
*us
)
797 struct uclient_http
*uh
= container_of(us
, struct uclient_http
, ussl
.stream
);
799 uclient_notify_eof(uh
);
802 static void uclient_ssl_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
804 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
806 uclient_http_error(uh
, UCLIENT_ERROR_CONNECT
);
809 static void uclient_ssl_notify_verify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
811 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
813 if (!uh
->ssl_require_validation
)
816 uclient_http_error(uh
, UCLIENT_ERROR_SSL_INVALID_CERT
);
819 static void uclient_ssl_notify_connected(struct ustream_ssl
*ssl
)
821 struct uclient_http
*uh
= container_of(ssl
, struct uclient_http
, ussl
);
823 if (!uh
->ssl_require_validation
)
826 if (!uh
->ussl
.valid_cn
)
827 uclient_http_error(uh
, UCLIENT_ERROR_SSL_CN_MISMATCH
);
830 static int uclient_setup_https(struct uclient_http
*uh
)
832 struct ustream
*us
= &uh
->ussl
.stream
;
839 return UCLIENT_ERROR_MISSING_SSL_CONTEXT
;
841 ret
= uclient_do_connect(uh
, "443");
843 return UCLIENT_ERROR_CONNECT
;
845 us
->string_data
= true;
846 us
->notify_state
= uclient_ssl_notify_state
;
847 us
->notify_read
= uclient_ssl_notify_read
;
848 us
->notify_write
= uclient_ssl_notify_write
;
849 uh
->ussl
.notify_error
= uclient_ssl_notify_error
;
850 uh
->ussl
.notify_verify_error
= uclient_ssl_notify_verify_error
;
851 uh
->ussl
.notify_connected
= uclient_ssl_notify_connected
;
852 uh
->ussl
.server_name
= uh
->uc
.url
->host
;
853 uh
->ssl_ops
->init(&uh
->ussl
, &uh
->ufd
.stream
, uh
->ssl_ctx
, false);
854 uh
->ssl_ops
->set_peer_cn(&uh
->ussl
, uh
->uc
.url
->host
);
859 static int uclient_http_connect(struct uclient
*cl
)
861 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
864 if (!cl
->eof
|| uh
->disconnect
|| uh
->connection_close
)
865 uclient_http_disconnect(uh
);
867 uclient_http_init_request(uh
);
872 uh
->ssl
= cl
->url
->prefix
== PREFIX_HTTPS
;
875 ret
= uclient_setup_https(uh
);
877 ret
= uclient_setup_http(uh
);
882 static void uclient_http_disconnect_cb(struct uloop_timeout
*timeout
)
884 struct uclient_http
*uh
= container_of(timeout
, struct uclient_http
, disconnect_t
);
886 uclient_http_disconnect(uh
);
889 static struct uclient
*uclient_http_alloc(void)
891 struct uclient_http
*uh
;
893 uh
= calloc_a(sizeof(*uh
));
894 uh
->disconnect_t
.cb
= uclient_http_disconnect_cb
;
895 blob_buf_init(&uh
->headers
, 0);
900 static void uclient_http_free_ssl_ctx(struct uclient_http
*uh
)
906 static void uclient_http_free(struct uclient
*cl
)
908 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
910 uclient_http_free_url_state(cl
);
911 uclient_http_free_ssl_ctx(uh
);
912 blob_buf_free(&uh
->headers
);
913 blob_buf_free(&uh
->meta
);
918 uclient_http_set_request_type(struct uclient
*cl
, const char *type
)
920 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
923 if (cl
->backend
!= &uclient_backend_http
)
926 if (uh
->state
> HTTP_STATE_INIT
)
929 for (i
= 0; i
< ARRAY_SIZE(request_types
); i
++) {
930 if (strcmp(request_types
[i
], type
) != 0)
941 uclient_http_reset_headers(struct uclient
*cl
)
943 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
945 blob_buf_init(&uh
->headers
, 0);
951 uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
)
953 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
955 if (cl
->backend
!= &uclient_backend_http
)
958 if (uh
->state
> HTTP_STATE_INIT
)
961 blobmsg_add_string(&uh
->headers
, name
, value
);
966 uclient_http_send_data(struct uclient
*cl
, const char *buf
, unsigned int len
)
968 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
970 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
973 uclient_http_send_headers(uh
);
976 ustream_printf(uh
->us
, "%X\r\n", len
);
977 ustream_write(uh
->us
, buf
, len
, false);
978 ustream_printf(uh
->us
, "\r\n");
985 uclient_http_request_done(struct uclient
*cl
)
987 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
989 if (uh
->state
>= HTTP_STATE_REQUEST_DONE
)
992 uclient_http_send_headers(uh
);
993 if (uh
->req_type
== REQ_POST
|| uh
->req_type
== REQ_PUT
)
994 ustream_printf(uh
->us
, "0\r\n\r\n");
995 uh
->state
= HTTP_STATE_REQUEST_DONE
;
1001 uclient_http_read(struct uclient
*cl
, char *buf
, unsigned int len
)
1003 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1005 char *data
, *data_end
;
1007 if (uh
->state
< HTTP_STATE_RECV_DATA
|| !uh
->us
)
1010 data
= ustream_get_read_buf(uh
->us
, &read_len
);
1011 if (!data
|| !read_len
)
1014 data_end
= data
+ read_len
;
1017 if (uh
->read_chunked
== 0) {
1020 if (data
[0] == '\r' && data
[1] == '\n') {
1025 sep
= strstr(data
, "\r\n");
1030 uh
->read_chunked
= strtoul(data
, NULL
, 16);
1032 read_len
+= sep
+ 2 - data
;
1035 if (!uh
->read_chunked
) {
1037 uh
->uc
.data_eof
= true;
1041 if (len
> data_end
- data
)
1042 len
= data_end
- data
;
1044 if (uh
->read_chunked
>= 0) {
1045 if (len
> uh
->read_chunked
)
1046 len
= uh
->read_chunked
;
1048 uh
->read_chunked
-= len
;
1049 } else if (uh
->content_length
>= 0) {
1050 if (len
> uh
->content_length
)
1051 len
= uh
->content_length
;
1053 uh
->content_length
-= len
;
1054 if (!uh
->content_length
) {
1056 uh
->uc
.data_eof
= true;
1062 memcpy(buf
, data
, len
);
1066 ustream_consume(uh
->us
, read_len
);
1068 uclient_notify_eof(uh
);
1070 /* Now that we consumed something and if this isn't EOF, start timer again */
1071 if (!uh
->uc
.eof
&& !cl
->connection_timeout
.pending
)
1072 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
1077 int uclient_http_redirect(struct uclient
*cl
)
1079 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1080 struct blobmsg_policy location
= {
1082 .type
= BLOBMSG_TYPE_STRING
,
1084 struct uclient_url
*url
= cl
->url
;
1085 struct blob_attr
*tb
;
1087 if (cl
->backend
!= &uclient_backend_http
)
1090 switch (cl
->status_code
) {
1099 blobmsg_parse(&location
, 1, &tb
, blob_data(uh
->meta
.head
), blob_len(uh
->meta
.head
));
1103 url
= uclient_get_url(blobmsg_data(tb
), url
->auth
);
1109 if (uclient_http_connect(cl
))
1112 uclient_http_request_done(cl
);
1117 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
1118 struct ustream_ssl_ctx
*ctx
, bool require_validation
)
1120 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1122 if (cl
->backend
!= &uclient_backend_http
)
1125 uclient_http_free_url_state(cl
);
1127 uclient_http_free_ssl_ctx(uh
);
1130 uh
->ssl_require_validation
= !!ctx
&& require_validation
;
1135 int uclient_http_set_address_family(struct uclient
*cl
, int af
)
1137 struct uclient_http
*uh
= container_of(cl
, struct uclient_http
, uc
);
1139 if (cl
->backend
!= &uclient_backend_http
)
1144 uh
->usock_flags
= USOCK_IPV4ONLY
;
1147 uh
->usock_flags
= USOCK_IPV6ONLY
;
1150 uh
->usock_flags
= 0;
1157 const struct uclient_backend uclient_backend_http
= {
1158 .prefix
= uclient_http_prefix
,
1160 .alloc
= uclient_http_alloc
,
1161 .free
= uclient_http_free
,
1162 .connect
= uclient_http_connect
,
1163 .disconnect
= uclient_http_request_disconnect
,
1164 .update_url
= uclient_http_free_url_state
,
1165 .update_proxy_url
= uclient_http_free_url_state
,
1167 .read
= uclient_http_read
,
1168 .write
= uclient_http_send_data
,
1169 .request
= uclient_http_request_done
,