add panic for ghttp.Server.GetBody

This commit is contained in:
John Guo 2022-01-06 21:47:36 +08:00
parent ee25d696eb
commit 09f83bdd58
2 changed files with 7 additions and 4 deletions

View File

@ -124,7 +124,6 @@ func (r *Request) doParse(pointer interface{}, requestType int) error {
return err
}
for i := 0; i < reflectVal2.Len(); i++ {
if err = gvalid.New().
Data(reflectVal2.Index(i)).Assoc(j.Get(gconv.String(i)).Map()).
Run(r.Context()); err != nil {
@ -146,7 +145,11 @@ func (r *Request) Get(key string, def ...interface{}) *gvar.Var {
// It can be called multiple times retrieving the same body content.
func (r *Request) GetBody() []byte {
if r.bodyContent == nil {
r.bodyContent, _ = ioutil.ReadAll(r.Body)
var err error
r.bodyContent, err = ioutil.ReadAll(r.Body)
if err != nil {
panic(gerror.WrapCode(gcode.CodeInternalError, err, `ReadAll from body failed`))
}
r.Body = utils.NewReadCloser(r.bodyContent, true)
}
return r.bodyContent
@ -192,7 +195,7 @@ func (r *Request) parseQuery() {
var err error
r.queryMap, err = gstr.Parse(r.URL.RawQuery)
if err != nil {
panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, ""))
panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, "Parse Query failed"))
}
}
}

View File

@ -91,7 +91,7 @@ func Test_ClientMaxBodySize(t *testing.T) {
}
t.Assert(
gstr.Trim(c.PostContent(ctx, "/", data)),
data[:1024],
`ReadAll from body failed: http: request body too large`,
)
})
}