mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 14:49:27 +08:00
Merge 3f68b0b0b42901e2d897ad5b7d26c6952d6e8ccb into dcaa4296d111981ffb31ac3eba90bb63e1eb5ab9
This commit is contained in:
commit
12e65cd9c9
@ -125,3 +125,9 @@ func validate(obj any) error {
|
||||
}
|
||||
return Validator.ValidateStruct(obj)
|
||||
}
|
||||
|
||||
// MaxBodySize is the maximum request body size in bytes that BSON and
|
||||
// Protobuf binders will read. If the request body exceeds this limit,
|
||||
// the binding returns an error. Defaults to 32 MB.
|
||||
var MaxBodySize int64 = 32 << 20
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@ -18,11 +19,14 @@ func (bsonBinding) Name() string {
|
||||
}
|
||||
|
||||
func (b bsonBinding) Bind(req *http.Request, obj any) error {
|
||||
buf, err := io.ReadAll(req.Body)
|
||||
if err == nil {
|
||||
err = b.BindBody(buf, obj)
|
||||
body, err := io.ReadAll(io.LimitReader(req.Body, MaxBodySize+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
if int64(len(body)) > MaxBodySize {
|
||||
return errors.New("request body too large")
|
||||
}
|
||||
return b.BindBody(body, obj)
|
||||
}
|
||||
|
||||
func (bsonBinding) BindBody(body []byte, obj any) error {
|
||||
|
||||
@ -19,11 +19,14 @@ func (protobufBinding) Name() string {
|
||||
}
|
||||
|
||||
func (b protobufBinding) Bind(req *http.Request, obj any) error {
|
||||
buf, err := io.ReadAll(req.Body)
|
||||
body, err := io.ReadAll(io.LimitReader(req.Body, MaxBodySize+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.BindBody(buf, obj)
|
||||
if int64(len(body)) > MaxBodySize {
|
||||
return errors.New("request body too large")
|
||||
}
|
||||
return b.BindBody(body, obj)
|
||||
}
|
||||
|
||||
func (protobufBinding) BindBody(body []byte, obj any) error {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user