Skip to content

Commit

Permalink
Handle headers that occur more than once
Browse files Browse the repository at this point in the history
As outlined in RFC 2616 Sec 4.2 paragraph 5
fixes #50
  • Loading branch information
nategood committed Sep 15, 2012
1 parent 45c41de commit c678733
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "http://github.com/nategood/httpful",
"license": "MIT",
"keywords": ["http", "curl", "rest", "restful", "api", "requests"],
"version": "0.1.6",
"version": "0.1.7",
"authors": [
{
"name": "Nate Good",
Expand Down
Binary file modified downloads/httpful.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Httpful/Httpful.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Httpful;

class Httpful {
const VERSION = '0.1.5';
const VERSION = '0.1.7';

private static $mimeRegistrar = array();
private static $default = null;
Expand Down
13 changes: 12 additions & 1 deletion src/Httpful/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ public function _parseHeaders($headers)
$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);
$key = trim($key);
$value = trim($raw_value);
if (array_key_exists($key, $parse_headers)) {
// See HTTP RFC Sec 4.2 Paragraph 5
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
// If a header appears more than once, it must also be able to
// be represented as a single header with a comma-separated
// list of values. We transform accordingly.
$parse_headers[$key] .= ',' . $value;
} else {
$parse_headers[$key] = $value;
}
}
return $parse_headers;
}
Expand Down
16 changes: 15 additions & 1 deletion tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ class HttpfulTest extends \PHPUnit_Framework_TestCase
Connection: keep-alive
Transfer-Encoding: chunked\r\n";
const SAMPLE_VENDOR_TYPE = "application/vnd.nategood.message+xml";

const SAMPLE_MULTI_HEADER =
"HTTP/1.1 200 OK
Content-Type: application/json
Connection: keep-alive
Transfer-Encoding: chunked
X-My-Header:Value1
X-My-Header:Value2\r\n";
function testInit()
{
$r = Request::init();
Expand Down Expand Up @@ -338,6 +344,14 @@ function test_parseHeaders()
$this->assertArrayHasKey('Connection', $parse_headers);
}

function testMultiHeaders()
{
$req = Request::init();
$response = new Response(self::SAMPLE_JSON_RESPONSE, self::SAMPLE_MULTI_HEADER, $req);
$parse_headers = $response->_parseHeaders(self::SAMPLE_MULTI_HEADER);
$this->assertEquals('Value1,Value2', $parse_headers['X-My-Header']);
}

function testDetectContentType()
{
$req = Request::init();
Expand Down

0 comments on commit c678733

Please sign in to comment.