Merge 819be6b14535772976cbab5924ee58859a8d6ebd into 80d3cc6b7512e487bcf58daf497e6b5206216818

This commit is contained in:
jarm 2015-05-24 13:29:07 +00:00
commit 6f28361184
2 changed files with 7 additions and 1 deletions

View File

@ -280,8 +280,10 @@ func (c *Context) ClientIP() string {
return clientIP
}
clientIP = c.Request.Header.Get("X-Forwarded-For")
clientIP = strings.Split(clientIP, ",")[0]
if len(clientIP) > 0 {
if strings.Contains(clientIP, ",") {
clientIP = strings.Split(clientIP, ",")[0]
}
return strings.TrimSpace(clientIP)
}
return c.Request.RemoteAddr

View File

@ -412,6 +412,10 @@ func TestContextClientIP(t *testing.T) {
assert.Equal(t, c.ClientIP(), "30.30.30.30")
c.Request.Header.Del("X-Forwarded-For")
assert.Equal(t, c.ClientIP(), "40.40.40.40")
c.Request.Header.Set("X-Forwarded-For", "30.30.30.30")
assert.Equal(t, c.ClientIP(), "30.30.30.30")
c.Request.Header.Del("X-Forwarded-For")
}
func TestContextContentType(t *testing.T) {