ENHANCEMENT Sapphire Doesn't Read HTTP Headers (fixes #6311)

This commit is contained in:
ajshort 2011-03-18 23:38:30 +00:00 committed by Ingo Schommer
parent 627a2916f4
commit a60f03f28e
2 changed files with 71 additions and 5 deletions

View File

@ -87,11 +87,18 @@ class Director {
array_merge((array)$_POST, (array)$_FILES),
@file_get_contents('php://input')
);
// @todo find better way to extract HTTP headers
if(isset($_SERVER['HTTP_ACCEPT'])) $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']);
if(isset($_SERVER['CONTENT_TYPE'])) $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']);
if(isset($_SERVER['HTTP_REFERER'])) $req->addHeader("Referer", $_SERVER['HTTP_REFERER']);
// Load the request headers. If we're not running on Apache, then we
// need to manually extract the headers from the $_SERVER array.
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
} else {
$headers = self::extract_request_headers($_SERVER);
}
foreach ($headers as $header => $value) {
$req->addHeader($header, $value);
}
// Load the session into the controller
$session = new Session(isset($_SESSION) ? $_SESSION : null);
@ -536,7 +543,31 @@ class Director {
$relativeUrl = Director::makeRelative($url);
return (bool)self::is_relative_url($relativeUrl);
}
/**
* Takes a $_SERVER data array and extracts HTTP request headers.
*
* @param array $data
* @return array
*/
protected static function extract_request_headers(array $server) {
$headers = array();
foreach($server as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$key = substr($key, 5);
$key = strtolower(str_replace('_', ' ', $key));
$key = str_replace(' ', '-', ucwords($key));
$headers[$key] = $value;
}
}
if(isset($server['CONTENT_TYPE'])) $headers['Content-Type'] = $server['CONTENT_TYPE'];
if(isset($server['CONTENT_LENGTH'])) $headers['Content-Length'] = $server['CONTENT_LENGTH'];
return $headers;
}
/**
* Given a filesystem reference relative to the site root, return the full file-system path.
*

View File

@ -225,6 +225,41 @@ class DirectorTest extends SapphireTest {
$this->assertFalse($output);
}
/**
* @covers Director::extract_request_headers()
*/
public function testExtractRequestHeaders() {
$request = array(
'REDIRECT_STATUS' => '200',
'HTTP_HOST' => 'host',
'HTTP_USER_AGENT' => 'User Agent',
'HTTP_ACCEPT' => 'text/html',
'HTTP_ACCEPT_LANGUAGE' => 'en-us',
'HTTP_COOKIE' => 'PastMember=1',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '/sapphire/main.php',
'CONTENT_TYPE' => 'text/xml',
'CONTENT_LENGTH' => 10
);
$headers = array(
'Host' => 'host',
'User-Agent' => 'User Agent',
'Accept' => 'text/html',
'Accept-Language' => 'en-us',
'Cookie' => 'PastMember=1',
'Content-Type' => 'text/xml',
'Content-Length' => '10'
);
$method = new ReflectionMethod('Director', 'extract_request_headers');
$method->setAccessible(true);
$this->assertEquals($headers, $method->invoke(null, $request));
}
}
class DirectorTestRequest_Controller extends Controller implements TestOnly {