From d32654ab9393be43edf825bce783f24443a3355b Mon Sep 17 00:00:00 2001 From: Emir Ribic Date: Sat, 8 Sep 2018 07:07:48 +0200 Subject: [PATCH 1/2] Simplify REALIP method --- context.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index cf780c513..914364764 100644 --- a/context.go +++ b/context.go @@ -256,14 +256,14 @@ func (c *context) Scheme() string { } func (c *context) RealIP() string { - ra := c.request.RemoteAddr if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" { - ra = strings.Split(ip, ", ")[0] - } else if ip := c.request.Header.Get(HeaderXRealIP); ip != "" { - ra = ip - } else { - ra, _, _ = net.SplitHostPort(ra) + return strings.Split(ip, ", ")[0] } + if ip := c.request.Header.Get(HeaderXRealIP); ip != "" { + return ip + } + ra := c.request.RemoteAddr + ra, _, _ = net.SplitHostPort(ra) return ra } From 648bf02710611aa6202df4e5af588b9cb093bf1f Mon Sep 17 00:00:00 2001 From: Emir Ribic Date: Sat, 8 Sep 2018 07:10:33 +0200 Subject: [PATCH 2/2] Remove alloc --- context.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/context.go b/context.go index 914364764..9a9d133ed 100644 --- a/context.go +++ b/context.go @@ -262,8 +262,7 @@ func (c *context) RealIP() string { if ip := c.request.Header.Get(HeaderXRealIP); ip != "" { return ip } - ra := c.request.RemoteAddr - ra, _, _ = net.SplitHostPort(ra) + ra, _, _ := net.SplitHostPort(c.request.RemoteAddr) return ra }