2
0

h2-pausing.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 download pausing
  26. * </DESC>
  27. */
  28. /* This is based on the PoC client of issue #11982
  29. */
  30. #include <curl/curl.h>
  31. #include <assert.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #ifndef _MSC_VER
  36. /* somewhat Unix-specific */
  37. #include <unistd.h> /* getopt() */
  38. #endif
  39. #ifndef _MSC_VER
  40. #define HANDLECOUNT 2
  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, xfer_id,
  76. 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. static int err(void)
  134. {
  135. fprintf(stderr, "something unexpected went wrong - bailing out!\n");
  136. exit(2);
  137. }
  138. static void usage(const char *msg)
  139. {
  140. if(msg)
  141. fprintf(stderr, "%s\n", msg);
  142. fprintf(stderr,
  143. "usage: [options] url\n"
  144. " pause downloads with following options:\n"
  145. " -V http_version (http/1.1, h2, h3) http version to use\n"
  146. );
  147. }
  148. struct handle
  149. {
  150. int idx;
  151. int paused;
  152. int resumed;
  153. int errored;
  154. int fail_write;
  155. CURL *h;
  156. };
  157. static size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
  158. {
  159. size_t realsize = size * nmemb;
  160. struct handle *handle = (struct handle *) clientp;
  161. curl_off_t totalsize;
  162. (void)data;
  163. if(curl_easy_getinfo(handle->h, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
  164. &totalsize) == CURLE_OK)
  165. fprintf(stderr, "INFO: [%d] write, Content-Length %"CURL_FORMAT_CURL_OFF_T
  166. "\n", handle->idx, totalsize);
  167. if(!handle->resumed) {
  168. ++handle->paused;
  169. fprintf(stderr, "INFO: [%d] write, PAUSING %d time on %lu bytes\n",
  170. handle->idx, handle->paused, (long)realsize);
  171. assert(handle->paused == 1);
  172. return CURL_WRITEFUNC_PAUSE;
  173. }
  174. if(handle->fail_write) {
  175. ++handle->errored;
  176. fprintf(stderr, "INFO: [%d] FAIL write of %lu bytes, %d time\n",
  177. handle->idx, (long)realsize, handle->errored);
  178. return CURL_WRITEFUNC_ERROR;
  179. }
  180. fprintf(stderr, "INFO: [%d] write, accepting %lu bytes\n",
  181. handle->idx, (long)realsize);
  182. return realsize;
  183. }
  184. #endif /* !_MSC_VER */
  185. int main(int argc, char *argv[])
  186. {
  187. #ifndef _MSC_VER
  188. struct handle handles[HANDLECOUNT];
  189. CURLM *multi_handle;
  190. int i, still_running = 1, msgs_left, numfds;
  191. CURLMsg *msg;
  192. int rounds = 0;
  193. int rc = 0;
  194. CURLU *cu;
  195. struct curl_slist *resolve = NULL;
  196. char resolve_buf[1024];
  197. char *url, *host = NULL, *port = NULL;
  198. int all_paused = 0;
  199. int resume_round = -1;
  200. int http_version = CURL_HTTP_VERSION_2_0;
  201. int ch;
  202. while((ch = getopt(argc, argv, "hV:")) != -1) {
  203. switch(ch) {
  204. case 'h':
  205. usage(NULL);
  206. return 2;
  207. case 'V': {
  208. if(!strcmp("http/1.1", optarg))
  209. http_version = CURL_HTTP_VERSION_1_1;
  210. else if(!strcmp("h2", optarg))
  211. http_version = CURL_HTTP_VERSION_2_0;
  212. else if(!strcmp("h3", optarg))
  213. http_version = CURL_HTTP_VERSION_3ONLY;
  214. else {
  215. usage("invalid http version");
  216. return 1;
  217. }
  218. break;
  219. }
  220. default:
  221. usage("invalid option");
  222. return 1;
  223. }
  224. }
  225. argc -= optind;
  226. argv += optind;
  227. if(argc != 1) {
  228. fprintf(stderr, "ERROR: need URL as argument\n");
  229. return 2;
  230. }
  231. url = argv[0];
  232. curl_global_init(CURL_GLOBAL_DEFAULT);
  233. curl_global_trace("ids,time,http/2,http/3");
  234. cu = curl_url();
  235. if(!cu) {
  236. fprintf(stderr, "out of memory\n");
  237. exit(1);
  238. }
  239. if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
  240. fprintf(stderr, "not a URL: '%s'\n", url);
  241. exit(1);
  242. }
  243. if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
  244. fprintf(stderr, "could not get host of '%s'\n", url);
  245. exit(1);
  246. }
  247. if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
  248. fprintf(stderr, "could not get port of '%s'\n", url);
  249. exit(1);
  250. }
  251. memset(&resolve, 0, sizeof(resolve));
  252. curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
  253. host, port);
  254. resolve = curl_slist_append(resolve, resolve_buf);
  255. for(i = 0; i < HANDLECOUNT; i++) {
  256. handles[i].idx = i;
  257. handles[i].paused = 0;
  258. handles[i].resumed = 0;
  259. handles[i].errored = 0;
  260. handles[i].fail_write = 1;
  261. handles[i].h = curl_easy_init();
  262. if(!handles[i].h ||
  263. curl_easy_setopt(handles[i].h, CURLOPT_WRITEFUNCTION, cb) != CURLE_OK ||
  264. curl_easy_setopt(handles[i].h, CURLOPT_WRITEDATA, &handles[i])
  265. != CURLE_OK ||
  266. curl_easy_setopt(handles[i].h, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK ||
  267. curl_easy_setopt(handles[i].h, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
  268. curl_easy_setopt(handles[i].h, CURLOPT_DEBUGFUNCTION, debug_cb)
  269. != CURLE_OK ||
  270. curl_easy_setopt(handles[i].h, CURLOPT_SSL_VERIFYPEER, 0L) != CURLE_OK ||
  271. curl_easy_setopt(handles[i].h, CURLOPT_RESOLVE, resolve) != CURLE_OK ||
  272. curl_easy_setopt(handles[i].h, CURLOPT_PIPEWAIT, 1L) ||
  273. curl_easy_setopt(handles[i].h, CURLOPT_URL, url) != CURLE_OK) {
  274. err();
  275. }
  276. curl_easy_setopt(handles[i].h, CURLOPT_HTTP_VERSION, (long)http_version);
  277. }
  278. multi_handle = curl_multi_init();
  279. if(!multi_handle)
  280. err();
  281. for(i = 0; i < HANDLECOUNT; i++) {
  282. if(curl_multi_add_handle(multi_handle, handles[i].h) != CURLM_OK)
  283. err();
  284. }
  285. for(rounds = 0;; rounds++) {
  286. fprintf(stderr, "INFO: multi_perform round %d\n", rounds);
  287. if(curl_multi_perform(multi_handle, &still_running) != CURLM_OK)
  288. err();
  289. if(!still_running) {
  290. int as_expected = 1;
  291. fprintf(stderr, "INFO: no more handles running\n");
  292. for(i = 0; i < HANDLECOUNT; i++) {
  293. if(!handles[i].paused) {
  294. fprintf(stderr, "ERROR: [%d] NOT PAUSED\n", i);
  295. as_expected = 0;
  296. }
  297. else if(handles[i].paused != 1) {
  298. fprintf(stderr, "ERROR: [%d] PAUSED %d times!\n",
  299. i, handles[i].paused);
  300. as_expected = 0;
  301. }
  302. else if(!handles[i].resumed) {
  303. fprintf(stderr, "ERROR: [%d] NOT resumed!\n", i);
  304. as_expected = 0;
  305. }
  306. else if(handles[i].errored != 1) {
  307. fprintf(stderr, "ERROR: [%d] NOT errored once, %d instead!\n",
  308. i, handles[i].errored);
  309. as_expected = 0;
  310. }
  311. }
  312. if(!as_expected) {
  313. fprintf(stderr, "ERROR: handles not in expected state "
  314. "after %d rounds\n", rounds);
  315. rc = 1;
  316. }
  317. break;
  318. }
  319. if(curl_multi_poll(multi_handle, NULL, 0, 100, &numfds) != CURLM_OK)
  320. err();
  321. /* !checksrc! disable EQUALSNULL 1 */
  322. while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
  323. if(msg->msg == CURLMSG_DONE) {
  324. for(i = 0; i < HANDLECOUNT; i++) {
  325. if(msg->easy_handle == handles[i].h) {
  326. if(handles[i].paused != 1 || !handles[i].resumed) {
  327. fprintf(stderr, "ERROR: [%d] done, pauses=%d, resumed=%d, "
  328. "result %d - wtf?\n", i, handles[i].paused,
  329. handles[i].resumed, msg->data.result);
  330. rc = 1;
  331. goto out;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. /* Successfully paused? */
  338. if(!all_paused) {
  339. for(i = 0; i < HANDLECOUNT; i++) {
  340. if(!handles[i].paused) {
  341. break;
  342. }
  343. }
  344. all_paused = (i == HANDLECOUNT);
  345. if(all_paused) {
  346. fprintf(stderr, "INFO: all transfers paused\n");
  347. /* give transfer some rounds to mess things up */
  348. resume_round = rounds + 2;
  349. }
  350. }
  351. if(resume_round > 0 && rounds == resume_round) {
  352. /* time to resume */
  353. for(i = 0; i < HANDLECOUNT; i++) {
  354. fprintf(stderr, "INFO: [%d] resumed\n", i);
  355. handles[i].resumed = 1;
  356. curl_easy_pause(handles[i].h, CURLPAUSE_CONT);
  357. }
  358. }
  359. }
  360. out:
  361. for(i = 0; i < HANDLECOUNT; i++) {
  362. curl_multi_remove_handle(multi_handle, handles[i].h);
  363. curl_easy_cleanup(handles[i].h);
  364. }
  365. curl_slist_free_all(resolve);
  366. curl_free(host);
  367. curl_free(port);
  368. curl_url_cleanup(cu);
  369. curl_multi_cleanup(multi_handle);
  370. curl_global_cleanup();
  371. return rc;
  372. #else
  373. (void)argc;
  374. (void)argv;
  375. fprintf(stderr, "Not supported with this compiler.\n");
  376. return 1;
  377. #endif /* !_MSC_VER */
  378. }