Skip to content

Commit

Permalink
Added normalize headers to 2.1 needed by lucadegasperi/oauth2-server-…
Browse files Browse the repository at this point in the history
…laravel
  • Loading branch information
freezy-sk authored and alexbilbie committed May 23, 2014
1 parent 67509d1 commit e72179e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/League/OAuth2/Server/Util/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function __construct(array $get = array(), array $post = array(), array $

if (empty($headers)) {
$this->headers = $this->readHeaders();
} else {
$this->headers = $this->normalizeHeaders($headers);
}
}

Expand Down Expand Up @@ -88,8 +90,8 @@ protected function readHeaders()
}
}

return $headers;
}
return $this->normalizeHeaders($headers);
}

protected function getPropertyValue($property, $index = null, $default = null)
{
Expand All @@ -106,4 +108,39 @@ protected function getPropertyValue($property, $index = null, $default = null)

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[ucfirst($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;
}
}
14 changes: 14 additions & 0 deletions tests/util/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ function test_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
*/
Expand Down

0 comments on commit e72179e

Please sign in to comment.