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