public function testExpiresToStringReturnsHeaderFormattedString() { $this->markTestIncomplete('Expires needs to be completed'); $expiresHeader = new Expires(); // @todo set some values, then test output $this->assertEmpty('Expires: xxx', $expiresHeader->toString()); }
protected function cacheResponse(Response $response) { // --- Caching --- //https://github.com/widmogrod/zf2-semi-http-cache/blob/master/src/WidHttpCache/Listener/HttpCacheListener.php //https://developers.google.com/speed/docs/best-practices/caching $headers = $response->getHeaders(); $pragma = new Pragma(); $headers->addHeader($pragma); //$pragma = $headers->get('Pragma'); //echo __FILE__ . ' Line: ' . __LINE__; var_dump($pragma); exit; //XXX $expires = new Expires(); $expires->setDate('2015-12-12'); $headers->addHeader($expires); /** @var $cacheControl CacheControl */ $cacheControl = new CacheControl(); $headers->addHeader($cacheControl); $cacheControl->addDirective('max-age', 31536000); return $response; }
public function testExpiresToStringReturnsHeaderFormattedString() { $expiresHeader = new Expires(); $expiresHeader->setDate('Sun, 06 Nov 1994 08:49:37 GMT'); $this->assertEquals('Expires: Sun, 06 Nov 1994 08:49:37 GMT', $expiresHeader->toString()); }
/** * @param Headers $headers * @return self */ public function setExpires(Headers $headers) { if (!empty($this->cacheConfig['expires']['value']) && (!empty($this->cacheConfig['expires']['override']) || !$headers->has('expires'))) { $expires = new Header\Expires(); try { $expires->setDate($this->cacheConfig['expires']['value']); } catch (Header\Exception\InvalidArgumentException $e) { $date = $headers->has('date') ? $headers->get('date')->date() : sprintf('@%s', $_SERVER['REQUEST_TIME']); $expires->setDate($date); } $headers->addHeader($expires); } return $this; }
/** * Examine Response for Cache Control Headers * ensure expiry value does not exceed life span of access token * * @param MvcEvent $e */ public function onFinish(MvcEvent $e) { $response = $e->getResponse(); if (!$response instanceof HttpResponse || !$response->isOk()) { return; } $cacheControl = $response->getHeaders()->get('Cache-Control'); if ($cacheControl instanceof CacheControl) { $expires = $cacheControl->getDirective('expires'); $identity = $this->authService->getIdentity(); if ($identity instanceof OAuth2Authenticated) { $accessToken = $identity->getAccessToken(); // Does the access token have an expiry? if (isset($accessToken['expires'])) { $accessTokenExpiryTimestamp = $accessToken['expires']; $dateTime = new \DateTime(); $currentTimestamp = $dateTime->getTimestamp(); $expiresTimestamp = $currentTimestamp + $expires; /** * If the expiry timestamp of the response is greater than that of the * current access token, we need to reset the cache expiry and response expiry * to match that of the access token. * * It prevents stale authentication taking place. */ if ($expiresTimestamp > $accessTokenExpiryTimestamp) { $newExpiry = $accessTokenExpiryTimestamp - $currentTimestamp; $cacheControl->addDirective('expires', $newExpiry); $cacheControl->addDirective('max-age', $newExpiry); $response->getHeaders()->addHeader($cacheControl); $expires = new Expires(); $dateTime->modify('+ ' . $newExpiry . 'seconds'); $expires->setDate($dateTime); $response->getHeaders()->addHeader($expires); } } } } }
/** * Cache Response for future requests * * @param MvcEvent $e * @return \Zend\Stdlib\ResponseInterface */ public function onFinish(MvcEvent $e) { $request = $e->getRequest(); if (!$request instanceof HttpRequest) { return; } if (!$request->isGet()) { return; } $response = $e->getResponse(); if ($response instanceof HttpResponse && !$response->isOk()) { return; } // Do not continue if weren't able to compose a key if (empty($this->cache_key)) { return; } if (!$this->cacheAdapter->hasItem($this->cache_key)) { $resourceIdentifier = $e->getRouteMatch()->getParam('resource'); $resource = call_user_func($this->getResourceLocatorService(), $resourceIdentifier); if (!$resource instanceof Resource || !$resource->isCacheable()) { return; } // Generate Response cache headers based on Resource CacheOptions $cacheOptions = $resource->getCacheOptions(); $cacheControl = new CacheControl(); $cacheControl->addDirective($cacheOptions->getAccess()); $cacheControl->addDirective('max-age', $cacheOptions->getMaxAge()); $cacheControl->addDirective('expires', $cacheOptions->getExpires()); $cacheControl->addDirective('must-revalidate'); $dateTime = new \DateTime(); $dateTime->modify('+ ' . $cacheOptions->getExpires() . 'seconds'); $expires = new Expires(); $expires->setDate($dateTime); $lastModified = new LastModified(); $lastModified->setDate(new \DateTime()); // Add Headers to Response Header $response->getHeaders()->addHeader($cacheControl); $response->getHeaders()->addHeader($expires); $response->getHeaders()->addHeaderLine('Pragma: ' . $cacheOptions->getAccess()); $response->getHeaders()->addHeader(Etag::fromString('Etag: ' . md5($response->getBody()))); $response->getHeaders()->addHeader($lastModified); // Set cache adapter's TTL using Resource cache expires value $this->cacheAdapter->getOptions()->setTtl($cacheOptions->getExpires()); $this->cacheAdapter->setItem($this->cache_key, $response); //return $response; } }