From 9410bd0b86e2436117f53d7671ada7be304d5408 Mon Sep 17 00:00:00 2001 From: daheige Date: Sat, 5 Jun 2021 14:14:20 +0800 Subject: [PATCH] rename validateHeader to getIPFromHeader and code adjust --- context.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index a6775abb..2a8d7745 100644 --- a/context.go +++ b/context.go @@ -749,7 +749,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 } @@ -783,10 +783,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) @@ -803,7 +810,8 @@ func validateHeader(header string) (clientIP string, valid bool) { valid = true } } - return + + return clientIP, valid } // ContentType returns the Content-Type header of the request.