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 * HTTP/2 server push
26 * </DESC>
27 */
28 /* curl stuff */
29 #include <curl/curl.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifndef _MSC_VER
36 /* somewhat Unix-specific */
37 #include <unistd.h> /* getopt() */
38 #endif
39
40 #ifndef CURLPIPE_MULTIPLEX
41 #error "too old libcurl, cannot do HTTP/2 server push!"
42 #endif
43
44 #ifndef _MSC_VER
45 static int verbose = 1;
46
log_line_start(FILE * log,const char * idsbuf,curl_infotype type)47 static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
48 {
49 /*
50 * This is the trace look that is similar to what libcurl makes on its
51 * own.
52 */
53 static const char * const s_infotype[] = {
54 "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
55 };
56 if(idsbuf && *idsbuf)
57 fprintf(log, "%s%s", idsbuf, s_infotype[type]);
58 else
59 fputs(s_infotype[type], log);
60 }
61
62 #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
63 #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
64 CURL_FORMAT_CURL_OFF_T "] "
65 /*
66 ** callback for CURLOPT_DEBUGFUNCTION
67 */
debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)68 static int debug_cb(CURL *handle, curl_infotype type,
69 char *data, size_t size,
70 void *userdata)
71 {
72 FILE *output = stderr;
73 static int newl = 0;
74 static int traced_data = 0;
75 char idsbuf[60];
76 curl_off_t xfer_id, conn_id;
77
78 (void)handle; /* not used */
79 (void)userdata;
80
81 if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
82 if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
83 conn_id >= 0) {
84 curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2, xfer_id,
85 conn_id);
86 }
87 else {
88 curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
89 }
90 }
91 else
92 idsbuf[0] = 0;
93
94 switch(type) {
95 case CURLINFO_HEADER_OUT:
96 if(size > 0) {
97 size_t st = 0;
98 size_t i;
99 for(i = 0; i < size - 1; i++) {
100 if(data[i] == '\n') { /* LF */
101 if(!newl) {
102 log_line_start(output, idsbuf, type);
103 }
104 (void)fwrite(data + st, i - st + 1, 1, output);
105 st = i + 1;
106 newl = 0;
107 }
108 }
109 if(!newl)
110 log_line_start(output, idsbuf, type);
111 (void)fwrite(data + st, i - st + 1, 1, output);
112 }
113 newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
114 traced_data = 0;
115 break;
116 case CURLINFO_TEXT:
117 case CURLINFO_HEADER_IN:
118 if(!newl)
119 log_line_start(output, idsbuf, type);
120 (void)fwrite(data, size, 1, output);
121 newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
122 traced_data = 0;
123 break;
124 case CURLINFO_DATA_OUT:
125 case CURLINFO_DATA_IN:
126 case CURLINFO_SSL_DATA_IN:
127 case CURLINFO_SSL_DATA_OUT:
128 if(!traced_data) {
129 if(!newl)
130 log_line_start(output, idsbuf, type);
131 fprintf(output, "[%ld bytes data]\n", (long)size);
132 newl = 0;
133 traced_data = 1;
134 }
135 break;
136 default: /* nada */
137 newl = 0;
138 traced_data = 1;
139 break;
140 }
141
142 return 0;
143 }
144
145 struct transfer {
146 int idx;
147 CURL *easy;
148 char filename[128];
149 FILE *out;
150 curl_off_t recv_size;
151 curl_off_t fail_at;
152 curl_off_t pause_at;
153 curl_off_t abort_at;
154 int started;
155 int paused;
156 int resumed;
157 int done;
158 };
159
160 static size_t transfer_count = 1;
161 static struct transfer *transfers;
162 static int forbid_reuse = 0;
163
get_transfer_for_easy(CURL * easy)164 static struct transfer *get_transfer_for_easy(CURL *easy)
165 {
166 size_t i;
167 for(i = 0; i < transfer_count; ++i) {
168 if(easy == transfers[i].easy)
169 return &transfers[i];
170 }
171 return NULL;
172 }
173
my_write_cb(char * buf,size_t nitems,size_t buflen,void * userdata)174 static size_t my_write_cb(char *buf, size_t nitems, size_t buflen,
175 void *userdata)
176 {
177 struct transfer *t = userdata;
178 size_t blen = (nitems * buflen);
179 size_t nwritten;
180
181 fprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
182 t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
183 if(!t->out) {
184 curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
185 t->idx);
186 t->out = fopen(t->filename, "wb");
187 if(!t->out)
188 return 0;
189 }
190
191 if(!t->resumed &&
192 t->recv_size < t->pause_at &&
193 ((t->recv_size + (curl_off_t)blen) >= t->pause_at)) {
194 fprintf(stderr, "[t-%d] PAUSE\n", t->idx);
195 t->paused = 1;
196 return CURL_WRITEFUNC_PAUSE;
197 }
198
199 nwritten = fwrite(buf, nitems, buflen, t->out);
200 if(nwritten < blen) {
201 fprintf(stderr, "[t-%d] write failure\n", t->idx);
202 return 0;
203 }
204 t->recv_size += (curl_off_t)nwritten;
205 if(t->fail_at > 0 && t->recv_size >= t->fail_at) {
206 fprintf(stderr, "[t-%d] FAIL by write callback at %ld bytes\n",
207 t->idx, (long)t->recv_size);
208 return CURL_WRITEFUNC_ERROR;
209 }
210
211 return (size_t)nwritten;
212 }
213
my_progress_cb(void * userdata,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)214 static int my_progress_cb(void *userdata,
215 curl_off_t dltotal, curl_off_t dlnow,
216 curl_off_t ultotal, curl_off_t ulnow)
217 {
218 struct transfer *t = userdata;
219 (void)ultotal;
220 (void)ulnow;
221 (void)dltotal;
222 if(t->abort_at > 0 && dlnow >= t->abort_at) {
223 fprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes\n",
224 t->idx, (long)dlnow);
225 return 1;
226 }
227 return 0;
228 }
229
setup(CURL * hnd,const char * url,struct transfer * t,int http_version)230 static int setup(CURL *hnd, const char *url, struct transfer *t,
231 int http_version)
232 {
233 curl_easy_setopt(hnd, CURLOPT_URL, url);
234 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
235 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
236 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
237 curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
238 curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_cb);
239 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
240 curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
241 curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_cb);
242 curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
243 if(forbid_reuse)
244 curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
245
246 /* please be verbose */
247 if(verbose) {
248 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
249 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, debug_cb);
250 }
251
252 #if (CURLPIPE_MULTIPLEX > 0)
253 /* wait for pipe connection to confirm */
254 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
255 #endif
256 return 0; /* all is good */
257 }
258
usage(const char * msg)259 static void usage(const char *msg)
260 {
261 if(msg)
262 fprintf(stderr, "%s\n", msg);
263 fprintf(stderr,
264 "usage: [options] url\n"
265 " download a url with following options:\n"
266 " -a abort paused transfer\n"
267 " -m number max parallel downloads\n"
268 " -n number total downloads\n"
269 " -A number abort transfer after `number` response bytes\n"
270 " -F number fail writing response after `number` response bytes\n"
271 " -P number pause transfer after `number` response bytes\n"
272 " -V http_version (http/1.1, h2, h3) http version to use\n"
273 );
274 }
275 #endif /* !_MSC_VER */
276
277 /*
278 * Download a file over HTTP/2, take care of server push.
279 */
main(int argc,char * argv[])280 int main(int argc, char *argv[])
281 {
282 #ifndef _MSC_VER
283 CURLM *multi_handle;
284 struct CURLMsg *m;
285 const char *url;
286 size_t i, n, max_parallel = 1;
287 size_t active_transfers;
288 size_t pause_offset = 0;
289 size_t abort_offset = 0;
290 size_t fail_offset = 0;
291 int abort_paused = 0;
292 struct transfer *t;
293 int http_version = CURL_HTTP_VERSION_2_0;
294 int ch;
295
296 while((ch = getopt(argc, argv, "afhm:n:A:F:P:V:")) != -1) {
297 switch(ch) {
298 case 'h':
299 usage(NULL);
300 return 2;
301 case 'a':
302 abort_paused = 1;
303 break;
304 case 'f':
305 forbid_reuse = 1;
306 break;
307 case 'm':
308 max_parallel = (size_t)strtol(optarg, NULL, 10);
309 break;
310 case 'n':
311 transfer_count = (size_t)strtol(optarg, NULL, 10);
312 break;
313 case 'A':
314 abort_offset = (size_t)strtol(optarg, NULL, 10);
315 break;
316 case 'F':
317 fail_offset = (size_t)strtol(optarg, NULL, 10);
318 break;
319 case 'P':
320 pause_offset = (size_t)strtol(optarg, NULL, 10);
321 break;
322 case 'V': {
323 if(!strcmp("http/1.1", optarg))
324 http_version = CURL_HTTP_VERSION_1_1;
325 else if(!strcmp("h2", optarg))
326 http_version = CURL_HTTP_VERSION_2_0;
327 else if(!strcmp("h3", optarg))
328 http_version = CURL_HTTP_VERSION_3ONLY;
329 else {
330 usage("invalid http version");
331 return 1;
332 }
333 break;
334 }
335 default:
336 usage("invalid option");
337 return 1;
338 }
339 }
340 argc -= optind;
341 argv += optind;
342
343 curl_global_init(CURL_GLOBAL_DEFAULT);
344 curl_global_trace("ids,time,http/2,http/3");
345
346 if(argc != 1) {
347 usage("not enough arguments");
348 return 2;
349 }
350 url = argv[0];
351
352 transfers = calloc(transfer_count, sizeof(*transfers));
353 if(!transfers) {
354 fprintf(stderr, "error allocating transfer structs\n");
355 return 1;
356 }
357
358 multi_handle = curl_multi_init();
359 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
360
361 active_transfers = 0;
362 for(i = 0; i < transfer_count; ++i) {
363 t = &transfers[i];
364 t->idx = (int)i;
365 t->abort_at = (curl_off_t)abort_offset;
366 t->fail_at = (curl_off_t)fail_offset;
367 t->pause_at = (curl_off_t)pause_offset;
368 }
369
370 n = (max_parallel < transfer_count) ? max_parallel : transfer_count;
371 for(i = 0; i < n; ++i) {
372 t = &transfers[i];
373 t->easy = curl_easy_init();
374 if(!t->easy || setup(t->easy, url, t, http_version)) {
375 fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
376 return 1;
377 }
378 curl_multi_add_handle(multi_handle, t->easy);
379 t->started = 1;
380 ++active_transfers;
381 fprintf(stderr, "[t-%d] STARTED\n", t->idx);
382 }
383
384 do {
385 int still_running; /* keep number of running handles */
386 CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
387
388 if(still_running) {
389 /* wait for activity, timeout or "nothing" */
390 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
391 }
392
393 if(mc)
394 break;
395
396 do {
397 int msgq = 0;
398 m = curl_multi_info_read(multi_handle, &msgq);
399 if(m && (m->msg == CURLMSG_DONE)) {
400 CURL *e = m->easy_handle;
401 --active_transfers;
402 curl_multi_remove_handle(multi_handle, e);
403 t = get_transfer_for_easy(e);
404 if(t) {
405 t->done = 1;
406 fprintf(stderr, "[t-%d] FINISHED\n", t->idx);
407 }
408 else {
409 curl_easy_cleanup(e);
410 fprintf(stderr, "unknown FINISHED???\n");
411 }
412 }
413
414
415 /* nothing happening, maintenance */
416 if(abort_paused) {
417 /* abort paused transfers */
418 for(i = 0; i < transfer_count; ++i) {
419 t = &transfers[i];
420 if(!t->done && t->paused && t->easy) {
421 curl_multi_remove_handle(multi_handle, t->easy);
422 t->done = 1;
423 active_transfers--;
424 fprintf(stderr, "[t-%d] ABORTED\n", t->idx);
425 }
426 }
427 }
428 else {
429 /* resume one paused transfer */
430 for(i = 0; i < transfer_count; ++i) {
431 t = &transfers[i];
432 if(!t->done && t->paused) {
433 t->resumed = 1;
434 t->paused = 0;
435 curl_easy_pause(t->easy, CURLPAUSE_CONT);
436 fprintf(stderr, "[t-%d] RESUMED\n", t->idx);
437 break;
438 }
439 }
440 }
441
442 while(active_transfers < max_parallel) {
443 for(i = 0; i < transfer_count; ++i) {
444 t = &transfers[i];
445 if(!t->started) {
446 t->easy = curl_easy_init();
447 if(!t->easy || setup(t->easy, url, t, http_version)) {
448 fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
449 return 1;
450 }
451 curl_multi_add_handle(multi_handle, t->easy);
452 t->started = 1;
453 ++active_transfers;
454 fprintf(stderr, "[t-%d] STARTED\n", t->idx);
455 break;
456 }
457 }
458 /* all started */
459 if(i == transfer_count)
460 break;
461 }
462 } while(m);
463
464 } while(active_transfers); /* as long as we have transfers going */
465
466 for(i = 0; i < transfer_count; ++i) {
467 t = &transfers[i];
468 if(t->out) {
469 fclose(t->out);
470 t->out = NULL;
471 }
472 if(t->easy) {
473 curl_easy_cleanup(t->easy);
474 t->easy = NULL;
475 }
476 }
477 free(transfers);
478
479 curl_multi_cleanup(multi_handle);
480
481 return 0;
482 #else
483 (void)argc;
484 (void)argv;
485 fprintf(stderr, "Not supported with this compiler.\n");
486 return 1;
487 #endif /* !_MSC_VER */
488 }
489