-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation][HttpKernel] Improving the request/response format autodetection #32344
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
Merged
Conversation
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
fca70a0
to
5fcb836
Compare
cc @dunglas |
fabpot
requested changes
Jul 3, 2019
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.
LGTM
5fcb836
to
f287be2
Compare
Tests added. Status: Needs Review |
f287be2
to
d9b77c4
Compare
d9b77c4
to
1952928
Compare
fabpot
approved these changes
Jul 3, 2019
Thank you @yceruto. |
fabpot
added a commit
that referenced
this pull request
Jul 3, 2019
…ponse format autodetection (yceruto) This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation][HttpKernel] Improving the request/response format autodetection | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Mainly for API-based apps, currently the response header `Content-Type` (if no provided) is guessed based on the request format (`_format` attribute), falling back to `html` by default. Especially for the new error renderer system, where any kind of error can occur and it becomes an http response, this PR improves this guesser mechanism by taking into account also the `Content-type` of the request. Example: ```bash $ curl -X POST -H 'Content-Type: application/json' -i 'https://127.0.0.1:8000/login' ``` **before:** ```bash HTTP/2 500 cache-control: no-cache, private content-type: text/html; charset=UTF-8 # <- inaccurate ... {"title":"Internal Server Error","status":500,"detail":"Invalid credentials!"} ``` Most of the 3rd-party bundles that I know (`api-platform/core`, `FOSRestBundle`) need a dedicated listener to achieve it right. **after:** ```bash HTTP/2 500 cache-control: no-cache, private content-type: application/json ... {"title":"Internal Server Error","status":500,"detail":"Invalid credentials!"} ``` Of course, this applies to all kind of responses, as long as the `Content-Type` is not explicitly provided. So, as a last chance, the `Accept` heading of the request is also taken into account to detect the preferred format: ```bash $ curl -H 'Accept: application/json' -i 'https://127.0.0.1:8000/userinfo' HTTP/2 404 cache-control: no-cache, private content-type: application/json ... {"title":"Not Found","status":404,"detail":"No route found for \"GET \/userinfo\""} ``` They could be other places in the code where this new method could also be useful, please advise :) WDYT? Commits ------- 1952928 Improving the request/response format autodetection
fabpot
added a commit
that referenced
this pull request
Jul 4, 2019
…tPreferredFormat() (dunglas) This PR was squashed before being merged into the 4.4 branch (closes #32348). Discussion ---------- [HttpFoundation] Accept must take the lead for Request::getPreferredFormat() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a Follow up PR to #32344: if both `Accept` and `Content-Type` are defined, `Accept` must take the lead because it explicitly tells what format the client expect as a response. Before: ``` $ curl -H 'Accept: application/json' -H 'Content-Type: text/xml' -i 'https://127.0.0.1:8000/userinfo' [snip] content-type: text/xml ``` After: ``` $ curl -H 'Accept: application/json' -H 'Content-Type: text/xml' -i 'https://127.0.0.1:8000/userinfo' [snip] content-type: application/json ``` Actually, I'm not sure that inferring the content type of the response using the `Content-Type` provided for the request body is a good idea. The HTTP RFC explicitly states that `Accept` must be used to hint a preferred response format (`Content-Type` on the request indicates the type of associated its the body). I would be in favor of being more conservative: use `Accept` if provided (a best practice anyway), and fallback to the default value (HTML by default) otherwise. WDYT? Commits ------- 60d997d [HttpFoundation] Accept must take the lead for Request::getPreferredFormat()
Merged
Merged
This was referenced Jan 30, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Mainly for API-based apps, currently the response header
Content-Type
(if not provided) is guessed based on the request format (_format
attribute), falling back tohtml
by default.Especially for the new error renderer system, where any kind of error can occur and it becomes an http response, this PR improves this guesser mechanism by taking into account also the
Content-type
of the request.Example:
before:
Most of the 3rd-party bundles that I know (
api-platform/core
,FOSRestBundle
) need a dedicated listener to achieve it right.after:
Of course, this applies to all kind of responses, as long as the
Content-Type
is not explicitly provided. So, as a last chance, theAccept
heading of the request is also taken into account to detect the preferred format:They could be other places in the code where this new method could also be useful, please advise :)
WDYT?