http2-upload.c 8.7 KB

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