Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddyfile: fix replacing variables on imported files (#2970)
* fix replacing variables on imported files * refactored replaceEnvVars to ensure it is always called * Use byte slices for easier use Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Mark Sargent committed
Jan 10, 2020 at 15:34 UTC
871abf1053e99274e7336db3143ca5eb17013e38
3 files changed
+22
-12
caddyconfig/caddyfile/dispenser_test.go
+1
-1
@@ -308,7 +308,7 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
308
}
309
310
func newTestDispenser(input string) *Dispenser {
311
- tokens, err := allTokens("Testfile", strings.NewReader(input))
311
+ tokens, err := allTokens("Testfile", []byte(input))
312
if err != nil && err != io.EOF {
313
log.Fatalf("getting all tokens from input: %v", err)
314
}
caddyconfig/caddyfile/parse.go
+16
-8
@@ -16,7 +16,7 @@ package caddyfile
16
17
import (
18
"bytes"
19
- "io"
19
+ "io/ioutil"
20
"log"
21
"os"
22
"path/filepath"
@@ -33,8 +33,7 @@ import (
33
// Environment variables in {$ENVIRONMENT_VARIABLE} notation
34
// will be replaced before parsing begins.
35
func Parse(filename string, input []byte) ([]ServerBlock, error) {
36
- input = replaceEnvVars(input)
37
- tokens, err := allTokens(filename, bytes.NewReader(input))
36
+ tokens, err := allTokens(filename, input)
37
if err != nil {
38
return nil, err
39
}
@@ -43,7 +42,7 @@ func Parse(filename string, input []byte) ([]ServerBlock, error) {
42
}
43
44
// replaceEnvVars replaces all occurrences of environment variables.
46
-func replaceEnvVars(input []byte) []byte {
45
+func replaceEnvVars(input []byte) ([]byte, error) {
46
var offset int
47
for {
48
begin := bytes.Index(input[offset:], spanOpen)
@@ -74,15 +73,19 @@ func replaceEnvVars(input []byte) []byte {
73
// continue at the end of the replacement
74
offset = begin + len(envVarValue)
75
}
77
- return input
76
+ return input, nil
77
}
78
79
// allTokens lexes the entire input, but does not parse it.
80
// It returns all the tokens from the input, unstructured
81
// and in order.
83
-func allTokens(filename string, input io.Reader) ([]Token, error) {
82
+func allTokens(filename string, input []byte) ([]Token, error) {
83
+ input, err := replaceEnvVars(input)
84
+ if err != nil {
85
+ return nil, err
86
+ }
87
l := new(lexer)
85
- err := l.load(input)
88
+ err = l.load(bytes.NewReader(input))
89
if err != nil {
90
return nil, err
91
}
@@ -368,7 +371,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
371
return nil, p.Errf("Could not import %s: is a directory", importFile)
372
}
373
371
- importedTokens, err := allTokens(importFile, file)
374
+ input, err := ioutil.ReadAll(file)
375
+ if err != nil {
376
+ return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
377
+ }
378
+
379
+ importedTokens, err := allTokens(importFile, input)
380
if err != nil {
381
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
382
}
caddyconfig/caddyfile/parse_test.go
+5
-3
@@ -19,12 +19,11 @@ import (
19
"io/ioutil"
20
"os"
21
"path/filepath"
22
- "strings"
22
"testing"
23
)
24
25
func TestAllTokens(t *testing.T) {
27
- input := strings.NewReader("a b c\nd e")
26
+ input := []byte("a b c\nd e")
27
expected := []string{"a", "b", "c", "d", "e"}
28
tokens, err := allTokens("TestAllTokens", input)
29
@@ -545,7 +544,10 @@ func TestEnvironmentReplacement(t *testing.T) {
544
expect: "}{$",
545
},
546
} {
548
- actual := replaceEnvVars([]byte(test.input))
547
+ actual, err := replaceEnvVars([]byte(test.input))
548
+ if err != nil {
549
+ t.Fatal(err)
550
+ }
551
if !bytes.Equal(actual, []byte(test.expect)) {
552
t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual)
553
}