diff --git a/doc/ChangeLog b/doc/ChangeLog index fbcd8d7737..123a9c2799 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,16 @@ +2013-10-17 Nikita V. Konstantinov + + * main/Flow/HttpRequest.class.php + * main/Net/Http/HttpHeaderCollection.class.php + * test/AllTests.php + * test/main/Net/Http/HttpHeaderCollectionTest.class.php: + added HttpHeaderCollection + +2013-10-15 Nikita V. Konstantinov + + * main/Flow/HttpRequest.class.php: + added HttpRequest::createFromGlobals() + 2013-09-03 Vasily M. Stashko * core/DB/MySQL.class.php @@ -253,7 +266,7 @@ Introducing Uncachers -2012-06-29 Nikita Konstantinov +2012-06-29 Nikita V. Konstantinov * core/DB/PgSQL.class.php test/core/DbConnectionTest.class.php: diff --git a/main/Flow/HttpRequest.class.php b/main/Flow/HttpRequest.class.php index 6fefa0b644..ab5b12ab7a 100644 --- a/main/Flow/HttpRequest.class.php +++ b/main/Flow/HttpRequest.class.php @@ -35,7 +35,7 @@ final class HttpRequest // all other sh1t private $attached = array(); - private $headers = array(); + private $headers = null; /** * @var HttpMethod @@ -47,7 +47,6 @@ final class HttpRequest */ private $url = null; - //for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS private $body = null; /** @@ -74,9 +73,12 @@ public static function createFromGlobals() if (isset($_SESSION)) $request->setSession($_SESSION); - foreach ($_SERVER as $name => $value) - if (substr($name, 0, 5) === 'HTTP_') - $request->setHeaderVar(substr($name, 5), $value); + foreach ($_SERVER as $name => $value) { + if (strpos($name, 'HTTP_') === 0) { + $name = str_replace('_', '-', substr($name, 5)); + $request->setHeaderVar($name, $value); + } + } if ( $request->hasServerVar('CONTENT_TYPE') @@ -86,6 +88,11 @@ public static function createFromGlobals() return $request; } + + public function __construct() + { + $this->headers = new HttpHeaderCollection(); + } public function &getGet() { @@ -218,7 +225,11 @@ public function &getSession() { return $this->session; } - + + /** + * @param string $name + * @return mixed + */ public function getSessionVar($name) { return $this->session[$name]; @@ -278,7 +289,11 @@ public function &getAttached() { return $this->attached; } - + + /** + * @param string $name + * @return mixed + */ public function getAttachedVar($name) { return $this->attached[$name]; @@ -306,7 +321,7 @@ public function getByType(RequestType $type) public function getHeaderList() { - return $this->headers; + return $this->headers->getAll(); } public function hasHeaderVar($name) @@ -316,7 +331,7 @@ public function hasHeaderVar($name) public function getHeaderVar($name) { - return $this->headers[$name]; + return $this->headers->get($name); } /** @@ -333,7 +348,7 @@ public function unsetHeaderVar($name) **/ public function setHeaderVar($name, $var) { - $this->headers[$name] = $var; + $this->headers->set($name, $var); return $this; } @@ -342,7 +357,7 @@ public function setHeaderVar($name, $var) **/ public function setHeaders(array $headers) { - $this->headers = $headers; + $this->headers = new HttpHeaderCollection($headers); return $this; } diff --git a/main/Net/Http/HttpHeaderCollection.class.php b/main/Net/Http/HttpHeaderCollection.class.php new file mode 100644 index 0000000000..224caa1029 --- /dev/null +++ b/main/Net/Http/HttpHeaderCollection.class.php @@ -0,0 +1,110 @@ + $value) + $this->set($name, $value); + } + + public function set($name, $value) + { + $this->headers[$this->normalizeName($name)]= + array_values((array) $value); + + return $this; + } + + public function add($name, $value) + { + $name = $this->normalizeName($name); + + if (array_key_exists($name, $this->headers)) + $this->headers[$name][] = $value; + else + $this->set($name, $value); + + return $this; + } + + public function remove($name) + { + if (!$this->has($name)) { + throw new MissingElementException( + sprintf('Header "%s" does not exist', $name) + ); + } + + unset($this->headers[$this->normalizeName($name)]); + + return $this; + } + + public function get($name) + { + $valueList = $this->getRaw($name); + + return count($valueList) > 1 ? $valueList : $valueList[0]; + } + + public function has($name) + { + return + array_key_exists( + $this->normalizeName($name), + $this->headers + ); + } + + public function getRaw($name) + { + if (!$this->has($name)) { + throw new MissingElementException( + sprintf('Header "%s" does not exist', $name) + ); + } + + return $this->headers[$this->normalizeName($name)]; + } + + public function getAll() + { + return $this->headers; + } + + public function getIterator() + { + return new ArrayIterator($this->headers); + } + + private function normalizeName($name) + { + return + preg_replace_callback( + '/(?[^-]+)/', + function ($match) { + return + strtoupper(substr($match['name'], 0, 1)) + .strtolower(substr($match['name'], 1)) + ; + }, + $name + ); + } + } +?> diff --git a/test/AllTests.php b/test/AllTests.php index 4f8e126fe4..60a482f1c4 100644 --- a/test/AllTests.php +++ b/test/AllTests.php @@ -27,6 +27,7 @@ ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Autoloader'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Ip'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'Routers'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'AMQP'.DIRECTORY_SEPARATOR, diff --git a/test/main/Net/Http/HttpHeaderCollectionTest.class.php b/test/main/Net/Http/HttpHeaderCollectionTest.class.php new file mode 100644 index 0000000000..6a282f66a7 --- /dev/null +++ b/test/main/Net/Http/HttpHeaderCollectionTest.class.php @@ -0,0 +1,67 @@ + 42) + ); + + return $collection; + } + + /** + * @depends testSetter + */ + public function testAddition(HttpHeaderCollection $collection) + { + $collection->add('x-foo', 'bar')->add('x-foo', 'baz'); + + return $collection; + } + + /** + * @depends testAddition + */ + public function testGetter(HttpHeaderCollection $collection) + { + $this->assertEquals(42, $collection->get('content-LeNgTh')); + $this->assertEquals(array(42), $collection->getRaw('content-LeNgTh')); + $this->assertEquals(array('bar', 'baz'), $collection->get('x-foo')); + + return $collection; + } + + /** + * @depends testGetter + */ + public function testRemoving(HttpHeaderCollection $collection) + { + $collection->remove('x-foo'); + + return $collection; + } + + /** + * @depends testRemoving + * @expectedException MissingElementException + */ + public function testFailedRemoving(HttpHeaderCollection $collection) + { + $collection->remove('x-foo'); + + return $collection; + } + } +?>