]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
getparam: make --rate accept "number of units"
authorDaniel Stenberg <daniel@haxx.se>
Sat, 20 Jul 2024 15:54:44 +0000 (17:54 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sat, 3 Aug 2024 18:27:29 +0000 (20:27 +0200)
Do no more than 5 transfers per 15 seconds with "5/15s" or limit it to 3
transfers per 4 hours with "3/4h" etc.

Previously it would always only work with a single time unit.

Ref: #14242
Closes #14245

docs/cmdline-opts/rate.md
src/tool_getparam.c

index 49d0010b8ffe6da81ce143111e8f64c798d45812..6de65165d0b8b1ba5ddfdfbb87a638c3be2b714e 100644 (file)
@@ -40,3 +40,7 @@ more than 1000 per second, it instead runs unrestricted.
 
 When retrying transfers, enabled with --retry, the separate retry delay logic
 is used and not this setting.
+
+Starting in version 8.10.0, you can specify number of time units in the rate
+expression. Make curl do no more than 5 transfers per 15 seconds with "5/15s"
+or limit it to 3 transfers per 4 hours with "3/4h". No spaces allowed.
index eaa00e2068a37cb2bd44f66203a22557f7cbab1d..523450230307a42bae9ed76db164050b450a5210 100644 (file)
@@ -1244,6 +1244,20 @@ static ParameterError set_rate(struct GlobalConfig *global,
 
   if(div) {
     char unit = div[1];
+    curl_off_t numunits;
+    char *endp;
+
+    if(curlx_strtoofft(&div[1], &endp, 10, &numunits)) {
+      /* if it fails, there is no legit number specified */
+      if(endp == &div[1])
+        /* if endp did not move, accept it as a 1 */
+        numunits = 1;
+      else
+        return PARAM_BAD_USE;
+    }
+    else
+      unit = *endp;
+
     switch(unit) {
     case 's': /* per second */
       numerator = 1000;
@@ -1261,6 +1275,14 @@ static ParameterError set_rate(struct GlobalConfig *global,
       err = PARAM_BAD_USE;
       break;
     }
+
+    if((LONG_MAX / numerator) < numunits) {
+      /* overflow, too large number */
+      errorf(global, "too large --rate unit");
+      err = PARAM_NUMBER_TOO_LARGE;
+    }
+    /* this typecast is okay based on the check above */
+    numerator *= (long)numunits;
   }
 
   if(err)