http2-download.c 7.8 KB

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