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