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