Fixes form submissions with empty file fields throwing exceptions #150
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Given a form with a file filed i.e.
<input type="file"/>
if the user does not include a file to upload when the form is submitted the browser will send a request with the following multipart body:Since there is no file but there is a file field, the browser will send a part with empty filename and body and octet-stream as the content type to signify a submission without a file to upload.
In cask to handle such form submissions you create an endpoint like the following:
The
postForm
decorator upon receiving the submission it will create an UndertowMultiPartParserDefinition
and try to parse the request. After parsing the boundary and the headers of the part Undertow will then try to parse the body which is empty and because there is nothing to parse it will never create a file for theFormValue
representing the empty file field (seeMultiPartParserDefinition
line 329):This results to
FormData
with aFormValue
where thefileitem
field isnull
.So when cask tries to convert the Undertow
FormValue
s to caskFormEntry
by callingFormEntry.fromUndertow
on each FormValue theFormValue.isFile
condition returns false and cask tries to convert the UndertowFormValue
to a caskFormValue
and an exception is thrown when callinggetValue()
on a binaryFormValue
.In essence if your endpoint is using the
postForm
decorator and is expecting aFormField
cask will throw an exception if the user doesn't submit a file and there's no chance for the developer to do any validation.This PR is preventing the exception from being thrown by detecting the empty binary field and replicating the Undertow behaviour of passing a
FormValue
with null values (it's converted to Option[Path] in cask land) so that the code has a chance to perform validation and accept or reject the form submission.Closes #149