From b86e419aab3c4f84a44326dc61542bf3f61552bf Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 24 Apr 2022 02:21:34 -0400 Subject: [PATCH 1/2] 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. --- caddyconfig/httpcaddyfile/httptype.go | 1 + 1 file changed, 1 insertion(+) diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index f5dd68a684c..e364ed99fcb 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -129,6 +129,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock, {regexp.MustCompile(`{header\.([\w-]*)}`), "{http.request.header.$1}"}, {regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"}, {regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"}, + {regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"}, } for _, sb := range originalServerBlocks { From c32e5cd075ae48add35557a28f579eacf8ba5b8c Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 24 Apr 2022 02:40:48 -0400 Subject: [PATCH 2/2] Sort vars by matchers in reverse --- caddyconfig/httpcaddyfile/directives.go | 29 ++++++--- .../caddyfile_adapt/sort_vars_in_reverse.txt | 59 +++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index 6b80e346daa..aaa8cb014c9 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -424,14 +424,29 @@ func sortRoutes(routes []ConfigValue) { jPathLen = len(jPM[0]) } - // if both directives have no path matcher, use whichever one - // has any kind of matcher defined first. - if iPathLen == 0 && jPathLen == 0 { - return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0 - } + // some directives involve setting values which can overwrite + // eachother, so it makes most sense to reverse the order so + // that the lease specific matcher is first; everything else + // has most-specific matcher first + if iDir == "vars" { + // if both directives have no path matcher, use whichever one + // has no matcher first. + if iPathLen == 0 && jPathLen == 0 { + return len(iRoute.MatcherSetsRaw) == 0 && len(jRoute.MatcherSetsRaw) > 0 + } + + // sort with the least-specific (shortest) path first + return iPathLen < jPathLen + } else { + // if both directives have no path matcher, use whichever one + // has any kind of matcher defined first. + if iPathLen == 0 && jPathLen == 0 { + return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0 + } - // sort with the most-specific (longest) path first - return iPathLen > jPathLen + // sort with the most-specific (longest) path first + return iPathLen > jPathLen + } }) } diff --git a/caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt b/caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt new file mode 100644 index 00000000000..dff75e1e123 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt @@ -0,0 +1,59 @@ +:80 + +vars /foobar foo last +vars /foo foo middle +vars * foo first +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":80" + ], + "routes": [ + { + "handle": [ + { + "foo": "first", + "handler": "vars" + } + ] + }, + { + "match": [ + { + "path": [ + "/foo" + ] + } + ], + "handle": [ + { + "foo": "middle", + "handler": "vars" + } + ] + }, + { + "match": [ + { + "path": [ + "/foobar" + ] + } + ], + "handle": [ + { + "foo": "last", + "handler": "vars" + } + ] + } + ] + } + } + } + } +} \ No newline at end of file