From 478981678ac38c166ea994b1a7cc54191f463165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20F=C4=85fara?= Date: Tue, 17 Oct 2017 14:11:00 +0200 Subject: [PATCH 1/2] Prevent header parser from making nested arrays for multiple header occurrences --- Parsers/HTTPHeadersParser.php | 8 +++++++- Tests/Functional/Parsers/HTTPHeadersParserTest.php | 7 ++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Parsers/HTTPHeadersParser.php b/Parsers/HTTPHeadersParser.php index d05b51d..f83e5b6 100644 --- a/Parsers/HTTPHeadersParser.php +++ b/Parsers/HTTPHeadersParser.php @@ -45,7 +45,13 @@ 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]); + } elseif (is_array($carry[$match[1]])) { + $carry[$match[1]][] = trim($match[2]); + } else { + $carry[$match[1]] = [$carry[$match[1]], trim($match[2])]; + } return $carry; }, []); diff --git a/Tests/Functional/Parsers/HTTPHeadersParserTest.php b/Tests/Functional/Parsers/HTTPHeadersParserTest.php index f44af13..85fd71d 100644 --- a/Tests/Functional/Parsers/HTTPHeadersParserTest.php +++ b/Tests/Functional/Parsers/HTTPHeadersParserTest.php @@ -30,7 +30,7 @@ * * @SuppressWarnings("PHPMD.StaticAccess") */ -class ResponseHeadersTest extends \PHPUnit_Framework_TestCase { +class ResponseHeadersTest extends \PHPUnit_Framework_TestCase { /** * @test @@ -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); } /** From 3fcac3ef5cc60cdd594485f4601c0fef04594696 Mon Sep 17 00:00:00 2001 From: T Date: Mon, 21 Jan 2019 10:27:02 +0100 Subject: [PATCH 2/2] Update HTTPHeadersParser.php --- Parsers/HTTPHeadersParser.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Parsers/HTTPHeadersParser.php b/Parsers/HTTPHeadersParser.php index f83e5b6..9201fc7 100644 --- a/Parsers/HTTPHeadersParser.php +++ b/Parsers/HTTPHeadersParser.php @@ -47,13 +47,17 @@ public static function parse($headers) { if (!isset($carry[$match[1]])) { $carry[$match[1]] = trim($match[2]); - } elseif (is_array($carry[$match[1]])) { - $carry[$match[1]][] = trim($match[2]); - } else { - $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; }, []); } -} \ No newline at end of file +}