mirror of https://github.com/gofiber/fiber.git
🐛 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
parent
e547bea49e
commit
640fd1f7c7
2
ctx.go
2
ctx.go
|
@ -749,7 +749,7 @@ iploop:
|
|||
j++
|
||||
}
|
||||
|
||||
for i < j && headerValue[i] == ' ' {
|
||||
for i < j && (headerValue[i] == ' ' || headerValue[i] == ',') {
|
||||
i++
|
||||
}
|
||||
|
||||
|
|
23
ctx_test.go
23
ctx_test.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue