diff --git a/README.md b/README.md index 9bad8ad4..0f5518a7 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,48 @@ $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. 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')); + } }