Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[HttpFoundation] Add support for parsing non-POST requests using request_parse_body() (PHP 8.4)#59358
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:7.4
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…ateFromGlobals()Introduce initial support for parsing `multipart/form-data` in PUT/PATCH requests using PHP 8.4's `request_parse_body()` function.Known issue: Some tests fail due to missing Content-Type handling and lack of proper test setup for PUT/PATCH requests. These will be addressed in follow-up changes.
carsonbot commentedJan 3, 2025
Hey! I see that this is your first PR. That is great! Welcome! Symfony has acontribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
You need to add a check for the php version then if it is only available in 8.4 |
Uh oh!
There was an error while loading.Please reload this page.
The problem is that the tests fail because they were designed for the old solution with At the moment, I’m unsure how to properly adapt or rewrite the tests to handle this scenario. I would appreciate any suggestions or ideas on how to proceed with testing for the new implementation (and with |
Uh oh!
There was an error while loading.Please reload this page.
I did some "manual tests" with a simple controller to see what the output is. The controller: class TestController{ #[Route('/test')]publicfunctiontest(Request$request):JsonResponse {returnnewJsonResponse(['method' =>$request->getMethod(),'content_type' =>$request->headers->get('content-type'),'request_all' =>$request->request->all(),'request_get_content' =>$request->getContent(),'payload_all' =>$request->getPayload()->all(),'files_all' =>$request->files->all(), ]); }} The requests (sent with cURL):
with textfile.txt:
|
Indeed you’d need |
Thanks for the notice, but unfortunately this also doesn't change it. I am a little bit confused because
|
Woops indeed my bad I missedf16e206 😅 |
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.
request_parse_body() is only available PHP >= 8.4.0 --> Backwards compatibility?
No need for a polyfill IMHO, upgrading PHP is the way to go to get support for this.
The existing tests fail (in environment with PHP 8.4) because application/x-www-form-urlencodedwill also use the new implementation. They should be adapted?
We should split the failing test cases and skip them on PHP 8.4
New tests for multipart/form-data should be implemented.
Given the way the PHP 8.4 function is implemented, this should be done using aphp -S
subprocess
Check if request_parse_body() is good in createFromGlobals() or if it should be implemented somewhere else (too?)?
Looks good to me.
if ( | ||
\in_array($method, ['PUT', 'DELETE', 'PATCH'], true) | ||
&& ( | ||
str_starts_with($contentType, 'application/x-www-form-urlencoded') |
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.
I'd let this check on the content-type to the request_parse_body function, no need to implement it ourselves
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.
What do you mean?
A content type check is required because if, for example, a request with content-typeapplication/json
is passed to the function, it will throwRequestParseBodyException: Content-Type "application/json" is not supported
. Or are we going to catch and handle it?RequestParseBodyException
is also thrown with invalid content, not just unsupported.
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.
I mean the check for the content-type: we can catch the exception and deal with it instead of checking the content-type ourselves (and diverging from the native check).
We have to add logic to deal with the exception anyway, we cannot let it bubble down unhandled (createFromGlobals isn't expected to throw such exceptions).
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.
Sorry, my bad. I overlooked the "on the content-type" in your message...
I checked this becauseRequestParseBodyException
is also thrown with invalid content, not just unsupported Content-Type. Or are we going to differentiate this?
Uh oh!
There was an error while loading.Please reload this page.
Oh, and we need to define the behavior when an exception is throw by the function! |
@nicolas-grekas Thank you for the input. |
nicolas-grekas commentedJan 5, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I guess so. We should behave as close as how PHP behaves with other methods. |
MatTheCat commentedJan 7, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Just a thought I had: wouldn’t the Runtime component be a better place for |
Good point, but another thought from me: I originally came across this via Laravel, which is using Symfony as foundation for requests. For instance, Laravel relies on Symfony’s Request class for parsing requests. If this functionality were implemented in the Request class, Laravel and other frameworks leveraging Symfony’s components could benefit from it as well, given that they don’t rely on the Runtime component, as far as I understand |
Up to move this PR forward@rottifant ? |
Yes, I will continue in the next days. |
florian2peaches commentedMar 26, 2025
What's the current problem? I would love to have this fix/feature, maybe I can help. |
@florian2peaches I would be very happy if you could take it over :-) The implementation itself seems to work. Test tests that cover this new function |
florian2peaches commentedMar 26, 2025
That's still turning CI red, any more info on that? |
Yes. As far as I found out, there is currently no way to test and simulate |
florian2peaches commentedApr 9, 2025
I'm on the same lane, I know php works that way so |
Unfortunately that was also the point where I got stuck and had no other idea than the "real" http server. Maybe the Symfony team has an idea how to implement this in the tests? |
MrMeshok commentedMay 26, 2025
In php-src there are helper functions which start and send requests to built-in cli server. So in theory you can start simple server which calls |
Uh oh!
There was an error while loading.Please reload this page.
Introduce initial support for parsing
multipart/form-data
in PUT/PATCH requests using PHP 8.4'srequest_parse_body()
function.This function can also be used for
application/x-www-form-urlencoded
.Problems & Todos:
request_parse_body()
is only available PHP >= 8.4.0 --> Backwards compatibility?application/x-www-form-urlencoded
will also use the new implementation. They should be adapted?multipart/form-data
should be implemented.request_parse_body()
is good increateFromGlobals()
or if it should be implemented somewhere else (too?)?Unfortunately I could not find a way to test the new implementation properly.
Looking forward to your ideas & feedback, thx.