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 的请求
|
||||
// 设置最大读取限制
|
||||
ctx.Request.SetBodyStream(ctx.Request.BodyStream(), int(limit))
|
||||
// 只有当 BodyStream 存在时才设置限制
|
||||
bodyStream := ctx.Request.BodyStream()
|
||||
if bodyStream != nil {
|
||||
ctx.Request.SetBodyStream(bodyStream, int(limit))
|
||||
|
||||
// 包装请求体读取以检测超限
|
||||
limitedReader := &limitedBodyReader{
|
||||
ctx: ctx,
|
||||
limit: limit,
|
||||
original: ctx.Request.BodyStream(),
|
||||
// 包装请求体读取以检测超限
|
||||
limitedReader := &limitedBodyReader{
|
||||
ctx: ctx,
|
||||
limit: limit,
|
||||
original: bodyStream,
|
||||
}
|
||||
ctx.Request.SetBodyStream(limitedReader, -1)
|
||||
}
|
||||
ctx.Request.SetBodyStream(limitedReader, -1)
|
||||
|
||||
next(ctx)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user