Skip to content
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

[RTM] Do not make the response private when saving the session #1388

Merged
merged 5 commits into from
Mar 1, 2018
Merged

[RTM] Do not make the response private when saving the session #1388

merged 5 commits into from
Mar 1, 2018

Conversation

leofeyer
Copy link
Member

@leofeyer leofeyer commented Feb 20, 2018

This PR fixes #1283.

Symfony has recently changed their session listener to always make the response private if the session has been started. Although we agree with the change, it renders the HTTP cache unusable, because Contao always starts a session (e.g. to store the user's language). This PR decorates the Symfony session listener and circumvents Symfony's changes by not making the response private if the request is a Contao front end request.

Of course, our long term goal should be to adjust Contao so it only starts a session if it really has to.

@leofeyer leofeyer added the bug label Feb 20, 2018
@leofeyer leofeyer added this to the 4.4.15 milestone Feb 20, 2018
@leofeyer leofeyer self-assigned this Feb 20, 2018
@leofeyer leofeyer requested a review from Toflar February 20, 2018 13:52
@leofeyer leofeyer changed the title [RTM] Set the front end response cache headers in an event listener [RTM] Set the front end response cache headers in a listener Feb 20, 2018
@Toflar Toflar mentioned this pull request Feb 20, 2018
9 tasks
@leofeyer leofeyer changed the title [RTM] Set the front end response cache headers in a listener [RFC] Do not make the response private when saving the session Feb 20, 2018
@leofeyer leofeyer changed the title [RFC] Do not make the response private when saving the session [RTM] Do not make the response private when saving the session Feb 20, 2018
@leofeyer leofeyer requested review from ausi, Toflar and aschempp and removed request for Toflar March 1, 2018 08:05
use Symfony\Component\HttpKernel\EventListener\SessionListener as BaseSessionListener;

/**
* Decorates the default session listener.
Copy link
Member

Choose a reason for hiding this comment

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

There's no explanation why this listener is present, what it does and when it will be removed again. I think that should be added because it's important information (it should also go into the PR description).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

return;
}

$session = $event->getRequest()->getSession();
Copy link
Member

Choose a reason for hiding this comment

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

@leofeyer There's an issue here. Either we have to call $this->inner->onKernelResponse($event); at the end of the method again or we can leave out the whole save() part here because we're overriding the method.

Copy link
Member Author

@leofeyer leofeyer Mar 23, 2018

Choose a reason for hiding this comment

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

No, we must not call $this->inner->onKernelResponse($event) here otherwise we have the same problem (private response) again. And saving is required to be forward compatible with Symfony 4.1.

Copy link
Member

Choose a reason for hiding this comment

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

This does not make sense to me. If you do not forward to the decorated service, why do you call save() then?

Copy link
Member Author

Choose a reason for hiding this comment

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

So the follow up listener, which checks $session->isStarted(), is not run.

Copy link
Member

Choose a reason for hiding this comment

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

Which follow up listener? I think you confuse stuff here. The one that checks for this is the one you decorate, no?

Copy link
Member Author

@leofeyer leofeyer Mar 23, 2018

Choose a reason for hiding this comment

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

I'm not confusing anything. We have carefully analyzed the program flow and set up the existing solution during the developer's meeting. Which issues does it cause for you?

symfony-splitter pushed a commit to symfony/http-kernel that referenced this pull request Mar 30, 2018
…rivate automatically (Toflar)

This PR was squashed before being merged into the 4.1-dev branch (closes #26681).

Discussion
----------

Allow to easily ask Symfony not to set a response to private automatically

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

This PR is related to the many discussions going on about the latest (Symfony 3.4+) changes regarding the way Symfony handles the session. I think we're almost there now, Symfony 4.1 automatically turns responses into `private` responses once the session is started and it's all done in the `AbstractSessionListener` where the session is also saved.
In other issues/PRs (symfony/symfony#25583, symfony/symfony#25699, symfony/symfony#24988) it was agreed that setting the response to `private` if the session is started is a good default for Symfony. It was also agreed that setting it to `private` does not always make sense because you **can share a response** across sessions, it just requires a more complex caching setup with shared user context etc.
So there must be an easy way to disable this behaviour. Right now it's very hard to do so because what you end up doing is basically decorating the `session_listener` which is very hard because you have to keep track on that over different Symfony versions as the base listener might get additional features etc.

The [FOSCacheBundle](FriendsOfSymfony/FOSHttpCacheBundle#438) is already having this problem, [Contao](contao/core-bundle#1388) has the same issue and there will be probably more. Basically everyone that wants to share a response cache across the session will have to decorate the default listener. That's just too hard, so I came up with this solution. The header is easy. Every project can add that easily. It does not require any extension, configuration or adjustment of any service. It's clean, transparent and has absolutely no impact on "default" Symfony setups.

Would be happy to have some feedback before 4.1 freeze.

Commits
-------

0f36710 Allow to easily ask Symfony not to set a response to private automatically
fabpot added a commit to symfony/symfony that referenced this pull request Mar 30, 2018
…rivate automatically (Toflar)

This PR was squashed before being merged into the 4.1-dev branch (closes #26681).

Discussion
----------

Allow to easily ask Symfony not to set a response to private automatically

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

This PR is related to the many discussions going on about the latest (Symfony 3.4+) changes regarding the way Symfony handles the session. I think we're almost there now, Symfony 4.1 automatically turns responses into `private` responses once the session is started and it's all done in the `AbstractSessionListener` where the session is also saved.
In other issues/PRs (#25583, #25699, #24988) it was agreed that setting the response to `private` if the session is started is a good default for Symfony. It was also agreed that setting it to `private` does not always make sense because you **can share a response** across sessions, it just requires a more complex caching setup with shared user context etc.
So there must be an easy way to disable this behaviour. Right now it's very hard to do so because what you end up doing is basically decorating the `session_listener` which is very hard because you have to keep track on that over different Symfony versions as the base listener might get additional features etc.

The [FOSCacheBundle](FriendsOfSymfony/FOSHttpCacheBundle#438) is already having this problem, [Contao](contao/core-bundle#1388) has the same issue and there will be probably more. Basically everyone that wants to share a response cache across the session will have to decorate the default listener. That's just too hard, so I came up with this solution. The header is easy. Every project can add that easily. It does not require any extension, configuration or adjustment of any service. It's clean, transparent and has absolutely no impact on "default" Symfony setups.

Would be happy to have some feedback before 4.1 freeze.

Commits
-------

0f36710 Allow to easily ask Symfony not to set a response to private automatically
@leofeyer leofeyer modified the milestones: 4.4.15, 4.4 May 14, 2019
leofeyer pushed a commit that referenced this pull request Mar 3, 2020
Description
-----------

Fixes #1388.

Commits
-------

49bf3a28 Update preview_toolbar_base.html.twig
b7a8e8f6 Update default.xlf
3710cd16 Use trans_default_domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants