From cbe336a5839c3eabd3ef41ea814d1f316b465544 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 30 May 2020 20:32:06 +0300 Subject: [PATCH] Context.GetDomain: use net.SplitHostPort better Former-commit-id: e0a13062f9c4d37e48a82367d23bc16be8f1d1ce --- context/context.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/context/context.go b/context/context.go index be200008..8e78d1ea 100644 --- a/context/context.go +++ b/context/context.go @@ -1942,21 +1942,23 @@ func (ctx *context) GetHeader(name string) string { // GetDomain resolves and returns the server's domain. func (ctx *context) GetDomain() string { - host := ctx.Host() - if portIdx := strings.IndexByte(host, ':'); portIdx > 0 { - host = host[0:portIdx] - } + hostport := ctx.Host() + if host, _, err := net.SplitHostPort(hostport); err == nil { + // has port. + switch host { + case "127.0.0.1", "0.0.0.0", "::1", "[::1]", "0:0:0:0:0:0:0", "0:0:0:0:0:0:1": + // loopback. + return "localhost" + default: + if domain, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil { + host = domain + } - switch host { - case "127.0.0.1", "0.0.0.0", "::1", "[::1]", "0:0:0:0:0:0:0", "0:0:0:0:0:0:1": - return "localhost" - default: - if domain, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil { - host = domain + return host } - - return host } + + return hostport } // IsAjax returns true if this request is an 'ajax request'( XMLHttpRequest)