Added Request.RemoveMultipartFormFiles for removing temporary files created during reading multipart/form-data request

This commit is contained in:
Aliaksandr Valialkin
2015-11-28 10:32:55 +02:00
parent cc79b27b51
commit d7ecf59c5d
2 changed files with 22 additions and 11 deletions
+20 -4
View File
@@ -220,9 +220,6 @@ var ErrNoMultipartForm = errors.New("request has no multipart/form-data Content-
//
// Returns ErrNoMultipartForm if request's content-type
// isn't 'multipart/form-data'.
//
// The caller must call RemoveAll on the returned form in order to remove
// all temporary files associated with the returned form.
func (req *Request) MultipartForm() (*multipart.Form, error) {
if req.multipartForm != nil {
return req.multipartForm, nil
@@ -268,7 +265,18 @@ func (req *Request) clearSkipHeader() {
req.parsedURI = false
req.postArgs.Reset()
req.parsedPostArgs = false
req.multipartForm = nil
req.RemoveMultipartFormFiles()
}
// RemoveMultipartFormFiles removes multipart/form-data temporary files
// associated with the request.
func (req *Request) RemoveMultipartFormFiles() {
if req.multipartForm != nil {
// Do not check for error, since these files may be deleted or moved
// to new places by user code.
req.multipartForm.RemoveAll()
req.multipartForm = nil
}
}
// Reset clears response contents.
@@ -283,6 +291,10 @@ func (resp *Response) clearSkipHeader() {
}
// Read reads request (including body) from the given r.
//
// RemoveMultipartFormFiles or Reset must be called after
// reading multipart/form-data request in order to delete temporarily
// uploaded files.
func (req *Request) Read(r *bufio.Reader) error {
return req.ReadLimitBody(r, 0)
}
@@ -293,6 +305,10 @@ const defaultMaxInMemoryFileSize = 16 * 1024 * 1024
//
// If maxBodySize > 0 and the body size exceeds maxBodySize,
// then ErrBodyTooLarge is returned.
//
// RemoveMultipartFormFiles or Reset must be called after
// reading multipart/form-data request in order to delete temporarily
// uploaded files.
func (req *Request) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
req.clearSkipHeader()
err := req.Header.Read(r)
+2 -7
View File
@@ -839,13 +839,8 @@ func (s *Server) serveConn(c net.Conn) error {
ctx.Response.Reset()
s.Handler(ctx)
if ctx.Request.multipartForm != nil {
// Remove temporary files, which may be uploaded during the request.
// Do not check for error, since these files may be deleted or moved
// to new places by RequestHandler.
ctx.Request.multipartForm.RemoveAll()
ctx.Request.multipartForm = nil
}
// Remove temporary files, which may be uploaded during the request.
ctx.Request.RemoveMultipartFormFiles()
errMsg = ctx.timeoutErrMsg
if len(errMsg) > 0 {