blob: d0d54695eec9887c0fd86a2a7de23e587b633e7a [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 uploads over a single connection
26 * </DESC>
27 */
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33#include <errno.h>
34
35/* somewhat unix-specific */
36#include <sys/time.h>
37#include <unistd.h>
38
39/* curl stuff */
40#include <curl/curl.h>
41#include <curl/mprintf.h>
42
43#ifndef CURLPIPE_MULTIPLEX
44/* This little trick will just make sure that we do not enable pipelining for
45 libcurls old enough to not have this symbol. It is _not_ defined to zero in
46 a recent libcurl header. */
47#define CURLPIPE_MULTIPLEX 0
48#endif
49
50#define NUM_HANDLES 1000
51
52struct input {
53 FILE *in;
54 size_t bytes_read; /* count up */
55 CURL *hnd;
56 int num;
57};
58
59static
60void dump(const char *text, int num, unsigned char *ptr, size_t size,
61 char nohex)
62{
63 size_t i;
64 size_t c;
65 unsigned int width = 0x10;
66
67 if(nohex)
68 /* without the hex output, we can fit more on screen */
69 width = 0x40;
70
71 fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
72 num, text, (unsigned long)size, (unsigned long)size);
73
74 for(i = 0; i<size; i += width) {
75
76 fprintf(stderr, "%4.4lx: ", (unsigned long)i);
77
78 if(!nohex) {
79 /* hex not disabled, show it */
80 for(c = 0; c < width; c++)
81 if(i + c < size)
82 fprintf(stderr, "%02x ", ptr[i + c]);
83 else
84 fputs(" ", stderr);
85 }
86
87 for(c = 0; (c < width) && (i + c < size); c++) {
88 /* check for 0D0A; if found, skip past and start a new line of output */
89 if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
90 ptr[i + c + 1] == 0x0A) {
91 i += (c + 2 - width);
92 break;
93 }
94 fprintf(stderr, "%c",
95 (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
96 /* check again for 0D0A, to avoid an extra \n if it's at width */
97 if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
98 ptr[i + c + 2] == 0x0A) {
99 i += (c + 3 - width);
100 break;
101 }
102 }
103 fputc('\n', stderr); /* newline */
104 }
105}
106
107static
108int my_trace(CURL *handle, curl_infotype type,
109 char *data, size_t size,
110 void *userp)
111{
112 char timebuf[60];
113 const char *text;
114 struct input *i = (struct input *)userp;
115 int num = i->num;
116 static time_t epoch_offset;
117 static int known_offset;
118 struct timeval tv;
119 time_t secs;
120 struct tm *now;
121 (void)handle; /* prevent compiler warning */
122
123 gettimeofday(&tv, NULL);
124 if(!known_offset) {
125 epoch_offset = time(NULL) - tv.tv_sec;
126 known_offset = 1;
127 }
128 secs = epoch_offset + tv.tv_sec;
129 now = localtime(&secs); /* not thread safe but we do not care */
130 curl_msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
131 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
132
133 switch(type) {
134 case CURLINFO_TEXT:
135 fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
136 /* FALLTHROUGH */
137 default: /* in case a new one is introduced to shock us */
138 return 0;
139
140 case CURLINFO_HEADER_OUT:
141 text = "=> Send header";
142 break;
143 case CURLINFO_DATA_OUT:
144 text = "=> Send data";
145 break;
146 case CURLINFO_SSL_DATA_OUT:
147 text = "=> Send SSL data";
148 break;
149 case CURLINFO_HEADER_IN:
150 text = "<= Recv header";
151 break;
152 case CURLINFO_DATA_IN:
153 text = "<= Recv data";
154 break;
155 case CURLINFO_SSL_DATA_IN:
156 text = "<= Recv SSL data";
157 break;
158 }
159
160 dump(text, num, (unsigned char *)data, size, 1);
161 return 0;
162}
163
164static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
165{
166 struct input *i = userp;
167 size_t retcode = fread(ptr, size, nmemb, i->in);
168 i->bytes_read += retcode;
169 return retcode;
170}
171
172static void setup(struct input *i, int num, const char *upload)
173{
174 FILE *out;
175 char url[256];
176 char filename[128];
177 struct stat file_info;
178 curl_off_t uploadsize;
179 CURL *hnd;
180
181 hnd = i->hnd = curl_easy_init();
182 i->num = num;
183 curl_msnprintf(filename, 128, "dl-%d", num);
184 out = fopen(filename, "wb");
185 if(!out) {
186 fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
187 strerror(errno));
188 exit(1);
189 }
190
191 curl_msnprintf(url, 256, "https://localhost:8443/upload-%d", num);
192
193 /* get the file size of the local file */
194 if(stat(upload, &file_info)) {
195 fprintf(stderr, "error: could not stat file %s: %s\n", upload,
196 strerror(errno));
197 exit(1);
198 }
199
200 uploadsize = file_info.st_size;
201
202 i->in = fopen(upload, "rb");
203 if(!i->in) {
204 fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
205 strerror(errno));
206 exit(1);
207 }
208
209 /* write to this file */
210 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
211
212 /* we want to use our own read function */
213 curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
214 /* read from this file */
215 curl_easy_setopt(hnd, CURLOPT_READDATA, i);
216 /* provide the size of the upload */
217 curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
218
219 /* send in the URL to store the upload as */
220 curl_easy_setopt(hnd, CURLOPT_URL, url);
221
222 /* upload please */
223 curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
224
225 /* please be verbose */
226 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
227 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
228 curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i);
229
230 /* HTTP/2 please */
231 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
232
233 /* we use a self-signed test server, skip verification during debugging */
234 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
235 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
236
237#if (CURLPIPE_MULTIPLEX > 0)
238 /* wait for pipe connection to confirm */
239 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
240#endif
241}
242
243/*
244 * Upload all files over HTTP/2, using the same physical connection!
245 */
246int main(int argc, char **argv)
247{
248 struct input trans[NUM_HANDLES];
249 CURLM *multi_handle;
250 int i;
251 int still_running = 0; /* keep number of running handles */
252 const char *filename = "index.html";
253 int num_transfers;
254
255 if(argc > 1) {
256 /* if given a number, do that many transfers */
257 num_transfers = atoi(argv[1]);
258
259 if(!num_transfers || (num_transfers > NUM_HANDLES))
260 num_transfers = 3; /* a suitable low default */
261
262 if(argc > 2)
263 /* if given a file name, upload this! */
264 filename = argv[2];
265 }
266 else
267 num_transfers = 3;
268
269 /* init a multi stack */
270 multi_handle = curl_multi_init();
271
272 for(i = 0; i<num_transfers; i++) {
273 setup(&trans[i], i, filename);
274
275 /* add the individual transfer */
276 curl_multi_add_handle(multi_handle, trans[i].hnd);
277 }
278
279 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
280
281 /* We do HTTP/2 so let's stick to one connection per host */
282 curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
283
284 do {
285 CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
286
287 if(still_running)
288 /* wait for activity, timeout or "nothing" */
289 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
290
291 if(mc)
292 break;
293
294 } while(still_running);
295
296 curl_multi_cleanup(multi_handle);
297
298 for(i = 0; i<num_transfers; i++) {
299 curl_multi_remove_handle(multi_handle, trans[i].hnd);
300 curl_easy_cleanup(trans[i].hnd);
301 }
302
303 return 0;
304}