http2-download.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Multiplexed HTTP/2 downloads over a single connection
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. /* curl stuff */
  33. #include <curl/curl.h>
  34. #include <curl/mprintf.h>
  35. #ifndef CURLPIPE_MULTIPLEX
  36. /* This little trick makes sure that we do not enable pipelining for libcurls
  37. old enough to not have this symbol. It is _not_ defined to zero in a recent
  38. libcurl header. */
  39. #define CURLPIPE_MULTIPLEX 0
  40. #endif
  41. struct transfer {
  42. CURL *easy;
  43. unsigned int num;
  44. FILE *out;
  45. };
  46. #define NUM_HANDLES 1000
  47. static
  48. void dump(const char *text, unsigned int num, unsigned char *ptr, size_t size,
  49. char nohex)
  50. {
  51. size_t i;
  52. size_t c;
  53. unsigned int width = 0x10;
  54. if(nohex)
  55. /* without the hex output, we can fit more on screen */
  56. width = 0x40;
  57. fprintf(stderr, "%u %s, %lu bytes (0x%lx)\n",
  58. num, text, (unsigned long)size, (unsigned long)size);
  59. for(i = 0; i < size; i += width) {
  60. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  61. if(!nohex) {
  62. /* hex not disabled, show it */
  63. for(c = 0; c < width; c++)
  64. if(i + c < size)
  65. fprintf(stderr, "%02x ", ptr[i + c]);
  66. else
  67. fputs(" ", stderr);
  68. }
  69. for(c = 0; (c < width) && (i + c < size); c++) {
  70. /* check for 0D0A; if found, skip past and start a new line of output */
  71. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  72. ptr[i + c + 1] == 0x0A) {
  73. i += (c + 2 - width);
  74. break;
  75. }
  76. fprintf(stderr, "%c",
  77. (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
  78. /* check again for 0D0A, to avoid an extra \n if it's at width */
  79. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  80. ptr[i + c + 2] == 0x0A) {
  81. i += (c + 3 - width);
  82. break;
  83. }
  84. }
  85. fputc('\n', stderr); /* newline */
  86. }
  87. }
  88. static
  89. int my_trace(CURL *handle, curl_infotype type,
  90. char *data, size_t size,
  91. void *userp)
  92. {
  93. const char *text;
  94. struct transfer *t = (struct transfer *)userp;
  95. unsigned int num = t->num;
  96. (void)handle; /* prevent compiler warning */
  97. switch(type) {
  98. case CURLINFO_TEXT:
  99. fprintf(stderr, "== %u Info: %s", num, data);
  100. return 0;
  101. case CURLINFO_HEADER_OUT:
  102. text = "=> Send header";
  103. break;
  104. case CURLINFO_DATA_OUT:
  105. text = "=> Send data";
  106. break;
  107. case CURLINFO_SSL_DATA_OUT:
  108. text = "=> Send SSL data";
  109. break;
  110. case CURLINFO_HEADER_IN:
  111. text = "<= Recv header";
  112. break;
  113. case CURLINFO_DATA_IN:
  114. text = "<= Recv data";
  115. break;
  116. case CURLINFO_SSL_DATA_IN:
  117. text = "<= Recv SSL data";
  118. break;
  119. default: /* in case a new one is introduced to shock us */
  120. return 0;
  121. }
  122. dump(text, num, (unsigned char *)data, size, 1);
  123. return 0;
  124. }
  125. static void setup(struct transfer *t, int num)
  126. {
  127. char filename[128];
  128. CURL *hnd;
  129. hnd = t->easy = curl_easy_init();
  130. curl_msnprintf(filename, 128, "dl-%d", num);
  131. t->out = fopen(filename, "wb");
  132. if(!t->out) {
  133. fprintf(stderr, "error: could not open file %s for writing: %s\n",
  134. filename, strerror(errno));
  135. exit(1);
  136. }
  137. /* write to this file */
  138. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t->out);
  139. /* set the same URL */
  140. curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
  141. /* please be verbose */
  142. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  143. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
  144. curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, t);
  145. /* enlarge the receive buffer for potentially higher transfer speeds */
  146. curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 100000L);
  147. /* HTTP/2 please */
  148. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  149. #if (CURLPIPE_MULTIPLEX > 0)
  150. /* wait for pipe connection to confirm */
  151. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  152. #endif
  153. }
  154. /*
  155. * Download many transfers over HTTP/2, using the same connection!
  156. */
  157. int main(int argc, char **argv)
  158. {
  159. struct transfer trans[NUM_HANDLES];
  160. CURLM *multi_handle;
  161. int i;
  162. int still_running = 0; /* keep number of running handles */
  163. int num_transfers;
  164. if(argc > 1) {
  165. /* if given a number, do that many transfers */
  166. num_transfers = atoi(argv[1]);
  167. if((num_transfers < 1) || (num_transfers > NUM_HANDLES))
  168. num_transfers = 3; /* a suitable low default */
  169. }
  170. else
  171. num_transfers = 3; /* suitable default */
  172. /* init a multi stack */
  173. multi_handle = curl_multi_init();
  174. for(i = 0; i < num_transfers; i++) {
  175. setup(&trans[i], i);
  176. /* add the individual transfer */
  177. curl_multi_add_handle(multi_handle, trans[i].easy);
  178. }
  179. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  180. do {
  181. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  182. if(still_running)
  183. /* wait for activity, timeout or "nothing" */
  184. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  185. if(mc)
  186. break;
  187. } while(still_running);
  188. for(i = 0; i < num_transfers; i++) {
  189. curl_multi_remove_handle(multi_handle, trans[i].easy);
  190. curl_easy_cleanup(trans[i].easy);
  191. }
  192. curl_multi_cleanup(multi_handle);
  193. return 0;
  194. }