h2-download.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <curl/mprintf.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. /* somewhat unix-specific */
  35. #include <sys/time.h>
  36. #include <unistd.h>
  37. #ifndef CURLPIPE_MULTIPLEX
  38. #error "too old libcurl, cannot do HTTP/2 server push!"
  39. #endif
  40. static int verbose = 1;
  41. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  42. {
  43. /*
  44. * This is the trace look that is similar to what libcurl makes on its
  45. * own.
  46. */
  47. static const char * const s_infotype[] = {
  48. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  49. };
  50. if(idsbuf && *idsbuf)
  51. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  52. else
  53. fputs(s_infotype[type], log);
  54. }
  55. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  56. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  57. CURL_FORMAT_CURL_OFF_T "] "
  58. /*
  59. ** callback for CURLOPT_DEBUGFUNCTION
  60. */
  61. static int debug_cb(CURL *handle, curl_infotype type,
  62. char *data, size_t size,
  63. void *userdata)
  64. {
  65. FILE *output = stderr;
  66. static int newl = 0;
  67. static int traced_data = 0;
  68. char idsbuf[60];
  69. curl_off_t xfer_id, conn_id;
  70. (void)handle; /* not used */
  71. (void)userdata;
  72. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  73. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  74. conn_id >= 0) {
  75. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
  76. xfer_id, conn_id);
  77. }
  78. else {
  79. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  80. }
  81. }
  82. else
  83. idsbuf[0] = 0;
  84. switch(type) {
  85. case CURLINFO_HEADER_OUT:
  86. if(size > 0) {
  87. size_t st = 0;
  88. size_t i;
  89. for(i = 0; i < size - 1; i++) {
  90. if(data[i] == '\n') { /* LF */
  91. if(!newl) {
  92. log_line_start(output, idsbuf, type);
  93. }
  94. (void)fwrite(data + st, i - st + 1, 1, output);
  95. st = i + 1;
  96. newl = 0;
  97. }
  98. }
  99. if(!newl)
  100. log_line_start(output, idsbuf, type);
  101. (void)fwrite(data + st, i - st + 1, 1, output);
  102. }
  103. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  104. traced_data = 0;
  105. break;
  106. case CURLINFO_TEXT:
  107. case CURLINFO_HEADER_IN:
  108. if(!newl)
  109. log_line_start(output, idsbuf, type);
  110. (void)fwrite(data, size, 1, output);
  111. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  112. traced_data = 0;
  113. break;
  114. case CURLINFO_DATA_OUT:
  115. case CURLINFO_DATA_IN:
  116. case CURLINFO_SSL_DATA_IN:
  117. case CURLINFO_SSL_DATA_OUT:
  118. if(!traced_data) {
  119. if(!newl)
  120. log_line_start(output, idsbuf, type);
  121. fprintf(output, "[%ld bytes data]\n", (long)size);
  122. newl = 0;
  123. traced_data = 1;
  124. }
  125. break;
  126. default: /* nada */
  127. newl = 0;
  128. traced_data = 1;
  129. break;
  130. }
  131. return 0;
  132. }
  133. struct transfer {
  134. int idx;
  135. CURL *easy;
  136. char filename[128];
  137. FILE *out;
  138. curl_off_t recv_size;
  139. curl_off_t fail_at;
  140. curl_off_t pause_at;
  141. curl_off_t abort_at;
  142. int started;
  143. int paused;
  144. int resumed;
  145. int done;
  146. };
  147. static size_t transfer_count = 1;
  148. static struct transfer *transfers;
  149. static int forbid_reuse = 0;
  150. static struct transfer *get_transfer_for_easy(CURL *easy)
  151. {
  152. size_t i;
  153. for(i = 0; i < transfer_count; ++i) {
  154. if(easy == transfers[i].easy)
  155. return &transfers[i];
  156. }
  157. return NULL;
  158. }
  159. static size_t my_write_cb(char *buf, size_t nitems, size_t buflen,
  160. void *userdata)
  161. {
  162. struct transfer *t = userdata;
  163. size_t blen = (nitems * buflen);
  164. size_t nwritten;
  165. fprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
  166. t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
  167. if(!t->out) {
  168. curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
  169. t->idx);
  170. t->out = fopen(t->filename, "wb");
  171. if(!t->out)
  172. return 0;
  173. }
  174. if(!t->resumed &&
  175. t->recv_size < t->pause_at &&
  176. ((t->recv_size + (curl_off_t)blen) >= t->pause_at)) {
  177. fprintf(stderr, "[t-%d] PAUSE\n", t->idx);
  178. t->paused = 1;
  179. return CURL_WRITEFUNC_PAUSE;
  180. }
  181. nwritten = fwrite(buf, nitems, buflen, t->out);
  182. if(nwritten < blen) {
  183. fprintf(stderr, "[t-%d] write failure\n", t->idx);
  184. return 0;
  185. }
  186. t->recv_size += (curl_off_t)nwritten;
  187. if(t->fail_at > 0 && t->recv_size >= t->fail_at) {
  188. fprintf(stderr, "[t-%d] FAIL by write callback at %ld bytes\n",
  189. t->idx, (long)t->recv_size);
  190. return CURL_WRITEFUNC_ERROR;
  191. }
  192. return (size_t)nwritten;
  193. }
  194. static int my_progress_cb(void *userdata,
  195. curl_off_t dltotal, curl_off_t dlnow,
  196. curl_off_t ultotal, curl_off_t ulnow)
  197. {
  198. struct transfer *t = userdata;
  199. (void)ultotal;
  200. (void)ulnow;
  201. (void)dltotal;
  202. if(t->abort_at > 0 && dlnow >= t->abort_at) {
  203. fprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes\n",
  204. t->idx, (long)dlnow);
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. static int setup(CURL *hnd, const char *url, struct transfer *t,
  210. int http_version)
  211. {
  212. curl_easy_setopt(hnd, CURLOPT_URL, url);
  213. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
  214. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  215. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  216. curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
  217. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_cb);
  218. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
  219. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
  220. curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_cb);
  221. curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
  222. if(forbid_reuse)
  223. curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
  224. /* please be verbose */
  225. if(verbose) {
  226. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  227. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, debug_cb);
  228. }
  229. #if (CURLPIPE_MULTIPLEX > 0)
  230. /* wait for pipe connection to confirm */
  231. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  232. #endif
  233. return 0; /* all is good */
  234. }
  235. static void usage(const char *msg)
  236. {
  237. if(msg)
  238. fprintf(stderr, "%s\n", msg);
  239. fprintf(stderr,
  240. "usage: [options] url\n"
  241. " download a url with following options:\n"
  242. " -a abort paused transfer\n"
  243. " -m number max parallel downloads\n"
  244. " -n number total downloads\n"
  245. " -A number abort transfer after `number` response bytes\n"
  246. " -F number fail writing response after `number` response bytes\n"
  247. " -P number pause transfer after `number` response bytes\n"
  248. " -V http_version (http/1.1, h2, h3) http version to use\n"
  249. );
  250. }
  251. /*
  252. * Download a file over HTTP/2, take care of server push.
  253. */
  254. int main(int argc, char *argv[])
  255. {
  256. CURLM *multi_handle;
  257. struct CURLMsg *m;
  258. const char *url;
  259. size_t i, n, max_parallel = 1;
  260. size_t active_transfers;
  261. size_t pause_offset = 0;
  262. size_t abort_offset = 0;
  263. size_t fail_offset = 0;
  264. int abort_paused = 0;
  265. struct transfer *t;
  266. int http_version = CURL_HTTP_VERSION_2_0;
  267. int ch;
  268. while((ch = getopt(argc, argv, "afhm:n:A:F:P:V:")) != -1) {
  269. switch(ch) {
  270. case 'h':
  271. usage(NULL);
  272. return 2;
  273. case 'a':
  274. abort_paused = 1;
  275. break;
  276. case 'f':
  277. forbid_reuse = 1;
  278. break;
  279. case 'm':
  280. max_parallel = (size_t)strtol(optarg, NULL, 10);
  281. break;
  282. case 'n':
  283. transfer_count = (size_t)strtol(optarg, NULL, 10);
  284. break;
  285. case 'A':
  286. abort_offset = (size_t)strtol(optarg, NULL, 10);
  287. break;
  288. case 'F':
  289. fail_offset = (size_t)strtol(optarg, NULL, 10);
  290. break;
  291. case 'P':
  292. pause_offset = (size_t)strtol(optarg, NULL, 10);
  293. break;
  294. case 'V': {
  295. if(!strcmp("http/1.1", optarg))
  296. http_version = CURL_HTTP_VERSION_1_1;
  297. else if(!strcmp("h2", optarg))
  298. http_version = CURL_HTTP_VERSION_2_0;
  299. else if(!strcmp("h3", optarg))
  300. http_version = CURL_HTTP_VERSION_3ONLY;
  301. else {
  302. usage("invalid http version");
  303. return 1;
  304. }
  305. break;
  306. }
  307. default:
  308. usage("invalid option");
  309. return 1;
  310. }
  311. }
  312. argc -= optind;
  313. argv += optind;
  314. curl_global_init(CURL_GLOBAL_DEFAULT);
  315. curl_global_trace("ids,time,http/2,http/3");
  316. if(argc != 1) {
  317. usage("not enough arguments");
  318. return 2;
  319. }
  320. url = argv[0];
  321. transfers = calloc(transfer_count, sizeof(*transfers));
  322. if(!transfers) {
  323. fprintf(stderr, "error allocating transfer structs\n");
  324. return 1;
  325. }
  326. multi_handle = curl_multi_init();
  327. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  328. active_transfers = 0;
  329. for(i = 0; i < transfer_count; ++i) {
  330. t = &transfers[i];
  331. t->idx = (int)i;
  332. t->abort_at = (curl_off_t)abort_offset;
  333. t->fail_at = (curl_off_t)fail_offset;
  334. t->pause_at = (curl_off_t)pause_offset;
  335. }
  336. n = (max_parallel < transfer_count)? max_parallel : transfer_count;
  337. for(i = 0; i < n; ++i) {
  338. t = &transfers[i];
  339. t->easy = curl_easy_init();
  340. if(!t->easy || setup(t->easy, url, t, http_version)) {
  341. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  342. return 1;
  343. }
  344. curl_multi_add_handle(multi_handle, t->easy);
  345. t->started = 1;
  346. ++active_transfers;
  347. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  348. }
  349. do {
  350. int still_running; /* keep number of running handles */
  351. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  352. if(still_running) {
  353. /* wait for activity, timeout or "nothing" */
  354. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  355. }
  356. if(mc)
  357. break;
  358. do {
  359. int msgq = 0;
  360. m = curl_multi_info_read(multi_handle, &msgq);
  361. if(m && (m->msg == CURLMSG_DONE)) {
  362. CURL *e = m->easy_handle;
  363. --active_transfers;
  364. curl_multi_remove_handle(multi_handle, e);
  365. t = get_transfer_for_easy(e);
  366. if(t) {
  367. t->done = 1;
  368. fprintf(stderr, "[t-%d] FINISHED\n", t->idx);
  369. }
  370. else {
  371. curl_easy_cleanup(e);
  372. fprintf(stderr, "unknown FINISHED???\n");
  373. }
  374. }
  375. /* nothing happening, maintenance */
  376. if(abort_paused) {
  377. /* abort paused transfers */
  378. for(i = 0; i < transfer_count; ++i) {
  379. t = &transfers[i];
  380. if(!t->done && t->paused && t->easy) {
  381. curl_multi_remove_handle(multi_handle, t->easy);
  382. t->done = 1;
  383. active_transfers--;
  384. fprintf(stderr, "[t-%d] ABORTED\n", t->idx);
  385. }
  386. }
  387. }
  388. else {
  389. /* resume one paused transfer */
  390. for(i = 0; i < transfer_count; ++i) {
  391. t = &transfers[i];
  392. if(!t->done && t->paused) {
  393. t->resumed = 1;
  394. t->paused = 0;
  395. curl_easy_pause(t->easy, CURLPAUSE_CONT);
  396. fprintf(stderr, "[t-%d] RESUMED\n", t->idx);
  397. break;
  398. }
  399. }
  400. }
  401. while(active_transfers < max_parallel) {
  402. for(i = 0; i < transfer_count; ++i) {
  403. t = &transfers[i];
  404. if(!t->started) {
  405. t->easy = curl_easy_init();
  406. if(!t->easy || setup(t->easy, url, t, http_version)) {
  407. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  408. return 1;
  409. }
  410. curl_multi_add_handle(multi_handle, t->easy);
  411. t->started = 1;
  412. ++active_transfers;
  413. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  414. break;
  415. }
  416. }
  417. /* all started */
  418. if(i == transfer_count)
  419. break;
  420. }
  421. } while(m);
  422. } while(active_transfers); /* as long as we have transfers going */
  423. for(i = 0; i < transfer_count; ++i) {
  424. t = &transfers[i];
  425. if(t->out) {
  426. fclose(t->out);
  427. t->out = NULL;
  428. }
  429. if(t->easy) {
  430. curl_easy_cleanup(t->easy);
  431. t->easy = NULL;
  432. }
  433. }
  434. free(transfers);
  435. curl_multi_cleanup(multi_handle);
  436. return 0;
  437. }