8000 feature #18105 [HttpFoundation] Add support for sending raw cookies i… · symfony/symfony@98ba4b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98ba4b7

Browse files
committed
feature #18105 [HttpFoundation] Add support for sending raw cookies in the response (jakzal)
This PR was merged into the 3.1-dev branch. Discussion ---------- [HttpFoundation] Add support for sending raw cookies in the response | Q | A | ------------- | --- | Branch | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13646 | License | MIT | Doc PR | TODO Commits ------- 43760a6 [HttpFoundation] Add support for sending raw cookies in the response
2 parents b19ce5e + 43760a6 commit 98ba4b7

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Cookie
2525
protected $path;
2626
protected $secure;
2727
protected $httpOnly;
28+
private $raw;
2829

2930
/**
3031
* Constructor.
@@ -36,10 +37,11 @@ class Cookie
3637
* @param string $domain The domain that the cookie is available to
3738
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
3839
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
40+
* @param bool $raw Whether the cookie value should be sent with no url encoding
3941
*
4042
* @throws \InvalidArgumentException
4143
*/
42-
public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
44+
public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false)
4345
{
4446
// from PHP source code
4547
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
@@ -68,6 +70,7 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
6870
$this->path = empty($path) ? '/' : $path;
6971
$this->secure = (bool) $secure;
7072
$this->httpOnly = (bool) $httpOnly;
73+
$this->raw = (bool) $raw;
7174
}
7275

7376
/**
@@ -187,4 +190,14 @@ public function isCleared()
187190
{
188191
return $this->expire < time();
189192
}
193+
194+
/**
195+
* Checks if the cookie value should be sent with no url encoding.
196+
*
197+
* @return bool
198+
*/
199+
public function isRaw()
200+
{
201+
return $this->raw;
202+
}
190203
}

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@ public function sendHeaders()
343343

344344
// cookies
345345
foreach ($this->headers->getCookies() as $cookie) {
346-
setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
346+
if ($cookie->isRaw()) {
347+
setrawcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
348+
} else {
349+
setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
350+
}
347351
}
348352

349353
return $this;

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,13 @@ public function testToString()
151151
$cookie = new Cookie('foo', 'bar', 0, '/', '');
152152
$this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
153153
}
154+
155+
public function testRawCookie()
156+
{
157+
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
158+
$this->assertFalse($cookie->isRaw());
159+
160+
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true);
161+
$this->assertTrue($cookie->isRaw());
162+
}
154163
}

0 commit comments

Comments
 (0)
0