Skip to content

Commit

Permalink
Merge pull request #49 from mishak87/dev_source_with_headers
Browse files Browse the repository at this point in the history
More forgiving storage for headers
  • Loading branch information
Nate Good committed Aug 30, 2012
2 parents e6ba832 + 0b02dcb commit 90a6062
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
19 changes: 1 addition & 18 deletions src/Httpful/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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")));
Expand Down
58 changes: 58 additions & 0 deletions src/Httpful/Response/Headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Httpful\Response;

final class Headers implements \ArrayAccess, \Countable {

private $headers;

private function __construct($headers)
{
$this->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;
}

}
10 changes: 4 additions & 6 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 90a6062

Please sign in to comment.