@marcel91200 / caddy Mirror · read-only / commits / 0afbab86
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

httpcaddyfile: Improve directive sorting logic (#3658)

* 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 <mholt@users.noreply.github.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Francis Lavoie committed Aug 17, 2020 at 18:15 UTC 0afbab866794448fc0fe8b6cd6a299d7a078c2f1
2 files changed +63
caddyconfig/httpcaddyfile/directives.go
+8
@@ -398,6 +398,14 @@ func sortRoutes(routes []ConfigValue) {
398 if len(jPM) > 0 {
399 jPathLen = len(jPM[0])
400 }
401 +
402 + // if both directives have no path matcher, use whichever one
403 + // has any kind of matcher defined first.
404 + if iPathLen == 0 && jPathLen == 0 {
405 + return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0
406 + }
407 +
408 + // sort with the most-specific (longest) path first
409 return iPathLen > jPathLen
410 })
411 }
caddytest/integration/caddyfile_adapt/sort_directives_with_any_matcher_first.txt new
+55
@@ -0,0 +1,55 @@
1 +:80
2 +
3 +file_server
4 +
5 +@untrusted not remote_ip 10.1.1.0/24
6 +file_server @untrusted
7 +----------
8 +{
9 + "apps": {
10 + "http": {
11 + "servers": {
12 + "srv0": {
13 + "listen": [
14 + ":80"
15 + ],
16 + "routes": [
17 + {
18 + "match": [
19 + {
20 + "not": [
21 + {
22 + "remote_ip": {
23 + "ranges": [
24 + "10.1.1.0/24"
25 + ]
26 + }
27 + }
28 + ]
29 + }
30 + ],
31 + "handle": [
32 + {
33 + "handler": "file_server",
34 + "hide": [
35 + "Caddyfile"
36 + ]
37 + }
38 + ]
39 + },
40 + {
41 + "handle": [
42 + {
43 + "handler": "file_server",
44 + "hide": [
45 + "Caddyfile"
46 + ]
47 + }
48 + ]
49 + }
50 + ]
51 + }
52 + }
53 + }
54 + }
55 +}
\ No newline at end of file