http2-upload.c 8.7 KB

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