Bug #77889 [NEW]: URL in Location header not used

From: Date: Sun, 14 Apr 2019 04:06:57 +0000
Subject: Bug #77889 [NEW]: URL in Location header not used
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220437@lists.php.net to get a copy of this message
From:             ASchmidt at Anamera dot net
Operating system: Windows x64
PHP version:      7.2.17
Package:          Streams related
Bug Type:         Bug
Bug description:URL in Location header not used

Description:
------------
For $host = 'www.finecars.cc', the HTML content is correctly received.

For $host = 'finecars.cc', the initial response is:
HTTP/1.1 301 Moved Permanently
Location: http://www.finecars.cc/

However, after that, PHP does NOT actually retrieve "www.finecars.cc" as
defined in the Location header, but continues to retry the original URL
"finecars.cc" until 'max_redirects' is exhausted.

Test script:
---------------
<?php
declare(strict_types=1);

$host = 'finecars.cc';

$headers = array(
    'Host'              =>  $host,
    'User-Agent'        =>  'Anamera/2.0',
    'Accept-Charset'    =>  'UTF-8',
    'Referer'           =>  ( '0' == $_SERVER['SERVER_PORT_SECURE']
?
'http' : 'https'
)."://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}",
    'Connection'        =>  'close',
    'Origin'            =>  ( '0' == $_SERVER['SERVER_PORT_SECURE']
?
'http' : 'https' )."://{$_SERVER['SERVER_NAME']}",
);

$header_lines = [];
foreach ( $headers as $name => $entry )
    $header_lines[] = "{$name}: {$entry}";

$http_options = array(
    'http' => array(
        'protocol_version'  =>  1.1,
        'timeout'           =>  30,         // float: seconds.
        'follow_location'   =>  1,
        'max_redirects'     =>  5,
        'ignore_errors'		=>	true,       // fetch content even on failure
status codes.
        
        'method'            =>	'GET',
        'header'            =>	$header_lines,
//      'user_agent'        =>  '',         // use:
'header'['User-Agent'].
//      'content'           =>  '',         // for POST and PUT.
    ),
);

$http_context = stream_context_create( $http_options );
$file = file_get_contents( "http://{$host}", false,
$http_context );

var_dump( $http_options, $http_response_header );
die( 'Current PHP version: ' . phpversion() );
?>


Expected result:
----------------
  0 => string 'HTTP/1.1 301 Moved Permanently' (length=30)
  1 => string 'Content-Type: text/html; charset=UTF-8' (length=38)
  2 => string 'Location: http://www.finecars.cc/' (length=33)
  3 => string 'Connection: close' (length=17)
  4 => string 'Content-Length: 146' (length=19)
  5 => string 'HTTP/1.1 200 OK' (length=15)
  6 => string 'Content-Type: text/html;charset=iso-8859-1' (length=42)
  7 => string 'Connection: close' (length=17)
  8 => string 'Content-Length: 87608' (length=21)



Actual result:
--------------
  0 => string 'HTTP/1.1 301 Moved Permanently' (length=30)
  1 => string 'Content-Type: text/html; charset=UTF-8' (length=38)
  2 => string 'Location: http://www.finecars.cc/' (length=33)
  3 => string 'Connection: close' (length=17)
  4 => string 'Content-Length: 146' (length=19)
  5 => string 'HTTP/1.1 301 Moved Permanently' (length=30)
  6 => string 'Content-Type: text/html; charset=UTF-8' (length=38)
  7 => string 'Location: http://www.finecars.cc/' (length=33)
  8 => string 'Connection: close' (length=17)
  9 => string 'Content-Length: 146' (length=19)  
  10 => string 'HTTP/1.1 301 Moved Permanently' (length=30)
  11 => string 'Content-Type: text/html; charset=UTF-8' (length=38)
  12 => string 'Location: http://www.finecars.cc/' (length=33)
  13 => string 'Connection: close' (length=17)
  14 => string 'Content-Length: 146' (length=19)
etc.



-- 
Edit bug report at https://bugs.php.net/bug.php?id=77889&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77889&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77889&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77889&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77889&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77889&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77889&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77889&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77889&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77889&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77889&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77889&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77889&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77889&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77889&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77889&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77889&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77889&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77889&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77889&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77889&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#220437) next »