From acf7dea68fe6ace110221faa26c2503a1ea432ce Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 14 Oct 2019 12:09:43 -0600 Subject: 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) --- modules/caddyhttp/replacer.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'modules/caddyhttp/replacer.go') diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index 205e4ed..db329be 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -146,14 +146,18 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon if err != nil { return "", false } - hostLabels := strings.Split(req.Host, ".") + reqHost, _, err := net.SplitHostPort(req.Host) + if err != nil { + reqHost = req.Host // OK; assume there was no port + } + hostLabels := strings.Split(reqHost, ".") if idx < 0 { return "", false } - if idx >= len(hostLabels) { + if idx > len(hostLabels) { return "", true } - return hostLabels[idx], true + return hostLabels[len(hostLabels)-idx-1], true } // path parts -- cgit v1.2.3