Merge 7b5e0399987ea10085e499e51b2e1da9b7e13495 into a0acf1df2814fcd828cb2d7128f2f4e2136d3fac

This commit is contained in:
Fair 2022-11-10 08:02:03 +09:00 committed by GitHub
commit baad57fdfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@
package gin package gin
import ( import (
"bytes"
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
@ -85,6 +86,9 @@ type Context struct {
// SameSite allows a server to define a cookie attribute making it impossible for // SameSite allows a server to define a cookie attribute making it impossible for
// the browser to send this cookie along with cross-site requests. // the browser to send this cookie along with cross-site requests.
sameSite http.SameSite sameSite http.SameSite
// rawData cache and reuse http.request.Body
rawData []byte
} }
/************************************/ /************************************/
@ -528,6 +532,10 @@ func (c *Context) initFormCache() {
if c.formCache == nil { if c.formCache == nil {
c.formCache = make(url.Values) c.formCache = make(url.Values)
req := c.Request req := c.Request
if req.Body != nil {
c.rawData, _ = ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewReader(c.rawData))
}
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if !errors.Is(err, http.ErrNotMultipart) { if !errors.Is(err, http.ErrNotMultipart) {
debugPrint("error on parse multipart form array: %v", err) debugPrint("error on parse multipart form array: %v", err)
@ -872,7 +880,9 @@ func (c *Context) GetHeader(key string) string {
// GetRawData returns stream data. // GetRawData returns stream data.
func (c *Context) GetRawData() ([]byte, error) { func (c *Context) GetRawData() ([]byte, error) {
return ioutil.ReadAll(c.Request.Body) // return ioutil.ReadAll(c.Request.Body)
c.initFormCache()
return c.rawData, nil
} }
// SetSameSite with cookie // SetSameSite with cookie