From b2e5af4aecf21b0272b705fd996ac93b56bbab93 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 14 Feb 2019 12:19:40 +0100 Subject: examples/http2-download: cleaned up To avoid scan-build warnings and global variables. --- docs/examples/http2-download.c | 73 ++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 39 deletions(-) (limited to 'docs') diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c index b4ac7c93e..b4358fb6e 100644 --- a/docs/examples/http2-download.c +++ b/docs/examples/http2-download.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2017, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2019, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -41,22 +41,13 @@ #define CURLPIPE_MULTIPLEX 0 #endif -#define NUM_HANDLES 1000 - -static void *curl_hnd[NUM_HANDLES]; -static int num_transfers; +struct transfer { + CURL *easy; + unsigned int num; + FILE *out; +}; -/* a handle to number lookup, highly ineffective when we do many - transfers... */ -static int hnd2num(CURL *hnd) -{ - int i; - for(i = 0; i< num_transfers; i++) { - if(curl_hnd[i] == hnd) - return i; - } - return 0; /* weird, but just a fail-safe */ -} +#define NUM_HANDLES 1000 static void dump(const char *text, int num, unsigned char *ptr, size_t size, @@ -113,9 +104,10 @@ int my_trace(CURL *handle, curl_infotype type, void *userp) { const char *text; - int num = hnd2num(handle); + struct transfer *t = (struct transfer *)userp; + unsigned int num = t->num; (void)handle; /* prevent compiler warning */ - (void)userp; + switch(type) { case CURLINFO_TEXT: fprintf(stderr, "== %d Info: %s", num, data); @@ -147,17 +139,19 @@ int my_trace(CURL *handle, curl_infotype type, return 0; } -static void setup(CURL *hnd, int num) +static void setup(struct transfer *t, int num) { - FILE *out; char filename[128]; + CURL *hnd; + + hnd = t->easy = curl_easy_init(); snprintf(filename, 128, "dl-%d", num); - out = fopen(filename, "wb"); + t->out = fopen(filename, "wb"); /* write to this file */ - curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out); + curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t->out); /* set the same URL */ curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html"); @@ -165,6 +159,7 @@ static void setup(CURL *hnd, int num) /* please be verbose */ curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L); curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace); + curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, t); /* HTTP/2 please */ curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); @@ -177,37 +172,35 @@ static void setup(CURL *hnd, int num) /* wait for pipe connection to confirm */ curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L); #endif - - curl_hnd[num] = hnd; } /* - * Simply download two files over HTTP/2, using the same physical connection! + * Download many transfers over HTTP/2, using the same connection! */ int main(int argc, char **argv) { - CURL *easy[NUM_HANDLES]; + struct transfer trans[NUM_HANDLES]; CURLM *multi_handle; int i; int still_running = 0; /* keep number of running handles */ - - if(argc > 1) + int num_transfers; + if(argc > 1) { /* if given a number, do that many transfers */ num_transfers = atoi(argv[1]); - - if(!num_transfers || (num_transfers > NUM_HANDLES)) - num_transfers = 3; /* a suitable low default */ + if((num_transfers < 1) || (num_transfers > NUM_HANDLES)) + num_transfers = 3; /* a suitable low default */ + } + else + num_transfers = 3; /* suitable default */ /* init a multi stack */ multi_handle = curl_multi_init(); - for(i = 0; i