2
0

hx-download.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * HTTP/2 server push
  26. * </DESC>
  27. */
  28. /* curl stuff */
  29. #include <curl/curl.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #ifndef _MSC_VER
  34. /* somewhat Unix-specific */
  35. #include <unistd.h> /* getopt() */
  36. #endif
  37. #ifndef CURLPIPE_MULTIPLEX
  38. #error "too old libcurl, cannot do HTTP/2 server push!"
  39. #endif
  40. #ifndef _MSC_VER
  41. static int verbose = 1;
  42. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  43. {
  44. /*
  45. * This is the trace look that is similar to what libcurl makes on its
  46. * own.
  47. */
  48. static const char * const s_infotype[] = {
  49. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  50. };
  51. if(idsbuf && *idsbuf)
  52. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  53. else
  54. fputs(s_infotype[type], log);
  55. }
  56. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  57. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  58. CURL_FORMAT_CURL_OFF_T "] "
  59. /*
  60. ** callback for CURLOPT_DEBUGFUNCTION
  61. */
  62. static int debug_cb(CURL *handle, curl_infotype type,
  63. char *data, size_t size,
  64. void *userdata)
  65. {
  66. FILE *output = stderr;
  67. static int newl = 0;
  68. static int traced_data = 0;
  69. char idsbuf[60];
  70. curl_off_t xfer_id, conn_id;
  71. (void)handle; /* not used */
  72. (void)userdata;
  73. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  74. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  75. conn_id >= 0) {
  76. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2, xfer_id,
  77. conn_id);
  78. }
  79. else {
  80. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  81. }
  82. }
  83. else
  84. idsbuf[0] = 0;
  85. switch(type) {
  86. case CURLINFO_HEADER_OUT:
  87. if(size > 0) {
  88. size_t st = 0;
  89. size_t i;
  90. for(i = 0; i < size - 1; i++) {
  91. if(data[i] == '\n') { /* LF */
  92. if(!newl) {
  93. log_line_start(output, idsbuf, type);
  94. }
  95. (void)fwrite(data + st, i - st + 1, 1, output);
  96. st = i + 1;
  97. newl = 0;
  98. }
  99. }
  100. if(!newl)
  101. log_line_start(output, idsbuf, type);
  102. (void)fwrite(data + st, i - st + 1, 1, output);
  103. }
  104. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  105. traced_data = 0;
  106. break;
  107. case CURLINFO_TEXT:
  108. case CURLINFO_HEADER_IN:
  109. if(!newl)
  110. log_line_start(output, idsbuf, type);
  111. (void)fwrite(data, size, 1, output);
  112. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  113. traced_data = 0;
  114. break;
  115. case CURLINFO_DATA_OUT:
  116. case CURLINFO_DATA_IN:
  117. case CURLINFO_SSL_DATA_IN:
  118. case CURLINFO_SSL_DATA_OUT:
  119. if(!traced_data) {
  120. if(!newl)
  121. log_line_start(output, idsbuf, type);
  122. fprintf(output, "[%ld bytes data]\n", (long)size);
  123. newl = 0;
  124. traced_data = 1;
  125. }
  126. break;
  127. default: /* nada */
  128. newl = 0;
  129. traced_data = 1;
  130. break;
  131. }
  132. return 0;
  133. }
  134. struct transfer {
  135. int idx;
  136. CURL *easy;
  137. char filename[128];
  138. FILE *out;
  139. curl_off_t recv_size;
  140. curl_off_t fail_at;
  141. curl_off_t pause_at;
  142. curl_off_t abort_at;
  143. int started;
  144. int paused;
  145. int resumed;
  146. int done;
  147. };
  148. static size_t transfer_count = 1;
  149. static struct transfer *transfers;
  150. static int forbid_reuse = 0;
  151. static struct transfer *get_transfer_for_easy(CURL *easy)
  152. {
  153. size_t i;
  154. for(i = 0; i < transfer_count; ++i) {
  155. if(easy == transfers[i].easy)
  156. return &transfers[i];
  157. }
  158. return NULL;
  159. }
  160. static size_t my_write_cb(char *buf, size_t nitems, size_t buflen,
  161. void *userdata)
  162. {
  163. struct transfer *t = userdata;
  164. size_t blen = (nitems * buflen);
  165. size_t nwritten;
  166. fprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
  167. t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
  168. if(!t->out) {
  169. curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
  170. t->idx);
  171. t->out = fopen(t->filename, "wb");
  172. if(!t->out)
  173. return 0;
  174. }
  175. if(!t->resumed &&
  176. t->recv_size < t->pause_at &&
  177. ((t->recv_size + (curl_off_t)blen) >= t->pause_at)) {
  178. fprintf(stderr, "[t-%d] PAUSE\n", t->idx);
  179. t->paused = 1;
  180. return CURL_WRITEFUNC_PAUSE;
  181. }
  182. nwritten = fwrite(buf, nitems, buflen, t->out);
  183. if(nwritten < blen) {
  184. fprintf(stderr, "[t-%d] write failure\n", t->idx);
  185. return 0;
  186. }
  187. t->recv_size += (curl_off_t)nwritten;
  188. if(t->fail_at > 0 && t->recv_size >= t->fail_at) {
  189. fprintf(stderr, "[t-%d] FAIL by write callback at %ld bytes\n",
  190. t->idx, (long)t->recv_size);
  191. return CURL_WRITEFUNC_ERROR;
  192. }
  193. return (size_t)nwritten;
  194. }
  195. static int my_progress_cb(void *userdata,
  196. curl_off_t dltotal, curl_off_t dlnow,
  197. curl_off_t ultotal, curl_off_t ulnow)
  198. {
  199. struct transfer *t = userdata;
  200. (void)ultotal;
  201. (void)ulnow;
  202. (void)dltotal;
  203. if(t->abort_at > 0 && dlnow >= t->abort_at) {
  204. fprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes\n",
  205. t->idx, (long)dlnow);
  206. return 1;
  207. }
  208. return 0;
  209. }
  210. static int setup(CURL *hnd, const char *url, struct transfer *t,
  211. int http_version, struct curl_slist *host,
  212. CURLSH *share, int use_earlydata)
  213. {
  214. curl_easy_setopt(hnd, CURLOPT_SHARE, share);
  215. curl_easy_setopt(hnd, CURLOPT_URL, url);
  216. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
  217. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  218. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  219. curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
  220. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_cb);
  221. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
  222. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
  223. curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_cb);
  224. curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
  225. if(use_earlydata)
  226. curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_EARLYDATA);
  227. if(forbid_reuse)
  228. curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
  229. if(host)
  230. curl_easy_setopt(hnd, CURLOPT_RESOLVE, host);
  231. /* please be verbose */
  232. if(verbose) {
  233. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  234. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, debug_cb);
  235. }
  236. #if (CURLPIPE_MULTIPLEX > 0)
  237. /* wait for pipe connection to confirm */
  238. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  239. #endif
  240. return 0; /* all is good */
  241. }
  242. static void usage(const char *msg)
  243. {
  244. if(msg)
  245. fprintf(stderr, "%s\n", msg);
  246. fprintf(stderr,
  247. "usage: [options] url\n"
  248. " download a url with following options:\n"
  249. " -a abort paused transfer\n"
  250. " -m number max parallel downloads\n"
  251. " -e use TLS early data when possible\n"
  252. " -f forbid connection reuse\n"
  253. " -n number total downloads\n");
  254. fprintf(stderr,
  255. " -A number abort transfer after `number` response bytes\n"
  256. " -F number fail writing response after `number` response bytes\n"
  257. " -P number pause transfer after `number` response bytes\n"
  258. " -r <host>:<port>:<addr> resolve information\n"
  259. " -V http_version (http/1.1, h2, h3) http version to use\n"
  260. );
  261. }
  262. #endif /* !_MSC_VER */
  263. /*
  264. * Download a file over HTTP/2, take care of server push.
  265. */
  266. int main(int argc, char *argv[])
  267. {
  268. #ifndef _MSC_VER
  269. CURLM *multi_handle;
  270. struct CURLMsg *m;
  271. CURLSH *share;
  272. const char *url;
  273. size_t i, n, max_parallel = 1;
  274. size_t active_transfers;
  275. size_t pause_offset = 0;
  276. size_t abort_offset = 0;
  277. size_t fail_offset = 0;
  278. int abort_paused = 0, use_earlydata = 0;
  279. struct transfer *t;
  280. int http_version = CURL_HTTP_VERSION_2_0;
  281. int ch;
  282. struct curl_slist *host = NULL;
  283. const char *resolve = NULL;
  284. while((ch = getopt(argc, argv, "aefhm:n:A:F:P:r:V:")) != -1) {
  285. switch(ch) {
  286. case 'h':
  287. usage(NULL);
  288. return 2;
  289. case 'a':
  290. abort_paused = 1;
  291. break;
  292. case 'e':
  293. use_earlydata = 1;
  294. break;
  295. case 'f':
  296. forbid_reuse = 1;
  297. break;
  298. case 'm':
  299. max_parallel = (size_t)strtol(optarg, NULL, 10);
  300. break;
  301. case 'n':
  302. transfer_count = (size_t)strtol(optarg, NULL, 10);
  303. break;
  304. case 'A':
  305. abort_offset = (size_t)strtol(optarg, NULL, 10);
  306. break;
  307. case 'F':
  308. fail_offset = (size_t)strtol(optarg, NULL, 10);
  309. break;
  310. case 'P':
  311. pause_offset = (size_t)strtol(optarg, NULL, 10);
  312. break;
  313. case 'r':
  314. resolve = optarg;
  315. break;
  316. case 'V': {
  317. if(!strcmp("http/1.1", optarg))
  318. http_version = CURL_HTTP_VERSION_1_1;
  319. else if(!strcmp("h2", optarg))
  320. http_version = CURL_HTTP_VERSION_2_0;
  321. else if(!strcmp("h3", optarg))
  322. http_version = CURL_HTTP_VERSION_3ONLY;
  323. else {
  324. usage("invalid http version");
  325. return 1;
  326. }
  327. break;
  328. }
  329. default:
  330. usage("invalid option");
  331. return 1;
  332. }
  333. }
  334. argc -= optind;
  335. argv += optind;
  336. curl_global_init(CURL_GLOBAL_DEFAULT);
  337. curl_global_trace("ids,time,http/2,http/3");
  338. if(argc != 1) {
  339. usage("not enough arguments");
  340. return 2;
  341. }
  342. url = argv[0];
  343. if(resolve)
  344. host = curl_slist_append(NULL, resolve);
  345. share = curl_share_init();
  346. if(!share) {
  347. fprintf(stderr, "error allocating share\n");
  348. return 1;
  349. }
  350. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  351. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  352. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  353. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  354. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
  355. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
  356. transfers = calloc(transfer_count, sizeof(*transfers));
  357. if(!transfers) {
  358. fprintf(stderr, "error allocating transfer structs\n");
  359. return 1;
  360. }
  361. multi_handle = curl_multi_init();
  362. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  363. active_transfers = 0;
  364. for(i = 0; i < transfer_count; ++i) {
  365. t = &transfers[i];
  366. t->idx = (int)i;
  367. t->abort_at = (curl_off_t)abort_offset;
  368. t->fail_at = (curl_off_t)fail_offset;
  369. t->pause_at = (curl_off_t)pause_offset;
  370. }
  371. n = (max_parallel < transfer_count) ? max_parallel : transfer_count;
  372. for(i = 0; i < n; ++i) {
  373. t = &transfers[i];
  374. t->easy = curl_easy_init();
  375. if(!t->easy ||
  376. setup(t->easy, url, t, http_version, host, share, use_earlydata)) {
  377. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  378. return 1;
  379. }
  380. curl_multi_add_handle(multi_handle, t->easy);
  381. t->started = 1;
  382. ++active_transfers;
  383. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  384. }
  385. do {
  386. int still_running; /* keep number of running handles */
  387. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  388. if(still_running) {
  389. /* wait for activity, timeout or "nothing" */
  390. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  391. }
  392. if(mc)
  393. break;
  394. do {
  395. int msgq = 0;
  396. m = curl_multi_info_read(multi_handle, &msgq);
  397. if(m && (m->msg == CURLMSG_DONE)) {
  398. CURL *e = m->easy_handle;
  399. --active_transfers;
  400. curl_multi_remove_handle(multi_handle, e);
  401. t = get_transfer_for_easy(e);
  402. if(t) {
  403. t->done = 1;
  404. fprintf(stderr, "[t-%d] FINISHED\n", t->idx);
  405. if(use_earlydata) {
  406. curl_off_t sent;
  407. curl_easy_getinfo(e, CURLINFO_EARLYDATA_SENT_T, &sent);
  408. fprintf(stderr, "[t-%d] EarlyData: %ld\n", t->idx, (long)sent);
  409. }
  410. }
  411. else {
  412. curl_easy_cleanup(e);
  413. fprintf(stderr, "unknown FINISHED???\n");
  414. }
  415. }
  416. /* nothing happening, maintenance */
  417. if(abort_paused) {
  418. /* abort paused transfers */
  419. for(i = 0; i < transfer_count; ++i) {
  420. t = &transfers[i];
  421. if(!t->done && t->paused && t->easy) {
  422. curl_multi_remove_handle(multi_handle, t->easy);
  423. t->done = 1;
  424. active_transfers--;
  425. fprintf(stderr, "[t-%d] ABORTED\n", t->idx);
  426. }
  427. }
  428. }
  429. else {
  430. /* resume one paused transfer */
  431. for(i = 0; i < transfer_count; ++i) {
  432. t = &transfers[i];
  433. if(!t->done && t->paused) {
  434. t->resumed = 1;
  435. t->paused = 0;
  436. curl_easy_pause(t->easy, CURLPAUSE_CONT);
  437. fprintf(stderr, "[t-%d] RESUMED\n", t->idx);
  438. break;
  439. }
  440. }
  441. }
  442. while(active_transfers < max_parallel) {
  443. for(i = 0; i < transfer_count; ++i) {
  444. t = &transfers[i];
  445. if(!t->started) {
  446. t->easy = curl_easy_init();
  447. if(!t->easy ||
  448. setup(t->easy, url, t, http_version, host, share,
  449. use_earlydata)) {
  450. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  451. return 1;
  452. }
  453. curl_multi_add_handle(multi_handle, t->easy);
  454. t->started = 1;
  455. ++active_transfers;
  456. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  457. break;
  458. }
  459. }
  460. /* all started */
  461. if(i == transfer_count)
  462. break;
  463. }
  464. } while(m);
  465. } while(active_transfers); /* as long as we have transfers going */
  466. curl_multi_cleanup(multi_handle);
  467. for(i = 0; i < transfer_count; ++i) {
  468. t = &transfers[i];
  469. if(t->out) {
  470. fclose(t->out);
  471. t->out = NULL;
  472. }
  473. if(t->easy) {
  474. curl_easy_cleanup(t->easy);
  475. t->easy = NULL;
  476. }
  477. }
  478. free(transfers);
  479. curl_share_cleanup(share);
  480. return 0;
  481. #else
  482. (void)argc;
  483. (void)argv;
  484. fprintf(stderr, "Not supported with this compiler.\n");
  485. return 1;
  486. #endif /* !_MSC_VER */
  487. }