From 0a55272ddebbf25e331da02ece6f2a6b555ead24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 13 Oct 2016 17:14:16 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Add=20request=20header=20accessors=20(?= =?UTF-8?q?=C3=A0=20la=20PSR-7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 45 ++++++++++++++++++++++++++++++++++ src/Request.php | 56 +++++++++++++++++++++++++++++++++++++++++++ tests/RequestTest.php | 34 ++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/README.md b/README.md index f73f9ab7..1a8cafe6 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,51 @@ $loop->run(); See also the [examples](examples). +## Usage + +### Server + +See the above usage example and the class outline for details. + +### Request + +See the above usage example and the class outline for details. + +#### getHeaders() + +The `getHeaders(): array` method can be used to +return ALL headers. + +This will return an (possibly empty) assoc array with header names as +key and header values as value. The header value will be a string if +there's only a single value or an array of strings if this header has +multiple values. + +Note that this differs from the PSR-7 implementation of this method. + +#### getHeader() + +The `getHeader(string $name): string[]` method can be used to +retrieve a message header value by the given case-insensitive name. + +Returns a list of all values for this header name or an empty array if header was not found + +#### getHeaderLine() + +The `getHeaderLine(string $name): string` method can be used to +retrieve a comma-separated string of the values for a single header. + +Returns a comma-separated list of all values for this header name or an empty string if header was not found + +#### hasHeader() + +The `hasHeader(string $name): bool` method can be used to +check if a header exists by the given case-insensitive name. + +### Response + +See the above usage example and the class outline for details. + ## Install The recommended way to install this library is [through Composer](http://getcomposer.org). diff --git a/src/Request.php b/src/Request.php index 605b909e..af11fc91 100644 --- a/src/Request.php +++ b/src/Request.php @@ -48,11 +48,67 @@ public function getHttpVersion() return $this->httpVersion; } + /** + * Returns ALL headers + * + * This will return an (possibly empty) assoc array with header names as + * key and header values as value. The header value will be a string if + * there's only a single value or an array of strings if this header has + * multiple values. + * + * Note that this differs from the PSR-7 implementation of this method. + * + * @return array + */ public function getHeaders() { return $this->headers; } + /** + * Retrieves a message header value by the given case-insensitive name. + * + * @param string $name + * @return string[] a list of all values for this header name or an empty array if header was not found + */ + public function getHeader($name) + { + $found = array(); + + $name = strtolower($name); + foreach ($this->headers as $key => $value) { + if (strtolower($key) === $name) { + foreach((array)$value as $one) { + $found []= $one; + } + } + } + + return $found; + } + + /** + * Retrieves a comma-separated string of the values for a single header. + * + * @param string $name + * @return string a comma-separated list of all values for this header name or an empty string if header was not found + */ + public function getHeaderLine($name) + { + return implode(', ', $this->getHeader($name)); + } + + /** + * Checks if a header exists by the given case-insensitive name. + * + * @param string $name + * @return bool + */ + public function hasHeader($name) + { + return !!$this->getHeader($name); + } + public function expectsContinue() { return isset($this->headers['Expect']) && '100-continue' === $this->headers['Expect']; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 1ad85221..d7dd2496 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -23,4 +23,38 @@ public function expectsContinueShouldBeTrueIfContinueExpected() $this->assertTrue($request->expectsContinue()); } + + public function testEmptyHeader() + { + $request = new Request('GET', '/'); + + $this->assertEquals(array(), $request->getHeaders()); + $this->assertFalse($request->hasHeader('Test')); + $this->assertEquals(array(), $request->getHeader('Test')); + $this->assertEquals('', $request->getHeaderLine('Test')); + } + + public function testHeaderIsCaseInsensitive() + { + $request = new Request('GET', '/', array(), '1.1', array( + 'TEST' => 'Yes', + )); + + $this->assertEquals(array('TEST' => 'Yes'), $request->getHeaders()); + $this->assertTrue($request->hasHeader('Test')); + $this->assertEquals(array('Yes'), $request->getHeader('Test')); + $this->assertEquals('Yes', $request->getHeaderLine('Test')); + } + + public function testHeaderWithMultipleValues() + { + $request = new Request('GET', '/', array(), '1.1', array( + 'Test' => array('a', 'b'), + )); + + $this->assertEquals(array('Test' => array('a', 'b')), $request->getHeaders()); + $this->assertTrue($request->hasHeader('Test')); + $this->assertEquals(array('a', 'b'), $request->getHeader('Test')); + $this->assertEquals('a, b', $request->getHeaderLine('Test')); + } } From 0a0f6833c0c4cfe4d1967aed6095153c51fd2791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 9 Feb 2017 21:48:59 +0100 Subject: [PATCH 2/2] Be explicit about differences with PSR-7 --- README.md | 14 ++++++++------ src/Request.php | 16 +++++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1a8cafe6..1076eac6 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,16 @@ See the above usage example and the class outline for details. #### getHeaders() The `getHeaders(): array` method can be used to -return ALL headers. +return an array with ALL headers. -This will return an (possibly empty) assoc array with header names as -key and header values as value. The header value will be a string if -there's only a single value or an array of strings if this header has -multiple values. +The keys represent the header name in the exact case in which they were +originally specified. The values will be a string if there's only a single +value for the respective header name or an array of strings if this header +has multiple values. -Note that this differs from the PSR-7 implementation of this method. +> Note that this differs from the PSR-7 implementation of this method, +which always returns an array for each header name, even if it only has a +single value. #### getHeader() diff --git a/src/Request.php b/src/Request.php index af11fc91..ec2041d6 100644 --- a/src/Request.php +++ b/src/Request.php @@ -49,14 +49,16 @@ public function getHttpVersion() } /** - * Returns ALL headers + * Returns an array with ALL headers * - * This will return an (possibly empty) assoc array with header names as - * key and header values as value. The header value will be a string if - * there's only a single value or an array of strings if this header has - * multiple values. + * The keys represent the header name in the exact case in which they were + * originally specified. The values will be a string if there's only a single + * value for the respective header name or an array of strings if this header + * has multiple values. * - * Note that this differs from the PSR-7 implementation of this method. + * Note that this differs from the PSR-7 implementation of this method, + * which always returns an array for each header name, even if it only has a + * single value. * * @return array */ @@ -79,7 +81,7 @@ public function getHeader($name) foreach ($this->headers as $key => $value) { if (strtolower($key) === $name) { foreach((array)$value as $one) { - $found []= $one; + $found[] = $one; } } }