5 #include <libubox/ustream.h>
6 #include <libubox/ustream-ssl.h>
7 #include <libubox/usock.h>
8 #include <libubox/blobmsg.h>
11 #include "uclient-utils.h"
12 #include "uclient-backend.h"
14 static struct ustream_ssl_ctx *ssl_ctx;
25 HTTP_STATE_HEADERS_SENT,
26 HTTP_STATE_REQUEST_DONE,
27 HTTP_STATE_RECV_HEADERS,
32 static const char * const request_types[__REQ_MAX] = {
43 struct ustream_fd ufd;
44 struct ustream_ssl ussl;
48 bool connection_close;
49 enum request_type req_type;
50 enum http_state state;
55 struct blob_buf headers;
65 static const char * const uclient_http_prefix[] = {
66 [PREFIX_HTTP] = "http://",
67 [PREFIX_HTTPS] = "https://",
71 static int uclient_do_connect(struct uclient_http *uh, const char *port)
76 port = uh->uc.url->port;
78 fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
82 ustream_fd_init(&uh->ufd, fd);
86 static void uclient_http_disconnect(struct uclient *cl)
88 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
94 ustream_free(&uh->ussl.stream);
95 ustream_free(&uh->ufd.stream);
100 static void uclient_notify_eof(struct uclient_http *uh)
102 struct ustream *us = uh->us;
105 if (!us->eof && !us->write_error)
108 if (ustream_pending_data(us, false))
112 uclient_backend_set_eof(&uh->uc);
114 if (uh->connection_close)
115 uclient_http_disconnect(&uh->uc);
118 static void uclient_http_process_headers(struct uclient_http *uh)
121 HTTP_HDR_TRANSFER_ENCODING,
123 HTTP_HDR_CONTENT_LENGTH,
126 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
127 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
128 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
129 [HTTP_HDR_CONNECTION] = hdr("connection"),
130 [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
133 struct blob_attr *tb[__HTTP_HDR_MAX];
134 struct blob_attr *cur;
136 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
138 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
139 if (cur && strstr(blobmsg_data(cur), "chunked"))
140 uh->read_chunked = 0;
142 cur = tb[HTTP_HDR_CONNECTION];
143 if (cur && strstr(blobmsg_data(cur), "close"))
144 uh->connection_close = true;
146 cur = tb[HTTP_HDR_CONTENT_LENGTH];
148 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
151 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
156 if (uh->state == HTTP_STATE_REQUEST_DONE) {
157 uh->state = HTTP_STATE_RECV_HEADERS;
162 uh->state = HTTP_STATE_RECV_DATA;
163 uh->uc.meta = uh->meta.head;
164 uclient_http_process_headers(uh);
165 if (uh->uc.cb->header_done)
166 uh->uc.cb->header_done(&uh->uc);
168 if (uh->req_type == REQ_HEAD) {
170 uclient_notify_eof(uh);
176 sep = strchr(data, ':');
182 for (name = data; *name; name++)
183 *name = tolower(*name);
186 while (isspace(*sep))
189 blobmsg_add_string(&uh->meta, name, sep);
192 static void __uclient_notify_read(struct uclient_http *uh)
194 struct uclient *uc = &uh->uc;
198 if (uh->state < HTTP_STATE_REQUEST_DONE)
201 data = ustream_get_read_buf(uh->us, &len);
205 if (uh->state < HTTP_STATE_RECV_DATA) {
210 sep = strstr(data, "\r\n");
214 /* Check for multi-line HTTP headers */
219 if (isspace(sep[2]) && sep[2] != '\r') {
227 cur_len = sep + 2 - data;
228 uclient_parse_http_line(uh, data);
229 ustream_consume(uh->us, cur_len);
232 data = ustream_get_read_buf(uh->us, &len);
233 } while (uh->state < HTTP_STATE_RECV_DATA);
239 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
240 uc->cb->data_read(uc);
243 static void uclient_notify_read(struct ustream *us, int bytes)
245 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
247 __uclient_notify_read(uh);
250 static void uclient_notify_state(struct ustream *us)
252 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
254 uclient_notify_eof(uh);
257 static int uclient_setup_http(struct uclient_http *uh)
259 struct ustream *us = &uh->ufd.stream;
263 us->string_data = true;
264 us->notify_state = uclient_notify_state;
265 us->notify_read = uclient_notify_read;
267 ret = uclient_do_connect(uh, "80");
274 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
276 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
278 __uclient_notify_read(uh);
281 static void uclient_ssl_notify_state(struct ustream *us)
283 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
285 uclient_notify_eof(uh);
288 static int uclient_setup_https(struct uclient_http *uh)
290 struct ustream *us = &uh->ussl.stream;
296 ret = uclient_do_connect(uh, "443");
301 ssl_ctx = ustream_ssl_context_new(false);
303 us->string_data = true;
304 us->notify_state = uclient_ssl_notify_state;
305 us->notify_read = uclient_ssl_notify_read;
306 ustream_ssl_init(&uh->ussl, &uh->ufd.stream, ssl_ctx, false);
311 static void uclient_http_reset_state(struct uclient_http *uh)
313 uclient_backend_reset_state(&uh->uc);
314 uh->read_chunked = -1;
315 uh->content_length = -1;
317 uh->connection_close = false;
318 uh->state = HTTP_STATE_INIT;
321 static int uclient_http_connect(struct uclient *cl)
323 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
325 uclient_http_reset_state(uh);
326 blob_buf_init(&uh->meta, 0);
331 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
334 return uclient_setup_https(uh);
336 return uclient_setup_http(uh);
339 static struct uclient *uclient_http_alloc(void)
341 struct uclient_http *uh;
343 uh = calloc_a(sizeof(*uh));
344 blob_buf_init(&uh->headers, 0);
349 static void uclient_http_free(struct uclient *cl)
351 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
353 uclient_http_disconnect(cl);
354 blob_buf_free(&uh->headers);
355 blob_buf_free(&uh->meta);
360 uclient_http_set_request_type(struct uclient *cl, const char *type)
362 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
365 if (cl->backend != &uclient_backend_http)
368 if (uh->state > HTTP_STATE_INIT)
371 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
372 if (strcmp(request_types[i], type) != 0)
383 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
385 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
387 blob_buf_init(&uh->headers, 0);
393 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
395 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
397 if (cl->backend != &uclient_backend_http)
400 if (uh->state > HTTP_STATE_INIT)
403 blobmsg_add_string(&uh->headers, name, value);
408 uclient_http_send_headers(struct uclient_http *uh)
410 struct uclient_url *url = uh->uc.url;
411 struct blob_attr *cur;
414 if (uh->state >= HTTP_STATE_HEADERS_SENT)
417 ustream_printf(uh->us,
418 "%s /%s HTTP/1.1\r\n"
420 request_types[uh->req_type],
421 url->location, url->host);
423 blobmsg_for_each_attr(cur, uh->headers.head, rem)
424 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
427 int auth_len = strlen(url->auth);
433 auth_buf = alloca(base64_len(auth_len) + 1);
434 base64_encode(url->auth, auth_len, auth_buf);
435 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
438 if (uh->req_type == REQ_POST)
439 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
441 ustream_printf(uh->us, "\r\n");
445 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
447 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
449 if (uh->state >= HTTP_STATE_REQUEST_DONE)
452 uclient_http_send_headers(uh);
454 ustream_printf(uh->us, "%X\r\n", len);
455 ustream_write(uh->us, buf, len, false);
456 ustream_printf(uh->us, "\r\n");
462 uclient_http_request_done(struct uclient *cl)
464 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
466 if (uh->state >= HTTP_STATE_REQUEST_DONE)
469 uclient_http_send_headers(uh);
470 uh->state = HTTP_STATE_REQUEST_DONE;
476 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
478 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
480 char *data, *data_end;
482 if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
485 data = ustream_get_read_buf(uh->us, &read_len);
486 if (!data || !read_len)
489 data_end = data + read_len;
492 if (uh->read_chunked == 0) {
495 if (data[0] == '\r' && data[1] == '\n') {
500 sep = strstr(data, "\r\n");
505 uh->read_chunked = strtoul(data, NULL, 16);
507 read_len += sep + 2 - data;
510 if (!uh->read_chunked)
514 if (len > data_end - data)
515 len = data_end - data;
517 if (uh->read_chunked >= 0) {
518 if (len > uh->read_chunked)
519 len = uh->read_chunked;
521 uh->read_chunked -= len;
522 } else if (uh->content_length >= 0) {
523 if (len > uh->content_length)
524 len = uh->content_length;
526 uh->content_length -= len;
527 if (!uh->content_length)
533 memcpy(buf, data, len);
537 ustream_consume(uh->us, read_len);
539 uclient_notify_eof(uh);
544 const struct uclient_backend uclient_backend_http __hidden = {
545 .prefix = uclient_http_prefix,
547 .alloc = uclient_http_alloc,
548 .free = uclient_http_free,
549 .connect = uclient_http_connect,
550 .update_url = uclient_http_disconnect,
552 .read = uclient_http_read,
553 .write = uclient_http_send_data,
554 .request = uclient_http_request_done,