http2-upload.c 8.1 KB

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