[MM-27293] Remove heap pre-allocation for file uploads (#15143)

* Validate Content-Length

* Avoid pre-allocating memory for file uploads

* Add small pre-allocation to help mitigate small uploads performance
Этот коммит содержится в:
Claudio Costa
2020-08-03 08:25:47 +02:00
коммит произвёл GitHub
родитель 8693a479d9
Коммит 38307c0b73
2 изменённых файлов: 26 добавлений и 26 удалений

Просмотреть файл

@@ -7,7 +7,6 @@ import (
"bytes"
"crypto/subtle"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
@@ -51,8 +50,7 @@ var MEDIA_CONTENT_TYPES = [...]string{
"audio/wav",
}
const maxUploadDrainBytes = (10 * 1024 * 1024) // 10Mb
const maxMultipartFormDataBytes = 10 * 1024 // 10Kb
const maxMultipartFormDataBytes = 10 * 1024 // 10Kb
func (api *API) InitFile() {
api.BaseRoutes.Files.Handle("", api.ApiSessionRequired(uploadFileStream)).Methods("POST")
@@ -97,9 +95,6 @@ func multipartReader(req *http.Request, stream io.Reader) (*multipart.Reader, er
}
func uploadFileStream(c *Context, w http.ResponseWriter, r *http.Request) {
// Drain any remaining bytes in the request body, up to a limit
defer io.CopyN(ioutil.Discard, r.Body, maxUploadDrainBytes)
if !*c.App.Config().FileSettings.EnableFileAttachments {
c.Err = model.NewAppError("uploadFileStream",
"api.file.attachments.disabled.app_error",
@@ -120,6 +115,13 @@ func uploadFileStream(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
if r.ContentLength == 0 {
c.Err = model.NewAppError("uploadFileStream",
"api.file.upload_file.read_request.app_error",
nil, "Content-Length should not be 0", http.StatusBadRequest)
return
}
timestamp := time.Now()
var fileUploadResponse *model.FileUploadResponse