Skip to content

Commit 6031a11

Browse files
committed
make anonymous hash header same between varnish and symfony
1 parent 1e522a1 commit 6031a11

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC
4040

4141
* BC BREAK: Constructors for PurgeSubscriber and RefreshSubscriber now use an
4242
options array for customization.
43+
* By default, no hash header is sent for anonymous users anymore, to sync
44+
behaviour with Varnish behaviour.
4345

4446
### Testing
4547

doc/symfony-cache-configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ options through the constructor:
181181

182182
* **anonymous_hash**: Hash used for anonymous user. This is a performance
183183
optimization to not do a backend request for users that are not logged in.
184+
By default, the header is skipped. If you specify a header, that header is
185+
used.
184186

185187
* **user_hash_accept_header**: Accept header value to be used to request the
186188
user hash to the backend application. Must match the setup of the backend

doc/user-context.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ client, moving step 2-4 into the cache. After the page is in cache, subsequent
6262
requests from clients that got the same hash can be served from the cache as
6363
well.
6464

65+
.. note::
66+
67+
If your application starts sessions for anonymous users, you will get one
68+
hash lookup request for each of those users. Your application can return
69+
the same hash for authenticated users with no special privileges as for
70+
anonymous users with a session cookie.
71+
72+
If there is no cookie and no authentication information, the hash lookup is
73+
skipped and no hash header added to the request. However, we can not avoid
74+
the initial hash lookup request per different cookie, as the caching proxy
75+
can not know which session cookies indicate a logged in user and which an
76+
anonymous session.
77+
6578
Proxy Client Configuration
6679
--------------------------
6780

doc/varnish-configuration.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Purge removes a specific URL (including query strings) in all its variants (as
5555
specified by the ``Vary`` header).
5656

5757
Subroutines are provided in ``resources/config/varnish-[version]/fos_purge.vcl``.
58-
To enable support add the following to ``your_varnish.vcl``:
58+
To enable this feature, add the following to ``your_varnish.vcl``:
5959

6060
.. configuration-block::
6161

@@ -96,7 +96,7 @@ Refreshing applies only to a specific URL including the query string, but *not*
9696
its variants.
9797

9898
Subroutines are provided in ``resources/config/varnish-[version]/fos_refresh.vcl``.
99-
To enable support, add the following to ``your_varnish.vcl``:
99+
To enable this feature, add the following to ``your_varnish.vcl``:
100100

101101
.. configuration-block::
102102

@@ -125,7 +125,7 @@ Ban
125125
Banning invalidates whole groups of cached entries with regular expressions.
126126

127127
Subroutines are provided in ``resources/config/varnish-[version]/fos_ban.vcl``
128-
To enable support add the following to ``your_varnish.vcl``:
128+
To enable this feature, add the following to ``your_varnish.vcl``:
129129

130130
.. configuration-block::
131131

@@ -202,11 +202,14 @@ User Context
202202

203203
Feature: :doc:`user context hashing <user-context>`
204204

205-
The ``fos_user_context.vcl`` needs the ``user_context_hash_url`` subroutine that sets a URL to the request lookup URL. The default URL is ``/_fos_user_context_hash`` and you can simply include ``resources/config/varnish-[version]/fos_user_context_url.vcl`` in your configuration to provide this. If you need a different URL, include a custom file implementing the ``user_context_hash_url`` subroutine.
206-
207-
208-
To enable support add the following to ``your_varnish.vcl``:
205+
The ``fos_user_context.vcl`` needs the ``user_context_hash_url`` subroutine
206+
that sets a URL to the request lookup URL. The default URL is
207+
``/_fos_user_context_hash`` and you can simply include
208+
``resources/config/varnish-[version]/fos_user_context_url.vcl`` in your
209+
configuration to provide this. If you need a different URL, include a custom
210+
file implementing the ``user_context_hash_url`` subroutine.
209211

212+
To enable this feature, add the following to ``your_varnish.vcl``:
210213

211214
.. configuration-block::
212215

@@ -358,7 +361,7 @@ sends an ``X-Cache-Debug`` header:
358361

359362
Subroutines are provided in ``fos_debug.vcl``.
360363

361-
To enable support add the following to ``your_varnish.vcl``:
364+
To enable this feature, add the following to ``your_varnish.vcl``:
362365

363366
.. configuration-block::
364367

src/SymfonyCache/UserContextSubscriber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(array $options = [])
6060
{
6161
$resolver = new OptionsResolver();
6262
$resolver->setDefaults([
63-
'anonymous_hash' => '38015b703d82206ebc01d17a39c727e5',
63+
'anonymous_hash' => null,
6464
'user_hash_accept_header' => 'application/vnd.fos.user-context-hash',
6565
'user_hash_header' => 'X-User-Context-Hash',
6666
'user_hash_uri' => '/_fos_user_context_hash',
@@ -104,8 +104,8 @@ public function preHandle(CacheEvent $event)
104104
return;
105105
}
106106

107-
if ($request->isMethodSafe()) {
108-
$request->headers->set($this->options['user_hash_header'], $this->getUserHash($event->getKernel(), $request));
107+
if ($request->isMethodSafe() && $hash = $this->getUserHash($event->getKernel(), $request)) {
108+
$request->headers->set($this->options['user_hash_header'], $hash);
109109
}
110110
}
111111

tests/Unit/SymfonyCache/UserContextSubscriberTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ public function testUserHashAnonymous($arg, $options)
107107
$response = $event->getResponse();
108108

109109
$this->assertNull($response);
110-
$this->assertTrue($request->headers->has($options['user_hash_header']));
111-
$this->assertSame($options['anonymous_hash'], $request->headers->get($options['user_hash_header']));
110+
if ($options['anonymous_hash']) {
111+
$this->assertTrue($request->headers->has($options['user_hash_header']));
112+
$this->assertSame($options['anonymous_hash'], $request->headers->get($options['user_hash_header']));
113+
} else {
114+
$this->assertFalse($request->headers->has($options['user_hash_header']));
115+
}
112116
}
113117

114118
/**

0 commit comments

Comments
 (0)