mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Merge 6bf060f179e4706d5ad994e35e4cea4ef9f9ab04 into 52fcc5dbf6e94df33ad313858fb94b713e9d1b4a
This commit is contained in:
commit
659a470f34
@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@ -291,7 +292,13 @@ func (c *Context) ClientIP() string {
|
||||
return clientIP
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(c.Request.RemoteAddr)
|
||||
|
||||
// http.Request.RemoteAddr also contains the connection's port number; only return the client's ip
|
||||
host, _, err := net.SplitHostPort(c.Request.RemoteAddr)
|
||||
if err != nil {
|
||||
host = c.Request.RemoteAddr
|
||||
}
|
||||
return strings.TrimSpace(host)
|
||||
}
|
||||
|
||||
// ContentType returns the Content-Type header of the request.
|
||||
|
@ -532,6 +532,9 @@ func TestContextClientIP(t *testing.T) {
|
||||
|
||||
c.Request.Header.Del("X-Forwarded-For")
|
||||
assert.Equal(t, c.ClientIP(), "40.40.40.40")
|
||||
|
||||
c.Request.RemoteAddr = " 40.40.40.40:12345 "
|
||||
assert.Equal(t, c.ClientIP(), "40.40.40.40")
|
||||
}
|
||||
|
||||
func TestContextContentType(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user