]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
http: make 416 not fail with resume + CURLOPT_FAILONERRROR
authorDaniel Stenberg <daniel@haxx.se>
Tue, 16 Mar 2021 13:41:06 +0000 (14:41 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 17 Mar 2021 07:26:46 +0000 (08:26 +0100)
When asked to resume a download, libcurl will convert that to HTTP logic
and if then the entire file is already transferred it will result in a
416 response from the HTTP server. With CURLOPT_FAILONERRROR set in that
scenario, it should *not* lead to an error return.

Updated test 1156, added test 1273

Reported-by: Jonathan Watt
Fixes #6740
Closes #6753

lib/http.c
tests/data/Makefile.inc
tests/data/test1156
tests/data/test1273 [new file with mode: 0644]
tests/libtest/lib1156.c

index 43eb840de3f5f43c7d1bad8eef40c27237d69e2d..7ec03e9bd1d4d93994d7bf68ecebb915741714ce 100644 (file)
@@ -1094,6 +1094,14 @@ static bool http_should_fail(struct Curl_easy *data)
   if(httpcode < 400)
     return FALSE;
 
+  /*
+  ** A 416 response to a resume request is presumably because the file is
+  ** already completely downloaded and thus not actually a fail.
+  */
+  if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
+     httpcode == 416)
+    return FALSE;
+
   /*
   ** Any code >= 400 that's not 401 or 407 is always
   ** a terminal error
index 6306b5980fb294344c8810bea03a748704b3f919..2c7a0ca89fd8fbf4ab10ddddf340252c342fae98 100644 (file)
@@ -153,7 +153,8 @@ test1240 test1241 test1242 test1243 test1244 test1245 test1246 test1247 \
 test1248 test1249 test1250 test1251 test1252 test1253 test1254 test1255 \
 test1256 test1257 test1258 test1259 test1260 test1261 test1262 test1263 \
 test1264 test1265 test1266 test1267 test1268 test1269 test1270 test1271 \
-test1272 \
+test1272 test1273 \
+\
 test1280 test1281 test1282 test1283 test1284 test1285 test1286 test1287 \
 test1288 test1289 test1290 test1291 test1292 test1293 test1294 test1295 \
 test1296 test1297 test1298 test1299 test1300 test1301 test1302 test1303 \
index f78dc998f08be45d5f8dff433224b21c32435f78..ff57bb0ec1027c045d9f793dffda26daaffcb123 100644 (file)
@@ -58,14 +58,15 @@ lib1156
 HTTP resume/range fail range-error content-range combinations
  </name>
  <command>
-http://%HOSTIP:%HTTPPORT/want/1156
+http://%HOSTIP:%HTTPPORT/want/%TESTNUMBER
 </command>
 </client>
 
 # Verify data after the test has been "shot"
 <verify>
-<errorcode>
+<stderr mode="text">
+URL: http://%HOSTIP:%HTTPPORT/want/%TESTNUMBER
 0
-</errorcode>
+</stderr>
 </verify>
 </testcase>
diff --git a/tests/data/test1273 b/tests/data/test1273
new file mode 100644 (file)
index 0000000..61d475a
--- /dev/null
@@ -0,0 +1,78 @@
+<testcase>
+# also verified by 1156 in libcurl API terms
+
+<info>
+<keywords>
+HTTP
+HTTP GET
+Resume
+</keywords>
+</info>
+
+# Server-side
+<reply>
+<data>
+HTTP/1.1 416 Invalid range\r
+Connection: close\r
+Content-Length: 0\r
+\r
+</data>
+
+# The file data that exists at the start of the test must be included in
+# the verification.
+<datacheck>
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+HTTP/1.1 416 Invalid range\r
+Connection: close\r
+Content-Length: 0\r
+\r
+</datacheck>
+
+</reply>
+
+# Client-side
+<client>
+<server>
+http
+</server>
+ <name>
+-f and resume transfer of an entirely-downloaded file
+ </name>
+ <command>
+http://%HOSTIP:%HTTPPORT/%TESTNUMBER -C - -f
+</command>
+<file name="log/curl%TESTNUMBER.out">
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+012345678
+</file>
+</client>
+
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+GET /%TESTNUMBER HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+Range: bytes=100-\r
+User-Agent: curl/%VERSION\r
+Accept: */*\r
+\r
+</protocol>
+</verify>
+</testcase>
index 9ffe19a790e0422eaf2ef377841c91251f6cd2e5..d8da643c0f1d868ad0643311c1c16dc312b84a0c 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -64,8 +64,7 @@ static const struct testparams params[] = {
   { F_RESUME |             F_FAIL | F_CONTENTRANGE,                CURLE_OK },
   { F_RESUME | F_HTTP416 |                           F_IGNOREBODY, CURLE_OK },
   { F_RESUME | F_HTTP416 |          F_CONTENTRANGE | F_IGNOREBODY, CURLE_OK },
-  { F_RESUME | F_HTTP416 | F_FAIL |                  F_IGNOREBODY,
-                                                  CURLE_HTTP_RETURNED_ERROR },
+  { F_RESUME | F_HTTP416 | F_FAIL |                  F_IGNOREBODY, CURLE_OK },
   { F_RESUME | F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
                                                   CURLE_HTTP_RETURNED_ERROR }
 };
