http2-download.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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 downloads over a single connection
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. /* somewhat unix-specific */
  31. #include <sys/time.h>
  32. #include <unistd.h>
  33. /* curl stuff */
  34. #include <curl/curl.h>
  35. #include <curl/mprintf.h>
  36. #ifndef CURLPIPE_MULTIPLEX
  37. /* This little trick will just make sure that we don't enable pipelining for
  38. libcurls old enough to not have this symbol. It is _not_ defined to zero in
  39. a recent libcurl header. */
  40. #define CURLPIPE_MULTIPLEX 0
  41. #endif
  42. struct transfer {
  43. CURL *easy;
  44. unsigned int num;
  45. FILE *out;
  46. };
  47. #define NUM_HANDLES 1000
  48. static
  49. void dump(const char *text, int num, unsigned char *ptr, size_t size,
  50. char nohex)
  51. {
  52. size_t i;
  53. size_t c;
  54. unsigned int width = 0x10;
  55. if(nohex)
  56. /* without the hex output, we can fit more on screen */
  57. width = 0x40;
  58. fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
  59. num, text, (unsigned long)size, (unsigned long)size);
  60. for(i = 0; i<size; i += width) {
  61. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  62. if(!nohex) {
  63. /* hex not disabled, show it */
  64. for(c = 0; c < width; c++)
  65. if(i + c < size)
  66. fprintf(stderr, "%02x ", ptr[i + c]);
  67. else
  68. fputs(" ", stderr);
  69. }
  70. for(c = 0; (c < width) && (i + c < size); c++) {
  71. /* check for 0D0A; if found, skip past and start a new line of output */
  72. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  73. ptr[i + c + 1] == 0x0A) {
  74. i += (c + 2 - width);
  75. break;
  76. }
  77. fprintf(stderr, "%c",
  78. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  79. /* check again for 0D0A, to avoid an extra \n if it's at width */
  80. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  81. ptr[i + c + 2] == 0x0A) {
  82. i += (c + 3 - width);
  83. break;
  84. }
  85. }
  86. fputc('\n', stderr); /* newline */
  87. }
  88. }
  89. static
  90. int my_trace(CURL *handle, curl_infotype type,
  91. char *data, size_t size,
  92. void *userp)
  93. {
  94. const char *text;
  95. struct transfer *t = (struct transfer *)userp;
  96. unsigned int num = t->num;
  97. (void)handle; /* prevent compiler warning */
  98. switch(type) {
  99. case CURLINFO_TEXT:
  100. fprintf(stderr, "== %u Info: %s", num, data);
  101. /* FALLTHROUGH */
  102. default: /* in case a new one is introduced to shock us */
  103. return 0;
  104. case CURLINFO_HEADER_OUT:
  105. text = "=> Send header";
  106. break;
  107. case CURLINFO_DATA_OUT:
  108. text = "=> Send data";
  109. break;
  110. case CURLINFO_SSL_DATA_OUT:
  111. text = "=> Send SSL data";
  112. break;
  113. case CURLINFO_HEADER_IN:
  114. text = "<= Recv header";
  115. break;
  116. case CURLINFO_DATA_IN:
  117. text = "<= Recv data";
  118. break;
  119. case CURLINFO_SSL_DATA_IN:
  120. text = "<= Recv SSL data";
  121. break;
  122. }
  123. dump(text, num, (unsigned char *)data, size, 1);
  124. return 0;
  125. }
  126. static void setup(struct transfer *t, int num)
  127. {
  128. char filename[128];
  129. CURL *hnd;
  130. hnd = t->easy = curl_easy_init();
  131. curl_msnprintf(filename, 128, "dl-%d", num);
  132. t->out = fopen(filename, "wb");
  133. if(!t->out) {
  134. fprintf(stderr, "error: could not open file %s for writing: %s\n",
  135. filename, strerror(errno));
  136. exit(1);
  137. }
  138. /* write to this file */
  139. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t->out);
  140. /* set the same URL */
  141. curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
  142. /* please be verbose */
  143. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  144. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
  145. curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, t);
  146. /* HTTP/2 please */
  147. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  148. /* we use a self-signed test server, skip verification during debugging */
  149. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  150. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  151. #if (CURLPIPE_MULTIPLEX > 0)
  152. /* wait for pipe connection to confirm */
  153. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  154. #endif
  155. }
  156. /*
  157. * Download many transfers over HTTP/2, using the same connection!
  158. */
  159. int main(int argc, char **argv)
  160. {
  161. struct transfer trans[NUM_HANDLES];
  162. CURLM *multi_handle;
  163. int i;
  164. int still_running = 0; /* keep number of running handles */
  165. int num_transfers;
  166. if(argc > 1) {
  167. /* if given a number, do that many transfers */
  168. num_transfers = atoi(argv[1]);
  169. if((num_transfers < 1) || (num_transfers > NUM_HANDLES))
  170. num_transfers = 3; /* a suitable low default */
  171. }
  172. else
  173. num_transfers = 3; /* suitable default */
  174. /* init a multi stack */
  175. multi_handle = curl_multi_init();
  176. for(i = 0; i < num_transfers; i++) {
  177. setup(&trans[i], i);
  178. /* add the individual transfer */
  179. curl_multi_add_handle(multi_handle, trans[i].easy);
  180. }
  181. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  182. /* we start some action by calling perform right away */
  183. curl_multi_perform(multi_handle, &still_running);
  184. while(still_running) {
  185. struct timeval timeout;
  186. int rc; /* select() return code */
  187. CURLMcode mc; /* curl_multi_fdset() return code */
  188. fd_set fdread;
  189. fd_set fdwrite;
  190. fd_set fdexcep;
  191. int maxfd = -1;
  192. long curl_timeo = -1;
  193. FD_ZERO(&fdread);
  194. FD_ZERO(&fdwrite);
  195. FD_ZERO(&fdexcep);
  196. /* set a suitable timeout to play around with */
  197. timeout.tv_sec = 1;
  198. timeout.tv_usec = 0;
  199. curl_multi_timeout(multi_handle, &curl_timeo);
  200. if(curl_timeo >= 0) {
  201. timeout.tv_sec = curl_timeo / 1000;
  202. if(timeout.tv_sec > 1)
  203. timeout.tv_sec = 1;
  204. else
  205. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  206. }
  207. /* get file descriptors from the transfers */
  208. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  209. if(mc != CURLM_OK) {
  210. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  211. break;
  212. }
  213. /* On success the value of maxfd is guaranteed to be >= -1. We call
  214. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  215. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  216. to sleep 100ms, which is the minimum suggested value in the
  217. curl_multi_fdset() doc. */
  218. if(maxfd == -1) {
  219. #ifdef _WIN32
  220. Sleep(100);
  221. rc = 0;
  222. #else
  223. /* Portable sleep for platforms other than Windows. */
  224. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  225. rc = select(0, NULL, NULL, NULL, &wait);
  226. #endif
  227. }
  228. else {
  229. /* Note that on some platforms 'timeout' may be modified by select().
  230. If you need access to the original value save a copy beforehand. */
  231. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  232. }
  233. switch(rc) {
  234. case -1:
  235. /* select error */
  236. break;
  237. case 0:
  238. default:
  239. /* timeout or readable/writable sockets */
  240. curl_multi_perform(multi_handle, &still_running);
  241. break;
  242. }
  243. }
  244. for(i = 0; i < num_transfers; i++) {
  245. curl_multi_remove_handle(multi_handle, trans[i].easy);
  246. curl_easy_cleanup(trans[i].easy);
  247. }
  248. curl_multi_cleanup(multi_handle);
  249. return 0;
  250. }