From ef802ea01ba6990cc760683470221110129f89db Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 16 Aug 2020 11:23:19 -0400 Subject: [PATCH] httpcaddyfile: Sort directives with any matcher before those with none --- caddyconfig/httpcaddyfile/directives.go | 6 ++ ...sort_directives_with_any_matcher_first.txt | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 caddytest/integration/caddyfile_adapt/sort_directives_with_any_matcher_first.txt diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index cd27597bf62..b9eee0e9f95 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -399,6 +399,12 @@ 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) > len(jRoute.MatcherSetsRaw) + } + // sort the "root" directive in reverse because we want the // most-specific path last such that the root variable // overrides a less-specific one. diff --git a/caddytest/integration/caddyfile_adapt/sort_directives_with_any_matcher_first.txt b/caddytest/integration/caddyfile_adapt/sort_directives_with_any_matcher_first.txt new file mode 100644 index 00000000000..d15f56bffc4 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/sort_directives_with_any_matcher_first.txt @@ -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" + ] + } + ] + } + ] + } + } + } + } +} \ No newline at end of file