4b8ca8b4d4e94b13807e5fd8c4cdd06c0895f9f4
[project/uclient.git] / uclient-http.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <unistd.h>
4
5 #include <libubox/ustream.h>
6 #include <libubox/ustream-ssl.h>
7 #include <libubox/usock.h>
8 #include <libubox/blobmsg.h>
9
10 #include "uclient.h"
11 #include "uclient-utils.h"
12 #include "uclient-backend.h"
13
14 static struct ustream_ssl_ctx *ssl_ctx;
15
16 enum request_type {
17         REQ_GET,
18         REQ_HEAD,
19         REQ_POST,
20         __REQ_MAX
21 };
22
23 enum http_state {
24         HTTP_STATE_INIT,
25         HTTP_STATE_HEADERS_SENT,
26         HTTP_STATE_REQUEST_DONE,
27         HTTP_STATE_RECV_HEADERS,
28         HTTP_STATE_RECV_DATA,
29         HTTP_STATE_ERROR,
30 };
31
32 static const char * const request_types[__REQ_MAX] = {
33         [REQ_GET] = "GET",
34         [REQ_HEAD] = "HEAD",
35         [REQ_POST] = "POST",
36 };
37
38 struct uclient_http {
39         struct uclient uc;
40
41         struct ustream *us;
42
43         struct ustream_fd ufd;
44         struct ustream_ssl ussl;
45
46         bool ssl;
47         bool eof;
48         bool connection_close;
49         enum request_type req_type;
50         enum http_state state;
51
52         long read_chunked;
53         long content_length;
54
55         struct blob_buf headers;
56         struct blob_buf meta;
57 };
58
59 enum {
60         PREFIX_HTTP,
61         PREFIX_HTTPS,
62         __PREFIX_MAX,
63 };
64
65 static const char * const uclient_http_prefix[] = {
66         [PREFIX_HTTP] = "http://",
67         [PREFIX_HTTPS] = "https://",
68         [__PREFIX_MAX] = NULL
69 };
70
71 static int uclient_do_connect(struct uclient_http *uh, const char *port)
72 {
73         int fd;
74
75         if (uh->uc.url->port)
76                 port = uh->uc.url->port;
77
78         fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
79         if (fd < 0)
80                 return -1;
81
82         ustream_fd_init(&uh->ufd, fd);
83         return 0;
84 }
85
86 static void uclient_http_disconnect(struct uclient *cl)
87 {
88         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
89
90         if (!uh->us)
91                 return;
92
93         if (uh->ssl)
94                 ustream_free(&uh->ussl.stream);
95         ustream_free(&uh->ufd.stream);
96         close(uh->ufd.fd.fd);
97         uh->us = NULL;
98 }
99
100 static void uclient_notify_eof(struct uclient_http *uh)
101 {
102         struct ustream *us = uh->us;
103
104         if (!uh->eof) {
105                 if (!us->eof && !us->write_error)
106                         return;
107
108                 if (ustream_pending_data(us, false))
109                         return;
110         }
111
112         uclient_backend_set_eof(&uh->uc);
113
114         if (uh->connection_close)
115                 uclient_http_disconnect(&uh->uc);
116 }
117
118 static void uclient_http_process_headers(struct uclient_http *uh)
119 {
120         enum {
121                 HTTP_HDR_TRANSFER_ENCODING,
122                 HTTP_HDR_CONNECTION,
123                 HTTP_HDR_CONTENT_LENGTH,
124                 __HTTP_HDR_MAX,
125         };
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"),
131 #undef hdr
132         };
133         struct blob_attr *tb[__HTTP_HDR_MAX];
134         struct blob_attr *cur;
135
136         blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
137
138         cur = tb[HTTP_HDR_TRANSFER_ENCODING];
139         if (cur && strstr(blobmsg_data(cur), "chunked"))
140                 uh->read_chunked = 0;
141
142         cur = tb[HTTP_HDR_CONNECTION];
143         if (cur && strstr(blobmsg_data(cur), "close"))
144                 uh->connection_close = true;
145
146         cur = tb[HTTP_HDR_CONTENT_LENGTH];
147         if (cur)
148                 uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
149 }
150
151 static void uclient_parse_http_line(struct uclient_http *uh, char *data)
152 {
153         char *name;
154         char *sep;
155
156         if (uh->state == HTTP_STATE_REQUEST_DONE) {
157                 uh->state = HTTP_STATE_RECV_HEADERS;
158                 return;
159         }
160
161         if (!*data) {
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);
167
168                 if (uh->req_type == REQ_HEAD) {
169                         uh->eof = true;
170                         uclient_notify_eof(uh);
171                 }
172
173                 return;
174         }
175
176         sep = strchr(data, ':');
177         if (!sep)
178                 return;
179
180         *(sep++) = 0;
181
182         for (name = data; *name; name++)
183                 *name = tolower(*name);
184
185         name = data;
186         while (isspace(*sep))
187                 sep++;
188
189         blobmsg_add_string(&uh->meta, name, sep);
190 }
191
192 static void __uclient_notify_read(struct uclient_http *uh)
193 {
194         struct uclient *uc = &uh->uc;
195         char *data;
196         int len;
197
198         if (uh->state < HTTP_STATE_REQUEST_DONE)
199                 return;
200
201         data = ustream_get_read_buf(uh->us, &len);
202         if (!data || !len)
203                 return;
204
205         if (uh->state < HTTP_STATE_RECV_DATA) {
206                 char *sep;
207                 int cur_len;
208
209                 do {
210                         sep = strstr(data, "\r\n");
211                         if (!sep)
212                                 break;
213
214                         /* Check for multi-line HTTP headers */
215                         if (sep > data) {
216                                 if (!sep[2])
217                                         return;
218
219                                 if (isspace(sep[2]) && sep[2] != '\r') {
220                                         sep[0] = ' ';
221                                         sep[1] = ' ';
222                                         continue;
223                                 }
224                         }
225
226                         *sep = 0;
227                         cur_len = sep + 2 - data;
228                         uclient_parse_http_line(uh, data);
229                         ustream_consume(uh->us, cur_len);
230                         len -= cur_len;
231
232                         data = ustream_get_read_buf(uh->us, &len);
233                 } while (uh->state < HTTP_STATE_RECV_DATA);
234
235                 if (!len)
236                         return;
237         }
238
239         if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
240                 uc->cb->data_read(uc);
241 }
242
243 static void uclient_notify_read(struct ustream *us, int bytes)
244 {
245         struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
246
247         __uclient_notify_read(uh);
248 }
249
250 static void uclient_notify_state(struct ustream *us)
251 {
252         struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
253
254         uclient_notify_eof(uh);
255 }
256
257 static int uclient_setup_http(struct uclient_http *uh)
258 {
259         struct ustream *us = &uh->ufd.stream;
260         int ret;
261
262         uh->us = us;
263         us->string_data = true;
264         us->notify_state = uclient_notify_state;
265         us->notify_read = uclient_notify_read;
266
267         ret = uclient_do_connect(uh, "80");
268         if (ret)
269                 return ret;
270
271         return 0;
272 }
273
274 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
275 {
276         struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
277
278         __uclient_notify_read(uh);
279 }
280
281 static void uclient_ssl_notify_state(struct ustream *us)
282 {
283         struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
284
285         uclient_notify_eof(uh);
286 }
287
288 static int uclient_setup_https(struct uclient_http *uh)
289 {
290         struct ustream *us = &uh->ussl.stream;
291         int ret;
292
293         uh->ssl = true;
294         uh->us = us;
295
296         ret = uclient_do_connect(uh, "443");
297         if (ret)
298                 return ret;
299
300         if (!ssl_ctx)
301                 ssl_ctx = ustream_ssl_context_new(false);
302
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);
307
308         return 0;
309 }
310
311 static void uclient_http_reset_state(struct uclient_http *uh)
312 {
313         uclient_backend_reset_state(&uh->uc);
314         uh->read_chunked = -1;
315         uh->content_length = -1;
316         uh->eof = false;
317         uh->connection_close = false;
318         uh->state = HTTP_STATE_INIT;
319 }
320
321 static int uclient_http_connect(struct uclient *cl)
322 {
323         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
324
325         uclient_http_reset_state(uh);
326         blob_buf_init(&uh->meta, 0);
327
328         if (uh->us)
329                 return 0;
330
331         uh->ssl = cl->url->prefix == PREFIX_HTTPS;
332
333         if (uh->ssl)
334                 return uclient_setup_https(uh);
335         else
336                 return uclient_setup_http(uh);
337 }
338
339 static struct uclient *uclient_http_alloc(void)
340 {
341         struct uclient_http *uh;
342
343         uh = calloc_a(sizeof(*uh));
344         blob_buf_init(&uh->headers, 0);
345
346         return &uh->uc;
347 }
348
349 static void uclient_http_free(struct uclient *cl)
350 {
351         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
352
353         uclient_http_disconnect(cl);
354         blob_buf_free(&uh->headers);
355         blob_buf_free(&uh->meta);
356         free(uh);
357 }
358
359 int
360 uclient_http_set_request_type(struct uclient *cl, const char *type)
361 {
362         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
363         int i;
364
365         if (cl->backend != &uclient_backend_http)
366                 return -1;
367
368         if (uh->state > HTTP_STATE_INIT)
369                 return -1;
370
371         for (i = 0; i < ARRAY_SIZE(request_types); i++) {
372                 if (strcmp(request_types[i], type) != 0)
373                         continue;
374
375                 uh->req_type = i;
376                 return 0;
377         }
378
379         return -1;
380 }
381
382 int
383 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
384 {
385         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
386
387         blob_buf_init(&uh->headers, 0);
388
389         return 0;
390 }
391
392 int
393 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
394 {
395         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
396
397         if (cl->backend != &uclient_backend_http)
398                 return -1;
399
400         if (uh->state > HTTP_STATE_INIT)
401                 return -1;
402
403         blobmsg_add_string(&uh->headers, name, value);
404         return 0;
405 }
406
407 static void
408 uclient_http_send_headers(struct uclient_http *uh)
409 {
410         struct uclient_url *url = uh->uc.url;
411         struct blob_attr *cur;
412         int rem;
413
414         if (uh->state >= HTTP_STATE_HEADERS_SENT)
415                 return;
416
417         ustream_printf(uh->us,
418                 "%s /%s HTTP/1.1\r\n"
419                 "Host: %s\r\n",
420                 request_types[uh->req_type],
421                 url->location, url->host);
422
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));
425
426         if (url->auth) {
427                 int auth_len = strlen(url->auth);
428                 char *auth_buf;
429
430                 if (auth_len > 512)
431                         return;
432
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);
436         }
437
438         if (uh->req_type == REQ_POST)
439                 ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
440
441         ustream_printf(uh->us, "\r\n");
442 }
443
444 static int
445 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
446 {
447         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
448
449         if (uh->state >= HTTP_STATE_REQUEST_DONE)
450                 return -1;
451
452         uclient_http_send_headers(uh);
453
454         ustream_printf(uh->us, "%X\r\n", len);
455         ustream_write(uh->us, buf, len, false);
456         ustream_printf(uh->us, "\r\n");
457
458         return len;
459 }
460
461 static int
462 uclient_http_request_done(struct uclient *cl)
463 {
464         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
465
466         if (uh->state >= HTTP_STATE_REQUEST_DONE)
467                 return -1;
468
469         uclient_http_send_headers(uh);
470         uh->state = HTTP_STATE_REQUEST_DONE;
471
472         return 0;
473 }
474
475 static int
476 uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
477 {
478         struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
479         int read_len = 0;
480         char *data, *data_end;
481
482         if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
483                 return 0;
484
485         data = ustream_get_read_buf(uh->us, &read_len);
486         if (!data || !read_len)
487                 return 0;
488
489         data_end = data + read_len;
490         read_len = 0;
491
492         if (uh->read_chunked == 0) {
493                 char *sep;
494
495                 if (data[0] == '\r' && data[1] == '\n') {
496                         data += 2;
497                         read_len += 2;
498                 }
499
500                 sep = strstr(data, "\r\n");
501                 if (!sep)
502                         return 0;
503
504                 *sep = 0;
505                 uh->read_chunked = strtoul(data, NULL, 16);
506
507                 read_len += sep + 2 - data;
508                 data = sep + 2;
509
510                 if (!uh->read_chunked)
511                         uh->eof = true;
512         }
513
514         if (len > data_end - data)
515                 len = data_end - data;
516
517         if (uh->read_chunked >= 0) {
518                 if (len > uh->read_chunked)
519                         len = uh->read_chunked;
520
521                 uh->read_chunked -= len;
522         } else if (uh->content_length >= 0) {
523                 if (len > uh->content_length)
524                         len = uh->content_length;
525
526                 uh->content_length -= len;
527                 if (!uh->content_length)
528                         uh->eof = true;
529         }
530
531         if (len > 0) {
532                 read_len += len;
533                 memcpy(buf, data, len);
534         }
535
536         if (read_len > 0)
537                 ustream_consume(uh->us, read_len);
538
539         uclient_notify_eof(uh);
540
541         return len;
542 }
543
544 const struct uclient_backend uclient_backend_http __hidden = {
545         .prefix = uclient_http_prefix,
546
547         .alloc = uclient_http_alloc,
548         .free = uclient_http_free,
549         .connect = uclient_http_connect,
550         .update_url = uclient_http_disconnect,
551
552         .read = uclient_http_read,
553         .write = uclient_http_send_data,
554         .request = uclient_http_request_done,
555 };