@marcel91200 / caddy Mirror · read-only / commits / 0ba8786b
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 21:00 UTC

caddyfile: Allow `block` to do nothing if nothing passed to `import` (#7206)

Artur H. committed Sep 12, 2025 at 22:29 UTC 0ba8786b35eaec5ec53f8562bec9587e6b1e6a8b
3 files changed +120 -11
caddyconfig/caddyfile/parse.go
+6 -11
@@ -533,29 +533,24 @@ func (p *parser) doImport(nesting int) error {
533 }
534 // if it is {block}, we substitute with all tokens in the block
535 // if it is {blocks.*}, we substitute with the tokens in the mapping for the *
536 - var skip bool
536 var tokensToAdd []Token
537 + foundBlockDirective := false
538 switch {
539 case token.Text == "{block}":
540 + foundBlockDirective = true
541 tokensToAdd = blockTokens
542 case strings.HasPrefix(token.Text, "{blocks.") && strings.HasSuffix(token.Text, "}"):
543 + foundBlockDirective = true
544 // {blocks.foo.bar} will be extracted to key `foo.bar`
545 blockKey := strings.TrimPrefix(strings.TrimSuffix(token.Text, "}"), "{blocks.")
546 val, ok := blockMapping[blockKey]
547 if ok {
548 tokensToAdd = val
549 }
548 - default:
549 - skip = true
550 }
551 - if !skip {
552 - if len(tokensToAdd) == 0 {
553 - // if there is no content in the snippet block, don't do any replacement
554 - // this allows snippets which contained {block}/{block.*} before this change to continue functioning as normal
555 - tokensCopy = append(tokensCopy, token)
556 - } else {
557 - tokensCopy = append(tokensCopy, tokensToAdd...)
558 - }
551 +
552 + if foundBlockDirective {
553 + tokensCopy = append(tokensCopy, tokensToAdd...)
554 continue
555 }
556
caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_block.caddyfiletest new
+57
@@ -0,0 +1,57 @@
1 +(snippet) {
2 + header {
3 + reverse_proxy localhost:3000
4 + {block}
5 + }
6 +}
7 +
8 +example.com {
9 + import snippet
10 +}
11 +----------
12 +{
13 + "apps": {
14 + "http": {
15 + "servers": {
16 + "srv0": {
17 + "listen": [
18 + ":443"
19 + ],
20 + "routes": [
21 + {
22 + "match": [
23 + {
24 + "host": [
25 + "example.com"
26 + ]
27 + }
28 + ],
29 + "handle": [
30 + {
31 + "handler": "subroute",
32 + "routes": [
33 + {
34 + "handle": [
35 + {
36 + "handler": "headers",
37 + "response": {
38 + "set": {
39 + "Reverse_proxy": [
40 + "localhost:3000"
41 + ]
42 + }
43 + }
44 + }
45 + ]
46 + }
47 + ]
48 + }
49 + ],
50 + "terminal": true
51 + }
52 + ]
53 + }
54 + }
55 + }
56 + }
57 +}
\ No newline at end of file
caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_key_block.caddyfiletest new
+57
@@ -0,0 +1,57 @@
1 +(snippet) {
2 + header {
3 + reverse_proxy localhost:3000
4 + {blocks.content_type}
5 + }
6 +}
7 +
8 +example.com {
9 + import snippet
10 +}
11 +----------
12 +{
13 + "apps": {
14 + "http": {
15 + "servers": {
16 + "srv0": {
17 + "listen": [
18 + ":443"
19 + ],
20 + "routes": [
21 + {
22 + "match": [
23 + {
24 + "host": [
25 + "example.com"
26 + ]
27 + }
28 + ],
29 + "handle": [
30 + {
31 + "handler": "subroute",
32 + "routes": [
33 + {
34 + "handle": [
35 + {
36 + "handler": "headers",
37 + "response": {
38 + "set": {
39 + "Reverse_proxy": [
40 + "localhost:3000"
41 + ]
42 + }
43 + }
44 + }
45 + ]
46 + }
47 + ]
48 + }
49 + ],
50 + "terminal": true
51 + }
52 + ]
53 + }
54 + }
55 + }
56 + }
57 +}
\ No newline at end of file