Skip to content

Commit 4e6d42f

Browse files
authored
IBX-11331: Fixed X-User-Context-Hash usage (#3064)
* IBX-11331: Fixed X-User-Context-Hash usage * Apply suggestions from code review
1 parent 7b669b1 commit 4e6d42f

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

docs/infrastructure_and_maintenance/cache/http_cache/content_aware_cache.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ Next, use nslookup to find the IP:
424424
### Fetching user context hash
425425

426426
As explained in [User Context Hash caching](context_aware_cache.md#user-context-hash-caching), the HTTP cache indexes the cache based on the user-context-hash.
427-
Users with the same user-context-hash here the same cache (as long as [[= product_name =]] responds with `Vary: X-Context-User-Hash`).
427+
Users with the same user-context-hash share the same cache (as long as [[= product_name =]] responds with `Vary: X-User-Context-Hash`).
428428

429429
To simulate the requests the HTTP cache sends to [[= product_name =]], you need this user-context-hash.
430430
To obtain it, use `curl`.

docs/infrastructure_and_maintenance/cache/http_cache/context_aware_cache.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This is called (user) context-aware cache.
1010
It means that HTTP cache is unique per set of user permissions (roles and limitations), and there are variations of cache shared only among users that have the exact same permissions.
1111
So if a user browses a list of children locations, they only see children locations they have access to, even if their rendering is served from HTTP cache.
1212

13-
This is accomplished by varying on a header called `X-Context-User-Hash`, which the system populates on the request.
13+
This is accomplished by varying on a header called `X-User-Context-Hash`, which the system populates on the request.
1414
The [logic for this](#request-lifecycle) is accomplished in the provided VCL for Varnish and Fastly.
1515
A similar but internal logic is done in the provided enhanced Symfony Proxy (AppCache).
1616

@@ -21,9 +21,9 @@ This expands steps covered in [FOSHttpCacheBundle documentation on user context
2121
1. A client (browser) requests URI `/foo`.
2222
1. The caching proxy receives the request and holds it. It first sends a hash request to the application's context hash route: `/_fos_user_context_hash`.
2323
1. The application receives the hash request. An event subscriber (`UserContextSubscriber`) aborts the request immediately after the Symfony firewall is applied.
24-
The application calculates the hash (`HashGenerator`) and then sends a response with the hash in a custom header (`X-Context-User-Hash`).
24+
The application calculates the hash (`HashGenerator`) and then sends a response with the hash in a custom header (`X-User-Context-Hash`).
2525
1. The caching proxy receives the hash response, copies the hash header to the client's original request for `/foo` and restarts the modified original request.
26-
1. If the response to `/foo` should differ per user context, the application sets a `Vary: X-Context-User-Hash` header, which makes Proxy store the variations of this cache varying on the hash value.
26+
1. If the response to `/foo` should differ per user context, the application sets a `Vary: X-User-Context-Hash` header, which makes Proxy store the variations of this cache varying on the hash value.
2727

2828
The next time a request comes in from the same user, application lookup for the hash (step 3) doesn't take place, as the hash lookup itself is cached by the cache proxy as described below.
2929

@@ -33,7 +33,7 @@ Example of a response sent to reverse proxy from `/_fos_user_context_hash` with
3333

3434
```
3535
HTTP/1.1 200 OK
36-
X-Context-User-Hash: <hash>
36+
X-User-Context-Hash: <hash>
3737
Content-Type: application/vnd.fos.user-context-hash
3838
Cache-Control: public, max-age=600
3939
Vary: Cookie, Authorization
@@ -104,7 +104,7 @@ This should be done as Ajax/JS lookup to avoid the uncached request that slows d
104104

105105
This solution requires more effort depending on project requirements (for example, traffic load).
106106

107-
3\. Custom vary by logic, for example, `X-User-Preference-Hash` inspired by `X-Context-User-Hash`:
107+
3\. Custom vary by logic, for example, `X-User-Preference-Hash` inspired by `X-User-Context-Hash`:
108108

109109
This method allows for fine-grained caching as you can explicitly vary on this in only the places that need it.
110110

@@ -157,15 +157,15 @@ public function addPreferenceHash(FilterResponseEvent $event)
157157
@@ -174,6 +174,7 @@ sub ez_user_context_hash {
158158
if (req.restarts == 0
159159
&& (req.http.accept ~ "application/vnd.fos.user-context-hash"
160-
|| req.http.X-Context-User-Hash
160+
|| req.http.x-user-context-hash
161161
+ || req.http.x-user-preference-hash
162162
)
163163
) {
164164
return (synth(400, "Bad Request"));
165165
@@ -263,12 +264,19 @@ sub vcl_deliver {
166166
&& resp.http.content-type ~ "application/vnd.fos.user-context-hash"
167167
) {
168-
set req.http.X-Context-User-Hash = resp.http.X-Context-User-Hash;
168+
set req.http.x-user-context-hash = resp.http.x-user-context-hash;
169169
+ set req.http.x-user-preference-hash = resp.http.x-user-preference-hash;
170170

171171
return (restart);
@@ -181,7 +181,7 @@ public function addPreferenceHash(FilterResponseEvent $event)
181181
+
182182
// Remove the vary on user context hash, this is nothing public. Keep all
183183
// other vary headers.
184-
if (resp.http.Vary ~ "X-Context-User-Hash") {
184+
if (resp.http.Vary ~ "X-User-Context-Hash") {
185185
```
186186

187187
3\. Add `Vary` in your custom controller or content view controller:
@@ -190,5 +190,5 @@ public function addPreferenceHash(FilterResponseEvent $event)
190190
$response->setVary('X-User-Preference-Hash');
191191

192192
// If you _also_ need to vary on [[= product_name =]] permissions, instead use:
193-
//$response->setVary(['X-Context-User-Hash', 'X-User-Preference-Hash']);
193+
//$response->setVary(['X-User-Context-Hash', 'X-User-Preference-Hash']);
194194
```

0 commit comments

Comments
 (0)