-
-
Notifications
You must be signed in to change notification settings - Fork 9.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.3
Are you sure you want to change the base?
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.
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution 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 |
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 |
I did some "manual tests" with a simple controller to see what the output is. The controller: class TestController
{
#[Route('/test')]
public function test(Request $request): JsonResponse
{
return new JsonResponse([
'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 missed f16e206 😅 |
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 a php -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-type application/json
is passed to the function, it will throw RequestParseBodyException: 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 because RequestParseBodyException
is also thrown with invalid content, not just unsupported Content-Type. Or are we going to differentiate this?
Oh, and we need to define the behavior when an exception is throw by the function! |
@nicolas-grekas Thank you for the input. |
I guess so. We should behave as close as how PHP behaves with other methods. |
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. |
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 |
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 |
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? |
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.