Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to parse big form #2725

Closed
arthur-xie opened this issue Jun 15, 2017 · 1 comment
Closed

Fail to parse big form #2725

arthur-xie opened this issue Jun 15, 2017 · 1 comment

Comments

@arthur-xie
Copy link

  1. code in the file: context/input.go
// CopyBody returns the raw request body data as bytes.
func (input *BeegoInput) CopyBody(MaxMemory int64) []byte {
	if input.Context.Request.Body == nil {
		return []byte{}
	}
	safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory}
	requestbody, _ := ioutil.ReadAll(safe)
	input.Context.Request.Body.Close()
	bf := bytes.NewBuffer(requestbody)
	input.Context.Request.Body = ioutil.NopCloser(bf)
	input.RequestBody = requestbody
	return requestbody
}

Beego set request body to ioutil.NopCloser(bf).

  1. code in go 1.8.1 src/net/http/request.go
func parsePostForm(r *Request) (vs url.Values, err error) {
...
	case ct == "application/x-www-form-urlencoded":
		var reader io.Reader = r.Body
		maxFormSize := int64(1<<63 - 1)
		if _, ok := r.Body.(*maxBytesReader); !ok {
			maxFormSize = int64(10 << 20) // 10 MB is a lot of text.
			reader = io.LimitReader(r.Body, maxFormSize+1)
		}
		b, e := ioutil.ReadAll(reader)
		if e != nil {
			if err == nil {
				err = e
			}
			break
		}
...
}

But for big form, go http library need the request body to be *maxBytesReader

  1. Modify beego code:
// CopyBody returns the raw request body data as bytes.
func (input *BeegoInput) CopyBody(MaxMemory int64) []byte {
	if input.Context.Request.Body == nil {
		return []byte{}
	}
	safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory}
	requestbody, _ := ioutil.ReadAll(safe)
	input.Context.Request.Body.Close()
	bf := bytes.NewBuffer(requestbody)
	input.Context.Request.Body = http.MaxBytesReader(input.Context.ResponseWriter, ioutil.NopCloser(bf), MaxMemory)
	input.RequestBody = requestbody
	return requestbody
}
@astaxie
Copy link
Member

astaxie commented Jul 4, 2017

Thanks

@astaxie astaxie mentioned this issue Jul 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants