set eof on HEAD requests after header end
[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
137 if (uh->req_type == REQ_HEAD) {
138 uh->eof = true;
139 uclient_notify_eof(uh);
140 }
141
142 return;
143 }
144
145 sep = strchr(data, ':');
146 if (!sep)
147 return;
148
149 *(sep++) = 0;
150
151 for (name = data; *name; name++)
152 *name = tolower(*name);
153
154 name = data;
155 while (isspace(*sep))
156 sep++;
157
158 blobmsg_add_string(&uh->meta, name, sep);
159 }
160
161 static void __uclient_notify_read(struct uclient_http *uh)
162 {
163 struct uclient *uc = &uh->uc;
164 char *data;
165 int len;
166
167 if (uh->state < HTTP_STATE_REQUEST_DONE)
168 return;
169
170 data = ustream_get_read_buf(uh->us, &len);
171 if (!data || !len)
172 return;
173
174 if (uh->state < HTTP_STATE_RECV_DATA) {
175 char *sep;
176 int cur_len;
177
178 do {
179 sep = strstr(data, "\r\n");
180 if (!sep)
181 break;
182
183 /* Check for multi-line HTTP headers */
184 if (sep > data) {
185 if (!sep[2])
186 return;
187
188 if (isspace(sep[2]) && sep[2] != '\r') {
189 sep[0] = ' ';
190 sep[1] = ' ';
191 continue;
192 }
193 }
194
195 *sep = 0;
196 cur_len = sep + 2 - data;
197 uclient_parse_http_line(uh, data);
198 ustream_consume(uh->us, cur_len);
199 len -= cur_len;
200
201 data = ustream_get_read_buf(uh->us, &len);
202 } while (uh->state < HTTP_STATE_RECV_DATA);
203
204 if (!len)
205 return;
206 }
207
208 if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
209 uc->cb->data_read(uc);
210 }
211
212 static void uclient_notify_read(struct ustream *us, int bytes)
213 {
214 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
215
216 __uclient_notify_read(uh);
217 }
218
219 static void uclient_notify_state(struct ustream *us)
220 {
221 struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
222
223 uclient_notify_eof(uh);
224 }
225
226 static int uclient_setup_http(struct uclient_http *uh)
227 {
228 struct ustream *us = &uh->ufd.stream;
229 int ret;
230
231 uh->us = us;
232 us->string_data = true;
233 us->notify_state = uclient_notify_state;
234 us->notify_read = uclient_notify_read;
235
236 ret = uclient_do_connect(uh, "80");
237 if (ret)
238 return ret;
239
240 return 0;
241 }
242
243 static void uclient_ssl_notify_read(struct ustream *us, int bytes)
244 {
245 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
246
247 __uclient_notify_read(uh);
248 }
249
250 static void uclient_ssl_notify_state(struct ustream *us)
251 {
252 struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
253
254 uclient_notify_eof(uh);
255 }
256
257 static int uclient_setup_https(struct uclient_http *uh)
258 {
259 struct ustream *us = &uh->ussl.stream;
260 int ret;
261
262 uh->ssl = true;
263 uh->us = us;
264
265 ret = uclient_do_connect(uh, "443");
266 if (ret)
267 return ret;
268
269 if (!ssl_ctx)
270 ssl_ctx = ustream_ssl_context_new(false);
271
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);
276
277 return 0;
278 }
279
280 static void uclient_http_disconnect(struct uclient_http *uh)
281 {
282 uclient_backend_reset_state(&uh->uc);
283 uh->read_chunked = -1;
284 uh->eof = false;
285
286 if (!uh->us)
287 return;
288
289 if (uh->ssl)
290 ustream_free(&uh->ussl.stream);
291 ustream_free(&uh->ufd.stream);
292 close(uh->ufd.fd.fd);
293 uh->us = NULL;
294 }
295
296 static int uclient_http_connect(struct uclient *cl)
297 {
298 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
299
300 uclient_http_disconnect(uh);
301 blob_buf_init(&uh->meta, 0);
302
303 uh->ssl = cl->url->prefix == PREFIX_HTTPS;
304 uh->state = HTTP_STATE_INIT;
305
306 if (uh->ssl)
307 return uclient_setup_https(uh);
308 else
309 return uclient_setup_http(uh);
310 }
311
312 static struct uclient *uclient_http_alloc(void)
313 {
314 struct uclient_http *uh;
315
316 uh = calloc_a(sizeof(*uh));
317 blob_buf_init(&uh->headers, 0);
318
319 return &uh->uc;
320 }
321
322 static void uclient_http_free(struct uclient *cl)
323 {
324 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
325
326 uclient_http_disconnect(uh);
327 blob_buf_free(&uh->headers);
328 blob_buf_free(&uh->meta);
329 free(uh);
330 }
331
332 int
333 uclient_http_set_request_type(struct uclient *cl, const char *type)
334 {
335 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
336 int i;
337
338 if (cl->backend != &uclient_backend_http)
339 return -1;
340
341 if (uh->state > HTTP_STATE_INIT)
342 return -1;
343
344 for (i = 0; i < ARRAY_SIZE(request_types); i++) {
345 if (strcmp(request_types[i], type) != 0)
346 continue;
347
348 uh->req_type = i;
349 return 0;
350 }
351
352 return -1;
353 }
354
355 int
356 uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
357 {
358 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
359
360 blob_buf_init(&uh->headers, 0);
361
362 return 0;
363 }
364
365 int
366 uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
367 {
368 struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
369
370 if (cl->backend != &uclient_backend_http)
371 return -1;
372
373 if (uh->state > HTTP_STATE_INIT)
374 return -1;
375
376 blobmsg_add_string(&uh->headers, name, value);
377 return 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 };