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