mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
Merge 819be6b14535772976cbab5924ee58859a8d6ebd into 80d3cc6b7512e487bcf58daf497e6b5206216818
This commit is contained in:
commit
6f28361184
@ -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
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user