Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 09:30 UTC
replacer: `{file.*}` global placeholder strips trailing newline (#6411)
Co-authored-by: Kanashimia <chad@redpilled.dev>
Steffen Busch committed
Aug 7, 2024 at 21:39 UTC
b85b6c64690dc75fd3096e452dbf8881456d0da7
4 files changed
+21
caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt
new
+2
@@ -0,0 +1,2 @@
1
+foo
2
+
caddytest/integration/testdata/foo_with_trailing_newline.txt
new
+1
@@ -0,0 +1 @@
1
+foo
replacer.go
+3
@@ -15,6 +15,7 @@
15
package caddy
16
17
import (
18
+ "bytes"
19
"fmt"
20
"io"
21
"net/http"
@@ -354,6 +355,8 @@ func (f fileReplacementProvider) replace(key string) (any, bool) {
355
zap.Error(err))
356
return nil, true
357
}
358
+ body = bytes.TrimSuffix(body, []byte("\n"))
359
+ body = bytes.TrimSuffix(body, []byte("\r"))
360
return string(body), true
361
}
362
replacer_test.go
+15
@@ -431,6 +431,14 @@ func TestReplacerNew(t *testing.T) {
431
variable: "file.caddytest/integration/testdata/foo.txt",
432
value: "foo",
433
},
434
+ {
435
+ variable: "file.caddytest/integration/testdata/foo_with_trailing_newline.txt",
436
+ value: "foo",
437
+ },
438
+ {
439
+ variable: "file.caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt",
440
+ value: "foo" + getEOL(),
441
+ },
442
} {
443
if val, ok := repl.providers[1].replace(tc.variable); ok {
444
if val != tc.value {
@@ -442,6 +450,13 @@ func TestReplacerNew(t *testing.T) {
450
}
451
}
452
453
+func getEOL() string {
454
+ if os.PathSeparator == '\\' {
455
+ return "\r\n" // Windows EOL
456
+ }
457
+ return "\n" // Unix and modern macOS EOL
458
+}
459
+
460
func TestReplacerNewWithoutFile(t *testing.T) {
461
repl := NewReplacer().WithoutFile()
462