1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Multiplexed HTTP/2 downloads over a single connection
30 /* somewhat unix-specific */
35 #include <curl/curl.h>
37 #ifndef CURLPIPE_MULTIPLEX
38 /* This little trick will just make sure that we don't enable pipelining for
39 libcurls old enough to not have this symbol. It is _not_ defined to zero in
40 a recent libcurl header. */
41 #define CURLPIPE_MULTIPLEX 0
44 #define NUM_HANDLES 1000
46 static void *curl_hnd[NUM_HANDLES];
47 static int num_transfers;
49 /* a handle to number lookup, highly ineffective when we do many
51 static int hnd2num(CURL *hnd)
54 for(i = 0; i< num_transfers; i++) {
55 if(curl_hnd[i] == hnd)
58 return 0; /* weird, but just a fail-safe */
62 void dump(const char *text, int num, unsigned char *ptr, size_t size,
68 unsigned int width = 0x10;
71 /* without the hex output, we can fit more on screen */
74 fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
75 num, text, (long)size, (long)size);
77 for(i = 0; i<size; i += width) {
79 fprintf(stderr, "%4.4lx: ", (long)i);
82 /* hex not disabled, show it */
83 for(c = 0; c < width; c++)
85 fprintf(stderr, "%02x ", ptr[i + c]);
90 for(c = 0; (c < width) && (i + c < size); c++) {
91 /* check for 0D0A; if found, skip past and start a new line of output */
92 if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
93 ptr[i + c + 1] == 0x0A) {
98 (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
99 /* check again for 0D0A, to avoid an extra \n if it's at width */
100 if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
101 ptr[i + c + 2] == 0x0A) {
102 i += (c + 3 - width);
106 fputc('\n', stderr); /* newline */
111 int my_trace(CURL *handle, curl_infotype type,
112 char *data, size_t size,
116 int num = hnd2num(handle);
117 (void)handle; /* prevent compiler warning */
121 fprintf(stderr, "== %d Info: %s", num, data);
123 default: /* in case a new one is introduced to shock us */
126 case CURLINFO_HEADER_OUT:
127 text = "=> Send header";
129 case CURLINFO_DATA_OUT:
130 text = "=> Send data";
132 case CURLINFO_SSL_DATA_OUT:
133 text = "=> Send SSL data";
135 case CURLINFO_HEADER_IN:
136 text = "<= Recv header";
138 case CURLINFO_DATA_IN:
139 text = "<= Recv data";
141 case CURLINFO_SSL_DATA_IN:
142 text = "<= Recv SSL data";
146 dump(text, num, (unsigned char *)data, size, 1);
150 static void setup(CURL *hnd, int num)
155 snprintf(filename, 128, "dl-%d", num);
157 out = fopen(filename, "wb");
159 /* write to this file */
160 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
162 /* set the same URL */
163 curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
165 /* please be verbose */
166 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
167 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
170 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
172 /* we use a self-signed test server, skip verification during debugging */
173 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
174 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
176 #if (CURLPIPE_MULTIPLEX > 0)
177 /* wait for pipe connection to confirm */
178 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
185 * Simply download two files over HTTP/2, using the same physical connection!
187 int main(int argc, char **argv)
189 CURL *easy[NUM_HANDLES];
192 int still_running; /* keep number of running handles */
195 /* if given a number, do that many transfers */
196 num_transfers = atoi(argv[1]);
198 if(!num_transfers || (num_transfers > NUM_HANDLES))
199 num_transfers = 3; /* a suitable low default */
201 /* init a multi stack */
202 multi_handle = curl_multi_init();
204 for(i = 0; i<num_transfers; i++) {
205 easy[i] = curl_easy_init();
209 /* add the individual transfer */
210 curl_multi_add_handle(multi_handle, easy[i]);
213 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
215 /* we start some action by calling perform right away */
216 curl_multi_perform(multi_handle, &still_running);
219 struct timeval timeout;
220 int rc; /* select() return code */
221 CURLMcode mc; /* curl_multi_fdset() return code */
228 long curl_timeo = -1;
234 /* set a suitable timeout to play around with */
238 curl_multi_timeout(multi_handle, &curl_timeo);
239 if(curl_timeo >= 0) {
240 timeout.tv_sec = curl_timeo / 1000;
241 if(timeout.tv_sec > 1)
244 timeout.tv_usec = (curl_timeo % 1000) * 1000;
247 /* get file descriptors from the transfers */
248 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
251 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
255 /* On success the value of maxfd is guaranteed to be >= -1. We call
256 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
257 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
258 to sleep 100ms, which is the minimum suggested value in the
259 curl_multi_fdset() doc. */
266 /* Portable sleep for platforms other than Windows. */
267 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
268 rc = select(0, NULL, NULL, NULL, &wait);
272 /* Note that on some platforms 'timeout' may be modified by select().
273 If you need access to the original value save a copy beforehand. */
274 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
283 /* timeout or readable/writable sockets */
284 curl_multi_perform(multi_handle, &still_running);
287 } while(still_running);
289 curl_multi_cleanup(multi_handle);
291 for(i = 0; i<num_transfers; i++)
292 curl_easy_cleanup(easy[i]);