-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Create cookie from string + synchronize response cookies #20569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 1 commit into
symfony:master
from
ro0NL:http-foundation/cookie-from-string
Dec 17, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,28 +54,28 @@ public function __construct(array $headers = array()) | |
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Returns the headers, with original capitalizations. | ||
* | ||
* @return array An array of headers | ||
*/ | ||
public function __toString() | ||
public function allPreserveCase() | ||
{ | ||
$cookies = ''; | ||
foreach ($this->getCookies() as $cookie) { | ||
$cookies .= 'Set-Cookie: '.$cookie."\r\n"; | ||
$headers = array(); | ||
foreach ($this->all() as $name => $value) { | ||
$headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value; | ||
} | ||
|
||
ksort($this->headerNames); | ||
|
||
return parent::__toString().$cookies; | ||
return $headers; | ||
} | ||
|
||
/** | ||
* Returns the headers, with original capitalizations. | ||
* | ||
* @return array An array of headers | ||
*/ | ||
public function allPreserveCase() | ||
public function allPreserveCaseWithoutCookies() | ||
{ | ||
return array_combine($this->headerNames, $this->headers); | ||
$headers = $this->allPreserveCase(); | ||
if (isset($this->headerNames['set-cookie'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if a case is not provided for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
unset($headers[$this->headerNames['set-cookie']]); | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
/** | ||
|
@@ -95,13 +95,39 @@ public function replace(array $headers = array()) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($key, $values, $replace = true) | ||
public function all() | ||
{ | ||
parent::set($key, $values, $replace); | ||
$headers = parent::all(); | ||
foreach ($this->getCookies() as $cookie) { | ||
$headers['set-cookie'][] = (string) $cookie; | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($key, $values, $replace = true) | ||
{ | ||
$uniqueKey = str_replace('_', '-', strtolower($key)); | ||
|
||
if ('set-cookie' === $uniqueKey) { | ||
if ($replace) { | ||
$this->cookies = array(); | ||
} | ||
foreach ((array) $values as $cookie) { | ||
$this->setCookie(Cookie::fromString($cookie)); | ||
} | ||
$this->headerNames[$uniqueKey] = $key; | ||
|
||
return; | ||
} | ||
|
||
$this->headerNames[$uniqueKey] = $key; | ||
|
||
parent::set($key, $values, $replace); | ||
|
||
// ensure the cache-control header has sensible defaults | ||
if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) { | ||
$computed = $this->computeCacheControlValue(); | ||
|
@@ -116,11 +142,17 @@ public function set($key, $values, $replace = true) | |
*/ | ||
public function remove($key) | ||
{ | ||
parent::remove($key); | ||
|
||
$uniqueKey = str_replace('_', '-', strtolower($key)); | ||
unset($this->headerNames[$uniqueKey]); | ||
|
||
if ('set-cookie' === $uniqueKey) { | ||
$this->cookies = array(); | ||
|
||
return; | ||
} | ||
|
||
parent::remove($key); | ||
|
||
if ('cache-control' === $uniqueKey) { | ||
$this->computedCacheControl = array(); | ||
} | ||
|
@@ -150,6 +182,7 @@ public function getCacheControlDirective($key) | |
public function setCookie(Cookie $cookie) | ||
{ | ||
$this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; | ||
$this->headerNames['set-cookie'] = 'Set-Cookie'; | ||
} | ||
|
||
/** | ||
|
@@ -174,6 +207,10 @@ public function removeCookie($name, $path = '/', $domain = null) | |
unset($this->cookies[$domain]); | ||
} | ||
} | ||
|
||
if (empty($this->cookies)) { | ||
unset($this->headerNames['set-cookie']); | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you remove
headers
usage, the property should be removed as well... but it is protected, so that's a BC break.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResponsHeaderBag
overridesall()
by including the additional cookie headers.HeaderBag::all
still returns$this->headers
... so it's not removed.However internally it now uses
all()
(the API method) instead of$headers
(the property) so those cookie headers are taken into account when using for exampleHeaderBag::has
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the property is still used.
In the normal HeaderBag,
->all()
is a simple getter for it. But it is now overwritten in ResponseHeaderBag to add the set-cookie header