http2-download.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 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 do not 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. do {
  183. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  184. if(still_running)
  185. /* wait for activity, timeout or "nothing" */
  186. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  187. if(mc)
  188. break;
  189. } while(still_running);
  190. for(i = 0; i < num_transfers; i++) {
  191. curl_multi_remove_handle(multi_handle, trans[i].easy);
  192. curl_easy_cleanup(trans[i].easy);
  193. }
  194. curl_multi_cleanup(multi_handle);
  195. return 0;
  196. }