]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_operate: don't truncate the etag save file by default
authorGusted <postmaster@gusted.xyz>
Sat, 20 Apr 2024 16:44:42 +0000 (18:44 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 23 Apr 2024 09:33:20 +0000 (11:33 +0200)
This fixes a regression of 75d79a4486b279100209ddf8c7fdb12955fb66e9. The
code in tool-operate truncated the etag save file, under the assumption
that the file would be written with a new etag value. However since
75d79a4486b279100209ddf8c7fdb12955fb66e9 that might not be the case
anymore and could result in the file being truncated when --etag-compare
and --etag-save was used and that the etag value matched with what the
server responded. Instead the truncation should not be done when a new
etag value should be written.

Test 3204 was added to verify that the file with the etag value doesn't
change the contents when used by --etag-compare and --etage-save and
that value matches with what the server returns on a non 2xx response.

Closes #13432

src/tool_cb_hdr.c
src/tool_operate.c
tests/data/Makefile.inc
tests/data/test3204 [new file with mode: 0644]

index a64654eaf62b3e07854758af61efee8ecac68193..21407aa2095ab728db0908b3cbea03e99f1af99e 100644 (file)
@@ -24,6 +24,9 @@
 #include "tool_setup.h"
 
 #include "strcase.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 #define ENABLE_CURLX_PRINTF
 /* use our own printf() functions */
@@ -130,6 +133,19 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
 
         if(eot >= etag_h) {
           size_t etag_length = eot - etag_h + 1;
+          /*
+           * Truncate the etag save stream, it can have an existing etag value.
+           */
+#ifdef HAVE_FTRUNCATE
+          if(ftruncate(fileno(etag_save->stream), 0)) {
+            return CURL_WRITEFUNC_ERROR;
+          }
+#else
+          if(fseek(etag_save->stream, 0, SEEK_SET)) {
+            return CURL_WRITEFUNC_ERROR;
+          }
+#endif
+
           fwrite(etag_h, size, etag_length, etag_save->stream);
           /* terminate with newline */
           fputc('\n', etag_save->stream);
index eb29dcd36570658cbc1e6f7d8a9a0b8f8bd4d907..77b2dbd2f0fccda7668da62416bfce28d4dea1d7 100644 (file)
@@ -925,7 +925,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         if(config->etag_save_file) {
           /* open file for output: */
           if(strcmp(config->etag_save_file, "-")) {
-            FILE *newfile = fopen(config->etag_save_file, "wb");
+            FILE *newfile = fopen(config->etag_save_file, "ab");
             if(!newfile) {
               warnf(global, "Failed creating file for saving etags: \"%s\". "
                     "Skip this transfer", config->etag_save_file);
index fb94dd002a58e78f767768c3a28daa7affa8759f..b891c1154cfad296b33f97ad8747688215846c4b 100644 (file)
@@ -261,4 +261,4 @@ test3024 test3025 test3026 test3027 test3028 test3029 test3030 \
 \
 test3100 test3101 test3102 test3103 \
 test3200 \
-test3201 test3202 test3203
+test3201 test3202 test3203 test3204
diff --git a/tests/data/test3204 b/tests/data/test3204
new file mode 100644 (file)
index 0000000..8cc9c2f
--- /dev/null
@@ -0,0 +1,52 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+HTTP GET
+</keywords>
+</info>
+
+#
+# Server-side
+<reply>
+<data>
+HTTP/1.1 304 Not Modified
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Server: test-server/fake
+ETag: "21025-dc7-39462498"
+
+</data>
+</reply>
+
+#
+# Client-side
+<client>
+<server>
+http
+</server>
+<name>
+Use --etag-compare and --etag-save on an existing file
+</name>
+<file name="%LOGDIR/etag%TESTNUMBER">
+"21025-dc7-39462498"
+</file>
+<command>
+http://%HOSTIP:%HTTPPORT/%TESTNUMBER --etag-compare %LOGDIR/etag%TESTNUMBER --etag-save %LOGDIR/etag%TESTNUMBER
+</command>
+</client>
+
+# Verify that the file still exists with the correct etag value.
+<verify>
+<protocol>
+GET /%TESTNUMBER HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+User-Agent: curl/%VERSION\r
+Accept: */*\r
+If-None-Match: "21025-dc7-39462498"\r
+\r
+</protocol>
+<file name="%LOGDIR/etag%TESTNUMBER">
+"21025-dc7-39462498"
+</file>
+</verify>
+</testcase>