Skip to content

Commit

Permalink
Added more headers (auth and content-md5).
Browse files Browse the repository at this point in the history
- Added Content-MD5 header.
- Added check for REDIRECT_HTTP_AUTHORIZATION
- Added check for PHP_AUTH_USER + PHP_AUTH_PW
- Added check for PHP_AUTH_DIGEST
  • Loading branch information
ralouphie committed Feb 11, 2016
1 parent 086cda5 commit 05c6a7e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 43 deletions.
34 changes: 27 additions & 7 deletions src/getallheaders.php
Expand Up @@ -10,16 +10,36 @@
function getallheaders()
{
$headers = array();

$copy_server = [
'CONTENT_TYPE' => 'Content-Type',
'CONTENT_LENGTH' => 'Content-Length',
'CONTENT_MD5' => 'Content-MD5',
];

foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) == 'HTTP_') {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
$headers[$key] = $value;
} elseif ($key == "CONTENT_TYPE") {
$headers["Content-Type"] = $value;
} elseif ($key == "CONTENT_LENGTH") {
$headers["Content-Length"] = $value;
if (substr($key, 0, 5) === 'HTTP_') {
$key = substr($key, 5);
if (!isset($copy_server[$key])) {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
$headers[$key] = $value;
}
} elseif (isset($copy_server[$key])) {
$headers[$copy_server[$key]] = $value;
}
}

if (!isset($headers['Authorization'])) {
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
} elseif (isset($_SERVER['PHP_AUTH_USER'])) {
$basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
$headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
} elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
$headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
}
}

return $headers;
}

Expand Down
126 changes: 90 additions & 36 deletions tests/GetAllHeadersTest.php
Expand Up @@ -2,48 +2,102 @@

class GetAllHeadersTest extends \PHPUnit_Framework_TestCase
{
public function testNormalCase()
{
$expected = array(
'Key-One' => 'foo',
'Key-Two' => 'bar',
'Another-Key-For-Testing' => 'baz'
);

$_SERVER['HTTP_KEY_ONE'] = 'foo';
$_SERVER['HTTP_KEY_TWO'] = 'bar';
$_SERVER['HTTP_ANOTHER_KEY_FOR_TESTING'] = 'baz';

$result = getallheaders();

$this->assertEquals($expected, $result);
}

public function testContentType()
/**
* @dataProvider testWorksData
*/
public function testWorks($test_type, $expected, $server)
{
$expected = array(
'Content-Type' => 'two'
);

$_SERVER['HTTP_CONTENT_TYPE'] = 'one';
$_SERVER['CONTENT_TYPE'] = 'two';

foreach ($server as $key => $val) {
$_SERVER[$key] = $val;
}
$result = getallheaders();

$this->assertEquals($expected, $result);
$this->assertEquals($expected, $result, "Error testing $test_type works.");
}

public function testContentLength()
public function testWorksData()
{
$expected = array(
'Content-Length' => '222'
return array(
array(
'normal case',
array(
'Key-One' => 'foo',
'Key-Two' => 'bar',
'Another-Key-For-Testing' => 'baz'
),
array(
'HTTP_KEY_ONE' => 'foo',
'HTTP_KEY_TWO' => 'bar',
'HTTP_ANOTHER_KEY_FOR_TESTING' => 'baz'
)
),
array(
'Content-Type',
array(
'Content-Type' => 'two'
),
array(
'HTTP_CONTENT_TYPE' => 'one',
'CONTENT_TYPE' => 'two'
)
),
array(
'Content-Length',
array(
'Content-Length' => '222'
),
array(
'CONTENT_LENGTH' => '222',
'HTTP_CONTENT_LENGTH' => '111'
)
),
array(
'Content-MD5',
array(
'Content-MD5' => 'aef123'
),
array(
'CONTENT_MD5' => 'aef123',
'HTTP_CONTENT_MD5' => 'fea321'
)
),
array(
'Authorization (normal)',
array(
'Authorization' => 'testing'
),
array(
'HTTP_AUTHORIZATION' => 'testing',
)
),
array(
'Authorization (redirect)',
array(
'Authorization' => 'testing redirect'
),
array(
'REDIRECT_HTTP_AUTHORIZATION' => 'testing redirect',
)
),
array(
'Authorization (PHP_AUTH_USER + PHP_AUTH_PW)',
array(
'Authorization' => 'Basic ' . base64_encode('foo:bar')
),
array(
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar'
)
),
array(
'Authorization (PHP_AUTH_DIGEST)',
array(
'Authorization' => 'example-digest'
),
array(
'PHP_AUTH_DIGEST' => 'example-digest'
)
)
);

$_SERVER['HTTP_CONTENT_LENGTH'] = '111';
$_SERVER['CONTENT_LENGTH'] = '222';

$result = getallheaders();

$this->assertEquals($expected, $result);
}
}

0 comments on commit 05c6a7e

Please sign in to comment.