blob: e88f578cdf72e732c8e0d7efe04d929d8f41ace7 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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
48struct transfer {
49 CURL *easy;
50 unsigned int num;
51 FILE *out;
52};
53
54#define NUM_HANDLES 1000
55
56static
57void 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
105static
106int 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
146static 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 /* HTTP/2 please */
174 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
175
176 /* we use a self-signed test server, skip verification during debugging */
177 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
178 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
179
180#if (CURLPIPE_MULTIPLEX > 0)
181 /* wait for pipe connection to confirm */
182 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
183#endif
184}
185
186/*
187 * Download many transfers over HTTP/2, using the same connection!
188 */
189int main(int argc, char **argv)
190{
191 struct transfer trans[NUM_HANDLES];
192 CURLM *multi_handle;
193 int i;
194 int still_running = 0; /* keep number of running handles */
195 int num_transfers;
196 if(argc > 1) {
197 /* if given a number, do that many transfers */
198 num_transfers = atoi(argv[1]);
199 if((num_transfers < 1) || (num_transfers > NUM_HANDLES))
200 num_transfers = 3; /* a suitable low default */
201 }
202 else
203 num_transfers = 3; /* suitable default */
204
205 /* init a multi stack */
206 multi_handle = curl_multi_init();
207
208 for(i = 0; i < num_transfers; i++) {
209 setup(&trans[i], i);
210
211 /* add the individual transfer */
212 curl_multi_add_handle(multi_handle, trans[i].easy);
213 }
214
215 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
216
217 do {
218 CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
219
220 if(still_running)
221 /* wait for activity, timeout or "nothing" */
222 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
223
224 if(mc)
225 break;
226 } while(still_running);
227
228 for(i = 0; i < num_transfers; i++) {
229 curl_multi_remove_handle(multi_handle, trans[i].easy);
230 curl_easy_cleanup(trans[i].easy);
231 }
232
233 curl_multi_cleanup(multi_handle);
234
235 return 0;
236}