cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH 4/4] src/tool: allow timeouts to accept decimal values

From: Dave Reisner <d_at_falconindy.com>
Date: Mon, 6 May 2013 14:19:51 -0400

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.

---
 src/tool_cfgable.h  |  4 ++--
 src/tool_getparam.c |  4 ++--
 src/tool_operate.c  |  2 +-
 src/tool_paramhlp.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 5 deletions(-)
diff --git a/src/tool_cfgable.h b/src/tool_cfgable.h
index e6611fc..9a9b6d8 100644
--- a/src/tool_cfgable.h
+++ b/src/tool_cfgable.h
@@ -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;
diff --git a/src/tool_getparam.c b/src/tool_getparam.c
index fb82708..5eb2c9f 100644
--- a/src/tool_getparam.c
+++ b/src/tool_getparam.c
@@ -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;
diff --git a/src/tool_operate.c b/src/tool_operate.c
index 3a15bed..d27e40f 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -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) {
 
diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c
index d232450..f234915 100644
--- a/src/tool_paramhlp.c
+++ b/src/tool_paramhlp.c
@@ -188,6 +188,48 @@ ParameterError str2unum(long *val, const char *str)
 }
 
 /*
+ * 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;
+    long 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.
  *
-- 
1.8.2.2
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-05-06