🐛 fix parse ips return invalid in abnormal case (#2642)

* 🐛 fix parse ips return invalid in abnormal case

* ♻️ change benchmark to test cases

---------

Co-authored-by: Khúc Ngọc Huy <huykn0710@gmail.com>
pull/2647/head
huykn 2023-09-21 16:06:02 +07:00 committed by GitHub
parent e547bea49e
commit 640fd1f7c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

2
ctx.go
View File

@ -749,7 +749,7 @@ iploop:
j++
}
for i < j && headerValue[i] == ' ' {
for i < j && (headerValue[i] == ' ' || headerValue[i] == ',') {
i++
}

View File

@ -5317,3 +5317,26 @@ func Test_Ctx_RepeatParserWithSameStruct(t *testing.T) {
testDecodeParser(MIMEApplicationForm, "body_param=body_param")
testDecodeParser(MIMEMultipartForm+`;boundary="b"`, "--b\r\nContent-Disposition: form-data; name=\"body_param\"\r\n\r\nbody_param\r\n--b--")
}
// go test -run Test_Ctx_extractIPsFromHeader -v
func Test_Ctx_extractIPsFromHeader(t *testing.T) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.Request().Header.Set("x-forwarded-for", "1.1.1.1,8.8.8.8 , /n, \n,1.1, a.c, 6.,6., , a,,42.118.81.169,10.0.137.108")
ips := c.IPs()
res := ips[len(ips)-2]
utils.AssertEqual(t, "42.118.81.169", res)
}
// go test -run Test_Ctx_extractIPsFromHeader -v
func Test_Ctx_extractIPsFromHeader_EnableValidateIp(t *testing.T) {
app := New()
app.config.EnableIPValidation = true
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.Request().Header.Set("x-forwarded-for", "1.1.1.1,8.8.8.8 , /n, \n,1.1, a.c, 6.,6., , a,,42.118.81.169,10.0.137.108")
ips := c.IPs()
res := ips[len(ips)-2]
utils.AssertEqual(t, "42.118.81.169", res)
}