From 43760a69b625b05a822273fe35aad38e88bc5ac1 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Fri, 11 Mar 2016 09:58:00 +0000 Subject: [PATCH] [HttpFoundation] Add support for sending raw cookies in the response --- src/Symfony/Component/HttpFoundation/Cookie.php | 15 ++++++++++++++- src/Symfony/Component/HttpFoundation/Response.php | 6 +++++- .../Component/HttpFoundation/Tests/CookieTest.php | 9 +++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Cookie.php b/src/Symfony/Component/HttpFoundation/Cookie.php index 13d69f3bd2edd..c537dda4240a6 100644 --- a/src/Symfony/Component/HttpFoundation/Cookie.php +++ b/src/Symfony/Component/HttpFoundation/Cookie.php @@ -25,6 +25,7 @@ class Cookie protected $path; protected $secure; protected $httpOnly; + private $raw; /** * Constructor. @@ -36,10 +37,11 @@ class Cookie * @param string $domain The domain that the cookie is available to * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol + * @param bool $raw Whether the cookie value should be sent with no url encoding * * @throws \InvalidArgumentException */ - public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true) + public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false) { // from PHP source code if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { @@ -68,6 +70,7 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom $this->path = empty($path) ? '/' : $path; $this->secure = (bool) $secure; $this->httpOnly = (bool) $httpOnly; + $this->raw = (bool) $raw; } /** @@ -187,4 +190,14 @@ public function isCleared() { return $this->expire < time(); } + + /** + * Checks if the cookie value should be sent with no url encoding. + * + * @return bool + */ + public function isRaw() + { + return $this->raw; + } } diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 7ef986ea061d2..f6cd2fe3734f5 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -343,7 +343,11 @@ public function sendHeaders() // cookies foreach ($this->headers->getCookies() as $cookie) { - setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); + if ($cookie->isRaw()) { + setrawcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); + } else { + setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); + } } return $this; diff --git a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php index 222786533ecca..9134c1570c262 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php @@ -151,4 +151,13 @@ public function testToString() $cookie = new Cookie('foo', 'bar', 0, '/', ''); $this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString()); } + + public function testRawCookie() + { + $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true); + $this->assertFalse($cookie->isRaw()); + + $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true); + $this->assertTrue($cookie->isRaw()); + } }