@marcel91200 / caddy Mirror · read-only / commits / acf7dea6
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

caddyhttp: host labels placeholders endianness from right->left

https://caddy.community/t/labeln-placeholder-endian-issue/5366 (I thought we had this before but it must have gotten lost somewhere)

Matthew Holt committed Oct 14, 2019 at 12:09 UTC acf7dea68fe6ace110221faa26c2503a1ea432ce
2 files changed +17 -5
modules/caddyhttp/replacer.go
+7 -3
@@ -146,14 +146,18 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon
146 if err != nil {
147 return "", false
148 }
149 - hostLabels := strings.Split(req.Host, ".")
149 + reqHost, _, err := net.SplitHostPort(req.Host)
150 + if err != nil {
151 + reqHost = req.Host // OK; assume there was no port
152 + }
153 + hostLabels := strings.Split(reqHost, ".")
154 if idx < 0 {
155 return "", false
156 }
153 - if idx >= len(hostLabels) {
157 + if idx > len(hostLabels) {
158 return "", true
159 }
156 - return hostLabels[idx], true
160 + return hostLabels[len(hostLabels)-idx-1], true
161 }
162
163 // path parts
modules/caddyhttp/replacer_test.go
+10 -2
@@ -34,8 +34,8 @@ func TestHTTPVarReplacement(t *testing.T) {
34 addHTTPVarsToReplacer(repl, req, res)
35
36 for i, tc := range []struct {
37 - input string
38 - expect string
37 + input string
38 + expect string
39 }{
40 {
41 input: "{http.request.scheme}",
@@ -61,6 +61,14 @@ func TestHTTPVarReplacement(t *testing.T) {
61 input: "{http.request.remote.port}",
62 expect: "1234",
63 },
64 + {
65 + input: "{http.request.host.labels.0}",
66 + expect: "com",
67 + },
68 + {
69 + input: "{http.request.host.labels.1}",
70 + expect: "example",
71 + },
72 } {
73 actual := repl.ReplaceAll(tc.input, "<empty>")
74 if actual != tc.expect {