mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
fix SubdomainRedirect on loopbacks
fix: https://github.com/kataras/iris/issues/1584#issuecomment-674001454
This commit is contained in:
parent
dc35391ceb
commit
9f0872719f
|
@ -904,9 +904,12 @@ func (ctx *Context) GetHeader(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDomain resolves and returns the server's domain.
|
// GetDomain resolves and returns the server's domain.
|
||||||
func (ctx *Context) GetDomain() string {
|
func GetDomain(hostport string) string {
|
||||||
hostport := ctx.Host()
|
host := hostport
|
||||||
if host, _, err := net.SplitHostPort(hostport); err == nil {
|
if tmp, _, err := net.SplitHostPort(hostport); err == nil {
|
||||||
|
host = tmp
|
||||||
|
}
|
||||||
|
|
||||||
// has port.
|
// has port.
|
||||||
switch host {
|
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":
|
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":
|
||||||
|
@ -919,9 +922,11 @@ func (ctx *Context) GetDomain() string {
|
||||||
|
|
||||||
return host
|
return host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostport
|
// 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)
|
// IsAjax returns true if this request is an 'ajax request'( XMLHttpRequest)
|
||||||
|
|
|
@ -21,7 +21,8 @@ func init() {
|
||||||
|
|
||||||
// IsLoopbackSubdomain checks if a string is a subdomain or a hostname.
|
// IsLoopbackSubdomain checks if a string is a subdomain or a hostname.
|
||||||
var IsLoopbackSubdomain = func(s string) bool {
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +35,17 @@ var IsLoopbackSubdomain = func(s string) bool {
|
||||||
return valid
|
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
|
// IsLoopbackHost tries to catch the local addresses when a developer
|
||||||
// navigates to a subdomain that its hostname differs from Application.Config.Addr.
|
// navigates to a subdomain that its hostname differs from Application.Config.Addr.
|
||||||
// Developer may want to override this function to return always false
|
// Developer may want to override this function to return always false
|
||||||
|
|
|
@ -112,6 +112,10 @@ func (s *subdomainRedirectWrapper) Wrapper(w http.ResponseWriter, r *http.Reques
|
||||||
|
|
||||||
host := context.GetHost(r)
|
host := context.GetHost(r)
|
||||||
root := s.root()
|
root := s.root()
|
||||||
|
if loopback := netutil.GetLoopbackSubdomain(root); loopback != "" {
|
||||||
|
root = strings.Replace(root, loopback, context.GetDomain(host), 1)
|
||||||
|
}
|
||||||
|
// println("root: " + root)
|
||||||
hasSubdomain := host != root
|
hasSubdomain := host != root
|
||||||
|
|
||||||
if !hasSubdomain && !s.isFromRoot {
|
if !hasSubdomain && !s.isFromRoot {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user