diff --git a/context.go b/context.go index ecf74ba9..5b257d69 100644 --- a/context.go +++ b/context.go @@ -760,7 +760,7 @@ func (c *Context) ClientIP() string { if trusted && c.engine.ForwardedByClientIP && c.engine.RemoteIPHeaders != nil { for _, headerName := range c.engine.RemoteIPHeaders { - ip, valid := validateHeader(c.requestHeader(headerName)) + ip, valid := getIPFromHeader(c.requestHeader(headerName)) if valid { return ip } @@ -794,10 +794,17 @@ func (c *Context) RemoteIP() (net.IP, bool) { return remoteIP, false } -func validateHeader(header string) (clientIP string, valid bool) { +// getIPFromHeader return clientIP,valid when validate ip address +func getIPFromHeader(header string) (string, bool) { if header == "" { return "", false } + + var ( + clientIP string + valid bool + ) + items := strings.Split(header, ",") for i, ipStr := range items { ipStr = strings.TrimSpace(ipStr) @@ -814,7 +821,8 @@ func validateHeader(header string) (clientIP string, valid bool) { valid = true } } - return + + return clientIP, valid } // ContentType returns the Content-Type header of the request.