xref: /curl/tests/http/clients/hx-download.c (revision 962097b8)
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,struct curl_slist * host,CURLSH * share,int use_earlydata)230 static int setup(CURL *hnd, const char *url, struct transfer *t,
231                  int http_version, struct curl_slist *host,
232                  CURLSH *share, int use_earlydata)
233 {
234   curl_easy_setopt(hnd, CURLOPT_SHARE, share);
235   curl_easy_setopt(hnd, CURLOPT_URL, url);
236   curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
237   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
238   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
239   curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
240   curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_cb);
241   curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
242   curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
243   curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_cb);
244   curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
245   if(use_earlydata)
246     curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_EARLYDATA);
247   if(forbid_reuse)
248     curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
249   if(host)
250     curl_easy_setopt(hnd, CURLOPT_RESOLVE, host);
251 
252   /* please be verbose */
253   if(verbose) {
254     curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
255     curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, debug_cb);
256   }
257 
258 #if (CURLPIPE_MULTIPLEX > 0)
259   /* wait for pipe connection to confirm */
260   curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
261 #endif
262   return 0; /* all is good */
263 }
264 
usage(const char * msg)265 static void usage(const char *msg)
266 {
267   if(msg)
268     fprintf(stderr, "%s\n", msg);
269   fprintf(stderr,
270     "usage: [options] url\n"
271     "  download a url with following options:\n"
272     "  -a         abort paused transfer\n"
273     "  -m number  max parallel downloads\n"
274     "  -e         use TLS early data when possible\n"
275     "  -f         forbid connection reuse\n"
276     "  -n number  total downloads\n");
277   fprintf(stderr,
278     "  -A number  abort transfer after `number` response bytes\n"
279     "  -F number  fail writing response after `number` response bytes\n"
280     "  -P number  pause transfer after `number` response bytes\n"
281     "  -r <host>:<port>:<addr>  resolve information\n"
282     "  -V http_version (http/1.1, h2, h3) http version to use\n"
283   );
284 }
285 #endif /* !_MSC_VER */
286 
287 /*
288  * Download a file over HTTP/2, take care of server push.
289  */
main(int argc,char * argv[])290 int main(int argc, char *argv[])
291 {
292 #ifndef _MSC_VER
293   CURLM *multi_handle;
294   struct CURLMsg *m;
295   CURLSH *share;
296   const char *url;
297   size_t i, n, max_parallel = 1;
298   size_t active_transfers;
299   size_t pause_offset = 0;
300   size_t abort_offset = 0;
301   size_t fail_offset = 0;
302   int abort_paused = 0, use_earlydata = 0;
303   struct transfer *t;
304   int http_version = CURL_HTTP_VERSION_2_0;
305   int ch;
306   struct curl_slist *host = NULL;
307   const char *resolve = NULL;
308 
309   while((ch = getopt(argc, argv, "aefhm:n:A:F:P:r:V:")) != -1) {
310     switch(ch) {
311     case 'h':
312       usage(NULL);
313       return 2;
314     case 'a':
315       abort_paused = 1;
316       break;
317     case 'e':
318       use_earlydata = 1;
319       break;
320     case 'f':
321       forbid_reuse = 1;
322       break;
323     case 'm':
324       max_parallel = (size_t)strtol(optarg, NULL, 10);
325       break;
326     case 'n':
327       transfer_count = (size_t)strtol(optarg, NULL, 10);
328       break;
329     case 'A':
330       abort_offset = (size_t)strtol(optarg, NULL, 10);
331       break;
332     case 'F':
333       fail_offset = (size_t)strtol(optarg, NULL, 10);
334       break;
335     case 'P':
336       pause_offset = (size_t)strtol(optarg, NULL, 10);
337       break;
338     case 'r':
339       resolve = optarg;
340       break;
341     case 'V': {
342       if(!strcmp("http/1.1", optarg))
343         http_version = CURL_HTTP_VERSION_1_1;
344       else if(!strcmp("h2", optarg))
345         http_version = CURL_HTTP_VERSION_2_0;
346       else if(!strcmp("h3", optarg))
347         http_version = CURL_HTTP_VERSION_3ONLY;
348       else {
349         usage("invalid http version");
350         return 1;
351       }
352       break;
353     }
354     default:
355      usage("invalid option");
356      return 1;
357     }
358   }
359   argc -= optind;
360   argv += optind;
361 
362   curl_global_init(CURL_GLOBAL_DEFAULT);
363   curl_global_trace("ids,time,http/2,http/3");
364 
365   if(argc != 1) {
366     usage("not enough arguments");
367     return 2;
368   }
369   url = argv[0];
370 
371   if(resolve)
372     host = curl_slist_append(NULL, resolve);
373 
374   share = curl_share_init();
375   if(!share) {
376     fprintf(stderr, "error allocating share\n");
377     return 1;
378   }
379   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
380   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
381   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
382   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
383   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
384   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
385 
386   transfers = calloc(transfer_count, sizeof(*transfers));
387   if(!transfers) {
388     fprintf(stderr, "error allocating transfer structs\n");
389     return 1;
390   }
391 
392   multi_handle = curl_multi_init();
393   curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
394 
395   active_transfers = 0;
396   for(i = 0; i < transfer_count; ++i) {
397     t = &transfers[i];
398     t->idx = (int)i;
399     t->abort_at = (curl_off_t)abort_offset;
400     t->fail_at = (curl_off_t)fail_offset;
401     t->pause_at = (curl_off_t)pause_offset;
402   }
403 
404   n = (max_parallel < transfer_count) ? max_parallel : transfer_count;
405   for(i = 0; i < n; ++i) {
406     t = &transfers[i];
407     t->easy = curl_easy_init();
408     if(!t->easy ||
409        setup(t->easy, url, t, http_version, host, share, use_earlydata)) {
410       fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
411       return 1;
412     }
413     curl_multi_add_handle(multi_handle, t->easy);
414     t->started = 1;
415     ++active_transfers;
416     fprintf(stderr, "[t-%d] STARTED\n", t->idx);
417   }
418 
419   do {
420     int still_running; /* keep number of running handles */
421     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
422 
423     if(still_running) {
424       /* wait for activity, timeout or "nothing" */
425       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
426     }
427 
428     if(mc)
429       break;
430 
431     do {
432       int msgq = 0;
433       m = curl_multi_info_read(multi_handle, &msgq);
434       if(m && (m->msg == CURLMSG_DONE)) {
435         CURL *e = m->easy_handle;
436         --active_transfers;
437         curl_multi_remove_handle(multi_handle, e);
438         t = get_transfer_for_easy(e);
439         if(t) {
440           t->done = 1;
441           fprintf(stderr, "[t-%d] FINISHED\n", t->idx);
442           if(use_earlydata) {
443             curl_off_t sent;
444             curl_easy_getinfo(e, CURLINFO_EARLYDATA_SENT_T, &sent);
445             fprintf(stderr, "[t-%d] EarlyData: %ld\n", t->idx, (long)sent);
446           }
447         }
448         else {
449           curl_easy_cleanup(e);
450           fprintf(stderr, "unknown FINISHED???\n");
451         }
452       }
453 
454 
455       /* nothing happening, maintenance */
456       if(abort_paused) {
457         /* abort paused transfers */
458         for(i = 0; i < transfer_count; ++i) {
459           t = &transfers[i];
460           if(!t->done && t->paused && t->easy) {
461             curl_multi_remove_handle(multi_handle, t->easy);
462             t->done = 1;
463             active_transfers--;
464             fprintf(stderr, "[t-%d] ABORTED\n", t->idx);
465           }
466         }
467       }
468       else {
469         /* resume one paused transfer */
470         for(i = 0; i < transfer_count; ++i) {
471           t = &transfers[i];
472           if(!t->done && t->paused) {
473             t->resumed = 1;
474             t->paused = 0;
475             curl_easy_pause(t->easy, CURLPAUSE_CONT);
476             fprintf(stderr, "[t-%d] RESUMED\n", t->idx);
477             break;
478           }
479         }
480       }
481 
482       while(active_transfers < max_parallel) {
483         for(i = 0; i < transfer_count; ++i) {
484           t = &transfers[i];
485           if(!t->started) {
486             t->easy = curl_easy_init();
487             if(!t->easy ||
488                setup(t->easy, url, t, http_version, host, share,
489                      use_earlydata)) {
490               fprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
491               return 1;
492             }
493             curl_multi_add_handle(multi_handle, t->easy);
494             t->started = 1;
495             ++active_transfers;
496             fprintf(stderr, "[t-%d] STARTED\n", t->idx);
497             break;
498           }
499         }
500         /* all started */
501         if(i == transfer_count)
502           break;
503       }
504     } while(m);
505 
506   } while(active_transfers); /* as long as we have transfers going */
507 
508   curl_multi_cleanup(multi_handle);
509 
510   for(i = 0; i < transfer_count; ++i) {
511     t = &transfers[i];
512     if(t->out) {
513       fclose(t->out);
514       t->out = NULL;
515     }
516     if(t->easy) {
517       curl_easy_cleanup(t->easy);
518       t->easy = NULL;
519     }
520   }
521   free(transfers);
522 
523   curl_share_cleanup(share);
524 
525   return 0;
526 #else
527   (void)argc;
528   (void)argv;
529   fprintf(stderr, "Not supported with this compiler.\n");
530   return 1;
531 #endif /* !_MSC_VER */
532 }
533