Context.GetDomain: use net.SplitHostPort better
@@ -1942,13 +1942,12 @@ 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 {
@@ -1959,6 +1958,9 @@ func (ctx *context) GetDomain() string {
}
}
+ return hostport
+}
+
// IsAjax returns true if this request is an 'ajax request'( XMLHttpRequest)
//
// There is no a 100% way of knowing that a request was made via Ajax.
