-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Represents the HTTP request header fields by normalising the $_SERVER array's | ||
* keys that start with "HTTP_". Notice: These headers are sent by the requester | ||
* and should not be trusted! | ||
* | ||
* Can be accessed as an associative array to reference headers in their | ||
* original format (i.e. $headers["accept-language"]) or as object properties | ||
* with underscores (i.e. $headers->accept_language). | ||
* | ||
* PHP.Gt (http://php.gt) | ||
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com) | ||
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses | ||
*/ | ||
namespace Gt\Request; | ||
use \Gt\Core\Obj; | ||
|
||
class HeaderList implements \ArrayAccess { | ||
|
||
private $headerArray = []; | ||
|
||
public function __construct($serverArray) { | ||
foreach ($serverArray as $key => $value) { | ||
if(strpos($key, "HTTP_") !== 0) { | ||
continue; | ||
} | ||
|
||
$headerName = str_replace("_", "-", substr($key, 5)); | ||
$headerName = strtolower($headerName); | ||
$this->headerArray[$headerName] = $value; | ||
} | ||
} | ||
|
||
public function offsetExists($offset) { | ||
$offset = strtolower($offset); | ||
return isset($this->headerArray[$offset]); | ||
} | ||
|
||
public function offsetGet($offset) { | ||
$offset = strtolower($offset); | ||
return $this->headerArray[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $value) { | ||
throw new \Gt\Core\Exception\InvalidAccessException(); | ||
} | ||
|
||
public function offsetUnset($offset) { | ||
throw new \Gt\Core\Exception\InvalidAccessException(); | ||
} | ||
|
||
public function __get($name) { | ||
$name = strtolower($name); | ||
|
||
$name = str_replace("_", "-", $name); | ||
return $this->headerArray[$name]; | ||
} | ||
|
||
}# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* PHP.Gt (http://php.gt) | ||
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com) | ||
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses | ||
*/ | ||
namespace Gt\Request; | ||
|
||
class HeaderList_Test extends \PHPUnit_Framework_TestCase { | ||
|
||
private $server = [ | ||
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"HTTP_ACCEPT_ENCODING" => "gzip,deflate,sdch", | ||
"HTTP_COOKIE" => "FIRST=Les; LAST=McQueen; BAND=crème brûlée", | ||
"HTTP_USER_AGENT" => "PHPUnit/PHP.Gt (http://php.gt)", | ||
]; | ||
|
||
public function testArrayAccess() { | ||
$headers = new HeaderList($this->server); | ||
|
||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT"], $headers["accept"]); | ||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT_ENCODING"], $headers["accept-encoding"]); | ||
$this->assertEquals( | ||
$this->server["HTTP_COOKIE"], $headers["cookie"]); | ||
$this->assertEquals( | ||
$this->server["HTTP_USER_AGENT"], $headers["user-agent"]); | ||
} | ||
|
||
public function testProperty() { | ||
$headers = new HeaderList($this->server); | ||
|
||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT"], $headers->accept); | ||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT_ENCODING"], $headers->accept_encoding); | ||
$this->assertEquals( | ||
$this->server["HTTP_COOKIE"], $headers->cookie); | ||
$this->assertEquals( | ||
$this->server["HTTP_USER_AGENT"], $headers->user_agent); | ||
} | ||
|
||
public function testCase() { | ||
$headers = new HeaderList($this->server); | ||
|
||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT"], $headers["Accept"]); | ||
$this->assertEquals( | ||
$this->server["HTTP_ACCEPT"], $headers->Accept); | ||
} | ||
|
||
public function testReadOnly() { | ||
$headers = new HeaderList($this->server); | ||
$this->setExpectedException("\Gt\Core\Exception\InvalidAccessException"); | ||
|
||
$headers["accept"] = "<?php system('echo \"HAX!\" && rm -rf /');"; | ||
} | ||
|
||
public function testNotUnsettable() { | ||
$headers = new HeaderList($this->server); | ||
$this->setExpectedException("\Gt\Core\Exception\InvalidAccessException"); | ||
|
||
unset($headers["accept"]); | ||
} | ||
|
||
public function testIsset() { | ||
$headers = new HeaderList($this->server); | ||
$this->assertTrue(isset($headers["Accept"])); | ||
} | ||
|
||
}# |