Skip to content

Commit

Permalink
httpcaddyfile: Improve directive sorting logic (#3658)
Browse files Browse the repository at this point in the history
* httpcaddyfile: Flip `root` directive sort order

* httpcaddyfile: Sort directives with any matcher before those with none

* httpcaddyfile: Generalize reverse sort directives, improve logic

* httpcaddyfile: Fix "spelling" issue

* httpcaddyfile: Turns out the second change precludes the first


httpcaddyfile: Delete test that no longer makes sense

* httpcaddyfile: Shorten logic

Co-authored-by: Matt Holt <[email protected]>

Co-authored-by: Matt Holt <[email protected]>
  • Loading branch information
francislavoie and mholt authored Aug 17, 2020
1 parent fc65320 commit 0afbab8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ func sortRoutes(routes []ConfigValue) {
if len(jPM) > 0 {
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
}

// sort with the most-specific (longest) path first
return iPathLen > jPathLen
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
:80

file_server

@untrusted not remote_ip 10.1.1.0/24
file_server @untrusted
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"not": [
{
"remote_ip": {
"ranges": [
"10.1.1.0/24"
]
}
}
]
}
],
"handle": [
{
"handler": "file_server",
"hide": [
"Caddyfile"
]
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"Caddyfile"
]
}
]
}
]
}
}
}
}
}

0 comments on commit 0afbab8

Please sign in to comment.