mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 22:53:34 +08:00
BSON and Protobuf binders used io.ReadAll with no cap. Add binding.MaxBodyBytes (default 0, unlimited) and a shared reader so apps can bound those binders without changing historical defaults. Oversized bodies return *http.MaxBytesError, which MustBindWith already maps to HTTP 413. Fixes #4759 Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
30 lines
572 B
Go
30 lines
572 B
Go
// Copyright 2025 Gin Core Team. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package binding
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type bsonBinding struct{}
|
|
|
|
func (bsonBinding) Name() string {
|
|
return "bson"
|
|
}
|
|
|
|
func (b bsonBinding) Bind(req *http.Request, obj any) error {
|
|
buf, err := readBody(req.Body)
|
|
if err == nil {
|
|
err = b.BindBody(buf, obj)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (bsonBinding) BindBody(body []byte, obj any) error {
|
|
return bson.Unmarshal(body, obj)
|
|
}
|