Bug #71245 [Opn->Csd]: file_get_contents() ignores "header" context option if it's a reference

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=71245&edit=1

 ID:                 71245
 Updated by:         [email protected]
 Reported by:        php at maisqi dot com
 Summary:            file_get_contents() ignores "header" context option
                     if it's a reference
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Streams related
 Operating System:   Windows 64 bits
 PHP Version:        7.0.1
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of [email protected]
Revision: http://git.php.net/?p=php-src.git;a=commit;h=90d2f509e2164b60fc7bc8407e63504c910cce11
Log: Fixed bug #71245 (file_get_contents() ignores &quot;header&quot; context option if it's a reference)


Previous Comments:
------------------------------------------------------------------------
[2015-12-30 11:09:27] php at maisqi dot com

Description:
------------
file_get_contents() does not pass the headers in the stream context if it's given as a reference to an array, as exemplified below:

  $headers = ['Host: maisqi.com'];
  $httpContext = [
     'http' => [
        'header' => &$headers,
     ],
  ];
  $uri = 'http://77.91.206.217/outros/bugs/php/_print_host_headers.php';
  $content = file_get_contents($uri, false, stream_context_create($httpContext));

In PHP 5.6, this works flawlessly; on PHP7, the request is issued with no headers. If no reference is given ('header' => $headers), this works on both versions.


Test script:
---------------
$headers = ['Host: maisqi.com'];
$httpContext = [
	'http' => [
		'protocol_version'	=> '1.1',
		'method'			=> 'GET',
		'header'			=> &$headers,
		'follow_location'	=> 0,
		'max_redirects'		=> 0,
		'ignore_errors'		=> true,
		'timeout'			=> 60,
	],
];

$uri = 'http://77.91.206.217/outros/bugs/php/_print_host_headers.php';
$content = file_get_contents($uri, false, stream_context_create($httpContext));

echo "Content:\n";
if ($content === false)
	echo '-';
else if ($content === '')
	echo '(empty)';
else
	echo htmlspecialchars($content);

echo "\n\n\$http_response_header:\n";
if (empty($http_response_header))
	echo '-';
else
	print_r($http_response_header);


Expected result:
----------------
It should print something like tbis:
  Content:
  HTTP_HOST: maisqi.com
  SERVER_NAME: maisqi.com


  $http_response_header:
  Array
  (
      [0] => HTTP/1.1 200 OK
      [1] => Date: Wed, 30 Dec 2015 11:04:09 GMT
      [2] => Server: Apache
      [3] => Connection: close
      [4] => Content-Type: text/html; charset=utf-8
  )


Actual result:
--------------
It prints the HTML of a "Page Not Found" page, because no specific Host header is requested.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71245&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.