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