]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool: determine the correct fopen option for -D
authorEmil Engler <me@emilengler.com>
Sun, 11 Dec 2022 17:08:17 +0000 (18:08 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 12 Dec 2022 08:04:51 +0000 (09:04 +0100)
This commit fixes a bug in the dump-header feature regarding the
determination of the second fopen(3) option.

Reported-by: u20221022 on github
See #4753
See #4762
Fixes #10074
Closes #10079

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

index 7af73a266c1e49cc4098c9ab4167fcba9b50f94e..79db063a500297c75d826ab0d8a5d9aecb67ab63 100644 (file)
@@ -969,7 +969,21 @@ static CURLcode single_transfer(struct GlobalConfig *global,
           /* open file for output: */
           if(strcmp(config->headerfile, "-")) {
             FILE *newfile;
-            newfile = fopen(config->headerfile, per->prev == NULL?"wb":"ab");
+
+            /*
+             * this checks if the previous transfer had the same
+             * OperationConfig, which would mean, that the an output file has
+             * already been created and data can be appened to it, instead
+             * of overwriting it.
+             * TODO: Consider placing the file handle inside the
+             * OperationConfig, so that it does not need to be opened/closed
+             * for every transfer.
+             */
+            if(per->prev && per->prev->config == config)
+              newfile = fopen(config->headerfile, "ab+");
+            else
+              newfile = fopen(config->headerfile, "wb+");
+
             if(!newfile) {
               warnf(global, "Failed to open %s\n", config->headerfile);
               result = CURLE_WRITE_ERROR;
index d7194376ebc72e5bc2d14b737e2c50879b5dc8df..e662dd1cdc25799ec9f7c209d2a77fa7b931dfd6 100644 (file)
@@ -248,7 +248,7 @@ test2500 \
 test3000 test3001 test3002 test3003 test3004 test3005 test3006 test3007 \
 test3008 test3009 test3010 test3011 test3012 test3013 test3014 test3015 \
 test3016 test3017 test3018 test3019 test3020 test3021 test3022 test3023 \
-test3024 test3025 test3026 test3027 test3028 \
+test3024 test3025 test3026 test3027 test3028 test3029 test3030 \
 \
 test3100 test3101 \
 test3200
diff --git a/tests/data/test3029 b/tests/data/test3029
new file mode 100644 (file)
index 0000000..a5bc14b
--- /dev/null
@@ -0,0 +1,39 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+</keywords>
+</info>
+
+<reply>
+<data>
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Content-Length: 6
+
+-foo-
+</data>
+</reply>
+
+<client>
+<server>
+http
+</server>
+<name>
+HTTP with multiple -D
+</name>
+<command>
+-D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER --next -D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER
+</command>
+</client>
+
+<verify>
+<file name="log/heads%TESTNUMBER">
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Content-Length: 6
+
+</file>
+</verify>
+
+</testcase>
\ No newline at end of file
diff --git a/tests/data/test3030 b/tests/data/test3030
new file mode 100644 (file)
index 0000000..b4d7ee3
--- /dev/null
@@ -0,0 +1,43 @@
+<testcase>
+<info>
+<keywords>
+HTTP
+</keywords>
+</info>
+
+<reply>
+<data>
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Content-Length: 6
+
+-foo-
+</data>
+</reply>
+
+<client>
+<server>
+http
+</server>
+<name>
+HTTP with multiple transfers in one -D
+</name>
+<command>
+-D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER
+</command>
+</client>
+
+<verify>
+<file name="log/heads%TESTNUMBER">
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Content-Length: 6
+
+HTTP/1.1 200 OK
+Date: Tue, 09 Nov 2010 14:49:00 GMT
+Content-Length: 6
+
+</file>
+</verify>
+
+</testcase>
\ No newline at end of file