Example #1
0
    public function testGenerateHash()
    {
        $hashGenerator = new HashGenerator(array(new FooProvider()));

        $expectedHash = hash('sha256', serialize(array('foo' => 'bar')));

        $this->assertEquals($expectedHash, $hashGenerator->generateHash());
    }
Example #2
0
    /**
     * 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);
    }