diff --git a/context/context.go b/context/context.go index ff27caed..09fbf3e5 100644 --- a/context/context.go +++ b/context/context.go @@ -904,24 +904,29 @@ func (ctx *Context) GetHeader(name string) string { } // GetDomain resolves and returns the server's domain. -func (ctx *Context) GetDomain() string { - 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:0:0:1": - // loopback. - return "localhost" - default: - if domain, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil { - host = domain - } - - return host - } +func GetDomain(hostport string) string { + host := hostport + if tmp, _, err := net.SplitHostPort(hostport); err == nil { + host = tmp } - return hostport + // 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:0:0:1": + // loopback. + return "localhost" + default: + if domain, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil { + host = domain + } + + return host + } +} + +// GetDomain resolves and returns the server's domain. +func (ctx *Context) GetDomain() string { + return GetDomain(ctx.Host()) } // IsAjax returns true if this request is an 'ajax request'( XMLHttpRequest) diff --git a/core/netutil/addr.go b/core/netutil/addr.go index 8a07b50b..8419cb14 100644 --- a/core/netutil/addr.go +++ b/core/netutil/addr.go @@ -21,7 +21,8 @@ func init() { // IsLoopbackSubdomain checks if a string is a subdomain or a hostname. var IsLoopbackSubdomain = func(s string) bool { - if strings.HasPrefix(s, "127.0.0.1:") || s == "127.0.0.1" { + if strings.HasPrefix(s, "127.0.0.1:") || s == "127.0.0.1" || + strings.HasPrefix(s, "0.0.0.0:") || s == "0.0.0.0" /* let's resolve that without regex (see below)*/ { return true } @@ -34,6 +35,17 @@ var IsLoopbackSubdomain = func(s string) bool { return valid } +// GetLoopbackSubdomain returns the part of the loopback subdomain. +func GetLoopbackSubdomain(s string) string { + if strings.HasPrefix(s, "127.0.0.1:") || s == "127.0.0.1" || + strings.HasPrefix(s, "0.0.0.0:") || s == "0.0.0.0" /* let's resolve that without regex (see below)*/ { + return s + } + + ss := loopbackSubRegex.FindString(s) + return ss +} + // IsLoopbackHost tries to catch the local addresses when a developer // navigates to a subdomain that its hostname differs from Application.Config.Addr. // Developer may want to override this function to return always false diff --git a/core/router/router_subdomain_redirect_wrapper.go b/core/router/router_subdomain_redirect_wrapper.go index 37a7b615..65b4c76e 100644 --- a/core/router/router_subdomain_redirect_wrapper.go +++ b/core/router/router_subdomain_redirect_wrapper.go @@ -112,6 +112,10 @@ func (s *subdomainRedirectWrapper) Wrapper(w http.ResponseWriter, r *http.Reques host := context.GetHost(r) root := s.root() + if loopback := netutil.GetLoopbackSubdomain(root); loopback != "" { + root = strings.Replace(root, loopback, context.GetDomain(host), 1) + } + // println("root: " + root) hasSubdomain := host != root if !hasSubdomain && !s.isFromRoot {