@@ -3,16 +3,22 @@ def __init__(self, get_response):
3
3
self .get_response = get_response
4
4
5
5
def __call__ (self , request ):
6
+ # Prepare request
7
+ self ._prepare_request_if_iframe (request )
6
8
response = self .get_response (request )
7
9
10
+ # Prepare response
8
11
self ._set_logged_in_cookie (request , response )
9
- self ._handle_iframe_cookie (request , response )
12
+ self ._persist_iframe_in_headers (request , response )
10
13
self ._cleanup_vary_header (response )
11
14
12
15
return response
13
16
14
17
def _set_logged_in_cookie (self , request , response ):
15
- """Set or update the 'logged-in' header based on authentication."""
18
+ """Set or update the 'logged-in' header based on authentication.
19
+ It is use to bypass cache by nginx.
20
+
21
+ If present, the logged_in cookie bypasses the cache."""
16
22
cookie_name = "logged_in"
17
23
18
24
# In some cases, gunicorn can be reached directly without going through
@@ -26,15 +32,32 @@ def _set_logged_in_cookie(self, request, response):
26
32
elif request .COOKIES .get (cookie_name ):
27
33
response .delete_cookie (cookie_name )
28
34
29
- def _handle_iframe_cookie (self , request , response ):
30
- """Manage iframe-related headers and cookies."""
31
- iframe_in_request = "iframe" in request .GET
32
- iframe_cookie = response .cookies .get ("iframe" )
35
+ def _prepare_request_if_iframe (self , request ):
36
+ """Detect if the request comes from an iframe mode.
37
+ The iframe mode is usually set on the initial request, and must be passed
38
+ during the navigation.
39
+ To be RGPD-compliant, and to satisfy some of our users constraints, we
40
+ cannot use Django session's cookie.
41
+ We rely on a mix between querystring and referrer.
33
42
34
- if iframe_in_request :
35
- response .set_cookie ("iframe" , "1" )
36
- response .headers ["iframe" ] = "1"
37
- elif iframe_cookie and iframe_cookie .value == "1" :
43
+ We also have a client-side fallback, based on sessionStorage : on initial
44
+ request, we set the iframe value in sessionStorage.
45
+ """
46
+ is_in_iframe_mode = False
47
+ if request .headers .get ("Sec-Fetch-Dest" ) == "iframe" :
48
+ is_in_iframe_mode = True
49
+
50
+ if "iframe" in request .GET :
51
+ is_in_iframe_mode = True
52
+
53
+ request .iframe = is_in_iframe_mode
54
+
55
+ def _persist_iframe_in_headers (self , request , response ):
56
+ """Persist iframe state in headers.
57
+ This is useful as headers are used as a cache key with nginx.
58
+ iFrame version of pages are cached differently."""
59
+
60
+ if request .iframe :
38
61
response .headers ["iframe" ] = "1"
39
62
else :
40
63
# Ensure the iframe header is not lingering
0 commit comments