http2-upload.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * Multiplexed HTTP/2 uploads over a single connection
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. /* somewhat Unix-specific */
  35. #ifndef _MSC_VER
  36. #include <sys/time.h>
  37. #include <unistd.h>
  38. #endif
  39. /* curl stuff */
  40. #include <curl/curl.h>
  41. #include <curl/mprintf.h>
  42. #ifndef CURLPIPE_MULTIPLEX
  43. /* This little trick makes sure that we do not enable pipelining for libcurls
  44. old enough to not have this symbol. It is _not_ defined to zero in a recent
  45. libcurl header. */
  46. #define CURLPIPE_MULTIPLEX 0
  47. #endif
  48. #define NUM_HANDLES 1000
  49. #ifdef _MSC_VER
  50. #define gettimeofday(a, b) my_gettimeofday((a), (b))
  51. static
  52. int my_gettimeofday(struct timeval *tp, void *tzp)
  53. {
  54. (void)tzp;
  55. if(tp) {
  56. /* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */
  57. #define _WIN32_FT_OFFSET (116444736000000000)
  58. union {
  59. CURL_TYPEOF_CURL_OFF_T ns100; /* time since 1 Jan 1601 in 100ns units */
  60. FILETIME ft;
  61. } _now;
  62. GetSystemTimeAsFileTime(&_now.ft);
  63. tp->tv_usec = (long)((_now.ns100 / 10) % 1000000);
  64. tp->tv_sec = (long)((_now.ns100 - _WIN32_FT_OFFSET) / 10000000);
  65. }
  66. return 0;
  67. }
  68. #endif
  69. struct input {
  70. FILE *in;
  71. size_t bytes_read; /* count up */
  72. CURL *hnd;
  73. int num;
  74. };
  75. static
  76. void dump(const char *text, int num, unsigned char *ptr, size_t size,
  77. char nohex)
  78. {
  79. size_t i;
  80. size_t c;
  81. unsigned int width = 0x10;
  82. if(nohex)
  83. /* without the hex output, we can fit more on screen */
  84. width = 0x40;
  85. fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
  86. num, text, (unsigned long)size, (unsigned long)size);
  87. for(i = 0; i < size; i += width) {
  88. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  89. if(!nohex) {
  90. /* hex not disabled, show it */
  91. for(c = 0; c < width; c++)
  92. if(i + c < size)
  93. fprintf(stderr, "%02x ", ptr[i + c]);
  94. else
  95. fputs(" ", stderr);
  96. }
  97. for(c = 0; (c < width) && (i + c < size); c++) {
  98. /* check for 0D0A; if found, skip past and start a new line of output */
  99. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  100. ptr[i + c + 1] == 0x0A) {
  101. i += (c + 2 - width);
  102. break;
  103. }
  104. fprintf(stderr, "%c",
  105. (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
  106. /* check again for 0D0A, to avoid an extra \n if it's at width */
  107. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  108. ptr[i + c + 2] == 0x0A) {
  109. i += (c + 3 - width);
  110. break;
  111. }
  112. }
  113. fputc('\n', stderr); /* newline */
  114. }
  115. }
  116. static
  117. int my_trace(CURL *handle, curl_infotype type,
  118. char *data, size_t size,
  119. void *userp)
  120. {
  121. char timebuf[60];
  122. const char *text;
  123. struct input *i = (struct input *)userp;
  124. int num = i->num;
  125. static time_t epoch_offset;
  126. static int known_offset;
  127. struct timeval tv;
  128. time_t secs;
  129. struct tm *now;
  130. (void)handle; /* prevent compiler warning */
  131. gettimeofday(&tv, NULL);
  132. if(!known_offset) {
  133. epoch_offset = time(NULL) - tv.tv_sec;
  134. known_offset = 1;
  135. }
  136. secs = epoch_offset + tv.tv_sec;
  137. now = localtime(&secs); /* not thread safe but we do not care */
  138. curl_msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
  139. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  140. switch(type) {
  141. case CURLINFO_TEXT:
  142. fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
  143. return 0;
  144. case CURLINFO_HEADER_OUT:
  145. text = "=> Send header";
  146. break;
  147. case CURLINFO_DATA_OUT:
  148. text = "=> Send data";
  149. break;
  150. case CURLINFO_SSL_DATA_OUT:
  151. text = "=> Send SSL data";
  152. break;
  153. case CURLINFO_HEADER_IN:
  154. text = "<= Recv header";
  155. break;
  156. case CURLINFO_DATA_IN:
  157. text = "<= Recv data";
  158. break;
  159. case CURLINFO_SSL_DATA_IN:
  160. text = "<= Recv SSL data";
  161. break;
  162. default: /* in case a new one is introduced to shock us */
  163. return 0;
  164. }
  165. dump(text, num, (unsigned char *)data, size, 1);
  166. return 0;
  167. }
  168. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  169. {
  170. struct input *i = userp;
  171. size_t retcode = fread(ptr, size, nmemb, i->in);
  172. i->bytes_read += retcode;
  173. return retcode;
  174. }
  175. static void setup(struct input *i, int num, const char *upload)
  176. {
  177. FILE *out;
  178. char url[256];
  179. char filename[128];
  180. struct stat file_info;
  181. curl_off_t uploadsize;
  182. CURL *hnd;
  183. hnd = i->hnd = curl_easy_init();
  184. i->num = num;
  185. curl_msnprintf(filename, 128, "dl-%d", num);
  186. out = fopen(filename, "wb");
  187. if(!out) {
  188. fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
  189. strerror(errno));
  190. exit(1);
  191. }
  192. curl_msnprintf(url, 256, "https://localhost:8443/upload-%d", num);
  193. /* get the file size of the local file */
  194. if(stat(upload, &file_info)) {
  195. fprintf(stderr, "error: could not stat file %s: %s\n", upload,
  196. strerror(errno));
  197. exit(1);
  198. }
  199. uploadsize = file_info.st_size;
  200. i->in = fopen(upload, "rb");
  201. if(!i->in) {
  202. fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
  203. strerror(errno));
  204. exit(1);
  205. }
  206. /* write to this file */
  207. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
  208. /* we want to use our own read function */
  209. curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
  210. /* read from this file */
  211. curl_easy_setopt(hnd, CURLOPT_READDATA, i);
  212. /* provide the size of the upload */
  213. curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
  214. /* send in the URL to store the upload as */
  215. curl_easy_setopt(hnd, CURLOPT_URL, url);
  216. /* upload please */
  217. curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
  218. /* please be verbose */
  219. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  220. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
  221. curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i);
  222. /* HTTP/2 please */
  223. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  224. /* we use a self-signed test server, skip verification during debugging */
  225. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  226. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  227. #if (CURLPIPE_MULTIPLEX > 0)
  228. /* wait for pipe connection to confirm */
  229. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  230. #endif
  231. }
  232. /*
  233. * Upload all files over HTTP/2, using the same physical connection!
  234. */
  235. int main(int argc, char **argv)
  236. {
  237. struct input trans[NUM_HANDLES];
  238. CURLM *multi_handle;
  239. int i;
  240. int still_running = 0; /* keep number of running handles */
  241. const char *filename = "index.html";
  242. int num_transfers;
  243. if(argc > 1) {
  244. /* if given a number, do that many transfers */
  245. num_transfers = atoi(argv[1]);
  246. if(!num_transfers || (num_transfers > NUM_HANDLES))
  247. num_transfers = 3; /* a suitable low default */
  248. if(argc > 2)
  249. /* if given a file name, upload this! */
  250. filename = argv[2];
  251. }
  252. else
  253. num_transfers = 3;
  254. /* init a multi stack */
  255. multi_handle = curl_multi_init();
  256. for(i = 0; i < num_transfers; i++) {
  257. setup(&trans[i], i, filename);
  258. /* add the individual transfer */
  259. curl_multi_add_handle(multi_handle, trans[i].hnd);
  260. }
  261. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  262. /* We do HTTP/2 so let's stick to one connection per host */
  263. curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
  264. do {
  265. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  266. if(still_running)
  267. /* wait for activity, timeout or "nothing" */
  268. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  269. if(mc)
  270. break;
  271. } while(still_running);
  272. curl_multi_cleanup(multi_handle);
  273. for(i = 0; i < num_transfers; i++) {
  274. curl_multi_remove_handle(multi_handle, trans[i].hnd);
  275. curl_easy_cleanup(trans[i].hnd);
  276. }
  277. return 0;
  278. }