From ebbd99aefd3dc82d8c2b5ea3bc8ad5ef89ca9ab2 Mon Sep 17 00:00:00 2001 From: daheige Date: Wed, 14 Jul 2021 22:51:25 +0800 Subject: [PATCH] rename validateHeader to getIPFromHeader --- context.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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.