http2-upload.c 8.1 KB

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