2
0

upload-pausing.c 7.2 KB

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