@marcel91200 / caddy Mirror · read-only / commits / a8bb4a66
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 13, 19:15 UTC

httpcaddyfile: Add `{vars.*}` placeholder shortcut, reverse `vars` sort order (#4726)

* httpcaddyfile: Add `{vars.*}` placeholder shortcut I'm yoinking this from my https://github.com/caddyserver/caddy/pull/4657 PR because I think we should get this in ASAP for v2.5.0 along with the new `vars` directive. * Sort vars by matchers in reverse

Francis Lavoie committed Apr 25, 2022 at 12:47 UTC a8bb4a665af358f61a7ac0eabac8df2110cb6a36
3 files changed +82 -7
caddyconfig/httpcaddyfile/directives.go
+22 -7
@@ -424,14 +424,29 @@ func sortRoutes(routes []ConfigValue) {
424 jPathLen = len(jPM[0])
425 }
426
427 - // if both directives have no path matcher, use whichever one
428 - // has any kind of matcher defined first.
429 - if iPathLen == 0 && jPathLen == 0 {
430 - return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0
431 - }
427 + // some directives involve setting values which can overwrite
428 + // eachother, so it makes most sense to reverse the order so
429 + // that the lease specific matcher is first; everything else
430 + // has most-specific matcher first
431 + if iDir == "vars" {
432 + // if both directives have no path matcher, use whichever one
433 + // has no matcher first.
434 + if iPathLen == 0 && jPathLen == 0 {
435 + return len(iRoute.MatcherSetsRaw) == 0 && len(jRoute.MatcherSetsRaw) > 0
436 + }
437 +
438 + // sort with the least-specific (shortest) path first
439 + return iPathLen < jPathLen
440 + } else {
441 + // if both directives have no path matcher, use whichever one
442 + // has any kind of matcher defined first.
443 + if iPathLen == 0 && jPathLen == 0 {
444 + return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0
445 + }
446
433 - // sort with the most-specific (longest) path first
434 - return iPathLen > jPathLen
447 + // sort with the most-specific (longest) path first
448 + return iPathLen > jPathLen
449 + }
450 })
451 }
452
caddyconfig/httpcaddyfile/httptype.go
+1
@@ -129,6 +129,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
129 {regexp.MustCompile(`{header\.([\w-]*)}`), "{http.request.header.$1}"},
130 {regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"},
131 {regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"},
132 + {regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"},
133 }
134
135 for _, sb := range originalServerBlocks {
caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt new
+59
@@ -0,0 +1,59 @@
1 +:80
2 +
3 +vars /foobar foo last
4 +vars /foo foo middle
5 +vars * foo first
6 +----------
7 +{
8 + "apps": {
9 + "http": {
10 + "servers": {
11 + "srv0": {
12 + "listen": [
13 + ":80"
14 + ],
15 + "routes": [
16 + {
17 + "handle": [
18 + {
19 + "foo": "first",
20 + "handler": "vars"
21 + }
22 + ]
23 + },
24 + {
25 + "match": [
26 + {
27 + "path": [
28 + "/foo"
29 + ]
30 + }
31 + ],
32 + "handle": [
33 + {
34 + "foo": "middle",
35 + "handler": "vars"
36 + }
37 + ]
38 + },
39 + {
40 + "match": [
41 + {
42 + "path": [
43 + "/foobar"
44 + ]
45 + }
46 + ],
47 + "handle": [
48 + {
49 + "foo": "last",
50 + "handler": "vars"
51 + }
52 + ]
53 + }
54 + ]
55 + }
56 + }
57 + }
58 + }
59 +}
\ No newline at end of file