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