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);
137 if (uh->req_type == REQ_HEAD) {
139 uclient_notify_eof(uh);
145 sep = strchr(data, ':');
151 for (name = data; *name; name++)
152 *name = tolower(*name);
155 while (isspace(*sep))
158 blobmsg_add_string(&uh->meta, name, sep);
161 static void __uclient_notify_read(struct uclient_http *uh)
163 struct uclient *uc = &uh->uc;
167 if (uh->state < HTTP_STATE_REQUEST_DONE)
170 data = ustream_get_read_buf(uh->us, &len);
174 if (uh->state < HTTP_STATE_RECV_DATA) {
179 sep = strstr(data, "\r\n");
183 /* Check for multi-line HTTP headers */
188 if (isspace(sep[2]) && sep[2] != '\r') {
196 cur_len = sep + 2 - data;
197 uclient_parse_http_line(uh, data);
198 ustream_consume(uh->us, cur_len);
201 data = ustream_get_read_buf(uh->us, &len);
202 } while (uh->state < HTTP_STATE_RECV_DATA);
208 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
209 uc->cb->data_read(uc);
212 static void uclient_notify_read(struct ustream *us, int bytes)
214 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
216 __uclient_notify_read(uh);
219 static void uclient_notify_state(struct ustream *us)
221 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
223 uclient_notify_eof(uh);
226 static int uclient_setup_http(struct uclient_http *uh)
228 struct ustream *us = &uh->ufd.stream;
232 us->string_data = true;
233 us->notify_state = uclient_notify_state;
234 us->notify_read = uclient_notify_read;
236 ret = uclient_do_connect(uh, "80");
243 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
245 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
247 __uclient_notify_read(uh);
250 static void uclient_ssl_notify_state(struct ustream *us)
252 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
254 uclient_notify_eof(uh);
257 static int uclient_setup_https(struct uclient_http *uh)
259 struct ustream *us = &uh->ussl.stream;
265 ret = uclient_do_connect(uh, "443");
270 ssl_ctx = ustream_ssl_context_new(false);
272 us->string_data = true;
273 us->notify_state = uclient_ssl_notify_state;
274 us->notify_read = uclient_ssl_notify_read;
275 ustream_ssl_init(&uh->ussl, &uh->ufd.stream, ssl_ctx, false);
280 static void uclient_http_disconnect(struct uclient_http *uh)
282 uclient_backend_reset_state(&uh->uc);
283 uh->read_chunked = -1;
290 ustream_free(&uh->ussl.stream);
291 ustream_free(&uh->ufd.stream);
292 close(uh->ufd.fd.fd);
296 static int uclient_http_connect(struct uclient *cl)
298 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
300 uclient_http_disconnect(uh);
301 blob_buf_init(&uh->meta, 0);
303 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
304 uh->state = HTTP_STATE_INIT;
307 return uclient_setup_https(uh);
309 return uclient_setup_http(uh);
312 static struct uclient *uclient_http_alloc(void)
314 struct uclient_http *uh;
316 uh = calloc_a(sizeof(*uh));
317 blob_buf_init(&uh->headers, 0);
322 static void uclient_http_free(struct uclient *cl)
324 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
326 uclient_http_disconnect(uh);
327 blob_buf_free(&uh->headers);
328 blob_buf_free(&uh->meta);
333 uclient_http_set_request_type(struct uclient *cl, const char *type)
335 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
338 if (cl->backend != &uclient_backend_http)
341 if (uh->state > HTTP_STATE_INIT)
344 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
345 if (strcmp(request_types[i], type) != 0)
356 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
358 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
360 blob_buf_init(&uh->headers, 0);
366 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
368 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
370 if (cl->backend != &uclient_backend_http)
373 if (uh->state > HTTP_STATE_INIT)
376 blobmsg_add_string(&uh->headers, name, value);
381 uclient_http_send_headers(struct uclient_http *uh)
383 struct uclient_url *url = uh->uc.url;
384 struct blob_attr *cur;
387 if (uh->state >= HTTP_STATE_HEADERS_SENT)
390 ustream_printf(uh->us,
391 "%s /%s HTTP/1.1\r\n"
393 "Connection: close\r\n",
394 request_types[uh->req_type],
395 url->location, url->host);
397 blobmsg_for_each_attr(cur, uh->headers.head, rem)
398 ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
401 int auth_len = strlen(url->auth);
407 auth_buf = alloca(base64_len(auth_len) + 1);
408 base64_encode(url->auth, auth_len, auth_buf);
409 ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
412 if (uh->req_type == REQ_POST)
413 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
415 ustream_printf(uh->us, "\r\n");
419 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
421 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
423 if (uh->state >= HTTP_STATE_REQUEST_DONE)
426 uclient_http_send_headers(uh);
428 ustream_printf(uh->us, "%X\r\n", len);
429 ustream_write(uh->us, buf, len, false);
430 ustream_printf(uh->us, "\r\n");
436 uclient_http_request_done(struct uclient *cl)
438 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
440 if (uh->state >= HTTP_STATE_REQUEST_DONE)
443 uclient_http_send_headers(uh);
444 uh->state = HTTP_STATE_REQUEST_DONE;
450 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
452 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
454 char *data, *data_end;
456 if (uh->state < HTTP_STATE_RECV_DATA)
459 data = ustream_get_read_buf(uh->us, &read_len);
460 if (!data || !read_len)
463 data_end = data + read_len;
466 if (uh->read_chunked == 0) {
469 if (data[0] == '\r' && data[1] == '\n') {
474 sep = strstr(data, "\r\n");
479 uh->read_chunked = strtoul(data, NULL, 16);
481 read_len += sep + 2 - data;
484 if (!uh->read_chunked)
488 if (len > data_end - data)
489 len = data_end - data;
491 if (uh->read_chunked >= 0) {
492 if (len > uh->read_chunked)
493 len = uh->read_chunked;
495 uh->read_chunked -= len;
500 memcpy(buf, data, len);
504 ustream_consume(uh->us, read_len);
506 uclient_notify_eof(uh);
511 const struct uclient_backend uclient_backend_http __hidden = {
512 .prefix = uclient_http_prefix,
514 .alloc = uclient_http_alloc,
515 .free = uclient_http_free,
516 .connect = uclient_http_connect,
518 .read = uclient_http_read,
519 .write = uclient_http_send_data,
520 .request = uclient_http_request_done,