]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
etag: save and use the full received contents
authorDaniel Stenberg <daniel@haxx.se>
Wed, 19 Aug 2020 22:34:44 +0000 (00:34 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 24 Aug 2020 08:02:41 +0000 (10:02 +0200)
... which makes it support weak tags and non-standard etags too!

Added test case 347 to verify blank incoming ETag:

Fixes #5610
Closes #5833

src/tool_cb_hdr.c
src/tool_operate.c
tests/data/Makefile.inc
tests/data/test1566
tests/data/test339
tests/data/test342
tests/data/test343
tests/data/test344
tests/data/test345
tests/data/test347 [new file with mode: 0644]

index 5741f0abac3e7f524fb822424554c308235f96e1..ee3ef73907bc0eda5d688989294c35174d16b954 100644 (file)
@@ -98,53 +98,27 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
 
   /*
    * Write etag to file when --etag-save option is given.
-   * etag string that we want is enveloped in double quotes
    */
   if(per->config->etag_save_file && etag_save->stream) {
     /* match only header that start with etag (case insensitive) */
     if(curl_strnequal(str, "etag:", 5)) {
-      char *etag_h = NULL;
-      char *first = NULL;
-      char *last = NULL;
-      size_t etag_length = 0;
-
-      etag_h = ptr;
-      /* point to first occurrence of double quote */
-      first = memchr(etag_h, '\"', cb);
-
-      /*
-       * if server side messed with the etag header and doesn't include
-       * double quotes around the etag, kindly exit with a warning
-       */
-
-      if(!first) {
-        warnf(per->config->global,
-              "Received header etag is missing double quote/s\n");
-        return failure;
-      }
-      else {
-        /* discard first double quote */
-        first++;
-      }
-
-      /* point to last occurrence of double quote */
-      last = memchr(first, '\"', cb);
-
-      if(!last) {
-        warnf(per->config->global,
-              "Received header etag is missing double quote/s\n");
-        return failure;
+      const char *etag_h = &str[5];
+      const char *eot = end - 1;
+      if(*eot == '\n') {
+        while(ISSPACE(*etag_h) && (etag_h < eot))
+          etag_h++;
+        while(ISSPACE(*eot))
+          eot--;
+
+        if(eot >= etag_h) {
+          size_t etag_length = eot - etag_h + 1;
+          fwrite(etag_h, size, etag_length, etag_save->stream);
+          /* terminate with newline */
+          fputc('\n', etag_save->stream);
+          (void)fflush(etag_save->stream);
+        }
       }
-
-      /* get length of desired etag */
-      etag_length = (size_t)last - (size_t)first;
-
-      fwrite(first, size, etag_length, etag_save->stream);
-      /* terminate with new line */
-      fputc('\n', etag_save->stream);
     }
-
-    (void)fflush(etag_save->stream);
   }
 
   /*
index ad4e85dd83eb890940755e064a00af947a7fa198..3936315a97cd43db2a930f7209d768cb5672fdb0 100644 (file)
@@ -922,7 +922,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
 
           if((PARAM_OK == file2string(&etag_from_file, file)) &&
              etag_from_file) {
-            header = aprintf("If-None-Match: \"%s\"", etag_from_file);
+            header = aprintf("If-None-Match: %s", etag_from_file);
             Curl_safefree(etag_from_file);
           }
           else
index 3752502ef51b0219e896bf37381975f01423b32d..9261ee221fed19cd428d44bc4ebba6f63f0de620 100644 (file)
@@ -58,7 +58,7 @@ test307 test308 test309 test310 test311 test312 test313 test314 test315 \
 test316 test317 test318 test319 test320 test321 test322 test323 test324 \
 test325 test326 test327 test328 test329 test330 test331 test332 test333 \
 test334 test335 test336 test337 test338 test339 test340 test341 test342 \
-test343 test344 test345 test346 \
+test343 test344 test345 test346 test347 \
 test350 test351 test352 test353 test354 test355 test356 test357 test358 \
 test359 \
 test393 test394 test395 test396 test397 \
index e6f81711ddfc7a3cd1b92b8a7a3e4001d0785962..cc3ac474a26780a1b496b045dc4b7b00085b1502 100644 (file)
@@ -35,7 +35,7 @@ http
 http://%HOSTIP:%HTTPPORT/1566 -o log/output1566 --etag-compare log/etag1566
 </command>
 <file name="log/etag1566">
-123456
+"123456"
 </file>
 <file1 name="log/output1566">
 downloaded already
index cd6e49892b20716a574d8b54c76ce7426afb0181..8da212e5571055d073b53cf7b7067c41e7370b0c 100644 (file)
@@ -14,7 +14,7 @@ Server: fakeit/0.9 fakeitbad/1.0
 Transfer-Encoding: chunked\r
 Trailer: chunky-trailer\r
 Connection: mooo\r
-ETag: "asdf"\r
+ETag: W/"asdf"\r
 \r
 40
 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
@@ -56,7 +56,7 @@ Accept: */*
 \r
 </protocol>
 <file name="log/etag339">
-asdf
+W/"asdf"
 </file>
 </verify>
 
index 95297ba500f5ea0e8595f3a735e7f8ca8fd051e7..75295cfa878ee6ce795ccdcc574e8080533ce46d 100644 (file)
@@ -34,7 +34,7 @@ http
 Check if --etag-compare set correct etag in header
 </name>
 <file name="log/etag342">
-21025-dc7-39462498
+"21025-dc7-39462498"
 </file>
 <command>
 http://%HOSTIP:%HTTPPORT/342 --etag-compare log/etag342
index e55a181d895c78f9896d467779916d5e32cab19c..5d877827f64e5025f7ed6cedd2671b976b55e35a 100644 (file)
@@ -34,7 +34,7 @@ http
 Both --etag-compare and --etag-save to save new Etag
 </name>
 <file name="log/etag343">
-21025-dc7-39462498
+"21025-dc7-39462498"
 </file>
 <command>
 http://%HOSTIP:%HTTPPORT/343 --etag-compare log/etag343 --etag-save log/out343
@@ -55,7 +55,7 @@ If-None-Match: "21025-dc7-39462498"
 \r
 </protocol>
 <file name="log/out343">
-21025-dc7-11111
+"21025-dc7-11111"
 </file>
 </verify>
 </testcase>
index 054d0351be284d305730bcf44e14983a4b209d81..2b730d52411c07b1693d844a7ce271770c337fd2 100644 (file)
@@ -52,7 +52,7 @@ If-None-Match: ""
 \r
 </protocol>
 <file name="log/etag344">
-21025-dc7-11111
+"21025-dc7-11111"
 </file>
 </verify>
 </testcase>
index 135e0c26aa1202bf985d08a9320fe8b31bbc6f97..c30c06de9692d81359c5de942a455c76a35bc9c2 100644 (file)
@@ -34,7 +34,7 @@ http
 Both --etag-compare and -save store new Etag using one pre-existing file
 </name>
 <file name="log/etag345">
-21025-dc7-39462498
+"21025-dc7-39462498"
 </file>
 <command>
 http://%HOSTIP:%HTTPPORT/345 --etag-compare log/etag345 --etag-save log/etag345
@@ -55,7 +55,7 @@ If-None-Match: "21025-dc7-39462498"
 \r
 </protocol>
 <file name="log/etag345">
-21025-dc7-11111
+"21025-dc7-11111"
 </file>
 </verify>
 </testcase>
diff --git a/tests/data/test347 b/tests/data/test347
new file mode 100644 (file)
index 0000000..702c270
--- /dev/null
@@ -0,0 +1,62 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+HTTP GET
+</keywords>
+</info>
+#
+# Server-side
+<reply>
+<data nocheck="yes">
+HTTP/1.1 200 funky chunky!\r
+Server: fakeit/0.9 fakeitbad/1.0\r
+Transfer-Encoding: chunked\r
+Trailer: chunky-trailer\r
+Connection: mooo\r
+ETag:  \r
+\r
+40
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+30
+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+21;heresatest=moooo
+cccccccccccccccccccccccccccccccc
+
+0
+chunky-trailer: header data
+
+</data>
+</reply>
+
+#
+# Client-side
+<client>
+<server>
+http
+</server>
+<name>
+--etag-save with blank incoming header
+</name>
+<command>
+http://%HOSTIP:%HTTPPORT/347 --etag-save log/etag347
+</command>
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+<strip>
+^User-Agent:.*
+</strip>
+<protocol>
+GET /347 HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+Accept: */*\r
+\r
+</protocol>
+<file name="log/etag347">
+</file>
+</verify>
+
+</testcase>