diff --git a/src/Httpful/Response.php b/src/Httpful/Response.php index 1075405..9b49c47 100644 --- a/src/Httpful/Response.php +++ b/src/Httpful/Response.php @@ -35,7 +35,7 @@ public function __construct($body, $headers, Request $request) $this->raw_body = $body; $this->code = $this->_parseCode($headers); - $this->headers = $this->_parseHeaders($headers); + $this->headers = Response\Headers::fromString($headers); $this->_interpretHeaders(); @@ -103,23 +103,6 @@ public function _parse($body) return Httpful::get($parse_with)->parse($body); } - /** - * Parse text headers from response into - * array of key value pairs - * @return array parse headers - * @param string $headers raw headers - */ - public function _parseHeaders($headers) - { - $headers = preg_split("/(\r|\n)+/", $headers, -1, \PREG_SPLIT_NO_EMPTY); - $parse_headers = array(); - for ($i = 1; $i < count($headers); $i++) { - list($key, $raw_value) = explode(':', $headers[$i], 2); - $parse_headers[trim($key)] = trim($raw_value); - } - return $parse_headers; - } - public function _parseCode($headers) { $parts = explode(' ', substr($headers, 0, strpos($headers, "\n"))); diff --git a/src/Httpful/Response/Headers.php b/src/Httpful/Response/Headers.php new file mode 100644 index 0000000..c340039 --- /dev/null +++ b/src/Httpful/Response/Headers.php @@ -0,0 +1,58 @@ +headers = $headers; + } + + public static function fromString($string) + { + $lines = preg_split("/(\r|\n)+/", $string, -1, PREG_SPLIT_NO_EMPTY); + array_shift($lines); // HTTP HEADER + $headers = array(); + foreach ($lines as $line) { + list($name, $value) = explode(':', $line, 2); + $headers[strtolower(trim($name))] = trim($value); + } + return new self($headers); + } + + public function offsetExists($offset) + { + return isset($this->headers[strtolower($offset)]); + } + + public function offsetGet($offset) + { + if (isset($this->headers[$name = strtolower($offset)])) { + return $this->headers[$name]; + } + } + + public function offsetSet($offset, $value) + { + throw new \Exception("Headers are read-only."); + } + + public function offsetUnset($offset) + { + throw new \Exception("Headers are read-only."); + } + + public function count() + { + return count($this->headers); + } + + public function toArray() + { + return $this->headers; + } + +} \ No newline at end of file diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 98231af..ae84ac7 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -257,7 +257,7 @@ function testParsingContentTypeCharset() // // Check default content type of iso-8859-1 $response = new Response(self::SAMPLE_JSON_RESPONSE, "HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8\r\n", $req); - $this->assertInternalType('array', $response->headers); + $this->assertInstanceOf('Httpful\Response\Headers', $response->headers); $this->assertEquals($response->headers['Content-Type'], 'text/plain; charset=utf-8'); $this->assertEquals($response->content_type, 'text/plain'); $this->assertEquals($response->charset, 'utf-8'); @@ -330,12 +330,10 @@ function testToString() function test_parseHeaders() { - $req = Request::init(); - $response = new Response(self::SAMPLE_JSON_RESPONSE, self::SAMPLE_JSON_HEADER, $req); - $parse_headers = $response->_parseHeaders(self::SAMPLE_JSON_HEADER); - $this->assertEquals(3, count($parse_headers)); + $parse_headers = Response\Headers::fromString(self::SAMPLE_JSON_HEADER); + $this->assertCount(3, $parse_headers); $this->assertEquals('application/json', $parse_headers['Content-Type']); - $this->assertArrayHasKey('Connection', $parse_headers); + $this->assertTrue(isset($parse_headers['Connection'])); } function testDetectContentType()