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 enum request_type req_type;
49 enum http_state state;
53 struct blob_buf headers;
63 static const char * const uclient_http_prefix[] = {
64 [PREFIX_HTTP] = "http://",
65 [PREFIX_HTTPS] = "https://",
69 static int uclient_do_connect(struct uclient_http *uh, const char *port)
74 port = uh->uc.url->port;
76 fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
80 ustream_fd_init(&uh->ufd, fd);
84 static void uclient_notify_eof(struct uclient_http *uh)
86 struct ustream *us = uh->us;
89 if (!us->eof && !us->write_error)
92 if (ustream_pending_data(us, false))
96 uclient_backend_set_eof(&uh->uc);
99 static void uclient_http_process_headers(struct uclient_http *uh)
102 HTTP_HDR_TRANSFER_ENCODING,
105 static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
106 #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
107 [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
110 struct blob_attr *tb[__HTTP_HDR_MAX];
111 struct blob_attr *cur;
113 blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
115 cur = tb[HTTP_HDR_TRANSFER_ENCODING];
116 if (cur && strstr(blobmsg_data(cur), "chunked"))
117 uh->read_chunked = 0;
120 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
125 if (uh->state == HTTP_STATE_REQUEST_DONE) {
126 uh->state = HTTP_STATE_RECV_HEADERS;
131 uh->state = HTTP_STATE_RECV_DATA;
132 uh->uc.meta = uh->meta.head;
133 uclient_http_process_headers(uh);
134 if (uh->uc.cb->header_done)
135 uh->uc.cb->header_done(&uh->uc);
139 sep = strchr(data, ':');
145 for (name = data; *name; name++)
146 *name = tolower(*name);
149 while (isspace(*sep))
152 blobmsg_add_string(&uh->meta, name, sep);
155 static void __uclient_notify_read(struct uclient_http *uh)
157 struct uclient *uc = &uh->uc;
161 if (uh->state < HTTP_STATE_REQUEST_DONE)
164 data = ustream_get_read_buf(uh->us, &len);
168 if (uh->state < HTTP_STATE_RECV_DATA) {
173 sep = strstr(data, "\r\n");
177 /* Check for multi-line HTTP headers */
182 if (isspace(sep[2]) && sep[2] != '\r') {
190 cur_len = sep + 2 - data;
191 uclient_parse_http_line(uh, data);
192 ustream_consume(uh->us, cur_len);
195 data = ustream_get_read_buf(uh->us, &len);
196 } while (uh->state < HTTP_STATE_RECV_DATA);
202 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
203 uc->cb->data_read(uc);
206 static void uclient_notify_read(struct ustream *us, int bytes)
208 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
210 __uclient_notify_read(uh);
213 static void uclient_notify_state(struct ustream *us)
215 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
217 uclient_notify_eof(uh);
220 static int uclient_setup_http(struct uclient_http *uh)
222 struct ustream *us = &uh->ufd.stream;
226 us->string_data = true;
227 us->notify_state = uclient_notify_state;
228 us->notify_read = uclient_notify_read;
230 ret = uclient_do_connect(uh, "80");
237 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
239 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
241 __uclient_notify_read(uh);
244 static void uclient_ssl_notify_state(struct ustream *us)
246 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
248 uclient_notify_eof(uh);
251 static int uclient_setup_https(struct uclient_http *uh)
253 struct ustream *us = &uh->ussl.stream;
259 ret = uclient_do_connect(uh, "443");
264 ssl_ctx = ustream_ssl_context_new(false);
266 us->string_data = true;
267 us->notify_state = uclient_ssl_notify_state;
268 us->notify_read = uclient_ssl_notify_read;
269 ustream_ssl_init(&uh->ussl, &uh->ufd.stream, ssl_ctx, false);
274 static void uclient_http_disconnect(struct uclient_http *uh)
276 uclient_backend_reset_state(&uh->uc);
277 uh->read_chunked = -1;
284 ustream_free(&uh->ussl.stream);
285 ustream_free(&uh->ufd.stream);
286 close(uh->ufd.fd.fd);
290 static int uclient_http_connect(struct uclient *cl)
292 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
294 uclient_http_disconnect(uh);
295 blob_buf_init(&uh->meta, 0);
297 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
298 uh->state = HTTP_STATE_INIT;
301 return uclient_setup_https(uh);
303 return uclient_setup_http(uh);
306 static struct uclient *uclient_http_alloc(void)
308 struct uclient_http *uh;
310 uh = calloc_a(sizeof(*uh));
311 blob_buf_init(&uh->headers, 0);
316 static void uclient_http_free(struct uclient *cl)
318 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
320 uclient_http_disconnect(uh);
321 blob_buf_free(&uh->headers);
322 blob_buf_free(&uh->meta);
327 uclient_http_set_request_type(struct uclient *cl, const char *type)
329 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
332 if (cl->backend != &uclient_backend_http)
335 if (uh->state > HTTP_STATE_INIT)
338 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
339 if (strcmp(request_types[i], type) != 0)
350 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
352 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
354 blob_buf_init(&uh->headers, 0);
360 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
362 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
364 if (cl->backend != &uclient_backend_http)
367 if (uh->state > HTTP_STATE_INIT)
370 blobmsg_add_string(&uh->headers, name, value);
375 uclient_http_send_headers(struct uclient_http *uh)
377 struct uclient_url *url = uh->uc.url;
378 struct blob_attr *cur;
381 if (uh->state >= HTTP_STATE_HEADERS_SENT)
384 ustream_printf(uh->us,
385 "%s /%s HTTP/1.1\r\n"
387 "Connection: close\r\n",
388 request_types[uh->req_type],
389 url->location, url->host);
391 blobmsg_for_each_attr(cur, uh->headers.head, rem)
392 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
395 int auth_len = strlen(url->auth);
401 auth_buf = alloca(base64_len(auth_len) + 1);
402 base64_encode(url->auth, auth_len, auth_buf);
403 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
406 if (uh->req_type == REQ_POST)
407 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
409 ustream_printf(uh->us, "\r\n");
413 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
415 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
417 if (uh->state >= HTTP_STATE_REQUEST_DONE)
420 uclient_http_send_headers(uh);
422 ustream_printf(uh->us, "%X\r\n", len);
423 ustream_write(uh->us, buf, len, false);
424 ustream_printf(uh->us, "\r\n");
430 uclient_http_request_done(struct uclient *cl)
432 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
434 if (uh->state >= HTTP_STATE_REQUEST_DONE)
437 uclient_http_send_headers(uh);
438 uh->state = HTTP_STATE_REQUEST_DONE;
444 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
446 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
448 char *data, *data_end;
450 if (uh->state < HTTP_STATE_RECV_DATA)
453 data = ustream_get_read_buf(uh->us, &read_len);
454 if (!data || !read_len)
457 data_end = data + read_len;
460 if (uh->read_chunked == 0) {
463 if (data[0] == '\r' && data[1] == '\n') {
468 sep = strstr(data, "\r\n");
473 uh->read_chunked = strtoul(data, NULL, 16);
475 read_len += sep + 2 - data;
478 if (!uh->read_chunked)
482 if (len > data_end - data)
483 len = data_end - data;
485 if (uh->read_chunked >= 0) {
486 if (len > uh->read_chunked)
487 len = uh->read_chunked;
489 uh->read_chunked -= len;
494 memcpy(buf, data, len);
498 ustream_consume(uh->us, read_len);
500 uclient_notify_eof(uh);
505 const struct uclient_backend uclient_backend_http __hidden = {
506 .prefix = uclient_http_prefix,
508 .alloc = uclient_http_alloc,
509 .free = uclient_http_free,
510 .connect = uclient_http_connect,
512 .read = uclient_http_read,
513 .write = uclient_http_send_data,
514 .request = uclient_http_request_done,