mirror of
https://github.com/gofiber/fiber.git
synced 2025-07-13 23:58:29 +00:00
Merge pull request #671 from kiyonlin/fix-ctx-ips
🩹 when X-Forwarded-For is empty, ctx.Ips() should be empty
This commit is contained in:
commit
415a6026c1
3
ctx.go
3
ctx.go
@ -502,6 +502,9 @@ func (ctx *Ctx) IP() string {
|
||||
// IPs returns an string slice of IP addresses specified in the X-Forwarded-For request header.
|
||||
func (ctx *Ctx) IPs() (ips []string) {
|
||||
header := ctx.Fasthttp.Request.Header.Peek(HeaderXForwardedFor)
|
||||
if len(header) == 0 {
|
||||
return
|
||||
}
|
||||
ips = make([]string, bytes.Count(header, []byte(","))+1)
|
||||
var commaPos, i int
|
||||
for {
|
||||
|
@ -706,6 +706,9 @@ func Test_Ctx_IPs(t *testing.T) {
|
||||
defer app.ReleaseCtx(ctx)
|
||||
ctx.Fasthttp.Request.Header.Set(HeaderXForwardedFor, "127.0.0.1, 127.0.0.1, 127.0.0.1")
|
||||
utils.AssertEqual(t, []string{"127.0.0.1", "127.0.0.1", "127.0.0.1"}, ctx.IPs())
|
||||
|
||||
ctx.Fasthttp.Request.Header.Set(HeaderXForwardedFor, "")
|
||||
utils.AssertEqual(t, 0, len(ctx.IPs()))
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_Ctx_IPs -benchmem -count=4
|
||||
|
Loading…
x
Reference in New Issue
Block a user