]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
config2setopt.c: refactor config2setopts
authorDaniel Stenberg <daniel@haxx.se>
Mon, 29 Jun 2026 10:02:14 +0000 (12:02 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 29 Jun 2026 20:24:57 +0000 (22:24 +0200)
Introduce sub functions

Closes #22215

src/config2setopts.c
src/tool_setopt.c
src/tool_setopt.h
tests/data/data1402.c
tests/data/data1404.c
tests/data/data1405.c
tests/data/data1406.c
tests/data/data1407.c
tests/data/data1465.c

index 8933668a2c7e5dd03cf8b2e6b5ed166b6fa0ca67..f27783d9fa450636b48bb257cd2bcda5945de276 100644 (file)
 
 #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);
-
-  /* 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);
+  CURLcode result = CURLE_OK;
 
-  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,33 +1033,43 @@ 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);
 
-  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);
+  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(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->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);
 
   if(config->unix_socket_path) {
     if(config->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;
 }
index 71e2ffba04a4afe73cc579675f1d35baac2a672d..51e22756aa4a9021ac1b44807492cffeb7e5a98d 100644 (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);
-}
index 20419733ba297ad06ed6b3c01f05b2808983a523..a737fcddf54fa4031df0a4c738a83d8e15a54ba9 100644 (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 */
index 682345485d38acf210067345494dae89078ff09b..0490c96ea2cf5fb2c3bb64d79f029d629d92b95a 100644 (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);
 
index df9364d1040449b0bf958c8631905016ea42f13d..a09467fa102292ea4a0fb190f17251a118efa51c 100644 (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);
 
index ec7a4a39c9a3ac4111403779cd37d53bbd666bd1..e79550806bf2962400ec2d136044c63c8a89f320 100644 (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
index 30aff8d58875fee5d86315d426195cc4771ad7fa..4f9a3fccafd9aa70553f9046e3e55703d811d2a0 100644 (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);
index 4f34db365527ff220c287b669bbc7cc478eb8904..b6f312741e18036d4fff2406e06bae04a39d4abb 100644 (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
index dea3b61988c0c878151b2b1d1f81d2f9863ecb26..ac3c6db10c9f75a538c7c317f0b1dcf56a20f898 100644 (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);