fix(middleware/bodylimit): 添加 BodyStream nil 检查防止空指针
在设置 body stream 限制前检查 stream 是否为 nil, 避免在无请求体的请求上调用 SetBodyStream 导致异常。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d5b4509014
commit
5b92e30b59
@ -157,16 +157,19 @@ func (bl *BodyLimit) Process(next fasthttp.RequestHandler) fasthttp.RequestHandl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 对于 chunked 传输或没有 Content-Length 的请求
|
// 对于 chunked 传输或没有 Content-Length 的请求
|
||||||
// 设置最大读取限制
|
// 只有当 BodyStream 存在时才设置限制
|
||||||
ctx.Request.SetBodyStream(ctx.Request.BodyStream(), int(limit))
|
bodyStream := ctx.Request.BodyStream()
|
||||||
|
if bodyStream != nil {
|
||||||
|
ctx.Request.SetBodyStream(bodyStream, int(limit))
|
||||||
|
|
||||||
// 包装请求体读取以检测超限
|
// 包装请求体读取以检测超限
|
||||||
limitedReader := &limitedBodyReader{
|
limitedReader := &limitedBodyReader{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
original: ctx.Request.BodyStream(),
|
original: bodyStream,
|
||||||
|
}
|
||||||
|
ctx.Request.SetBodyStream(limitedReader, -1)
|
||||||
}
|
}
|
||||||
ctx.Request.SetBodyStream(limitedReader, -1)
|
|
||||||
|
|
||||||
next(ctx)
|
next(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user