Browse Source

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
master
Francis Lavoie 3 years ago
committed by GitHub
parent
commit
a8bb4a665a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      caddyconfig/httpcaddyfile/directives.go
  2. 1
      caddyconfig/httpcaddyfile/httptype.go
  3. 59
      caddytest/integration/caddyfile_adapt/sort_vars_in_reverse.txt

15
caddyconfig/httpcaddyfile/directives.go

@ -424,6 +424,20 @@ func sortRoutes(routes []ConfigValue) {
jPathLen = len(jPM[0]) jPathLen = len(jPM[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 // if both directives have no path matcher, use whichever one
// has any kind of matcher defined first. // has any kind of matcher defined first.
if iPathLen == 0 && jPathLen == 0 { if iPathLen == 0 && jPathLen == 0 {
@ -432,6 +446,7 @@ func sortRoutes(routes []ConfigValue) {
// sort with the most-specific (longest) path first // sort with the most-specific (longest) path first
return iPathLen > jPathLen return iPathLen > jPathLen
}
}) })
} }

1
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(`{header\.([\w-]*)}`), "{http.request.header.$1}"},
{regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"}, {regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"},
{regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"}, {regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"},
{regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"},
} }
for _, sb := range originalServerBlocks { for _, sb := range originalServerBlocks {

59
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"
}
]
}
]
}
}
}
}
}
Loading…
Cancel
Save