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