-
-
Notifications
You must be signed in to change notification settings - Fork 102
Fix nested form body parsing happening for all content types #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cllns
wants to merge
6
commits into
main
Choose a base branch
from
fix/router-content-type-aware-body-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
866895b
Extract constant for media type
cllns 3a3330e
Add failing spec: router merges any body into params regardless of Co…
cllns 25c2f1d
Only parse_nested_query for urlencoded form submissions
cllns 948c928
Refactor into helper method, use guard clauses
cllns e297be0
Add spec for multipart payload
cllns 0cbfee2
Detect form-urlencoded via env["CONTENT_TYPE"], not Rack::Request
cllns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1040,18 +1040,36 @@ def _params(env, params) | |||||||
| params ||= {} | ||||||||
| env[PARAMS] ||= {} | ||||||||
|
|
||||||||
| if !env.key?(ROUTER_PARSED_BODY) && (input = env[::Rack::RACK_INPUT]) and input.rewind | ||||||||
| env[PARAMS].merge!(::Rack::Utils.parse_nested_query(input.read)) | ||||||||
| input.rewind | ||||||||
| end | ||||||||
|
|
||||||||
| _merge_form_urlencoded_body!(env) | ||||||||
| env[PARAMS].merge!(::Rack::Utils.parse_nested_query(env[::Rack::QUERY_STRING])) | ||||||||
| env[PARAMS].merge!(params) | ||||||||
| env[PARAMS] = Params.deep_symbolize(env[PARAMS]) | ||||||||
|
|
||||||||
| env | ||||||||
| end | ||||||||
|
|
||||||||
| # @since x.x.x | ||||||||
| # @api private | ||||||||
| def _merge_form_urlencoded_body!(env) | ||||||||
| return if env.key?(ROUTER_PARSED_BODY) | ||||||||
| return unless _form_urlencoded?(env) | ||||||||
| return unless (input = env[::Rack::RACK_INPUT]) | ||||||||
|
|
||||||||
| input.rewind | ||||||||
| env[PARAMS].merge!(::Rack::Utils.parse_nested_query(input.read)) | ||||||||
| input.rewind # leave the stream readable for downstream consumers | ||||||||
| end | ||||||||
|
|
||||||||
| # @since x.x.x | ||||||||
| # @api private | ||||||||
| def _form_urlencoded?(env) | ||||||||
| content_type = env[CONTENT_TYPE] | ||||||||
| return false unless content_type | ||||||||
|
|
||||||||
| content_type == FORM_URLENCODED_MEDIA_TYPE || | ||||||||
| content_type.start_with?("#{FORM_URLENCODED_MEDIA_TYPE};") | ||||||||
|
Comment on lines
+1069
to
+1070
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In fact, I wonder whether we could actually just simplify it to this? It'd work fine for both |
||||||||
| end | ||||||||
|
|
||||||||
| # @since 2.0.0 | ||||||||
| # @api private | ||||||||
| def _not_allowed_fixed(env) | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like we should do this for maximum compatibility, and preservation of our existing behaviour.
Rack::Request#media_typereturns a downcased string courtesy of its internalMediaType.type. And if we're aiming to follow the spec strictly, it does say that these values are case insensitive.