From 0824bae8b05727038dff702e735752c7ae963fbf Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 22 Feb 2026 15:17:18 +0100 Subject: [PATCH] config2setopts: split into smaller functions Reduces complexity from 63 to 45 Closes #20676 --- src/config2setopts.c | 277 ++++++++++++++++++++++++------------------- 1 file changed, 152 insertions(+), 125 deletions(-) diff --git a/src/config2setopts.c b/src/config2setopts.c index ecc0f7d164..a7d0a4e214 100644 --- a/src/config2setopts.c +++ b/src/config2setopts.c @@ -178,10 +178,14 @@ static CURLcode url_proto_and_rewrite(char **url, return result; } -static CURLcode ssh_setopts(struct OperationConfig *config, CURL *curl) +static CURLcode ssh_setopts(struct OperationConfig *config, CURL *curl, + const char *use_proto) { CURLcode result; + if(use_proto != proto_scp && use_proto != proto_sftp) + return CURLE_OK; + /* SSH and SSL private key uses same command-line option */ MY_SETOPT_STR(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key); MY_SETOPT_STR(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey); @@ -270,7 +274,7 @@ static long tlsversion(unsigned char mintls, } /* only called if libcurl supports TLS */ -static CURLcode ssl_setopts(struct OperationConfig *config, CURL *curl) +static CURLcode ssl_ca_setopts(struct OperationConfig *config, CURL *curl) { CURLcode result = CURLE_OK; @@ -322,6 +326,13 @@ static CURLcode ssl_setopts(struct OperationConfig *config, CURL *curl) } } #endif + return result; +} + +/* only called if libcurl supports TLS */ +static CURLcode ssl_setopts(struct OperationConfig *config, CURL *curl) +{ + CURLcode result = CURLE_OK; if(config->crlfile) MY_SETOPT_STR(curl, CURLOPT_CRLFILE, config->crlfile); @@ -476,57 +487,6 @@ static CURLcode ssl_setopts(struct OperationConfig *config, CURL *curl) return CURLE_OK; } -/* only called for HTTP transfers */ -static CURLcode http_setopts(struct OperationConfig *config, CURL *curl) -{ - CURLcode result = CURLE_OK; - long postRedir = 0; - - my_setopt_long(curl, CURLOPT_FOLLOWLOCATION, config->followlocation); - my_setopt_long(curl, CURLOPT_UNRESTRICTED_AUTH, config->unrestricted_auth); -#ifndef CURL_DISABLE_AWS - MY_SETOPT_STR(curl, CURLOPT_AWS_SIGV4, config->aws_sigv4); -#endif - my_setopt_long(curl, CURLOPT_AUTOREFERER, config->autoreferer); - - if(config->proxyheaders) { - my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders); - } - - my_setopt_long(curl, CURLOPT_MAXREDIRS, config->maxredirs); - - if(config->httpversion) - my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion); - - if(config->post301) - postRedir |= CURL_REDIR_POST_301; - if(config->post302) - postRedir |= CURL_REDIR_POST_302; - if(config->post303) - postRedir |= CURL_REDIR_POST_303; - my_setopt_long(curl, CURLOPT_POSTREDIR, postRedir); - - if(config->encoding) - MY_SETOPT_STR(curl, CURLOPT_ACCEPT_ENCODING, ""); - - if(config->tr_encoding) - my_setopt_long(curl, CURLOPT_TRANSFER_ENCODING, 1); - - my_setopt_long(curl, CURLOPT_HTTP09_ALLOWED, config->http09_allowed); - - if(config->altsvc) - MY_SETOPT_STR(curl, CURLOPT_ALTSVC, config->altsvc); - - if(config->hsts) - MY_SETOPT_STR(curl, CURLOPT_HSTS, config->hsts); - - if(config->expect100timeout_ms > 0) - my_setopt_long(curl, CURLOPT_EXPECT_100_TIMEOUT_MS, - config->expect100timeout_ms); - - return result; -} - static CURLcode cookie_setopts(struct OperationConfig *config, CURL *curl) { CURLcode result = CURLE_OK; @@ -571,6 +531,70 @@ static CURLcode cookie_setopts(struct OperationConfig *config, CURL *curl) return result; } +/* only for HTTP transfers */ +static CURLcode http_setopts(struct OperationConfig *config, CURL *curl, + const char *use_proto) +{ + CURLcode result = CURLE_OK; + long postRedir = 0; + + if(use_proto != proto_http && use_proto != proto_https) + return CURLE_OK; + + my_setopt_long(curl, CURLOPT_FOLLOWLOCATION, config->followlocation); + my_setopt_long(curl, CURLOPT_UNRESTRICTED_AUTH, config->unrestricted_auth); +#ifndef CURL_DISABLE_AWS + MY_SETOPT_STR(curl, CURLOPT_AWS_SIGV4, config->aws_sigv4); +#endif + my_setopt_long(curl, CURLOPT_AUTOREFERER, config->autoreferer); + + if(config->proxyheaders) { + my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders); + } + + my_setopt_long(curl, CURLOPT_MAXREDIRS, config->maxredirs); + + if(config->httpversion) + my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion); + + if(config->post301) + postRedir |= CURL_REDIR_POST_301; + if(config->post302) + postRedir |= CURL_REDIR_POST_302; + if(config->post303) + postRedir |= CURL_REDIR_POST_303; + my_setopt_long(curl, CURLOPT_POSTREDIR, postRedir); + + if(config->encoding) + MY_SETOPT_STR(curl, CURLOPT_ACCEPT_ENCODING, ""); + + if(config->tr_encoding) + my_setopt_long(curl, CURLOPT_TRANSFER_ENCODING, 1); + + my_setopt_long(curl, CURLOPT_HTTP09_ALLOWED, config->http09_allowed); + + if(config->altsvc) + MY_SETOPT_STR(curl, CURLOPT_ALTSVC, config->altsvc); + + if(config->hsts) + MY_SETOPT_STR(curl, CURLOPT_HSTS, config->hsts); + + if(config->expect100timeout_ms > 0) + my_setopt_long(curl, CURLOPT_EXPECT_100_TIMEOUT_MS, + config->expect100timeout_ms); + + if(!result) + result = cookie_setopts(config, curl); + if(result) + return result; + /* Enable header separation when using a proxy with HTTPS or proxytunnel + * to prevent --header content from leaking into CONNECT requests */ + if((config->proxy || config->proxyheaders) && + (use_proto == proto_https || config->proxytunnel)) + my_setopt_long(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE); + return result; +} + static void tcp_setopts(struct OperationConfig *config, CURL *curl) { if(!config->tcp_nodelay) @@ -596,9 +620,14 @@ static void tcp_setopts(struct OperationConfig *config, CURL *curl) my_setopt_long(curl, CURLOPT_TCP_KEEPALIVE, 0); } -static CURLcode ftp_setopts(struct OperationConfig *config, CURL *curl) +static CURLcode ftp_setopts(struct OperationConfig *config, CURL *curl, + const char *use_proto) { CURLcode result; + + if(use_proto != proto_ftp && use_proto != proto_ftps) + return CURLE_OK; + MY_SETOPT_STR(curl, CURLOPT_FTPPORT, config->ftpport); if(config->disable_epsv) @@ -743,6 +772,61 @@ static CURLcode tls_srp_setopts(struct OperationConfig *config, CURL *curl) return result; } +static CURLcode setopt_post(struct OperationConfig *config, CURL *curl) +{ + CURLcode result = CURLE_OK; + switch(config->httpreq) { + case TOOL_HTTPREQ_SIMPLEPOST: + if(config->resume_from) { + errorf("cannot mix --continue-at with --data"); + result = CURLE_FAILED_INIT; + } + else { + MY_SETOPT_STR(curl, CURLOPT_POSTFIELDS, + curlx_dyn_ptr(&config->postdata)); + my_setopt_offt(curl, CURLOPT_POSTFIELDSIZE_LARGE, + curlx_dyn_len(&config->postdata)); + } + break; + case TOOL_HTTPREQ_MIMEPOST: + /* free previous remainders */ + curl_mime_free(config->mimepost); + config->mimepost = NULL; + if(config->resume_from) { + errorf("cannot mix --continue-at with --form"); + result = CURLE_FAILED_INIT; + } + else { + result = tool2curlmime(curl, config->mimeroot, &config->mimepost); + if(!result) + result = my_setopt_mimepost(curl, CURLOPT_MIMEPOST, config->mimepost); + } + break; + default: + break; + } + return result; +} + +static void buffersize(struct OperationConfig *config, CURL *curl) +{ +#ifdef DEBUGBUILD + char *env = getenv("CURL_BUFFERSIZE"); + if(env) { + curl_off_t num; + const char *p = env; + if(!curlx_str_number(&p, &num, LONG_MAX)) + my_setopt_long(curl, CURLOPT_BUFFERSIZE, (long)num); + return; + } +#endif + if(config->recvpersecond && (config->recvpersecond < BUFFER_SIZE)) + /* use a smaller sized buffer for better sleeps */ + my_setopt_long(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond); + else + my_setopt_long(curl, CURLOPT_BUFFERSIZE, BUFFER_SIZE); +} + CURLcode config2setopts(struct OperationConfig *config, struct per_transfer *per, CURL *curl, @@ -770,23 +854,7 @@ CURLcode config2setopts(struct OperationConfig *config, gen_trace_setopts(config, curl); - { -#ifdef DEBUGBUILD - char *env = getenv("CURL_BUFFERSIZE"); - if(env) { - curl_off_t num; - const char *p = env; - if(!curlx_str_number(&p, &num, LONG_MAX)) - my_setopt_long(curl, CURLOPT_BUFFERSIZE, (long)num); - } - else -#endif - if(config->recvpersecond && (config->recvpersecond < BUFFER_SIZE)) - /* use a smaller sized buffer for better sleeps */ - my_setopt_long(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond); - else - my_setopt_long(curl, CURLOPT_BUFFERSIZE, BUFFER_SIZE); - } + buffersize(config, curl); MY_SETOPT_STR(curl, CURLOPT_URL, per->url); my_setopt_long(curl, CURLOPT_NOPROGRESS, @@ -821,36 +889,7 @@ CURLcode config2setopts(struct OperationConfig *config, my_setopt_ptr(curl, CURLOPT_ERRORBUFFER, per->errorbuffer); my_setopt_long(curl, CURLOPT_TIMEOUT_MS, config->timeout_ms); - switch(config->httpreq) { - case TOOL_HTTPREQ_SIMPLEPOST: - if(config->resume_from) { - errorf("cannot mix --continue-at with --data"); - result = CURLE_FAILED_INIT; - } - else { - MY_SETOPT_STR(curl, CURLOPT_POSTFIELDS, - curlx_dyn_ptr(&config->postdata)); - my_setopt_offt(curl, CURLOPT_POSTFIELDSIZE_LARGE, - curlx_dyn_len(&config->postdata)); - } - break; - case TOOL_HTTPREQ_MIMEPOST: - /* free previous remainders */ - curl_mime_free(config->mimepost); - config->mimepost = NULL; - if(config->resume_from) { - errorf("cannot mix --continue-at with --form"); - result = CURLE_FAILED_INIT; - } - else { - result = tool2curlmime(curl, config->mimeroot, &config->mimepost); - if(!result) - result = my_setopt_mimepost(curl, CURLOPT_MIMEPOST, config->mimepost); - } - break; - default: - break; - } + result = setopt_post(config, curl); if(result) return result; @@ -868,24 +907,11 @@ CURLcode config2setopts(struct OperationConfig *config, config->useragent : CURL_NAME "/" CURL_VERSION); } - if(use_proto == proto_http || use_proto == proto_https) { - result = http_setopts(config, curl); - if(!result) - result = cookie_setopts(config, curl); - if(result) - return result; - /* Enable header separation when using a proxy with HTTPS or proxytunnel - * to prevent --header content from leaking into CONNECT requests */ - if((config->proxy || config->proxyheaders) && - (use_proto == proto_https || config->proxytunnel)) - my_setopt_long(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE); - } - - if(use_proto == proto_ftp || use_proto == proto_ftps) { - result = ftp_setopts(config, curl); - if(result) - return result; - } + result = http_setopts(config, curl, use_proto); + if(!result) + result = ftp_setopts(config, curl, use_proto); + if(result) + return result; my_setopt_long(curl, CURLOPT_LOW_SPEED_LIMIT, config->low_speed_limit); my_setopt_long(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time); @@ -900,13 +926,14 @@ CURLcode config2setopts(struct OperationConfig *config, MY_SETOPT_STR(curl, CURLOPT_KEYPASSWD, config->key_passwd); MY_SETOPT_STR(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd); - if(use_proto == proto_scp || use_proto == proto_sftp) { - result = ssh_setopts(config, curl); - if(setopt_bad(result)) - return result; - } + result = ssh_setopts(config, curl, use_proto); + if(setopt_bad(result)) + return result; + if(feature_ssl) { - result = ssl_setopts(config, curl); + result = ssl_ca_setopts(config, curl); + if(!result) + result = ssl_setopts(config, curl); if(setopt_bad(result)) return result; }