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>
41 lines
915 B
Go
41 lines
915 B
Go
// Copyright 2014 Manu Martinez-Almeida. 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 (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type protobufBinding struct{}
|
|
|
|
func (protobufBinding) Name() string {
|
|
return "protobuf"
|
|
}
|
|
|
|
func (b protobufBinding) Bind(req *http.Request, obj any) error {
|
|
buf, err := readBody(req.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return b.BindBody(buf, obj)
|
|
}
|
|
|
|
func (protobufBinding) BindBody(body []byte, obj any) error {
|
|
msg, ok := obj.(proto.Message)
|
|
if !ok {
|
|
return errors.New("obj is not ProtoMessage")
|
|
}
|
|
if err := proto.Unmarshal(body, msg); err != nil {
|
|
return err
|
|
}
|
|
// Here it's same to return validate(obj), but until now we can't add
|
|
// `binding:""` to the struct which automatically generate by gen-proto
|
|
return nil
|
|
// return validate(obj)
|
|
}
|