Skip to content

Commit a8bb4a6

Browse files
httpcaddyfile: Add {vars.*} placeholder shortcut, reverse vars sort order (#4726)
* httpcaddyfile: Add `{vars.*}` placeholder shortcut I'm yoinking this from my #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
1 parent 3a1e0db commit a8bb4a6

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

caddyconfig/httpcaddyfile/directives.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,29 @@ func sortRoutes(routes []ConfigValue) {
424424
jPathLen = len(jPM[0])
425425
}
426426

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+
}
432446

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+
}
435450
})
436451
}
437452

caddyconfig/httpcaddyfile/httptype.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
129129
{regexp.MustCompile(`{header\.([\w-]*)}`), "{http.request.header.$1}"},
130130
{regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"},
131131
{regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"},
132+
{regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"},
132133
}
133134

134135
for _, sb := range originalServerBlocks {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)