Return appropriate errors if upload-length is misused in PATCH requests

This commit is contained in:
Adam Jensen 2018-06-03 12:24:51 -04:00
parent 22fdd3935b
commit a366b00e3e
1 changed files with 19 additions and 11 deletions

View File

@ -459,18 +459,26 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request
return return
} }
if handler.composer.UsesLengthDeferrer && info.SizeIsDeferred && r.Header.Get("Upload-Length") != "" { if r.Header.Get("Upload-Length") != "" {
uploadLength, err := strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64) if handler.composer.UsesLengthDeferrer {
if err != nil || uploadLength < 0 || uploadLength < info.Offset || uploadLength > handler.config.MaxSize { if info.SizeIsDeferred {
handler.sendError(w, r, ErrInvalidUploadLength) uploadLength, err := strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64)
return if err != nil || uploadLength < 0 || uploadLength < info.Offset || uploadLength > handler.config.MaxSize {
} handler.sendError(w, r, ErrInvalidUploadLength)
return
}
info.Size = uploadLength info.Size = uploadLength
info.SizeIsDeferred = false info.SizeIsDeferred = false
if err := handler.composer.LengthDeferrer.DeclareLength(id, info.Size); err != nil { if err := handler.composer.LengthDeferrer.DeclareLength(id, info.Size); err != nil {
handler.sendError(w, r, err) handler.sendError(w, r, err)
return return
}
} else {
handler.sendError(w, r, ErrInvalidUploadLength)
}
} else {
handler.sendError(w, r, ErrNotImplemented)
} }
} }