From b661becb2509df90427cb98c77eb27ac4314ff95 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 16 Aug 2020 11:23:04 -0400 Subject: [PATCH] httpcaddyfile: Flip `root` directive sort order --- caddyconfig/httpcaddyfile/directives.go | 9 +++ .../sort_root_with_path_last.txt | 57 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 caddytest/integration/caddyfile_adapt/sort_root_with_path_last.txt diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index e93fdd034f6..cd27597bf62 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -398,6 +398,15 @@ func sortRoutes(routes []ConfigValue) { if len(jPM) > 0 { jPathLen = len(jPM[0]) } + + // sort the "root" directive in reverse because we want the + // most-specific path last such that the root variable + // overrides a less-specific one. + if iDir == "root" { + return iPathLen < jPathLen + } + + // sort any other directives with the most-specific path first return iPathLen > jPathLen }) } diff --git a/caddytest/integration/caddyfile_adapt/sort_root_with_path_last.txt b/caddytest/integration/caddyfile_adapt/sort_root_with_path_last.txt new file mode 100644 index 00000000000..9af3895b7bd --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/sort_root_with_path_last.txt @@ -0,0 +1,57 @@ +:80 + +root * /foo +root /z* /bar + +file_server +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":80" + ], + "routes": [ + { + "group": "group1", + "handle": [ + { + "handler": "vars", + "root": "/foo" + } + ] + }, + { + "group": "group1", + "match": [ + { + "path": [ + "/z*" + ] + } + ], + "handle": [ + { + "handler": "vars", + "root": "/bar" + } + ] + }, + { + "handle": [ + { + "handler": "file_server", + "hide": [ + "Caddyfile" + ] + } + ] + } + ] + } + } + } + } +} \ No newline at end of file