examples: check more errors, fix cleanups, scope variables

Inspired by Joshua's report on examples.

Closes #19055
This commit is contained in:
Viktor Szakats 2025-10-13 22:57:01 +02:00
parent 61dcb56743
commit 64ed2ea196
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
42 changed files with 874 additions and 785 deletions

View file

@ -68,8 +68,7 @@
#ifdef _MSC_VER
#define gettimeofday(a, b) my_gettimeofday((a), (b))
static
int my_gettimeofday(struct timeval *tp, void *tzp)
static int my_gettimeofday(struct timeval *tp, void *tzp)
{
(void)tzp;
if(tp) {
@ -89,14 +88,14 @@ int my_gettimeofday(struct timeval *tp, void *tzp)
struct input {
FILE *in;
FILE *out;
size_t bytes_read; /* count up */
CURL *hnd;
int num;
};
static
void dump(const char *text, int num, unsigned char *ptr, size_t size,
char nohex)
static void dump(const char *text, int num, unsigned char *ptr, size_t size,
char nohex)
{
size_t i;
size_t c;
@ -142,10 +141,8 @@ void dump(const char *text, int num, unsigned char *ptr, size_t size,
}
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
static int my_trace(CURL *handle, curl_infotype type, char *data,
size_t size, void *userp)
{
char timebuf[60];
const char *text;
@ -208,7 +205,6 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
static int setup(struct input *i, int num, const char *upload)
{
FILE *out;
char url[256];
char filename[128];
struct stat file_info;
@ -219,8 +215,8 @@ static int setup(struct input *i, int num, const char *upload)
i->num = num;
snprintf(filename, sizeof(filename), "dl-%d", num);
out = fopen(filename, "wb");
if(!out) {
i->out = fopen(filename, "wb");
if(!i->out) {
fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
strerror(errno));
return 1;
@ -232,7 +228,8 @@ static int setup(struct input *i, int num, const char *upload)
if(!i->in) {
fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
strerror(errno));
fclose(out);
fclose(i->out);
i->out = NULL;
return 1;
}
@ -244,46 +241,49 @@ static int setup(struct input *i, int num, const char *upload)
#endif
fprintf(stderr, "error: could not stat file %s: %s\n", upload,
strerror(errno));
fclose(out);
fclose(i->out);
i->out = NULL;
return 1;
}
uploadsize = file_info.st_size;
hnd = i->hnd = curl_easy_init();
if(hnd) {
/* write to this file */
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
/* write to this file */
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, i->out);
/* we want to use our own read function */
curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
/* read from this file */
curl_easy_setopt(hnd, CURLOPT_READDATA, i);
/* provide the size of the upload */
curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
/* we want to use our own read function */
curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
/* read from this file */
curl_easy_setopt(hnd, CURLOPT_READDATA, i);
/* provide the size of the upload */
curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
/* send in the URL to store the upload as */
curl_easy_setopt(hnd, CURLOPT_URL, url);
/* send in the URL to store the upload as */
curl_easy_setopt(hnd, CURLOPT_URL, url);
/* upload please */
curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
/* upload please */
curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
/* please be verbose */
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i);
/* please be verbose */
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i);
/* HTTP/2 please */
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* HTTP/2 please */
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
#if (CURLPIPE_MULTIPLEX > 0)
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
#endif
}
return 0;
}
@ -296,7 +296,6 @@ int main(int argc, char **argv)
struct input trans[NUM_HANDLES];
CURLM *multi_handle;
int i;
int still_running = 0; /* keep number of running handles */
const char *filename = "index.html";
int num_transfers;
@ -318,41 +317,54 @@ int main(int argc, char **argv)
if(res)
return (int)res;
memset(trans, 0, sizeof(trans));
/* init a multi stack */
multi_handle = curl_multi_init();
if(multi_handle) {
for(i = 0; i < num_transfers; i++) {
if(setup(&trans[i], i, filename)) {
curl_global_cleanup();
return 1;
int still_running = 0; /* keep number of running handles */
for(i = 0; i < num_transfers; i++) {
if(setup(&trans[i], i, filename)) {
curl_global_cleanup();
return 1;
}
/* add the individual transfer */
curl_multi_add_handle(multi_handle, trans[i].hnd);
}
/* add the individual transfer */
curl_multi_add_handle(multi_handle, trans[i].hnd);
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
/* We do HTTP/2 so let's stick to one connection per host */
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
do {
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
for(i = 0; i < num_transfers; i++)
curl_multi_remove_handle(multi_handle, trans[i].hnd);
curl_multi_cleanup(multi_handle);
}
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
/* We do HTTP/2 so let's stick to one connection per host */
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
do {
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
if(mc)
break;
} while(still_running);
curl_multi_cleanup(multi_handle);
for(i = 0; i < num_transfers; i++) {
curl_multi_remove_handle(multi_handle, trans[i].hnd);
curl_easy_cleanup(trans[i].hnd);
if(trans[i].in)
fclose(trans[i].in);
if(trans[i].out)
fclose(trans[i].out);
}
curl_global_cleanup();