2
0

upload-pausing.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * upload pausing
  26. * </DESC>
  27. */
  28. /* This is based on the PoC client of issue #11769
  29. */
  30. #include <curl/curl.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #ifndef _MSC_VER
  35. /* somewhat Unix-specific */
  36. #include <unistd.h> /* getopt() */
  37. #endif
  38. #ifndef _MSC_VER
  39. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  40. {
  41. /*
  42. * This is the trace look that is similar to what libcurl makes on its
  43. * own.
  44. */
  45. static const char * const s_infotype[] = {
  46. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  47. };
  48. if(idsbuf && *idsbuf)
  49. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  50. else
  51. fputs(s_infotype[type], log);
  52. }
  53. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  54. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  55. CURL_FORMAT_CURL_OFF_T "] "
  56. /*
  57. ** callback for CURLOPT_DEBUGFUNCTION
  58. */
  59. static int debug_cb(CURL *handle, curl_infotype type,
  60. char *data, size_t size,
  61. void *userdata)
  62. {
  63. FILE *output = stderr;
  64. static int newl = 0;
  65. static int traced_data = 0;
  66. char idsbuf[60];
  67. curl_off_t xfer_id, conn_id;
  68. (void)handle; /* not used */
  69. (void)userdata;
  70. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  71. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  72. conn_id >= 0) {
  73. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2, xfer_id,
  74. conn_id);
  75. }
  76. else {
  77. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  78. }
  79. }
  80. else
  81. idsbuf[0] = 0;
  82. switch(type) {
  83. case CURLINFO_HEADER_OUT:
  84. if(size > 0) {
  85. size_t st = 0;
  86. size_t i;
  87. for(i = 0; i < size - 1; i++) {
  88. if(data[i] == '\n') { /* LF */
  89. if(!newl) {
  90. log_line_start(output, idsbuf, type);
  91. }
  92. (void)fwrite(data + st, i - st + 1, 1, output);
  93. st = i + 1;
  94. newl = 0;
  95. }
  96. }
  97. if(!newl)
  98. log_line_start(output, idsbuf, type);
  99. (void)fwrite(data + st, i - st + 1, 1, output);
  100. }
  101. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  102. traced_data = 0;
  103. break;
  104. case CURLINFO_TEXT:
  105. case CURLINFO_HEADER_IN:
  106. if(!newl)
  107. log_line_start(output, idsbuf, type);
  108. (void)fwrite(data, size, 1, output);
  109. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  110. traced_data = 0;
  111. break;
  112. case CURLINFO_DATA_OUT:
  113. case CURLINFO_DATA_IN:
  114. case CURLINFO_SSL_DATA_IN:
  115. case CURLINFO_SSL_DATA_OUT:
  116. if(!traced_data) {
  117. if(!newl)
  118. log_line_start(output, idsbuf, type);
  119. fprintf(output, "[%ld bytes data]\n", (long)size);
  120. newl = 0;
  121. traced_data = 1;
  122. }
  123. break;
  124. default: /* nada */
  125. newl = 0;
  126. traced_data = 1;
  127. break;
  128. }
  129. return 0;
  130. }
  131. #define PAUSE_READ_AFTER 1
  132. static size_t total_read = 0;
  133. static size_t read_callback(char *ptr, size_t size, size_t nmemb,
  134. void *userdata)
  135. {
  136. (void)size;
  137. (void)nmemb;
  138. (void)userdata;
  139. if(total_read >= PAUSE_READ_AFTER) {
  140. fprintf(stderr, "read_callback, return PAUSE\n");
  141. return CURL_READFUNC_PAUSE;
  142. }
  143. else {
  144. ptr[0] = '\n';
  145. ++total_read;
  146. fprintf(stderr, "read_callback, return 1 byte\n");
  147. return 1;
  148. }
  149. }
  150. static int progress_callback(void *clientp,
  151. curl_off_t dltotal,
  152. curl_off_t dlnow,
  153. curl_off_t ultotal,
  154. curl_off_t ulnow)
  155. {
  156. (void)dltotal;
  157. (void)dlnow;
  158. (void)ultotal;
  159. (void)ulnow;
  160. (void)clientp;
  161. #if 0
  162. /* Used to unpause on progress, but keeping for now. */
  163. {
  164. CURL *curl = (CURL *)clientp;
  165. curl_easy_pause(curl, CURLPAUSE_CONT);
  166. /* curl_easy_pause(curl, CURLPAUSE_RECV_CONT); */
  167. }
  168. #endif
  169. return 0;
  170. }
  171. static int err(void)
  172. {
  173. fprintf(stderr, "something unexpected went wrong - bailing out!\n");
  174. exit(2);
  175. }
  176. static void usage(const char *msg)
  177. {
  178. if(msg)
  179. fprintf(stderr, "%s\n", msg);
  180. fprintf(stderr,
  181. "usage: [options] url\n"
  182. " upload and pause, options:\n"
  183. " -V http_version (http/1.1, h2, h3) http version to use\n"
  184. );
  185. }
  186. #endif /* !_MSC_VER */
  187. int main(int argc, char *argv[])
  188. {
  189. #ifndef _MSC_VER
  190. CURL *curl;
  191. CURLcode rc = CURLE_OK;
  192. CURLU *cu;
  193. struct curl_slist *resolve = NULL;
  194. char resolve_buf[1024];
  195. char *url, *host = NULL, *port = NULL;
  196. int http_version = CURL_HTTP_VERSION_1_1;
  197. int ch;
  198. while((ch = getopt(argc, argv, "V:")) != -1) {
  199. switch(ch) {
  200. case 'V': {
  201. if(!strcmp("http/1.1", optarg))
  202. http_version = CURL_HTTP_VERSION_1_1;
  203. else if(!strcmp("h2", optarg))
  204. http_version = CURL_HTTP_VERSION_2_0;
  205. else if(!strcmp("h3", optarg))
  206. http_version = CURL_HTTP_VERSION_3ONLY;
  207. else {
  208. usage("invalid http version");
  209. return 1;
  210. }
  211. break;
  212. }
  213. default:
  214. usage("invalid option");
  215. return 1;
  216. }
  217. }
  218. argc -= optind;
  219. argv += optind;
  220. if(argc != 1) {
  221. usage("not enough arguments");
  222. return 2;
  223. }
  224. url = argv[0];
  225. curl_global_init(CURL_GLOBAL_DEFAULT);
  226. curl_global_trace("ids,time");
  227. cu = curl_url();
  228. if(!cu) {
  229. fprintf(stderr, "out of memory\n");
  230. exit(1);
  231. }
  232. if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
  233. fprintf(stderr, "not a URL: '%s'\n", url);
  234. exit(1);
  235. }
  236. if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
  237. fprintf(stderr, "could not get host of '%s'\n", url);
  238. exit(1);
  239. }
  240. if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
  241. fprintf(stderr, "could not get port of '%s'\n", url);
  242. exit(1);
  243. }
  244. memset(&resolve, 0, sizeof(resolve));
  245. curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
  246. host, port);
  247. resolve = curl_slist_append(resolve, resolve_buf);
  248. curl = curl_easy_init();
  249. if(!curl) {
  250. fprintf(stderr, "out of memory\n");
  251. exit(1);
  252. }
  253. /* We want to use our own read function. */
  254. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  255. /* It will help us to continue the read function. */
  256. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  257. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, curl);
  258. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  259. /* It will help us to ensure that keepalive does not help. */
  260. curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
  261. curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 1L);
  262. curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 1L);
  263. curl_easy_setopt(curl, CURLOPT_TCP_KEEPCNT, 1L);
  264. /* Enable uploading. */
  265. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  266. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  267. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  268. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  269. if(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
  270. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_cb)
  271. != CURLE_OK ||
  272. curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK)
  273. err();
  274. curl_easy_setopt(curl, CURLOPT_URL, url);
  275. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, http_version);
  276. rc = curl_easy_perform(curl);
  277. if(curl) {
  278. curl_easy_cleanup(curl);
  279. }
  280. curl_slist_free_all(resolve);
  281. curl_free(host);
  282. curl_free(port);
  283. curl_url_cleanup(cu);
  284. curl_global_cleanup();
  285. return (int)rc;
  286. #else
  287. (void)argc;
  288. (void)argv;
  289. fprintf(stderr, "Not supported with this compiler.\n");
  290. return 1;
  291. #endif /* !_MSC_VER */
  292. }