mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-23 05:29:52 +05:30
Normalizing headers to a Ucfirst-With-Dashes format.
This commit is contained in:
parent
879d98abfb
commit
69710a5909
@ -39,6 +39,8 @@ class Request implements RequestInterface
|
|||||||
|
|
||||||
if (empty($headers)) {
|
if (empty($headers)) {
|
||||||
$this->headers = $this->readHeaders();
|
$this->headers = $this->readHeaders();
|
||||||
|
} else {
|
||||||
|
$this->headers = $this->normalizeHeaders($headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +90,7 @@ class Request implements RequestInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $headers;
|
return $this->normalizeHeaders($headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPropertyValue($property, $index = null, $default = null)
|
protected function getPropertyValue($property, $index = null, $default = null)
|
||||||
@ -106,4 +108,39 @@ class Request implements RequestInterface
|
|||||||
|
|
||||||
return $this->{$property}[$index];
|
return $this->{$property}[$index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes all of the headers and normalizes them in a canonical form.
|
||||||
|
*
|
||||||
|
* @param array $headers The request headers.
|
||||||
|
* @return array An arry of headers with the header name normalized
|
||||||
|
*/
|
||||||
|
protected function normalizeHeaders(array $headers)
|
||||||
|
{
|
||||||
|
$normalized = array();
|
||||||
|
foreach ($headers as $key => $value) {
|
||||||
|
$normalized[$this->normalizeKey($key)] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform header name into canonical form
|
||||||
|
*
|
||||||
|
* Taken from the Slim codebase...
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function normalizeKey($key)
|
||||||
|
{
|
||||||
|
$key = strtolower($key);
|
||||||
|
$key = str_replace(array('-', '_'), ' ', $key);
|
||||||
|
$key = preg_replace('#^http #', '', $key);
|
||||||
|
$key = ucwords($key);
|
||||||
|
$key = str_replace(' ', '-', $key);
|
||||||
|
|
||||||
|
return $key;
|
||||||
|
}
|
||||||
}
|
}
|
@ -59,6 +59,20 @@ class Request_test extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(array('Host' => 'foobar.com'), $this->request->header());
|
$this->assertEquals(array('Host' => 'foobar.com'), $this->request->header());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_canonical_header()
|
||||||
|
{
|
||||||
|
$request = new League\OAuth2\Server\Util\Request(
|
||||||
|
array('foo' => 'bar'),
|
||||||
|
array('foo' => 'bar'),
|
||||||
|
array('foo' => 'bar'),
|
||||||
|
array('foo' => 'bar'),
|
||||||
|
array('HTTP_HOST' => 'foobar.com'),
|
||||||
|
array('authorization' => 'Bearer ajdfkljadslfjasdlkj')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals('Bearer ajdfkljadslfjasdlkj', $request->header('Authorization'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException InvalidArgumentException
|
* @expectedException InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user