From 9e4681963d71168560fd5f1a48542422005ea69d Mon Sep 17 00:00:00 2001 From: Patrick Allaert Date: Mon, 10 Dec 2012 10:07:46 +0100 Subject: [PATCH] Fixed: HeaderBag::parseCacheControl() not parsing quoted zero correctly When having a Cache-Control header like: max-age="0" isset($match[2]) is true but $match[2] containing: "0", it is evaluated as false and 'true' will be set to "max-age" entry instead of "0". --- src/Symfony/Component/HttpFoundation/HeaderBag.php | 2 +- .../Component/HttpFoundation/Tests/HeaderBagTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index f26899d007..2360e55bb4 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -317,7 +317,7 @@ class HeaderBag implements \IteratorAggregate, \Countable $cacheControl = array(); preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER); foreach ($matches as $match) { - $cacheControl[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true); + $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); } return $cacheControl; diff --git a/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php index bafccb2eba..67e6040d12 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php @@ -125,6 +125,13 @@ class HeaderBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control')); } + public function testCacheControlDirectiveParsingQuotedZero() + { + $bag = new HeaderBag(array('cache-control' => 'max-age="0"')); + $this->assertTrue($bag->hasCacheControlDirective('max-age')); + $this->assertEquals(0, $bag->getCacheControlDirective('max-age')); + } + public function testCacheControlDirectiveOverrideWithReplace() { $bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));