hx-upload.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 upload tests and tweaks
  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"
  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. const char *method;
  138. char filename[128];
  139. FILE *out;
  140. curl_off_t send_total;
  141. curl_off_t recv_size;
  142. curl_off_t send_size;
  143. curl_off_t fail_at;
  144. curl_off_t pause_at;
  145. curl_off_t abort_at;
  146. int started;
  147. int paused;
  148. int resumed;
  149. int done;
  150. };
  151. static size_t transfer_count = 1;
  152. static struct transfer *transfers;
  153. static int forbid_reuse = 0;
  154. static struct transfer *get_transfer_for_easy(CURL *easy)
  155. {
  156. size_t i;
  157. for(i = 0; i < transfer_count; ++i) {
  158. if(easy == transfers[i].easy)
  159. return &transfers[i];
  160. }
  161. return NULL;
  162. }
  163. static size_t my_write_cb(char *buf, size_t nitems, size_t buflen,
  164. void *userdata)
  165. {
  166. struct transfer *t = userdata;
  167. size_t blen = (nitems * buflen);
  168. size_t nwritten;
  169. fprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
  170. t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
  171. if(!t->out) {
  172. curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
  173. t->idx);
  174. t->out = fopen(t->filename, "wb");
  175. if(!t->out)
  176. return 0;
  177. }
  178. nwritten = fwrite(buf, nitems, buflen, t->out);
  179. if(nwritten < blen) {
  180. fprintf(stderr, "[t-%d] write failure\n", t->idx);
  181. return 0;
  182. }
  183. t->recv_size += (curl_off_t)nwritten;
  184. return (size_t)nwritten;
  185. }
  186. static size_t my_read_cb(char *buf, size_t nitems, size_t buflen,
  187. void *userdata)
  188. {
  189. struct transfer *t = userdata;
  190. size_t blen = (nitems * buflen);
  191. size_t nread;
  192. if(t->send_total <= t->send_size)
  193. nread = 0;
  194. else if((t->send_total - t->send_size) < (curl_off_t)blen)
  195. nread = (size_t)(t->send_total - t->send_size);
  196. else
  197. nread = blen;
  198. fprintf(stderr, "[t-%d] SEND %ld bytes, total=%ld, pause_at=%ld\n",
  199. t->idx, (long)nread, (long)t->send_total, (long)t->pause_at);
  200. if(!t->resumed &&
  201. t->send_size < t->pause_at &&
  202. ((t->send_size + (curl_off_t)blen) >= t->pause_at)) {
  203. fprintf(stderr, "[t-%d] PAUSE\n", t->idx);
  204. t->paused = 1;
  205. return CURL_READFUNC_PAUSE;
  206. }
  207. memset(buf, 'x', nread);
  208. t->send_size += (curl_off_t)nread;
  209. if(t->fail_at > 0 && t->send_size >= t->fail_at) {
  210. fprintf(stderr, "[t-%d] ABORT by read callback at %ld bytes\n",
  211. t->idx, (long)t->send_size);
  212. return CURL_READFUNC_ABORT;
  213. }
  214. return (size_t)nread;
  215. }
  216. static int my_progress_cb(void *userdata,
  217. curl_off_t dltotal, curl_off_t dlnow,
  218. curl_off_t ultotal, curl_off_t ulnow)
  219. {
  220. struct transfer *t = userdata;
  221. (void)ultotal;
  222. (void)dlnow;
  223. (void)dltotal;
  224. if(t->abort_at > 0 && ulnow >= t->abort_at) {
  225. fprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes sent\n",
  226. t->idx, (long)ulnow);
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. static int setup(CURL *hnd, const char *url, struct transfer *t,
  232. int http_version, struct curl_slist *host,
  233. CURLSH *share, int use_earlydata, int announce_length)
  234. {
  235. curl_easy_setopt(hnd, CURLOPT_SHARE, share);
  236. curl_easy_setopt(hnd, CURLOPT_URL, url);
  237. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
  238. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  239. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  240. curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
  241. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_cb);
  242. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
  243. if(use_earlydata)
  244. curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_EARLYDATA);
  245. if(!t->method || !strcmp("PUT", t->method))
  246. curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
  247. else if(!strcmp("POST", t->method))
  248. curl_easy_setopt(hnd, CURLOPT_POST, 1L);
  249. else {
  250. fprintf(stderr, "unsupported method '%s'\n", t->method);
  251. return 1;
  252. }
  253. curl_easy_setopt(hnd, CURLOPT_READFUNCTION, my_read_cb);
  254. curl_easy_setopt(hnd, CURLOPT_READDATA, t);
  255. if(announce_length)
  256. curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, t->send_total);
  257. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
  258. curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_cb);
  259. curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
  260. if(forbid_reuse)
  261. curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
  262. if(host)
  263. curl_easy_setopt(hnd, CURLOPT_RESOLVE, host);
  264. /* please be verbose */
  265. if(verbose) {
  266. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  267. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, debug_cb);
  268. }
  269. #if (CURLPIPE_MULTIPLEX > 0)
  270. /* wait for pipe connection to confirm */
  271. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  272. #endif
  273. return 0; /* all is good */
  274. }
  275. static void usage(const char *msg)
  276. {
  277. if(msg)
  278. fprintf(stderr, "%s\n", msg);
  279. fprintf(stderr,
  280. "usage: [options] url\n"
  281. " upload to a url with following options:\n"
  282. " -a abort paused transfer\n"
  283. " -e use TLS earlydata\n"
  284. " -m number max parallel uploads\n"
  285. " -n number total uploads\n"
  286. " -A number abort transfer after `number` request body bytes\n"
  287. " -F number fail reading request body after `number` of bytes\n"
  288. " -P number pause transfer after `number` request body bytes\n"
  289. " -r <host>:<port>:<addr> resolve information\n"
  290. " -S number size to upload\n"
  291. " -V http_version (http/1.1, h2, h3) http version to use\n"
  292. );
  293. }
  294. #endif /* !_MSC_VER */
  295. /*
  296. * Download a file over HTTP/2, take care of server push.
  297. */
  298. int main(int argc, char *argv[])
  299. {
  300. #ifndef _MSC_VER
  301. CURLM *multi_handle;
  302. struct CURLMsg *m;
  303. CURLSH *share;
  304. const char *url;
  305. const char *method = "PUT";
  306. size_t i, n, max_parallel = 1;
  307. size_t active_transfers;
  308. size_t pause_offset = 0;
  309. size_t abort_offset = 0;
  310. size_t fail_offset = 0;
  311. size_t send_total = (128 * 1024);
  312. int abort_paused = 0;
  313. int reuse_easy = 0;
  314. int use_earlydata = 0;
  315. int announce_length = 0;
  316. struct transfer *t;
  317. int http_version = CURL_HTTP_VERSION_2_0;
  318. struct curl_slist *host = NULL;
  319. const char *resolve = NULL;
  320. int ch;
  321. while((ch = getopt(argc, argv, "aefhlm:n:A:F:M:P:r:RS:V:")) != -1) {
  322. switch(ch) {
  323. case 'h':
  324. usage(NULL);
  325. return 2;
  326. case 'a':
  327. abort_paused = 1;
  328. break;
  329. case 'e':
  330. use_earlydata = 1;
  331. break;
  332. case 'f':
  333. forbid_reuse = 1;
  334. break;
  335. case 'l':
  336. announce_length = 1;
  337. break;
  338. case 'm':
  339. max_parallel = (size_t)strtol(optarg, NULL, 10);
  340. break;
  341. case 'n':
  342. transfer_count = (size_t)strtol(optarg, NULL, 10);
  343. break;
  344. case 'A':
  345. abort_offset = (size_t)strtol(optarg, NULL, 10);
  346. break;
  347. case 'F':
  348. fail_offset = (size_t)strtol(optarg, NULL, 10);
  349. break;
  350. case 'M':
  351. method = optarg;
  352. break;
  353. case 'P':
  354. pause_offset = (size_t)strtol(optarg, NULL, 10);
  355. break;
  356. case 'r':
  357. resolve = optarg;
  358. break;
  359. case 'R':
  360. reuse_easy = 1;
  361. break;
  362. case 'S':
  363. send_total = (size_t)strtol(optarg, NULL, 10);
  364. break;
  365. case 'V': {
  366. if(!strcmp("http/1.1", optarg))
  367. http_version = CURL_HTTP_VERSION_1_1;
  368. else if(!strcmp("h2", optarg))
  369. http_version = CURL_HTTP_VERSION_2_0;
  370. else if(!strcmp("h3", optarg))
  371. http_version = CURL_HTTP_VERSION_3ONLY;
  372. else {
  373. usage("invalid http version");
  374. return 1;
  375. }
  376. break;
  377. }
  378. default:
  379. usage("invalid option");
  380. return 1;
  381. }
  382. }
  383. argc -= optind;
  384. argv += optind;
  385. if(max_parallel > 1 && reuse_easy) {
  386. usage("cannot mix -R and -P");
  387. return 2;
  388. }
  389. curl_global_init(CURL_GLOBAL_DEFAULT);
  390. curl_global_trace("ids,time,http/2,http/3");
  391. if(argc != 1) {
  392. usage("not enough arguments");
  393. return 2;
  394. }
  395. url = argv[0];
  396. if(resolve)
  397. host = curl_slist_append(NULL, resolve);
  398. share = curl_share_init();
  399. if(!share) {
  400. fprintf(stderr, "error allocating share\n");
  401. return 1;
  402. }
  403. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  404. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  405. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  406. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  407. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
  408. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
  409. transfers = calloc(transfer_count, sizeof(*transfers));
  410. if(!transfers) {
  411. fprintf(stderr, "error allocating transfer structs\n");
  412. return 1;
  413. }
  414. active_transfers = 0;
  415. for(i = 0; i < transfer_count; ++i) {
  416. t = &transfers[i];
  417. t->idx = (int)i;
  418. t->method = method;
  419. t->send_total = (curl_off_t)send_total;
  420. t->abort_at = (curl_off_t)abort_offset;
  421. t->fail_at = (curl_off_t)fail_offset;
  422. t->pause_at = (curl_off_t)pause_offset;
  423. }
  424. if(reuse_easy) {
  425. CURL *easy = curl_easy_init();
  426. CURLcode rc = CURLE_OK;
  427. if(!easy) {
  428. fprintf(stderr, "failed to init easy handle\n");
  429. return 1;
  430. }
  431. for(i = 0; i < transfer_count; ++i) {
  432. t = &transfers[i];
  433. t->easy = easy;
  434. if(setup(t->easy, url, t, http_version, host, share, use_earlydata,
  435. announce_length)) {
  436. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  437. return 1;
  438. }
  439. fprintf(stderr, "[t-%d] STARTING\n", t->idx);
  440. rc = curl_easy_perform(easy);
  441. fprintf(stderr, "[t-%d] DONE -> %d\n", t->idx, rc);
  442. t->easy = NULL;
  443. curl_easy_reset(easy);
  444. }
  445. curl_easy_cleanup(easy);
  446. }
  447. else {
  448. multi_handle = curl_multi_init();
  449. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  450. n = (max_parallel < transfer_count) ? max_parallel : transfer_count;
  451. for(i = 0; i < n; ++i) {
  452. t = &transfers[i];
  453. t->easy = curl_easy_init();
  454. if(!t->easy || setup(t->easy, url, t, http_version, host, share,
  455. use_earlydata, announce_length)) {
  456. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  457. return 1;
  458. }
  459. curl_multi_add_handle(multi_handle, t->easy);
  460. t->started = 1;
  461. ++active_transfers;
  462. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  463. }
  464. do {
  465. int still_running; /* keep number of running handles */
  466. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  467. if(still_running) {
  468. /* wait for activity, timeout or "nothing" */
  469. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  470. }
  471. if(mc)
  472. break;
  473. do {
  474. int msgq = 0;
  475. m = curl_multi_info_read(multi_handle, &msgq);
  476. if(m && (m->msg == CURLMSG_DONE)) {
  477. CURL *e = m->easy_handle;
  478. --active_transfers;
  479. curl_multi_remove_handle(multi_handle, e);
  480. t = get_transfer_for_easy(e);
  481. if(t) {
  482. t->done = 1;
  483. fprintf(stderr, "[t-%d] FINISHED\n", t->idx);
  484. if(use_earlydata) {
  485. curl_off_t sent;
  486. curl_easy_getinfo(e, CURLINFO_EARLYDATA_SENT_T, &sent);
  487. fprintf(stderr, "[t-%d] EarlyData: %ld\n", t->idx, (long)sent);
  488. }
  489. }
  490. else {
  491. curl_easy_cleanup(e);
  492. fprintf(stderr, "unknown FINISHED???\n");
  493. }
  494. }
  495. /* nothing happening, maintenance */
  496. if(abort_paused) {
  497. /* abort paused transfers */
  498. for(i = 0; i < transfer_count; ++i) {
  499. t = &transfers[i];
  500. if(!t->done && t->paused && t->easy) {
  501. curl_multi_remove_handle(multi_handle, t->easy);
  502. t->done = 1;
  503. active_transfers--;
  504. fprintf(stderr, "[t-%d] ABORTED\n", t->idx);
  505. }
  506. }
  507. }
  508. else {
  509. /* resume one paused transfer */
  510. for(i = 0; i < transfer_count; ++i) {
  511. t = &transfers[i];
  512. if(!t->done && t->paused) {
  513. t->resumed = 1;
  514. t->paused = 0;
  515. curl_easy_pause(t->easy, CURLPAUSE_CONT);
  516. fprintf(stderr, "[t-%d] RESUMED\n", t->idx);
  517. break;
  518. }
  519. }
  520. }
  521. while(active_transfers < max_parallel) {
  522. for(i = 0; i < transfer_count; ++i) {
  523. t = &transfers[i];
  524. if(!t->started) {
  525. t->easy = curl_easy_init();
  526. if(!t->easy || setup(t->easy, url, t, http_version, host,
  527. share, use_earlydata, announce_length)) {
  528. fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
  529. return 1;
  530. }
  531. curl_multi_add_handle(multi_handle, t->easy);
  532. t->started = 1;
  533. ++active_transfers;
  534. fprintf(stderr, "[t-%d] STARTED\n", t->idx);
  535. break;
  536. }
  537. }
  538. /* all started */
  539. if(i == transfer_count)
  540. break;
  541. }
  542. } while(m);
  543. } while(active_transfers); /* as long as we have transfers going */
  544. curl_multi_cleanup(multi_handle);
  545. }
  546. for(i = 0; i < transfer_count; ++i) {
  547. t = &transfers[i];
  548. if(t->out) {
  549. fclose(t->out);
  550. t->out = NULL;
  551. }
  552. if(t->easy) {
  553. curl_easy_cleanup(t->easy);
  554. t->easy = NULL;
  555. }
  556. }
  557. free(transfers);
  558. curl_share_cleanup(share);
  559. return 0;
  560. #else
  561. (void)argc;
  562. (void)argv;
  563. fprintf(stderr, "Not supported with this compiler.\n");
  564. return 1;
  565. #endif /* !_MSC_VER */
  566. }