summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2021-04-22 20:29:04 -0400
committerGitHub <noreply@github.com>2021-04-22 18:29:04 -0600
commita8d45277ca387ca4769fb4e881d242a5896cca5a (patch)
tree35dbfdcb7371df0eb6e42806836f271f25b769b6
parent1e218e1d2e3986b7cc1cd7335b43f11a4f194b6e (diff)
caddyfile: Fix `import` replacing unrelated placeholders (#4129)
* caddyfile: Fix `import` replacing unrelated placeholders See https://caddy.community/t/snippet-issue-works-outside-snippet/12231 So it turns out that `NewReplacer()` gives a replacer with some global defaults (like `{env.*}` and some system and time placeholders), which is not ideal when running `import` because we just want to replace `{args.*}` only, and nothing else. * caddyfile: Add test
-rwxr-xr-xcaddyconfig/caddyfile/parse.go2
-rw-r--r--caddytest/integration/caddyfile_adapt/import_args_snippet_env_placeholder.txt31
-rw-r--r--replacer.go12
3 files changed, 44 insertions, 1 deletions
diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go
index 52abf47..96491bb 100755
--- a/caddyconfig/caddyfile/parse.go
+++ b/caddyconfig/caddyfile/parse.go
@@ -321,7 +321,7 @@ func (p *parser) doImport() error {
args := p.RemainingArgs()
// add args to the replacer
- repl := caddy.NewReplacer()
+ repl := caddy.NewEmptyReplacer()
for index, arg := range args {
repl.Set("args."+strconv.Itoa(index), arg)
}
diff --git a/caddytest/integration/caddyfile_adapt/import_args_snippet_env_placeholder.txt b/caddytest/integration/caddyfile_adapt/import_args_snippet_env_placeholder.txt
new file mode 100644
index 0000000..1bc907e
--- /dev/null
+++ b/caddytest/integration/caddyfile_adapt/import_args_snippet_env_placeholder.txt
@@ -0,0 +1,31 @@
+(foo) {
+ respond {env.FOO}
+}
+
+:80 {
+ import foo
+}
+----------
+{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":80"
+ ],
+ "routes": [
+ {
+ "handle": [
+ {
+ "body": "{env.FOO}",
+ "handler": "static_response"
+ }
+ ]
+ }
+ ]
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/replacer.go b/replacer.go
index 29d8e26..e6cfded 100644
--- a/replacer.go
+++ b/replacer.go
@@ -36,6 +36,18 @@ func NewReplacer() *Replacer {
return rep
}
+// NewEmptyReplacer returns a new Replacer,
+// without the global default replacements.
+func NewEmptyReplacer() *Replacer {
+ rep := &Replacer{
+ static: make(map[string]interface{}),
+ }
+ rep.providers = []ReplacerFunc{
+ rep.fromStatic,
+ }
+ return rep
+}
+
// Replacer can replace values in strings.
// A default/empty Replacer is not valid;
// use NewReplacer to make one.