uclient-fetch: add progress bar support
[project/uclient.git] / uclient-fetch.c
1 /*
2 * uclient - ustream based protocol client library
3 *
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define _GNU_SOURCE
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <dlfcn.h>
24 #include <getopt.h>
25 #include <fcntl.h>
26 #include <glob.h>
27 #include <stdint.h>
28 #include <inttypes.h>
29 #include <signal.h>
30
31 #include <libubox/blobmsg.h>
32
33 #include "progress.h"
34 #include "uclient.h"
35 #include "uclient-utils.h"
36
37 #ifdef __APPLE__
38 #define LIB_EXT "dylib"
39 #else
40 #define LIB_EXT "so"
41 #endif
42
43 static const char *user_agent = "uclient-fetch";
44 static const char *post_data;
45 static struct ustream_ssl_ctx *ssl_ctx;
46 static const struct ustream_ssl_ops *ssl_ops;
47 static int quiet = false;
48 static bool verify = true;
49 static bool proxy = true;
50 static bool default_certs = false;
51 static bool no_output;
52 static const char *output_file;
53 static int output_fd = -1;
54 static int error_ret;
55 static off_t out_offset;
56 static off_t out_bytes;
57 static off_t out_len;
58 static char *auth_str;
59 static char **urls;
60 static int n_urls;
61 static int timeout;
62 static bool resume, cur_resume;
63
64 static struct progress pmt;
65 static struct uloop_timeout pmt_timer;
66
67 static int init_request(struct uclient *cl);
68 static void request_done(struct uclient *cl);
69
70 static void pmt_update(struct uloop_timeout *t)
71 {
72 progress_update(&pmt, out_offset, out_bytes, out_len);
73 uloop_timeout_set(t, 1000);
74 }
75
76 static const char *
77 get_proxy_url(char *url)
78 {
79 char prefix[16];
80 char *sep;
81
82 if (!proxy)
83 return NULL;
84
85 sep = strchr(url, ':');
86 if (!sep)
87 return NULL;
88
89 if (sep - url > 5)
90 return NULL;
91
92 memcpy(prefix, url, sep - url);
93 strcpy(prefix + (sep - url), "_proxy");
94 return getenv(prefix);
95 }
96
97 static int open_output_file(const char *path, uint64_t resume_offset)
98 {
99 char *filename = NULL;
100 int flags;
101 int ret;
102
103 if (cur_resume)
104 flags = O_RDWR;
105 else
106 flags = O_WRONLY | O_EXCL;
107
108 flags |= O_CREAT;
109
110 if (output_file) {
111 if (!strcmp(output_file, "-")) {
112 if (!quiet)
113 fprintf(stderr, "Writing to stdout\n");
114
115 return STDOUT_FILENO;
116 }
117 } else {
118 filename = uclient_get_url_filename(path, "index.html");
119 output_file = filename;
120 }
121
122 if (!quiet)
123 fprintf(stderr, "Writing to '%s'\n", output_file);
124 ret = open(output_file, flags, 0644);
125 if (ret < 0)
126 goto free;
127
128 if (resume_offset &&
129 lseek(ret, resume_offset, SEEK_SET) < 0) {
130 if (!quiet)
131 fprintf(stderr, "Failed to seek %"PRIu64" bytes in output file\n", resume_offset);
132 close(ret);
133 ret = -1;
134 goto free;
135 }
136
137 out_offset = resume_offset;
138 out_bytes += resume_offset;
139 if (!quiet) {
140 progress_init(&pmt, output_file);
141 pmt_timer.cb = pmt_update;
142 pmt_timer.cb(&pmt_timer);
143 }
144
145 free:
146 free(filename);
147 return ret;
148 }
149
150 static void header_done_cb(struct uclient *cl)
151 {
152 enum {
153 H_RANGE,
154 H_LEN,
155 __H_MAX
156 };
157 static const struct blobmsg_policy policy[__H_MAX] = {
158 [H_RANGE] = { .name = "content-range", .type = BLOBMSG_TYPE_STRING },
159 [H_LEN] = { .name = "content-length", .type = BLOBMSG_TYPE_STRING },
160 };
161 struct blob_attr *tb[__H_MAX];
162 uint64_t resume_offset = 0, resume_end, resume_size;
163 static int retries;
164
165 if (retries < 10 && uclient_http_redirect(cl)) {
166 if (!quiet)
167 fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
168
169 retries++;
170 return;
171 }
172
173 if (cl->status_code == 204 && cur_resume) {
174 /* Resume attempt failed, try normal download */
175 cur_resume = false;
176 init_request(cl);
177 return;
178 }
179
180 blobmsg_parse(policy, __H_MAX, tb, blob_data(cl->meta), blob_len(cl->meta));
181
182 switch (cl->status_code) {
183 case 416:
184 if (!quiet)
185 fprintf(stderr, "File download already fully retrieved; nothing to do.\n");
186 request_done(cl);
187 break;
188 case 206:
189 if (!cur_resume) {
190 if (!quiet)
191 fprintf(stderr, "Error: Partial content received, full content requested\n");
192 error_ret = 8;
193 request_done(cl);
194 break;
195 }
196
197 if (!tb[H_RANGE]) {
198 if (!quiet)
199 fprintf(stderr, "Content-Range header is missing\n");
200 error_ret = 8;
201 break;
202 }
203
204 if (sscanf(blobmsg_get_string(tb[H_RANGE]),
205 "bytes %"PRIu64"-%"PRIu64"/%"PRIu64,
206 &resume_offset, &resume_end, &resume_size) != 3) {
207 if (!quiet)
208 fprintf(stderr, "Content-Range header is invalid\n");
209 error_ret = 8;
210 break;
211 }
212 case 204:
213 case 200:
214 if (no_output)
215 break;
216
217 if (tb[H_LEN])
218 out_len = strtoul(blobmsg_get_string(tb[H_LEN]), NULL, 10);
219
220 output_fd = open_output_file(cl->url->location, resume_offset);
221 if (output_fd < 0) {
222 if (!quiet)
223 perror("Cannot open output file");
224 error_ret = 3;
225 request_done(cl);
226 }
227 break;
228
229 default:
230 if (!quiet)
231 fprintf(stderr, "HTTP error %d\n", cl->status_code);
232 request_done(cl);
233 error_ret = 8;
234 break;
235 }
236 }
237
238 static void read_data_cb(struct uclient *cl)
239 {
240 char buf[256];
241 int len;
242
243 if (!no_output && output_fd < 0)
244 return;
245
246 while (1) {
247 len = uclient_read(cl, buf, sizeof(buf));
248 if (!len)
249 return;
250
251 out_bytes += len;
252 if (!no_output)
253 write(output_fd, buf, len);
254 }
255 }
256
257 static void msg_connecting(struct uclient *cl)
258 {
259 char addr[INET6_ADDRSTRLEN];
260 int port;
261
262 if (quiet)
263 return;
264
265 uclient_get_addr(addr, &port, &cl->remote_addr);
266 fprintf(stderr, "Connecting to %s:%d\n", addr, port);
267 }
268
269 static void check_resume_offset(struct uclient *cl)
270 {
271 char range_str[64];
272 struct stat st;
273 char *file;
274 int ret;
275
276 file = uclient_get_url_filename(cl->url->location, "index.html");
277 if (!file)
278 return;
279
280 ret = stat(file, &st);
281 free(file);
282 if (ret)
283 return;
284
285 if (!st.st_size)
286 return;
287
288 snprintf(range_str, sizeof(range_str), "bytes=%"PRIu64"-", (uint64_t) st.st_size);
289 uclient_http_set_header(cl, "Range", range_str);
290 }
291
292 static int init_request(struct uclient *cl)
293 {
294 int rc;
295
296 out_offset = 0;
297 out_bytes = 0;
298 out_len = 0;
299 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
300
301 if (timeout)
302 cl->timeout_msecs = timeout * 1000;
303
304 rc = uclient_connect(cl);
305 if (rc)
306 return rc;
307
308 msg_connecting(cl);
309
310 rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
311 if (rc)
312 return rc;
313
314 uclient_http_reset_headers(cl);
315 uclient_http_set_header(cl, "User-Agent", user_agent);
316 if (cur_resume)
317 check_resume_offset(cl);
318
319 if (post_data) {
320 uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
321 uclient_write(cl, post_data, strlen(post_data));
322 }
323
324 rc = uclient_request(cl);
325 if (rc)
326 return rc;
327
328 return 0;
329 }
330
331 static void request_done(struct uclient *cl)
332 {
333 const char *proxy_url;
334
335 if (n_urls) {
336 proxy_url = get_proxy_url(*urls);
337 if (proxy_url) {
338 uclient_set_url(cl, proxy_url, NULL);
339 uclient_set_proxy_url(cl, *urls, auth_str);
340 } else {
341 uclient_set_url(cl, *urls, auth_str);
342 }
343 n_urls--;
344 cur_resume = resume;
345 error_ret = init_request(cl);
346 if (error_ret == 0)
347 return;
348 }
349
350 if (output_fd >= 0 && !output_file) {
351 close(output_fd);
352 output_fd = -1;
353 }
354 uclient_disconnect(cl);
355 uloop_end();
356 }
357
358
359 static void eof_cb(struct uclient *cl)
360 {
361 if (!quiet) {
362 pmt_update(&pmt_timer);
363 uloop_timeout_cancel(&pmt_timer);
364 }
365
366 if (!cl->data_eof) {
367 if (!quiet)
368 fprintf(stderr, "Connection reset prematurely\n");
369 error_ret = 4;
370 } else if (!quiet) {
371 fprintf(stderr, "Download completed (%"PRIu64" bytes)\n", (uint64_t) out_bytes);
372 }
373 request_done(cl);
374 }
375
376 static void handle_uclient_error(struct uclient *cl, int code)
377 {
378 const char *type = "Unknown error";
379 bool ignore = false;
380
381 switch(code) {
382 case UCLIENT_ERROR_CONNECT:
383 type = "Connection failed";
384 error_ret = 4;
385 break;
386 case UCLIENT_ERROR_TIMEDOUT:
387 type = "Connection timed out";
388 error_ret = 4;
389 break;
390 case UCLIENT_ERROR_SSL_INVALID_CERT:
391 type = "Invalid SSL certificate";
392 ignore = !verify;
393 error_ret = 5;
394 break;
395 case UCLIENT_ERROR_SSL_CN_MISMATCH:
396 type = "Server hostname does not match SSL certificate";
397 ignore = !verify;
398 error_ret = 5;
399 break;
400 default:
401 error_ret = 1;
402 break;
403 }
404
405 if (!quiet)
406 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
407
408 if (ignore)
409 error_ret = 0;
410 else
411 request_done(cl);
412 }
413
414 static const struct uclient_cb cb = {
415 .header_done = header_done_cb,
416 .data_read = read_data_cb,
417 .data_eof = eof_cb,
418 .error = handle_uclient_error,
419 };
420
421 static int usage(const char *progname)
422 {
423 fprintf(stderr,
424 "Usage: %s [options] <URL>\n"
425 "Options:\n"
426 " -q: Turn off status messages\n"
427 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
428 " -P <dir>: Set directory for output files\n"
429 " --user=<user> HTTP authentication username\n"
430 " --password=<password> HTTP authentication password\n"
431 " --user-agent|-U <str> Set HTTP user agent\n"
432 " --post-data=STRING use the POST method; send STRING as the data\n"
433 " --spider|-s Spider mode - only check file existence\n"
434 " --timeout=N|-T N Set connect/request timeout to N seconds\n"
435 " --proxy=on|off|-Y on|off Enable/disable env var configured proxy\n"
436 "\n"
437 "HTTPS options:\n"
438 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
439 " --no-check-certificate: don't validate the server's certificate\n"
440 "\n", progname);
441 return 1;
442 }
443
444 static void init_ca_cert(void)
445 {
446 glob_t gl;
447 int i;
448
449 glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
450 for (i = 0; i < gl.gl_pathc; i++)
451 ssl_ops->context_add_ca_crt_file(ssl_ctx, gl.gl_pathv[i]);
452 }
453
454 static void init_ustream_ssl(void)
455 {
456 void *dlh;
457
458 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
459 if (!dlh)
460 return;
461
462 ssl_ops = dlsym(dlh, "ustream_ssl_ops");
463 if (!ssl_ops)
464 return;
465
466 ssl_ctx = ssl_ops->context_new(false);
467 }
468
469 static int no_ssl(const char *progname)
470 {
471 fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
472 return 1;
473 }
474
475 enum {
476 L_NO_CHECK_CERTIFICATE,
477 L_CA_CERTIFICATE,
478 L_USER,
479 L_PASSWORD,
480 L_USER_AGENT,
481 L_POST_DATA,
482 L_SPIDER,
483 L_TIMEOUT,
484 L_CONTINUE,
485 L_PROXY,
486 L_NO_PROXY,
487 };
488
489 static const struct option longopts[] = {
490 [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
491 [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
492 [L_USER] = { "user", required_argument },
493 [L_PASSWORD] = { "password", required_argument },
494 [L_USER_AGENT] = { "user-agent", required_argument },
495 [L_POST_DATA] = { "post-data", required_argument },
496 [L_SPIDER] = { "spider", no_argument },
497 [L_TIMEOUT] = { "timeout", required_argument },
498 [L_CONTINUE] = { "continue", no_argument },
499 [L_PROXY] = { "proxy", required_argument },
500 [L_NO_PROXY] = { "no-proxy", no_argument },
501 {}
502 };
503
504
505
506 int main(int argc, char **argv)
507 {
508 const char *progname = argv[0];
509 const char *proxy_url;
510 char *username = NULL;
511 char *password = NULL;
512 struct uclient *cl;
513 int longopt_idx = 0;
514 bool has_cert = false;
515 int i, ch;
516 int rc;
517
518 signal(SIGPIPE, SIG_IGN);
519 init_ustream_ssl();
520
521 while ((ch = getopt_long(argc, argv, "cO:P:qsU:Y:", longopts, &longopt_idx)) != -1) {
522 switch(ch) {
523 case 0:
524 switch (longopt_idx) {
525 case L_NO_CHECK_CERTIFICATE:
526 verify = false;
527 break;
528 case L_CA_CERTIFICATE:
529 has_cert = true;
530 if (ssl_ctx)
531 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
532 break;
533 case L_USER:
534 if (!strlen(optarg))
535 break;
536 username = strdup(optarg);
537 memset(optarg, '*', strlen(optarg));
538 break;
539 case L_PASSWORD:
540 if (!strlen(optarg))
541 break;
542 password = strdup(optarg);
543 memset(optarg, '*', strlen(optarg));
544 break;
545 case L_USER_AGENT:
546 user_agent = optarg;
547 break;
548 case L_POST_DATA:
549 post_data = optarg;
550 break;
551 case L_SPIDER:
552 no_output = true;
553 break;
554 case L_TIMEOUT:
555 timeout = atoi(optarg);
556 break;
557 case L_CONTINUE:
558 resume = true;
559 break;
560 case L_PROXY:
561 if (strcmp(optarg, "on") != 0)
562 proxy = false;
563 break;
564 case L_NO_PROXY:
565 proxy = false;
566 break;
567 default:
568 return usage(progname);
569 }
570 break;
571 case 'c':
572 resume = true;
573 break;
574 case 'U':
575 user_agent = optarg;
576 break;
577 case 'O':
578 output_file = optarg;
579 break;
580 case 'P':
581 if (chdir(optarg)) {
582 if (!quiet)
583 perror("Change output directory");
584 exit(1);
585 }
586 break;
587 case 'q':
588 quiet = true;
589 break;
590 case 's':
591 no_output = true;
592 break;
593 case 'T':
594 timeout = atoi(optarg);
595 break;
596 case 'Y':
597 if (strcmp(optarg, "on") != 0)
598 proxy = false;
599 break;
600 default:
601 return usage(progname);
602 }
603 }
604
605 argv += optind;
606 argc -= optind;
607
608 if (verify && !has_cert)
609 default_certs = true;
610
611 if (argc < 1)
612 return usage(progname);
613
614 if (!ssl_ctx) {
615 for (i = 0; i < argc; i++) {
616 if (!strncmp(argv[i], "https", 5))
617 return no_ssl(progname);
618 }
619 }
620
621 urls = argv + 1;
622 n_urls = argc - 1;
623
624 uloop_init();
625
626 if (username) {
627 if (password)
628 asprintf(&auth_str, "%s:%s", username, password);
629 else
630 auth_str = username;
631 }
632
633 if (!quiet)
634 fprintf(stderr, "Downloading '%s'\n", argv[0]);
635
636 proxy_url = get_proxy_url(argv[0]);
637 if (proxy_url) {
638 cl = uclient_new(proxy_url, auth_str, &cb);
639 uclient_set_proxy_url(cl, argv[0], NULL);
640 } else {
641 cl = uclient_new(argv[0], auth_str, &cb);
642 }
643 if (!cl) {
644 fprintf(stderr, "Failed to allocate uclient context\n");
645 return 1;
646 }
647
648 if (ssl_ctx && default_certs)
649 init_ca_cert();
650
651 cur_resume = resume;
652 rc = init_request(cl);
653 if (!rc) {
654 /* no error received, we can enter main loop */
655 uloop_run();
656 } else {
657 fprintf(stderr, "Failed to establish connection\n");
658 error_ret = 4;
659 }
660
661 uloop_done();
662
663 uclient_free(cl);
664
665 if (output_fd >= 0 && output_fd != STDOUT_FILENO)
666 close(output_fd);
667
668 if (ssl_ctx)
669 ssl_ops->context_free(ssl_ctx);
670
671 return error_ret;
672 }