forked from FriendsOfSymfony/FOSHttpCacheBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserContextSubscriber.php
More file actions
154 lines (134 loc) · 4.39 KB
/
UserContextSubscriber.php
File metadata and controls
154 lines (134 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCacheBundle\EventListener;
use FOS\HttpCache\UserContext\HashGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Check requests and responses with the matcher.
*
* Abort context hash requests immediately and return the hash.
* Add the vary information on responses to normal requests.
*
* @author Stefan Paschke <[email protected]>
* @author Joel Wurtz <[email protected]>
*/
class UserContextSubscriber implements EventSubscriberInterface
{
/**
* @var RequestMatcherInterface
*/
private $requestMatcher;
/**
* @var HashGenerator
*/
private $hashGenerator;
/**
* @var string[]
*/
private $userIdentifierHeaders;
/**
* @var string
*/
private $hashHeader;
/**
* @var integer
*/
private $ttl;
public function __construct(
RequestMatcherInterface $requestMatcher,
HashGenerator $hashGenerator,
array $userIdentifierHeaders = array('Cookie', 'Authorization'),
$hashHeader = "X-User-Context-Hash",
$ttl = 0
) {
if (!count($userIdentifierHeaders)) {
throw new \InvalidArgumentException('The user context must vary on some request headers');
}
$this->requestMatcher = $requestMatcher;
$this->hashGenerator = $hashGenerator;
$this->userIdentifierHeaders = $userIdentifierHeaders;
$this->hashHeader = $hashHeader;
$this->ttl = $ttl;
}
/**
* Return the response to the context hash request with a header containing
* the generated hash.
*
* If the ttl is bigger than 0, cache headers will be set for this response.
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
if (!$this->requestMatcher->matches($event->getRequest())) {
return;
}
$hash = $this->hashGenerator->generateHash();
// status needs to be 200 as otherwise varnish will not cache the response.
$response = new Response('', 200, array(
$this->hashHeader => $hash,
'Content-Type' => 'application/vnd.fos.user-context-hash',
));
if ($this->ttl > 0) {
$response->setClientTtl($this->ttl);
$response->setVary($this->userIdentifierHeaders);
$response->setPublic();
} else {
$response->setClientTtl(0);
$response->headers->addCacheControlDirective('no-cache');
}
$event->setResponse($response);
}
/**
* Add the context hash header to the headers to vary on if the header was
* present in the request.
*
* @param FilterResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
{
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$response = $event->getResponse();
$vary = $response->getVary();
if ($event->getRequest()->headers->has($this->hashHeader)) {
if (!in_array($this->hashHeader, $vary)) {
$vary[] = $this->hashHeader;
}
} else {
foreach ($this->userIdentifierHeaders as $header) {
if (!in_array($header, $vary)) {
$vary[] = $header;
}
}
}
$response->setVary($vary, true);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::REQUEST => array('onKernelRequest', 7),
);
}
}