|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-07-10 16:00 UTC] mike dot hall at twistdigital dot co dot uk
Description:
------------
The HTTP location header can either be used to direct the user to another
resource (when accompanied by a 3xx status code) or to inform the user of the
location of the document they just created (with a 2xx) status code.
It doesn't make sense to treat the location header as a redirect in the second
context - the location header indicates a redirect only when accompanied by a 3xx
status code.
Currently, PHP follows Location headers as if they are redirects regardless of
the returned status code.
Test script:
---------------
$context = stream_context_create([
"http" => [
"method" => "POST"
"header" => "Content-Length: 13"
"content" => "{\"foo\":\"bar\"}",
],
]);
// Returns HTTP/1.1 201 Created
// Location: http://example.com/mydb/documentid
//
// {"status":"ok"}
$fp = fopen('http://example.com/mydb', 'r', null, $context);
$data = stream_get_contents($fp);
list($headers, $body) = explode("\r\n\r\n", $data, 2);
echo $body;
Expected result:
----------------
{"status":"ok"}
Actual result:
--------------
{"foo":"bar"}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
A more complete example confirms this behavior: I also fixed some syntax errors <?php header('Location: http://php.net', true, 201); if (isset($_GET['waa'])) return; $context = stream_context_create(array( "http" => array( "method" => "POST", "header" => "Content-Length: 13", "content" => "{\"foo\":\"bar\"}", ), )); $fp = fopen('http://'.$_SERVER['SERVER_NAME']. $_SERVER['PHP_SELF'] .'?waa=1', 'r', null, $context); print(stream_get_contents($fp));I attach a git diff with proposed changes: diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 870f904..a3f193b 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -731,9 +731,9 @@ finish: http_header_line[http_header_line_length] = '\0'; if (!strncasecmp(http_header_line, "Location: ", 10)) { - /* we only care about Location for 300, 301, 302, 303 and 307 */ + /* we only care about Location for 300, 301, 302, 303, 307 and 308 */ /* see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1 */ - if ((response_code >= 300 && response_code < 304 || 307 == response_code) && context && php_stream_context_get_option(context, "http", "follow_location", &tmpzval) == SUCCESS) { + if ((response_code >= 300 && response_code < 304 || 307 == response_code || 308 == response_code) && context && php_stream_context_get_option(context, "http", "follow_location", &tmpzval) == SUCCESS) { SEPARATE_ZVAL(tmpzval); convert_to_long_ex(tmpzval); follow_location = Z_LVAL_PP(tmpzval);