mirror of
https://github.com/curl/curl.git
synced 2026-07-29 05:23:12 +03:00
src/tool: allow timeouts to accept decimal values
Implement wrappers around strtod to convert the user argument to a double with sane error checking. Use this to allow --max-time and --connect-timeout to accept decimal values instead of strictly integers. The manpage is updated to make mention of this feature and, additionally, forewarn that the actual timeout of the operation can vary in its precision (particularly as the value increases in its decimal precision).
This commit is contained in:
parent
c0a7a98aee
commit
d8c04909fa
6 changed files with 58 additions and 8 deletions
|
|
@ -53,8 +53,8 @@ struct Configurable {
|
|||
char *postfields;
|
||||
curl_off_t postfieldsize;
|
||||
char *referer;
|
||||
long timeout;
|
||||
long connecttimeout;
|
||||
double timeout;
|
||||
double connecttimeout;
|
||||
long maxredirs;
|
||||
curl_off_t max_filesize;
|
||||
char *headerfile;
|
||||
|
|
|
|||
|
|
@ -498,7 +498,7 @@ ParameterError getparameter(char *flag, /* f or -long-flag */
|
|||
GetStr(&config->egd_file, nextarg);
|
||||
break;
|
||||
case 'c': /* connect-timeout */
|
||||
err = str2unum(&config->connecttimeout, nextarg);
|
||||
err = str2udouble(&config->connecttimeout, nextarg);
|
||||
if(err)
|
||||
return err;
|
||||
break;
|
||||
|
|
@ -1404,7 +1404,7 @@ ParameterError getparameter(char *flag, /* f or -long-flag */
|
|||
break;
|
||||
case 'm':
|
||||
/* specified max time */
|
||||
err = str2unum(&config->timeout, nextarg);
|
||||
err = str2udouble(&config->timeout, nextarg);
|
||||
if(err)
|
||||
return err;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -946,7 +946,7 @@ int operate(struct Configurable *config, int argc, argv_item_t argv[])
|
|||
my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
|
||||
my_setopt_str(curl, CURLOPT_RANGE, config->range);
|
||||
my_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
|
||||
my_setopt(curl, CURLOPT_TIMEOUT, config->timeout);
|
||||
my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
|
||||
|
||||
if(built_in_protos & CURLPROTO_HTTP) {
|
||||
|
||||
|
|
@ -1134,7 +1134,8 @@ int operate(struct Configurable *config, int argc, argv_item_t argv[])
|
|||
/* new in libcurl 7.7: */
|
||||
my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
|
||||
my_setopt(curl, CURLOPT_EGDSOCKET, config->egd_file);
|
||||
my_setopt(curl, CURLOPT_CONNECTTIMEOUT, config->connecttimeout);
|
||||
my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
|
||||
(long)(config->connecttimeout * 1000));
|
||||
|
||||
if(config->cipher_list)
|
||||
my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
|
||||
|
|
|
|||
|
|
@ -187,6 +187,48 @@ ParameterError str2unum(long *val, const char *str)
|
|||
return PARAM_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse the string and write the double in the given address. Return PARAM_OK
|
||||
* on success, otherwise a parameter specific error enum.
|
||||
*
|
||||
* Since this function gets called with the 'nextarg' pointer from within the
|
||||
* getparameter a lot, we must check it for NULL before accessing the str
|
||||
* data.
|
||||
*/
|
||||
|
||||
ParameterError str2double(double *val, const char *str)
|
||||
{
|
||||
if(str) {
|
||||
char *endptr;
|
||||
double num = strtod(str, &endptr);
|
||||
if((endptr != str) && (endptr == str + strlen(str))) {
|
||||
*val = num;
|
||||
return PARAM_OK; /* Ok */
|
||||
}
|
||||
}
|
||||
return PARAM_BAD_NUMERIC; /* badness */
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse the string and write the double in the given address. Return PARAM_OK
|
||||
* on success, otherwise a parameter error enum. ONLY ACCEPTS POSITIVE NUMBERS!
|
||||
*
|
||||
* Since this function gets called with the 'nextarg' pointer from within the
|
||||
* getparameter a lot, we must check it for NULL before accessing the str
|
||||
* data.
|
||||
*/
|
||||
|
||||
ParameterError str2udouble(double *val, const char *str)
|
||||
{
|
||||
ParameterError result = str2double(val, str);
|
||||
if(result != PARAM_OK)
|
||||
return result;
|
||||
if(*val < 0)
|
||||
return PARAM_NEGATIVE_NUMERIC;
|
||||
|
||||
return PARAM_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse the string and modify the long in the given address. Return
|
||||
* non-zero on failure, zero on success.
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ void cleanarg(char *str);
|
|||
|
||||
ParameterError str2num(long *val, const char *str);
|
||||
ParameterError str2unum(long *val, const char *str);
|
||||
ParameterError str2double(double *val, const char *str);
|
||||
ParameterError str2udouble(double *val, const char *str);
|
||||
|
||||
long proto2num(struct Configurable *config, long *val, const char *str);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue