diff --git a/src/Headers.php b/src/Headers.php index 8240a28..9d8c299 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -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; } } } @@ -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), '_', ' ')), ' ', '-'); + } } diff --git a/tests/HeadersTest.php b/tests/HeadersTest.php index 6a5d041..c21771d 100644 --- a/tests/HeadersTest.php +++ b/tests/HeadersTest.php @@ -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() @@ -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() @@ -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()