summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSteffen Busch <[email protected]>2024-08-07 21:39:15 +0200
committerGitHub <[email protected]>2024-08-07 19:39:15 +0000
commitb85b6c64690dc75fd3096e452dbf8881456d0da7 (patch)
tree8e60c2f9fdcbaa56558a08f8c9323c9618ffb496
parent59cbb2c83a03b6fe352ae0b5d05581d9148a4d24 (diff)
downloadcaddy-b85b6c64690dc75fd3096e452dbf8881456d0da7.tar.gz
caddy-b85b6c64690dc75fd3096e452dbf8881456d0da7.zip
replacer: `{file.*}` global placeholder strips trailing newline (#6411)
Co-authored-by: Kanashimia <[email protected]>
-rw-r--r--caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt2
-rw-r--r--caddytest/integration/testdata/foo_with_trailing_newline.txt1
-rw-r--r--replacer.go3
-rw-r--r--replacer_test.go15
4 files changed, 21 insertions, 0 deletions
diff --git a/caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt b/caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt
new file mode 100644
index 000000000..75d7bfb87
--- /dev/null
+++ b/caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt
@@ -0,0 +1,2 @@
+foo
+
diff --git a/caddytest/integration/testdata/foo_with_trailing_newline.txt b/caddytest/integration/testdata/foo_with_trailing_newline.txt
new file mode 100644
index 000000000..257cc5642
--- /dev/null
+++ b/caddytest/integration/testdata/foo_with_trailing_newline.txt
@@ -0,0 +1 @@
+foo
diff --git a/replacer.go b/replacer.go
index e5d2913e9..65815c92a 100644
--- a/replacer.go
+++ b/replacer.go
@@ -15,6 +15,7 @@
package caddy
import (
+ "bytes"
"fmt"
"io"
"net/http"
@@ -354,6 +355,8 @@ func (f fileReplacementProvider) replace(key string) (any, bool) {
zap.Error(err))
return nil, true
}
+ body = bytes.TrimSuffix(body, []byte("\n"))
+ body = bytes.TrimSuffix(body, []byte("\r"))
return string(body), true
}
diff --git a/replacer_test.go b/replacer_test.go
index cf4d321b6..1c1a7048f 100644
--- a/replacer_test.go
+++ b/replacer_test.go
@@ -431,6 +431,14 @@ func TestReplacerNew(t *testing.T) {
variable: "file.caddytest/integration/testdata/foo.txt",
value: "foo",
},
+ {
+ variable: "file.caddytest/integration/testdata/foo_with_trailing_newline.txt",
+ value: "foo",
+ },
+ {
+ variable: "file.caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt",
+ value: "foo" + getEOL(),
+ },
} {
if val, ok := repl.providers[1].replace(tc.variable); ok {
if val != tc.value {
@@ -442,6 +450,13 @@ func TestReplacerNew(t *testing.T) {
}
}
+func getEOL() string {
+ if os.PathSeparator == '\\' {
+ return "\r\n" // Windows EOL
+ }
+ return "\n" // Unix and modern macOS EOL
+}
+
func TestReplacerNewWithoutFile(t *testing.T) {
repl := NewReplacer().WithoutFile()