config2setopt.c: refactor config2setopts

Introduce sub functions

Closes #22215
This commit is contained in:
Daniel Stenberg 2026-06-29 12:02:14 +02:00
parent 7791b1629b
commit 8aeef462e3
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
9 changed files with 172 additions and 109 deletions

View file

@ -46,6 +46,14 @@
#define BUFFER_SIZE 102400L
/* return TRUE if the error code is "lethal" */
static bool setopt_bad(CURLcode result)
{
return result &&
(result != CURLE_NOT_BUILT_IN) &&
(result != CURLE_UNKNOWN_OPTION);
}
#ifdef IP_TOS
static int get_address_family(curl_socket_t sockfd)
{
@ -773,6 +781,8 @@ static CURLcode proxy_setopts(struct OperationConfig *config, CURL *curl)
if(config->haproxy_clientip)
MY_SETOPT_STR(curl, CURLOPT_HAPROXY_CLIENT_IP, config->haproxy_clientip);
MY_SETOPT_STR(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd);
return result;
}
@ -852,54 +862,12 @@ static void buffersize(struct OperationConfig *config, CURL *curl)
my_setopt_long(curl, CURLOPT_BUFFERSIZE, BUFFER_SIZE);
}
CURLcode config2setopts(struct OperationConfig *config,
struct per_transfer *per,
CURL *curl,
CURLSH *share)
static CURLcode credentials_and_headers_setopts(struct OperationConfig *config,
CURL *curl)
{
const char *use_proto;
CURLcode result = url_proto_and_rewrite(&per->url, config, &use_proto);
CURLcode result = CURLE_OK;
/* Avoid having this setopt added to the --libcurl source output. */
if(!result)
result = curl_easy_setopt(curl, CURLOPT_SHARE, share);
if(result)
return result;
if(TRUE
#ifdef DEBUGBUILD
&& getenv("CURL_QUICK_EXIT")
#endif
) {
/* QUICK_EXIT allows for running threads to be detached and not
* joined. Preferably in non-debug runs. */
result = curl_easy_setopt(curl, CURLOPT_QUICK_EXIT, 1L);
if(result)
return result;
}
gen_trace_setopts(config, curl);
buffersize(config, curl);
MY_SETOPT_STR(curl, CURLOPT_URL, per->url);
my_setopt_long(curl, CURLOPT_NOPROGRESS,
global->noprogress || global->silent);
/* call after the line above. It may override CURLOPT_NOPROGRESS */
gen_cb_setopts(config, per, curl);
my_setopt_long(curl, CURLOPT_NOBODY, config->no_body);
MY_SETOPT_STR(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
result = proxy_setopts(config, curl);
if(setopt_bad(result) || config->synthetic_error)
return result;
my_setopt_long(curl, CURLOPT_FAILONERROR, config->fail == FAIL_WO_BODY);
MY_SETOPT_STR(curl, CURLOPT_REQUEST_TARGET, config->request_target);
my_setopt_long(curl, CURLOPT_UPLOAD, !!per->uploadfile);
my_setopt_long(curl, CURLOPT_DIRLISTONLY, config->dirlistonly);
my_setopt_long(curl, CURLOPT_APPEND, config->ftp_append);
if(config->netrc_opt)
my_setopt_enum(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
else if(config->netrc || config->netrc_file)
@ -908,19 +876,8 @@ CURLcode config2setopts(struct OperationConfig *config,
my_setopt_enum(curl, CURLOPT_NETRC, CURL_NETRC_IGNORED);
MY_SETOPT_STR(curl, CURLOPT_NETRC_FILE, config->netrc_file);
my_setopt_long(curl, CURLOPT_TRANSFERTEXT, config->use_ascii);
MY_SETOPT_STR(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
MY_SETOPT_STR(curl, CURLOPT_USERPWD, config->userpwd);
MY_SETOPT_STR(curl, CURLOPT_RANGE, config->range);
my_setopt_ptr(curl, CURLOPT_ERRORBUFFER, per->errorbuffer);
my_setopt_long(curl, CURLOPT_TIMEOUT_MS, config->timeout_ms);
result = setopt_post(config, curl);
if(result)
return result;
if(config->mime_options)
my_setopt_long(curl, CURLOPT_MIME_OPTIONS, config->mime_options);
if(config->authtype)
my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, config->authtype);
@ -933,12 +890,35 @@ CURLcode config2setopts(struct OperationConfig *config,
config->useragent : CURL_NAME "/" CURL_VERSION);
}
result = http_setopts(config, curl, use_proto);
if(!result)
result = ftp_setopts(config, curl, use_proto);
MY_SETOPT_STR(curl, CURLOPT_KEYPASSWD, config->key_passwd);
return result;
}
static CURLcode transfer_setopts(struct OperationConfig *config,
struct per_transfer *per,
CURL *curl)
{
CURLcode result = CURLE_OK;
my_setopt_long(curl, CURLOPT_NOBODY, config->no_body);
my_setopt_long(curl, CURLOPT_FAILONERROR, config->fail == FAIL_WO_BODY);
MY_SETOPT_STR(curl, CURLOPT_REQUEST_TARGET, config->request_target);
my_setopt_long(curl, CURLOPT_UPLOAD, !!per->uploadfile);
my_setopt_long(curl, CURLOPT_DIRLISTONLY, config->dirlistonly);
my_setopt_long(curl, CURLOPT_APPEND, config->ftp_append);
my_setopt_long(curl, CURLOPT_TRANSFERTEXT, config->use_ascii);
MY_SETOPT_STR(curl, CURLOPT_RANGE, config->range);
my_setopt_ptr(curl, CURLOPT_ERRORBUFFER, per->errorbuffer);
my_setopt_long(curl, CURLOPT_TIMEOUT_MS, config->timeout_ms);
result = setopt_post(config, curl);
if(result)
return result;
if(config->mime_options)
my_setopt_long(curl, CURLOPT_MIME_OPTIONS, config->mime_options);
my_setopt_long(curl, CURLOPT_LOW_SPEED_LIMIT, config->low_speed_limit);
my_setopt_long(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
my_setopt_offt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, config->sendpersecond);
@ -949,8 +929,39 @@ CURLcode config2setopts(struct OperationConfig *config,
else
my_setopt_offt(curl, CURLOPT_RESUME_FROM_LARGE, 0);
MY_SETOPT_STR(curl, CURLOPT_KEYPASSWD, config->key_passwd);
MY_SETOPT_STR(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd);
if(config->path_as_is)
my_setopt_long(curl, CURLOPT_PATH_AS_IS, 1);
if(config->no_body || config->remote_time)
my_setopt_long(curl, CURLOPT_FILETIME, 1);
my_setopt_long(curl, CURLOPT_CRLF, config->crlf);
my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
my_setopt_enum(curl, CURLOPT_TIMECONDITION, config->timecond);
my_setopt_offt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
MY_SETOPT_STR(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
customrequest_helper(config->httpreq, config->customrequest);
return result;
}
static CURLcode protocol_setopts(struct OperationConfig *config,
struct per_transfer *per,
CURL *curl,
const char *use_proto)
{
CURLcode result = CURLE_OK;
#ifndef DEBUGBUILD
(void)per;
#endif
result = http_setopts(config, curl, use_proto);
if(!result)
result = ftp_setopts(config, curl, use_proto);
if(result)
return result;
result = ssh_setopts(config, curl, use_proto);
if(setopt_bad(result))
@ -971,23 +982,21 @@ CURLcode config2setopts(struct OperationConfig *config,
#endif
}
if(config->path_as_is)
my_setopt_long(curl, CURLOPT_PATH_AS_IS, 1);
if(feature_tls_srp) {
result = tls_srp_setopts(config, curl);
if(setopt_bad(result))
return result;
}
if(config->no_body || config->remote_time)
/* no body or use remote time */
my_setopt_long(curl, CURLOPT_FILETIME, 1);
return result;
}
my_setopt_long(curl, CURLOPT_CRLF, config->crlf);
my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
static CURLcode dns_and_network_setopts(struct OperationConfig *config,
struct per_transfer *per,
CURL *curl)
{
CURLcode result = CURLE_OK;
my_setopt_enum(curl, CURLOPT_TIMECONDITION, config->timecond);
my_setopt_offt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
MY_SETOPT_STR(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
customrequest_helper(config->httpreq, config->customrequest);
my_setopt_ptr(curl, CURLOPT_STDERR, tool_stderr);
MY_SETOPT_STR(curl, CURLOPT_INTERFACE, config->iface);
progressbarinit(&per->progressbar, config);
MY_SETOPT_STR(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
@ -1024,34 +1033,44 @@ CURLcode config2setopts(struct OperationConfig *config,
if(config->tftp_blksize && proto_tftp)
my_setopt_long(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
return result;
}
static CURLcode mail_and_sasl_setopts(struct OperationConfig *config,
CURL *curl)
{
CURLcode result = CURLE_OK;
MY_SETOPT_STR(curl, CURLOPT_MAIL_FROM, config->mail_from);
my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
my_setopt_long(curl, CURLOPT_MAIL_RCPT_ALLOWFAILS,
config->mail_rcpt_allowfails);
MY_SETOPT_STR(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
MY_SETOPT_STR(curl, CURLOPT_SASL_AUTHZID, config->sasl_authzid);
my_setopt_long(curl, CURLOPT_SASL_IR, config->sasl_ir);
if(config->create_file_mode)
my_setopt_long(curl, CURLOPT_NEW_FILE_PERMS, config->create_file_mode);
return result;
}
static CURLcode misc_setopts(struct OperationConfig *config, CURL *curl)
{
CURLcode result = CURLE_OK;
my_setopt_ptr(curl, CURLOPT_STDERR, tool_stderr);
my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
if(config->gssapi_delegation)
my_setopt_long(curl, CURLOPT_GSSAPI_DELEGATION, config->gssapi_delegation);
if(config->proto_present)
MY_SETOPT_STR(curl, CURLOPT_PROTOCOLS_STR, config->proto_str);
if(config->proto_redir_present)
MY_SETOPT_STR(curl, CURLOPT_REDIR_PROTOCOLS_STR, config->proto_redir_str);
my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
if(feature_tls_srp) {
result = tls_srp_setopts(config, curl);
if(setopt_bad(result))
return result;
}
if(config->gssapi_delegation)
my_setopt_long(curl, CURLOPT_GSSAPI_DELEGATION, config->gssapi_delegation);
MY_SETOPT_STR(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
MY_SETOPT_STR(curl, CURLOPT_SASL_AUTHZID, config->sasl_authzid);
my_setopt_long(curl, CURLOPT_SASL_IR, config->sasl_ir);
if(config->unix_socket_path) {
if(config->abstract_unix_socket)
MY_SETOPT_STR(curl, CURLOPT_ABSTRACT_UNIX_SOCKET,
@ -1087,6 +1106,61 @@ CURLcode config2setopts(struct OperationConfig *config,
}
#endif
}
my_setopt_long(curl, CURLOPT_UPLOAD_FLAGS, config->upload_flags);
if(!result)
my_setopt_long(curl, CURLOPT_UPLOAD_FLAGS, config->upload_flags);
return result;
}
CURLcode config2setopts(struct OperationConfig *config,
struct per_transfer *per,
CURL *curl,
CURLSH *share)
{
const char *use_proto;
CURLcode result = url_proto_and_rewrite(&per->url, config, &use_proto);
/* Avoid having this setopt added to the --libcurl source output. */
if(!result)
result = curl_easy_setopt(curl, CURLOPT_SHARE, share);
if(result)
return result;
#ifdef DEBUGBUILD
if(getenv("CURL_QUICK_EXIT"))
#endif
{
/* QUICK_EXIT allows for running threads to be detached and not
* joined. Preferably in non-debug runs. */
result = curl_easy_setopt(curl, CURLOPT_QUICK_EXIT, 1L);
if(result)
return result;
}
gen_trace_setopts(config, curl);
buffersize(config, curl);
MY_SETOPT_STR(curl, CURLOPT_URL, per->url);
my_setopt_long(curl, CURLOPT_NOPROGRESS,
global->noprogress || global->silent);
/* call after the line above. It may override CURLOPT_NOPROGRESS */
gen_cb_setopts(config, per, curl);
result = proxy_setopts(config, curl);
if(setopt_bad(result) || config->synthetic_error)
return result;
result = credentials_and_headers_setopts(config, curl);
if(!setopt_bad(result))
result = transfer_setopts(config, per, curl);
if(!setopt_bad(result))
result = protocol_setopts(config, per, curl, use_proto);
if(!setopt_bad(result))
result = dns_and_network_setopts(config, per, curl);
if(!setopt_bad(result))
result = mail_and_sasl_setopts(config, curl);
if(!setopt_bad(result))
result = misc_setopts(config, curl);
return result;
}

View file

@ -712,11 +712,3 @@ CURLcode tool_setopt_str(CURL *curl, struct OperationConfig *config,
}
#endif /* CURL_DISABLE_LIBCURL_OPTION */
/* return TRUE if the error code is "lethal" */
bool setopt_bad(CURLcode result)
{
return result &&
(result != CURLE_NOT_BUILT_IN) &&
(result != CURLE_UNKNOWN_OPTION);
}

View file

@ -29,9 +29,6 @@
* Macros used in operate()
*/
/* return TRUE if the error code is "lethal" */
bool setopt_bad(CURLcode result);
#ifndef CURL_DISABLE_LIBCURL_OPTION
/* Associate symbolic names with option values */

View file

@ -13,9 +13,9 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "http://%HOSTIP:%HTTPPORT/we/want/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "foo=bar&baz=quux");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)16);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

View file

@ -24,6 +24,7 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "http://%HOSTIP:%HTTPPORT/we/want/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
mime1 = curl_mime_init(curl);
part1 = curl_mime_addpart(mime1);
curl_mime_data(part1, "value", CURL_ZERO_TERMINATED);
@ -44,7 +45,6 @@ int main(int argc, char *argv[])
mime2 = NULL;
curl_mime_name(part1, "file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

View file

@ -25,10 +25,10 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "ftp://%HOSTIP:%FTPPORT/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(curl, CURLOPT_QUOTE, slist1);
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, slist2);
curl_easy_setopt(curl, CURLOPT_PREQUOTE, slist3);
curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* Here is a list of options the curl code used that cannot get generated

View file

@ -18,8 +18,8 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "smtp://%HOSTIP:%SMTPPORT/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "sender@example.com");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, slist1);

View file

@ -13,8 +13,8 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "pop3://%HOSTIP:%POP3PORT/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:secret");
curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* Here is a list of options the curl code used that cannot get generated

View file

@ -13,9 +13,9 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl, CURLOPT_URL, "http://%HOSTIP:%HTTPPORT/we/want/%TESTNUMBER");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "ab\201cd\000e\\\"\?\r\n\t\001fghi\x1ajklm\xfd");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)24);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/%VERSION");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);