Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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
merged 1 commit into from
Dec 17, 2016
Merged

[HttpFoundation] Create cookie from string + synchronize response cookies #20569

merged 1 commit into from
Dec 17, 2016

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Nov 19, 2016

Q A
Branch? "master"
Bug fix? yes
New feature? yes
BC breaks? no-ish
Deprecations? no
Tests pass? yes
Fixed tickets comma-separated list of tickets fixed by the PR, if any
License MIT
Doc PR reference to the documentation PR, if any

Fixes the conversion/synchroniczation of cookies between the object API and string API, ie;

$headers->setCookie(new Cookie('foo', 'bar'));
$headers->has('set-cookie'); // true

$headers->set('set-cookie', 'foo2=bar2', false);
count($headers->getCookies()); // 2

With an additional feature Cookie::fromString($cookie)

The BC break happens in terms you cannot set a custom Set-Cookie header anymore, all cookies are set via set(raw)cookie by PHP.

We could go one step further by tracking misc. attributes (Cookie::getAttributes()) and populate accordingly from Cookie::fromString. If the cookie has attributes we should bypass setcookie and write a header instead, but im not sure it's worth it.

@@ -32,6 +32,56 @@ class Cookie
const SAMESITE_STRICT = 'strict';

/**
* Create cookie from raw header string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creates

@HeahDude
Copy link
Contributor

It seems there is no deprecation in your PR, wrong description or WIP?

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 19, 2016

Not sure :) i tend to think it solves more then it would break. Right now all cookies are dealt with in a consistent manner, which is good.

However we could lose some raw cookie attributes though, and people benefit from this bug before in terms of preserving those attributes. I think Cookie should fix this in terms of getting the same output string for the given input string. Which means no BC break :)

@HeahDude

wrong description

yes :) deprecation label should be removed

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 19, 2016

The synchronization is also missing in all, has, etc. Should this go all the way?

Or what about the other way around? Add a Set-Cookie header from setCookie().

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 19, 2016

Ready :)


$headers = str_replace("\r\n", PHP_EOL, (string) $bag);
$this->assertRegExp('#^Set-Cookie:\s+foo=bar; path=/path/foo; domain=foo.bar; httponly$#m', $headers);
$this->assertRegExp('#^Set-Cookie:\s+foo=bar; path=/path/foo; domain=foo.bar; httponly$#m', $headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be path=/path/bar;?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, also regex escaping was missing. Fixed.

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 20, 2016

Green :)

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
if ($this->path) {
$str .= '; path='.$this->path;
}
$str .= '; path='.$this->getPath() ?: '/';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? IIRC, path is optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://github.com/ro0NL/symfony/blob/721fc351b2dcf81ba1888682a42155288f5474d2/src/Symfony/Component/HttpFoundation/Cookie.php#L124 i chose to force / by default here as well. Maybe it's not needed indeed.

I guess the only case we dont have a path is when a developer overrides Cookie::$path (set it manually).

What about

if ($this->getPath()) {
   //..
}

to be consistent with the domain attribute handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #20910

@@ -40,14 +40,14 @@ public function __construct(array $headers = array())
*/
public function __toString()
{
if (!$this->headers) {
if (!$headers = $this->all()) {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponsHeaderBag overrides all() 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 example HeaderBag::has.

Copy link
Member

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


if ('' === (string) $this->getValue()) {
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
} else {
$str .= urlencode($this->getValue());
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a bug fix ? If yes, it should go in older branches

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess so. I will separate it this evening 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #20910

@@ -40,14 +40,14 @@ public function __construct(array $headers = array())
*/
public function __toString()
{
if (!$this->headers) {
if (!$headers = $this->all()) {
Copy link
Member

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

{
return array_combine($this->headerNames, $this->headers);
$headers = $this->allPreserveCase();
if (isset($this->headerNames['set-cookie'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if a case is not provided for the set-cookie header ? you will not remove it at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always provided.. ie. here and here

fabpot added a commit that referenced this pull request Dec 14, 2016
…ookies (ro0NL)

This PR was squashed before being merged into the 3.1 branch (closes #20910).

Discussion
----------

[HttpFoundation] Fix cookie to string conversion for raw cookies

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | not really
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20569 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Separated from #20569

This mimics PHP's `setrawcookie` behavior.

Commits
-------

5e899cd [HttpFoundation] Fix cookie to string conversion for raw cookies
fabpot added a commit that referenced this pull request Dec 15, 2016
This PR was merged into the 3.3-dev branch.

Discussion
----------

[HttpFoundation] Compute cookie max-age attribute

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

See https://wiki.php.net/rfc/cookie_max-age, PHP sends it since 5.5.

~~The date format actually differs now from PHP (`Saturday` instead of `Sat`). But it really doesnt matter much imo, using the constant seems more reliable to me and max-age overrules anyway ;)~~

Relates to #20569

Commits
-------

8c28317 [HttpFoundation] Compute cookie max-age attribute
@ro0NL
Copy link
Contributor Author

ro0NL commented Dec 17, 2016

Rebased with the latest cookie changes. Still all good :)

@fabpot
Copy link
Member

fabpot commented Dec 17, 2016

Thank you @ro0NL.

@fabpot fabpot merged commit 7314456 into symfony:master Dec 17, 2016
fabpot added a commit that referenced this pull request Dec 17, 2016
…ze response cookies (ro0NL)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[HttpFoundation] Create cookie from string + synchronize response cookies

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no-ish
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

Fixes the conversion/synchroniczation of cookies between the object API and string API, ie;

```php
$headers->setCookie(new Cookie('foo', 'bar'));
$headers->has('set-cookie'); // true

$headers->set('set-cookie', 'foo2=bar2', false);
count($headers->getCookies()); // 2
```

With an additional feature `Cookie::fromString($cookie)`

The BC break happens in terms you cannot set a custom `Set-Cookie` header anymore, all cookies are set via `set(raw)cookie` by PHP.

We could go one step further by tracking misc. attributes (`Cookie::getAttributes()`) and populate accordingly from `Cookie::fromString`. If the cookie has attributes we should bypass `setcookie` and write a header instead, but im not sure it's worth it.

Commits
-------

7314456 [HttpFoundation] Create cookie from string + synchronize response cookies
@ro0NL ro0NL deleted the http-foundation/cookie-from-string branch December 17, 2016 19:31
fabpot added a commit that referenced this pull request Dec 19, 2016
This PR was merged into the 3.3-dev branch.

Discussion
----------

[HttpFoundation] update changelog

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20569
| License       | MIT
| Doc PR        |

Commits
-------

e91a654 [HttpFoundation] update changelog
fabpot added a commit that referenced this pull request Dec 22, 2016
This PR was merged into the 3.3-dev branch.

Discussion
----------

[HttpKernel] Continuation of #20569

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20567 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Forgotten in #20569 and makes #20567 truly a feature only.

Before

![image](https://cloud.githubusercontent.com/assets/1047696/21361738/971e4f8e-c6e5-11e6-8b08-8a5f8ab4f1f0.png)

After

![image](https://cloud.githubusercontent.com/assets/1047696/21361752/a6dc5380-c6e5-11e6-9f18-3bc3d7db94be.png)

Commits
-------

e1616b3 [HttpKernel] Continuation of #20569
fabpot added a commit that referenced this pull request Jan 6, 2017
This PR was squashed before being merged into the 2.7 branch (closes #20972).

Discussion
----------

[HttpFoundation] Improved set cookie header tests

| Q             | A
| ------------- | ---
| Branch?       | 2.7 (already in master)
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Separated from #20569

Commits
-------

05bce71 [HttpFoundation] Improved set cookie header tests
fabpot added a commit that referenced this pull request Mar 22, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[WebProfilerBundle] Improved cookie traffic

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

![image](https://cloud.githubusercontent.com/assets/1047696/20455635/a033a814-ae60-11e6-8500-e60146f4619e.png)

Relates to #20569 in terms of getting _all_ the cookies.

Commits
-------

171c6d1 [WebProfilerBundle] Improved cookie traffic
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
@fabpot fabpot mentioned this pull request May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants