uclient-fetch.c 16 KB

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