2
0

http2-download.c 6.2 KB

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