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
postFormdecorator upon receiving the submission it will create an UndertowMultiPartParserDefinitionand 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 theFormValuerepresenting the empty file field (seeMultiPartParserDefinitionline 329):This results to
FormDatawith aFormValuewhere thefileitemfield isnull.So when cask tries to convert the Undertow
FormValues to caskFormEntryby callingFormEntry.fromUndertowon each FormValue theFormValue.isFilecondition returns false and cask tries to convert the UndertowFormValueto a caskFormValueand an exception is thrown when callinggetValue()on a binaryFormValue.In essence if your endpoint is using the
postFormdecorator and is expecting aFormFieldcask 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
FormValuewith 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