@@ -82,7 +81,8 @@ static size_t writedata(char *data, size_t size, size_t nmemb, void *userdata)
   return size * nmemb;
 }
 
-static int onetest(CURL *curl, const char *url, const struct testparams *p)
+static int onetest(CURL *curl, const char *url, const struct testparams *p,
+                   size_t num)
 {
   CURLcode res;
   unsigned int replyselector;
@@ -100,22 +100,22 @@ static int onetest(CURL *curl, const char *url, const struct testparams *p)
   hasbody = 0;
   res = curl_easy_perform(curl);
   if(res != p->result) {
-    fprintf(stderr, "bad error code (%d): resume=%s, fail=%s, http416=%s, "
-                    "content-range=%s, expected=%d\n", res,
-                    (p->flags & F_RESUME)? "yes": "no",
-                    (p->flags & F_FAIL)? "yes": "no",
-                    (p->flags & F_HTTP416)? "yes": "no",
-                    (p->flags & F_CONTENTRANGE)? "yes": "no",
-                    p->result);
+    fprintf(stderr, "%d: bad error code (%d): resume=%s, fail=%s, http416=%s, "
+            "content-range=%s, expected=%d\n", num, res,
+            (p->flags & F_RESUME)? "yes": "no",
+            (p->flags & F_FAIL)? "yes": "no",
+            (p->flags & F_HTTP416)? "yes": "no",
+            (p->flags & F_CONTENTRANGE)? "yes": "no",
+            p->result);
     return 1;
   }
   if(hasbody && (p->flags & F_IGNOREBODY)) {
     fprintf(stderr, "body should be ignored and is not: resume=%s, fail=%s, "
-                    "http416=%s, content-range=%s\n",
-                    (p->flags & F_RESUME)? "yes": "no",
-                    (p->flags & F_FAIL)? "yes": "no",
-                    (p->flags & F_HTTP416)? "yes": "no",
-                    (p->flags & F_CONTENTRANGE)? "yes": "no");
+            "http416=%s, content-range=%s\n",
+            (p->flags & F_RESUME)? "yes": "no",
+            (p->flags & F_FAIL)? "yes": "no",
+            (p->flags & F_HTTP416)? "yes": "no",
+            (p->flags & F_CONTENTRANGE)? "yes": "no");
     return 1;
   }
   return 0;
@@ -147,10 +147,11 @@ int test(char *URL)
   test_setopt(curl, CURLOPT_WRITEFUNCTION, writedata);
 
   for(i = 0; i < sizeof(params) / sizeof(params[0]); i++)
-    status |= onetest(curl, URL, params + i);
+    status |= onetest(curl, URL, params + i, i);
 
   curl_easy_cleanup(curl);
   curl_global_cleanup();
+  fprintf(stderr, "%d\n", status);
   return status;
 
   test_cleanup: