Merge pull request #671 from kiyonlin/fix-ctx-ips

🩹 when X-Forwarded-For is empty, ctx.Ips() should be empty
pull/675/head
fenny 2020-07-24 17:28:26 +02:00 committed by GitHub
commit 415a6026c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

3
ctx.go
View File

@ -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 {

View File

@ -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