3 Star 16 Fork 3

Gitee 极速下载 / go-tagexpr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/bytedance/go-tagexpr/
克隆/下载
body.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
package binding
import (
"bytes"
jsonpkg "encoding/json"
"errors"
"io"
"net/http"
"strings"
"github.com/gogo/protobuf/proto"
)
func getBodyCodec(req Request) codec {
ct := req.GetContentType()
idx := strings.Index(ct, ";")
if idx != -1 {
ct = strings.TrimRight(ct[:idx], " ")
}
switch ct {
case "application/json":
return bodyJSON
case "application/x-protobuf":
return bodyProtobuf
case "application/x-www-form-urlencoded", "multipart/form-data":
return bodyForm
default:
return bodyUnsupport
}
}
// Body body copy
type Body struct {
*bytes.Buffer
bodyBytes []byte
}
// Close close.
func (Body) Close() error { return nil }
// Reset zero offset.
func (b *Body) Reset() {
b.Buffer = bytes.NewBuffer(b.bodyBytes)
}
// Bytes returns all of the body bytes.
func (b *Body) Bytes() []byte {
return b.bodyBytes
}
// Len returns all of the body length.
func (b *Body) Len() int {
return len(b.bodyBytes)
}
// GetBody get the body from http.Request
func GetBody(r *http.Request) (*Body, error) {
switch body := r.Body.(type) {
case *Body:
body.Reset()
return body, nil
default:
var buf bytes.Buffer
_, err := io.Copy(&buf, r.Body)
r.Body.Close()
if err != nil {
return nil, err
}
_body := &Body{
Buffer: &buf,
bodyBytes: buf.Bytes(),
}
r.Body = _body
return _body, nil
}
}
func bindJSON(pointer interface{}, bodyBytes []byte) error {
if jsonUnmarshalFunc != nil {
return jsonUnmarshalFunc(bodyBytes, pointer)
}
return jsonpkg.Unmarshal(bodyBytes, pointer)
}
func bindProtobuf(pointer interface{}, bodyBytes []byte) error {
msg, ok := pointer.(proto.Message)
if !ok {
return errors.New("protobuf content type is not supported")
}
return proto.Unmarshal(bodyBytes, msg)
}
1
https://gitee.com/mirrors/go-tagexpr.git
git@gitee.com:mirrors/go-tagexpr.git
mirrors
go-tagexpr
go-tagexpr
v2.7.4

搜索帮助

53164aa7 5694891 3bd8fe86 5694891