Skip to content

Commit

Permalink
Merge pull request #47 from archdevil666pl/master
Browse files Browse the repository at this point in the history
Prevent from making nested arrays of headers
  • Loading branch information
TobiasHauck authored Jan 21, 2019
2 parents 0ed92b7 + 3fcac3e commit f1d01e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 12 additions & 2 deletions Parsers/HTTPHeadersParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ public static function parse($headers) {
return strtoupper($matches[0]);
}, strtolower(trim($match[1])));

$carry[$match[1]] = isset($carry[$match[1]]) ? [$carry[$match[1]], $match[2]] : trim($match[2]);
if (!isset($carry[$match[1]])) {
$carry[$match[1]] = trim($match[2]);
return $carry;
}

if (is_array($carry[$match[1]])) {
$carry[$match[1]][] = trim($match[2]);
return $carry;
}

$carry[$match[1]] = [$carry[$match[1]], trim($match[2])];

return $carry;
}, []);
}
}
}
7 changes: 4 additions & 3 deletions Tests/Functional/Parsers/HTTPHeadersParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class ResponseHeadersTest extends \PHPUnit_Framework_TestCase {
class ResponseHeadersTest extends \PHPUnit_Framework_TestCase {

/**
* @test
Expand Down Expand Up @@ -66,13 +66,14 @@ public function testParse() {
*/
public function testParseCookie() {
$headers = "Set-Cookie: foo=bar\r\n".
"Set-Cookie: baz=quux\r\n";
"Set-Cookie: baz=quux\r\n".
"Set-Cookie: quux=baz\r\n";
$returnValue = HTTPHeadersParser::parse($headers);
$this->assertTrue(is_array($returnValue));

$this->assertTrue(isset($returnValue['Set-Cookie']));
$this->assertTrue(is_array($returnValue['Set-Cookie']));
$this->assertEquals(count($returnValue['Set-Cookie']), 2);
$this->assertEquals(count($returnValue['Set-Cookie']), 3);
}

/**
Expand Down

0 comments on commit f1d01e9

Please sign in to comment.