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);
374 #define ustream_printf(us, ...) do { \
375 fprintf(stderr, "send: " __VA_ARGS__); \
376 ustream_printf(us, __VA_ARGS__); \
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,