-
Notifications
You must be signed in to change notification settings - Fork 7.9k
POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect #11274
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
Comments
I think the relevant code causing the bugs is here:
Ideally redirects with 307 or 308 status codes would NOT trigger this logic, while other redirects stick with the current behavior (for 301/302 it's generally assumed and expected that the method changes to GET). |
Yes, and there's a few locations mores where the issue lies. I started working on a fix. |
…ntext_create switches to GET after a HTTP 308 redirect RFC 7231 states that status code 307 should keep the POST method upon redirect. RFC 7538 does the same for code 308. Although it's not mandated by the RFCs that PATCH is also kept (we can choose), it seems like keeping PATCH will be the most consistent and understandable behaviour. This patch also changes an existing test because it was testing for the wrong behaviour.
* PHP-8.1: Fix GH-11274: POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect
* PHP-8.2: Fix GH-11274: POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect
Description
The following code:
Resulted in this output:
But I expected this output instead:
The server on 127.0.0.1:8080 is this small Flask app (I'm not really a PHP person anymore so I couldn't be bothered to use PHP for this after not having written any PHP code in the last 15 or so years ;)):
When sending a request to the URL without the trailing slash, it uses a HTTP 308 redirect to redirect to the URL with that slash.
Clients are required to keep the method and payload when encountering such a redirect:
See MDN:
And even RFC7538 specifying this status code is pretty clear about the expected behavior from clients:
PHP Version
8.2.6
Operating System
No response
The text was updated successfully, but these errors were encountered: