http2-download.c 7.8 KB

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