1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:10:54 +00:00

Merge pull request #2813 from mlgd/develop

Add IPV6 compatibility
This commit is contained in:
astaxie 2017-08-23 16:54:59 +08:00 committed by GitHub
commit c6cef853c7

View File

@ -20,6 +20,7 @@ import (
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"reflect"
@ -115,9 +116,8 @@ func (input *BeegoInput) Domain() string {
// if no host info in request, return localhost.
func (input *BeegoInput) Host() string {
if input.Context.Request.Host != "" {
hostParts := strings.Split(input.Context.Request.Host, ":")
if len(hostParts) > 0 {
return hostParts[0]
if hostPart, _, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
return hostPart
}
return input.Context.Request.Host
}
@ -206,20 +206,20 @@ func (input *BeegoInput) AcceptsJSON() bool {
// IP returns request client ip.
// if in proxy, return first proxy id.
// if error, return 127.0.0.1.
// if error, return RemoteAddr.
func (input *BeegoInput) IP() string {
ips := input.Proxy()
if len(ips) > 0 && ips[0] != "" {
rip := strings.Split(ips[0], ":")
return rip[0]
rip, _, err := net.SplitHostPort(ips[0])
if err != nil {
rip = ips[0]
}
ip := strings.Split(input.Context.Request.RemoteAddr, ":")
if len(ip) > 0 {
if ip[0] != "[" {
return ip[0]
return rip
}
if ip, _, err := net.SplitHostPort(input.Context.Request.RemoteAddr); err == nil {
return ip
}
return "127.0.0.1"
return input.Context.Request.RemoteAddr
}
// Proxy returns proxy client ips slice.
@ -253,9 +253,8 @@ func (input *BeegoInput) SubDomains() string {
// Port returns request client port.
// when error or empty, return 80.
func (input *BeegoInput) Port() int {
parts := strings.Split(input.Context.Request.Host, ":")
if len(parts) == 2 {
port, _ := strconv.Atoi(parts[1])
if _, portPart, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
port, _ := strconv.Atoi(portPart)
return port
}
return 80