Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Slim/Http/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Headers extends Collection implements HeadersInterface
public static function createFromEnvironment(Environment $environment)
{
$data = [];
$environment = self::determineAuthorization($environment);
foreach ($environment as $key => $value) {
$key = strtoupper($key);
if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
Expand All @@ -63,6 +64,30 @@ public static function createFromEnvironment(Environment $environment)
return new static($data);
}

/**
* If HTTP_AUTHORIZATION does not exist tries to get it from
* getallheaders() when available.
*
* @param Environment $environment The Slim application Environment
*
* @return Environment
*/

public static function determineAuthorization(Environment $environment)
{
$authorization = $environment->get('HTTP_AUTHORIZATION');

if (null === $authorization && is_callable('getallheaders')) {
$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_LOWER);
if (isset($headers['authorization'])) {
$environment->set('HTTP_AUTHORIZATION', $headers['authorization']);
}
}

return $environment;
}

/**
* Return array of HTTP header names and values.
* This method returns the _original_ header name
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,14 @@ public function testNormalizeKey()
$this->assertEquals('foo-bar', $h->normalizeKey('http_foo_bar'));
$this->assertEquals('foo-bar', $h->normalizeKey('http-foo-bar'));
}

public function testDetermineAuthorization()
{
$e = Environment::mock([]);
$en = Headers::determineAuthorization($e);
$h = Headers::createFromEnvironment($e);

$this->assertEquals('electrolytes', $en->get('HTTP_AUTHORIZATION'));
$this->assertEquals(['electrolytes'], $h->get('Authorization'));
}
}
4 changes: 2 additions & 2 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,8 @@ public function testWithUploadedFiles()

public function testGetServerParams()
{
$mockEnv = Environment::mock();
$request = $this->requestFactory();
$mockEnv = Environment::mock(["HTTP_AUTHORIZATION" => "test"]);
$request = $this->requestFactory(["HTTP_AUTHORIZATION" => "test"]);

$serverParams = $request->getServerParams();
foreach ($serverParams as $key => $value) {
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
/** @var \Composer\Autoload\ClassLoader $autoloader */
$autoloader = require dirname(__DIR__) . '/vendor/autoload.php';

require dirname(__FILE__) . '/getallheaders.php';

// Register test classes
$autoloader->addPsr4('Slim\Tests\\', __DIR__);
7 changes: 7 additions & 0 deletions tests/getallheaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
if (!function_exists('getallheaders')) {
function getallheaders()
{
return ['Authorization' => 'electrolytes'];
}
}