mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Adding a few more ways to detect HTTP headers, extensions and the accept header.
This commit is contained in:
parent
4ff07b745a
commit
43f7fcc735
2 changed files with 79 additions and 24 deletions
|
@ -112,8 +112,8 @@ class CakeRequest implements ArrayAccess {
|
||||||
'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino'
|
'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino'
|
||||||
)),
|
)),
|
||||||
'requested' => array('param' => 'requested', 'value' => 1),
|
'requested' => array('param' => 'requested', 'value' => 1),
|
||||||
'json' => array('header' => array('application/json')),
|
'json' => array('accept' => array('application/json'), 'param' => 'ext', 'value' => 'json'),
|
||||||
'xml' => array('header' => array('application/xml', 'text/xml')),
|
'xml' => array('accept' => array('application/xml', 'text/xml'), 'param' => 'ext', 'value' => 'xml'),
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -506,6 +506,9 @@ class CakeRequest implements ArrayAccess {
|
||||||
if (isset($detect['header']) && $this->_headerDetector($detect)) {
|
if (isset($detect['header']) && $this->_headerDetector($detect)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (isset($detect['accept']) && $this->_acceptHeaderDetector($detect)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (isset($detect['param']) && $this->_paramDetector($detect)) {
|
if (isset($detect['param']) && $this->_paramDetector($detect)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -528,6 +531,38 @@ class CakeRequest implements ArrayAccess {
|
||||||
return $headers;
|
return $headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detects if an URL extension is present.
|
||||||
|
*
|
||||||
|
* @param array $detect Detector options array.
|
||||||
|
* @return bool Whether or not the request is the type you are checking.
|
||||||
|
*/
|
||||||
|
protected function _extensionDetector($detect) {
|
||||||
|
if (is_string($detect['extension'])) {
|
||||||
|
$detect['extension'] = array($detect['extension']);
|
||||||
|
}
|
||||||
|
if (in_array($this->params['ext'], $detect['extension'])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detects if a specific accept header is present.
|
||||||
|
*
|
||||||
|
* @param array $detect Detector options array.
|
||||||
|
* @return bool Whether or not the request is the type you are checking.
|
||||||
|
*/
|
||||||
|
protected function _acceptHeaderDetector($detect) {
|
||||||
|
$acceptHeaders = $this->getAcceptHeaders();
|
||||||
|
foreach ($detect['accept'] as $header) {
|
||||||
|
if (in_array($header, $acceptHeaders)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detects if a specific header is present.
|
* Detects if a specific header is present.
|
||||||
*
|
*
|
||||||
|
@ -535,10 +570,13 @@ class CakeRequest implements ArrayAccess {
|
||||||
* @return bool Whether or not the request is the type you are checking.
|
* @return bool Whether or not the request is the type you are checking.
|
||||||
*/
|
*/
|
||||||
protected function _headerDetector($detect) {
|
protected function _headerDetector($detect) {
|
||||||
$acceptHeaders = $this->getAcceptHeaders();
|
foreach ($detect['header'] as $header => $value) {
|
||||||
foreach ($detect['header'] as $header) {
|
$header = 'HTTP_' . strtoupper($header);
|
||||||
if (in_array($header, $acceptHeaders)) {
|
if (isset($_SERVER[$header])) {
|
||||||
return true;
|
if (is_callable($value)) {
|
||||||
|
return call_user_func($value, $_SERVER[$header]);
|
||||||
|
}
|
||||||
|
return ($_SERVER[$header] === $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -52,16 +52,6 @@ class TestCakeRequest extends CakeRequest {
|
||||||
$this->here = $this->base . '/' . $this->url;
|
$this->here = $this->base . '/' . $this->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detects if a specific header is present.
|
|
||||||
*
|
|
||||||
* @param $detect Detector options array.
|
|
||||||
* @return bool Whether or not the request is the type you are checking.
|
|
||||||
*/
|
|
||||||
public function headerDetector($detect) {
|
|
||||||
return $this->_headerDetector($detect);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,15 +95,42 @@ class CakeRequestTest extends CakeTestCase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testHeaderDetector() {
|
public function testHeaderDetector() {
|
||||||
$request = $this->getMock('TestCakeRequest', array('getAcceptHeaders'));
|
$request = new CakeRequest('some/path');
|
||||||
|
$request->addDetector('host', array('header' => array('host' => 'cakephp.org')));
|
||||||
|
|
||||||
|
$_SERVER['HTTP_HOST'] = 'cakephp.org';
|
||||||
|
$this->assertTrue($request->is('host'));
|
||||||
|
|
||||||
|
$_SERVER['HTTP_HOST'] = 'php.net';
|
||||||
|
$this->assertFalse($request->is('host'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the accept header detector.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testExtensionDetector() {
|
||||||
|
$request = new CakeRequest('some/path');
|
||||||
|
$request->params['ext'] = 'json';
|
||||||
|
$this->assertTrue($request->is('json'));
|
||||||
|
|
||||||
|
$request->params['ext'] = 'xml';
|
||||||
|
$this->assertFalse($request->is('json'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the accept header detector.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAcceptHeaderDetector() {
|
||||||
|
$request = new CakeRequest('some/path');
|
||||||
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
|
||||||
$detector = array('header' => array('application/json'), 'param' => 'ext', 'value' => 'json');
|
$this->assertTrue($request->is('json'));
|
||||||
$request->expects($this->once())
|
|
||||||
->method('getAcceptHeaders')
|
$_SERVER['HTTP_ACCEPT'] = 'text/plain, */*';
|
||||||
->will($this->returnValue(array(
|
$this->assertFalse($request->is('json'));
|
||||||
'application/json'
|
|
||||||
)));
|
|
||||||
$this->assertTrue($request->headerDetector($detector));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue