Skip to content

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

Closed
ThiefMaster opened this issue May 19, 2023 · 2 comments · Fixed by ThePHPF/thephp.foundation#90

Comments

@ThiefMaster
Copy link

ThiefMaster commented May 19, 2023

Description

The following code:

<?php
echo file_get_contents('http://127.0.0.1:8080/test', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test', false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test/', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test/', false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));

Resulted in this output:

{"method":"GET"}
{"method":"GET"}
{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}

But I expected this output instead:

{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}
{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}

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 ;)):

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/test/', methods=['GET', 'POST', 'PATCH'])
def test():
    return jsonify(method=request.method, **request.form)

app.run(port=8080)

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:

The request method and the body will not be altered, whereas 301 may incorrectly sometimes be changed to a GET method.

And even RFC7538 specifying this status code is pretty clear about the expected behavior from clients:

Note: This status code is similar to 301 (Moved Permanently) ([RFC7231], Section 6.4.2), except that it does not allow changing the request method from POST to GET.

PHP Version

8.2.6

Operating System

No response

@ThiefMaster
Copy link
Author

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).

@nielsdos
Copy link
Member

Yes, and there's a few locations mores where the issue lies. I started working on a fix.

@nielsdos nielsdos self-assigned this May 19, 2023
nielsdos added a commit to nielsdos/php-src that referenced this issue May 19, 2023
…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.
nielsdos added a commit that referenced this issue May 19, 2023
* PHP-8.1:
  Fix GH-11274: POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect
nielsdos added a commit that referenced this issue May 19, 2023
* PHP-8.2:
  Fix GH-11274: POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment