curl: add my_setopt_long() and _offt()

Two new dedicated functions for setting long and curl_off_t options with
curl_easy_setopt(). These make it easier to make sure we pass on the
right option (types) so that the --libcurl code also gets right.

Corrected a few errors.

Closes #16669
This commit is contained in:
Daniel Stenberg 2025-03-11 10:34:47 +01:00
parent 763fa529df
commit dc12ecd5db
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 197 additions and 195 deletions

View file

@ -601,6 +601,50 @@ nomem:
return ret;
}
/* options that set long */
CURLcode tool_setopt_long(CURL *curl, struct GlobalConfig *global,
const char *name, CURLoption tag,
long lval)
{
long defval = 0L;
const struct NameValue *nv = NULL;
CURLcode ret = CURLE_OK;
DEBUGASSERT(tag < CURLOPTTYPE_OBJECTPOINT);
for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
if(!strcmp(name, nv->name)) {
defval = nv->value;
break; /* found it */
}
}
ret = curl_easy_setopt(curl, tag, lval);
if((lval != defval) && global->libcurl && !ret) {
/* we only use this for real if --libcurl was used */
CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
}
nomem:
return ret;
}
/* options that set curl_off_t */
CURLcode tool_setopt_offt(CURL *curl, struct GlobalConfig *global,
const char *name, CURLoption tag,
curl_off_t lval)
{
CURLcode ret = CURLE_OK;
DEBUGASSERT((tag >= CURLOPTTYPE_OFF_T) && (tag < CURLOPTTYPE_BLOB));
ret = curl_easy_setopt(curl, tag, lval);
if(global->libcurl && !ret && lval) {
/* we only use this for real if --libcurl was used */
CODE2("curl_easy_setopt(hnd, %s, (curl_off_t)%"
CURL_FORMAT_CURL_OFF_T ");", name, lval);
}
nomem:
return ret;
}
/* generic setopt wrapper for all other options.
* Some type information is encoded in the tag value. */
CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global,
@ -608,87 +652,47 @@ CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global,
const char *name, CURLoption tag, ...)
{
va_list arg;
char buf[256];
const char *value = NULL;
bool remark = FALSE;
bool skip = FALSE;
bool escape = FALSE;
char *escaped = NULL;
CURLcode ret = CURLE_OK;
void *pval;
va_start(arg, tag);
if(tag < CURLOPTTYPE_OBJECTPOINT) {
/* Value is expected to be a long */
long lval = va_arg(arg, long);
long defval = 0L;
const struct NameValue *nv = NULL;
for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
if(!strcmp(name, nv->name)) {
defval = nv->value;
break; /* found it */
}
}
DEBUGASSERT(tag >= CURLOPTTYPE_OBJECTPOINT);
DEBUGASSERT((tag < CURLOPTTYPE_OFF_T) || (tag >= CURLOPTTYPE_BLOB));
msnprintf(buf, sizeof(buf), "%ldL", lval);
value = buf;
ret = curl_easy_setopt(curl, tag, lval);
if(lval == defval)
skip = TRUE;
}
else if(tag < CURLOPTTYPE_OFF_T) {
/* Value is some sort of object pointer */
void *pval = va_arg(arg, void *);
/* we never set _BLOB options in the curl tool */
DEBUGASSERT(tag < CURLOPTTYPE_BLOB);
/* function pointers are never printable */
if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
if(pval) {
value = "function pointer";
remark = TRUE;
}
else
skip = TRUE;
}
/* Value is some sort of object pointer */
pval = va_arg(arg, void *);
else if(pval && str) {
value = (char *)pval;
escape = TRUE;
}
else if(pval) {
value = "object pointer";
/* function pointers are never printable */
if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
if(pval) {
value = "function pointer";
remark = TRUE;
}
else
skip = TRUE;
ret = curl_easy_setopt(curl, tag, pval);
}
else if(tag < CURLOPTTYPE_BLOB) {
/* Value is expected to be curl_off_t */
curl_off_t oval = va_arg(arg, curl_off_t);
msnprintf(buf, sizeof(buf),
"(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
value = buf;
ret = curl_easy_setopt(curl, tag, oval);
if(!oval)
skip = TRUE;
else if(pval && str) {
value = (char *)pval;
escape = TRUE;
}
else {
/* Value is a blob */
void *pblob = va_arg(arg, void *);
/* blobs are never printable */
if(pblob) {
value = "blob pointer";
remark = TRUE;
}
else
skip = TRUE;
ret = curl_easy_setopt(curl, tag, pblob);
else if(pval) {
value = "object pointer";
remark = TRUE;
}
else
skip = TRUE;
ret = curl_easy_setopt(curl, tag, pval);
va_end(arg);