Skip to content

Commit

Permalink
Merge branch '1ma-issue-2091'
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
akrabat committed Jun 29, 2017
2 parents fe18195 + c1ff9c7 commit c4c5a12
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function createFromGlobals(array $globals)
$key = strtoupper($key);
if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
if ($key !== 'HTTP_CONTENT_LENGTH') {
$data[$key] = $value;
$data[self::reconstructOriginalKey($key)] = $value;
}
}
}
Expand Down Expand Up @@ -216,4 +216,25 @@ public function normalizeKey($key)

return $key;
}

/**
* Reconstruct original header name
*
* This method takes an HTTP header name from the Environment
* and returns it as it was probably formatted by the actual client.
*
* @param string $key An HTTP header key from the $_SERVER global variable
*
* @return string The reconstructed key
*
* @example CONTENT_TYPE => Content-Type
* @example HTTP_USER_AGENT => User-Agent
*/
private static function reconstructOriginalKey($key)
{
if (strpos($key, 'HTTP_') === 0) {
$key = substr($key, 5);
}
return strtr(ucwords(strtr(strtolower($key), '_', ' ')), ' ', '-');
}
}
3 changes: 3 additions & 0 deletions tests/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function testCreateFromGlobals()

$this->assertTrue(is_array($prop->getValue($h)['accept']));
$this->assertEquals('application/json', $prop->getValue($h)['accept']['value'][0]);
$this->assertEquals('Accept', $prop->getValue($h)['accept']['originalKey']);
}

public function testCreateFromGlobalsWithSpecialHeaders()
Expand All @@ -39,6 +40,7 @@ public function testCreateFromGlobalsWithSpecialHeaders()

$this->assertTrue(is_array($prop->getValue($h)['content-type']));
$this->assertEquals('application/json', $prop->getValue($h)['content-type']['value'][0]);
$this->assertEquals('Content-Type', $prop->getValue($h)['content-type']['originalKey']);
}

public function testCreateFromGlobalsIgnoresHeaders()
Expand All @@ -52,6 +54,7 @@ public function testCreateFromGlobalsIgnoresHeaders()
$prop->setAccessible(true);

$this->assertNotContains('content-length', $prop->getValue($h));
$this->assertEquals('Content-Type', $prop->getValue($h)['content-type']['originalKey']);
}

public function testConstructor()
Expand Down

0 comments on commit c4c5a12

Please sign in to comment.