mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Sapphire Doesn't Read HTTP Headers (fixes #6311)
This commit is contained in:
parent
627a2916f4
commit
a60f03f28e
@ -88,10 +88,17 @@ class Director {
|
|||||||
@file_get_contents('php://input')
|
@file_get_contents('php://input')
|
||||||
);
|
);
|
||||||
|
|
||||||
// @todo find better way to extract HTTP headers
|
// Load the request headers. If we're not running on Apache, then we
|
||||||
if(isset($_SERVER['HTTP_ACCEPT'])) $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']);
|
// need to manually extract the headers from the $_SERVER array.
|
||||||
if(isset($_SERVER['CONTENT_TYPE'])) $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']);
|
if (function_exists('apache_request_headers')) {
|
||||||
if(isset($_SERVER['HTTP_REFERER'])) $req->addHeader("Referer", $_SERVER['HTTP_REFERER']);
|
$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
|
// Load the session into the controller
|
||||||
$session = new Session(isset($_SESSION) ? $_SESSION : null);
|
$session = new Session(isset($_SESSION) ? $_SESSION : null);
|
||||||
@ -537,6 +544,30 @@ class Director {
|
|||||||
return (bool)self::is_relative_url($relativeUrl);
|
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.
|
* Given a filesystem reference relative to the site root, return the full file-system path.
|
||||||
*
|
*
|
||||||
|
@ -225,6 +225,41 @@ class DirectorTest extends SapphireTest {
|
|||||||
$this->assertFalse($output);
|
$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 {
|
class DirectorTestRequest_Controller extends Controller implements TestOnly {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user