2010-02-17 13:53:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:53:18 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:18 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation;
|
|
|
|
|
2010-02-17 13:53:18 +00:00
|
|
|
/**
|
2010-05-03 10:23:53 +01:00
|
|
|
* Response represents an HTTP response.
|
2010-02-17 13:53:18 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-17 13:53:18 +00:00
|
|
|
*/
|
2010-04-24 08:38:58 +01:00
|
|
|
class Response
|
2010-02-17 13:53:18 +00:00
|
|
|
{
|
2010-07-10 13:46:25 +01:00
|
|
|
/**
|
2011-01-09 18:51:06 +00:00
|
|
|
* @var \Symfony\Component\HttpFoundation\ResponseHeaderBag
|
2010-07-10 13:46:25 +01:00
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public $headers;
|
|
|
|
|
|
|
|
protected $content;
|
|
|
|
protected $version;
|
|
|
|
protected $statusCode;
|
|
|
|
protected $statusText;
|
2011-02-21 15:56:42 +00:00
|
|
|
protected $charset;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-07-09 13:38:28 +01:00
|
|
|
public static $statusTexts = array(
|
2010-05-06 12:25:53 +01:00
|
|
|
100 => 'Continue',
|
|
|
|
101 => 'Switching Protocols',
|
|
|
|
200 => 'OK',
|
|
|
|
201 => 'Created',
|
|
|
|
202 => 'Accepted',
|
|
|
|
203 => 'Non-Authoritative Information',
|
|
|
|
204 => 'No Content',
|
|
|
|
205 => 'Reset Content',
|
|
|
|
206 => 'Partial Content',
|
|
|
|
300 => 'Multiple Choices',
|
|
|
|
301 => 'Moved Permanently',
|
|
|
|
302 => 'Found',
|
|
|
|
303 => 'See Other',
|
|
|
|
304 => 'Not Modified',
|
|
|
|
305 => 'Use Proxy',
|
|
|
|
307 => 'Temporary Redirect',
|
|
|
|
400 => 'Bad Request',
|
|
|
|
401 => 'Unauthorized',
|
|
|
|
402 => 'Payment Required',
|
|
|
|
403 => 'Forbidden',
|
|
|
|
404 => 'Not Found',
|
|
|
|
405 => 'Method Not Allowed',
|
|
|
|
406 => 'Not Acceptable',
|
|
|
|
407 => 'Proxy Authentication Required',
|
|
|
|
408 => 'Request Timeout',
|
|
|
|
409 => 'Conflict',
|
|
|
|
410 => 'Gone',
|
|
|
|
411 => 'Length Required',
|
|
|
|
412 => 'Precondition Failed',
|
|
|
|
413 => 'Request Entity Too Large',
|
|
|
|
414 => 'Request-URI Too Long',
|
|
|
|
415 => 'Unsupported Media Type',
|
|
|
|
416 => 'Requested Range Not Satisfiable',
|
|
|
|
417 => 'Expectation Failed',
|
2011-01-18 17:32:02 +00:00
|
|
|
418 => 'I\'m a teapot',
|
2010-05-06 12:25:53 +01:00
|
|
|
500 => 'Internal Server Error',
|
|
|
|
501 => 'Not Implemented',
|
|
|
|
502 => 'Bad Gateway',
|
|
|
|
503 => 'Service Unavailable',
|
|
|
|
504 => 'Gateway Timeout',
|
|
|
|
505 => 'HTTP Version Not Supported',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $content The response content
|
|
|
|
* @param integer $status The response status code
|
|
|
|
* @param array $headers An array of response headers
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function __construct($content = '', $status = 200, $headers = array())
|
|
|
|
{
|
2011-05-16 07:46:36 +01:00
|
|
|
$this->headers = new ResponseHeaderBag($headers);
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->setContent($content);
|
|
|
|
$this->setStatusCode($status);
|
|
|
|
$this->setProtocolVersion('1.0');
|
2011-05-16 07:46:36 +01:00
|
|
|
if (!$this->headers->has('Date')) {
|
|
|
|
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-23 08:58:39 +01:00
|
|
|
* Returns the response content as it will be sent (with the headers).
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The response content
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2011-07-11 10:21:18 +01:00
|
|
|
$this->prepare();
|
2010-06-23 08:58:39 +01:00
|
|
|
|
2011-05-15 11:38:12 +01:00
|
|
|
return
|
|
|
|
sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
|
|
|
|
$this->headers."\r\n".
|
|
|
|
$this->getContent();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones the current Response instance.
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
$this->headers = clone $this->headers;
|
|
|
|
}
|
|
|
|
|
2011-07-11 10:21:18 +01:00
|
|
|
/**
|
|
|
|
* Prepares the Response before it is sent to the client.
|
|
|
|
*
|
|
|
|
* This method tweaks the Response to ensure that it is
|
|
|
|
* compliant with RFC 2616.
|
|
|
|
*/
|
|
|
|
public function prepare()
|
|
|
|
{
|
|
|
|
if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
|
|
|
|
$this->setContent('');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix Content-Type
|
|
|
|
$charset = $this->charset ?: 'UTF-8';
|
|
|
|
if (!$this->headers->has('Content-Type')) {
|
|
|
|
$this->headers->set('Content-Type', 'text/html; charset='.$charset);
|
2012-01-11 19:20:58 +00:00
|
|
|
} elseif (0 === strpos($this->headers->get('Content-Type'), 'text/') && false === strpos($this->headers->get('Content-Type'), 'charset')) {
|
2011-07-11 10:21:18 +01:00
|
|
|
// add the charset
|
|
|
|
$this->headers->set('Content-Type', $this->headers->get('Content-Type').'; charset='.$charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix Content-Length
|
|
|
|
if ($this->headers->has('Transfer-Encoding')) {
|
|
|
|
$this->headers->remove('Content-Length');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-06-23 15:24:24 +01:00
|
|
|
* Sends HTTP headers.
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function sendHeaders()
|
|
|
|
{
|
2011-07-07 06:31:02 +01:00
|
|
|
// headers have already been sent by the developer
|
|
|
|
if (headers_sent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 10:21:18 +01:00
|
|
|
$this->prepare();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
// status
|
|
|
|
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
|
|
|
|
|
|
|
|
// headers
|
2010-06-23 15:24:24 +01:00
|
|
|
foreach ($this->headers->all() as $name => $values) {
|
|
|
|
foreach ($values as $value) {
|
2011-06-10 17:16:33 +01:00
|
|
|
header($name.': '.$value, false);
|
2010-06-23 15:24:24 +01:00
|
|
|
}
|
2010-02-17 13:53:18 +00:00
|
|
|
}
|
2011-01-15 17:53:30 +00:00
|
|
|
|
|
|
|
// cookies
|
|
|
|
foreach ($this->headers->getCookies() as $cookie) {
|
2011-04-13 19:09:04 +01:00
|
|
|
setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
|
2011-01-15 17:53:30 +00:00
|
|
|
}
|
2010-02-17 13:53:18 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sends content for the current web response.
|
|
|
|
*/
|
|
|
|
public function sendContent()
|
|
|
|
{
|
|
|
|
echo $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends HTTP headers and content.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function send()
|
|
|
|
{
|
|
|
|
$this->sendHeaders();
|
|
|
|
$this->sendContent();
|
2011-06-13 17:54:20 +01:00
|
|
|
|
2011-06-09 19:11:02 +01:00
|
|
|
if (function_exists('fastcgi_finish_request')) {
|
|
|
|
fastcgi_finish_request();
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the response content.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-06-21 12:06:36 +01:00
|
|
|
* Valid types are strings, numbers, and objects that implement a __toString() method.
|
|
|
|
*
|
|
|
|
* @param mixed $content
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setContent($content)
|
|
|
|
{
|
2011-06-22 14:23:37 +01:00
|
|
|
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
|
2011-06-21 12:06:36 +01:00
|
|
|
throw new \UnexpectedValueException('The Response content must be a string or object implementing __toString(), "'.gettype($content).'" given.');
|
2011-06-21 10:59:20 +01:00
|
|
|
}
|
2011-06-22 14:22:06 +01:00
|
|
|
|
|
|
|
$this->content = (string) $content;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Gets the current response content.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string Content
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the HTTP protocol version (1.0 or 1.1).
|
|
|
|
*
|
|
|
|
* @param string $version The HTTP protocol version
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setProtocolVersion($version)
|
|
|
|
{
|
|
|
|
$this->version = $version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the HTTP protocol version.
|
|
|
|
*
|
|
|
|
* @return string The HTTP protocol version
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getProtocolVersion()
|
|
|
|
{
|
|
|
|
return $this->version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the response status code.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param integer $code HTTP status code
|
2012-07-19 16:48:12 +01:00
|
|
|
* @param mixed $text HTTP status text
|
|
|
|
*
|
|
|
|
* If the status text is null it will be automatically populated for the known
|
|
|
|
* status codes and left empty otherwise.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the HTTP status code is not valid
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setStatusCode($code, $text = null)
|
|
|
|
{
|
2012-07-19 16:48:12 +01:00
|
|
|
$this->statusCode = $code = (int) $code;
|
2011-03-05 13:27:35 +00:00
|
|
|
if ($this->isInvalid()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
|
|
|
|
}
|
|
|
|
|
2012-07-19 16:48:12 +01:00
|
|
|
if (null === $text) {
|
|
|
|
$this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : '';
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $text) {
|
|
|
|
$this->statusText = '';
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->statusText = $text;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Retrieves the status code for the current web response.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string Status code
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getStatusCode()
|
|
|
|
{
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
|
|
|
|
2010-12-15 11:10:29 +00:00
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the response charset.
|
2010-12-15 11:10:29 +00:00
|
|
|
*
|
|
|
|
* @param string $charset Character set
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-15 11:10:29 +00:00
|
|
|
*/
|
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
|
|
|
$this->charset = $charset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the response charset.
|
|
|
|
*
|
|
|
|
* @return string Character set
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-15 11:10:29 +00:00
|
|
|
*/
|
|
|
|
public function getCharset()
|
|
|
|
{
|
|
|
|
return $this->charset;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the response is worth caching under any circumstance.
|
|
|
|
*
|
|
|
|
* Responses marked "private" with an explicit Cache-Control directive are
|
|
|
|
* considered uncacheable.
|
|
|
|
*
|
|
|
|
* Responses with neither a freshness lifetime (Expires, max-age) nor cache
|
|
|
|
* validator (Last-Modified, ETag) are considered uncacheable.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the response is worth caching, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function isCacheable()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!in_array($this->statusCode, array(200, 203, 300, 301, 302, 404, 410))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
if ($this->headers->hasCacheControlDirective('no-store') || $this->headers->getCacheControlDirective('private')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isValidateable() || $this->isFresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the response is "fresh".
|
|
|
|
*
|
|
|
|
* Fresh responses may be served from cache without any interaction with the
|
|
|
|
* origin. A response is considered fresh when it includes a Cache-Control/max-age
|
|
|
|
* indicator or Expiration header and the calculated age is less than the freshness lifetime.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the response is fresh, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function isFresh()
|
|
|
|
{
|
|
|
|
return $this->getTtl() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the response includes headers that can be used to validate
|
|
|
|
* the response with the origin server using a conditional GET request.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the response is validateable, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function isValidateable()
|
|
|
|
{
|
|
|
|
return $this->headers->has('Last-Modified') || $this->headers->has('ETag');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-12 18:45:28 +00:00
|
|
|
* Marks the response as "private".
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* It makes the response ineligible for serving other clients.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-12 18:45:28 +00:00
|
|
|
*/
|
|
|
|
public function setPrivate()
|
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->headers->removeCacheControlDirective('public');
|
|
|
|
$this->headers->addCacheControlDirective('private');
|
2010-11-12 18:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the response as "public".
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-11-12 18:45:28 +00:00
|
|
|
* It makes the response eligible for serving other clients.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-11-12 18:45:28 +00:00
|
|
|
public function setPublic()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->headers->addCacheControlDirective('public');
|
|
|
|
$this->headers->removeCacheControlDirective('private');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the response must be revalidated by caches.
|
|
|
|
*
|
|
|
|
* This method indicates that the response must not be served stale by a
|
|
|
|
* cache in any circumstance without first revalidating with the origin.
|
2010-10-16 11:32:57 +01:00
|
|
|
* When present, the TTL of the response should not be overridden to be
|
2010-05-06 12:25:53 +01:00
|
|
|
* greater than the value provided by the origin.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the response must be revalidated by a cache, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function mustRevalidate()
|
|
|
|
{
|
2012-05-16 10:17:19 +01:00
|
|
|
return $this->headers->hasCacheControlDirective('must-revalidate') || $this->headers->has('proxy-revalidate');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Date header as a DateTime instance.
|
|
|
|
*
|
|
|
|
* @return \DateTime A \DateTime instance
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @throws \RuntimeException When the header is not parseable
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getDate()
|
|
|
|
{
|
2012-09-30 09:47:54 +01:00
|
|
|
return $this->headers->getDate('Date', new \DateTime());
|
2011-05-16 07:46:36 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-05-16 07:46:36 +01:00
|
|
|
/**
|
|
|
|
* Sets the Date header.
|
|
|
|
*
|
|
|
|
* @param \DateTime $date A \DateTime instance
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-05-16 07:46:36 +01:00
|
|
|
*/
|
|
|
|
public function setDate(\DateTime $date)
|
|
|
|
{
|
|
|
|
$date->setTimezone(new \DateTimeZone('UTC'));
|
|
|
|
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the age of the response.
|
|
|
|
*
|
|
|
|
* @return integer The age of the response in seconds
|
|
|
|
*/
|
|
|
|
public function getAge()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($age = $this->headers->get('Age')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $age;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max(time() - $this->getDate()->format('U'), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the response stale by setting the Age header to be equal to the maximum age of the response.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function expire()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->isFresh()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->headers->set('Age', $this->getMaxAge());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the Expires header as a DateTime instance.
|
|
|
|
*
|
|
|
|
* @return \DateTime A DateTime instance
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getExpires()
|
|
|
|
{
|
|
|
|
return $this->headers->getDate('Expires');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the Expires HTTP header with a DateTime instance.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-11-23 08:42:19 +00:00
|
|
|
* If passed a null value, it removes the header.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param \DateTime $date A \DateTime instance
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setExpires(\DateTime $date = null)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $date) {
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->headers->remove('Expires');
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-10-27 09:56:56 +01:00
|
|
|
$date = clone $date;
|
|
|
|
$date->setTimezone(new \DateTimeZone('UTC'));
|
|
|
|
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of seconds after the time specified in the response's Date
|
|
|
|
* header when the the response should no longer be considered fresh.
|
|
|
|
*
|
|
|
|
* First, it checks for a s-maxage directive, then a max-age directive, and then it falls
|
|
|
|
* back on an expires header. It returns null when no maximum age can be established.
|
|
|
|
*
|
|
|
|
* @return integer|null Number of seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getMaxAge()
|
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
if ($age = $this->headers->getCacheControlDirective('s-maxage')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $age;
|
|
|
|
}
|
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
if ($age = $this->headers->getCacheControlDirective('max-age')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $age;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->getExpires()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getExpires()->format('U') - $this->getDate()->format('U');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of seconds after which the response should no longer be considered fresh.
|
|
|
|
*
|
|
|
|
* This methods sets the Cache-Control max-age directive.
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @param integer $value Number of seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setMaxAge($value)
|
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->headers->addCacheControlDirective('max-age', $value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
|
|
|
|
*
|
|
|
|
* This methods sets the Cache-Control s-maxage directive.
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @param integer $value Number of seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setSharedMaxAge($value)
|
|
|
|
{
|
2011-04-13 14:58:48 +01:00
|
|
|
$this->setPublic();
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->headers->addCacheControlDirective('s-maxage', $value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the response's time-to-live in seconds.
|
|
|
|
*
|
|
|
|
* It returns null when no freshness information is present in the response.
|
|
|
|
*
|
|
|
|
* When the responses TTL is <= 0, the response may not be served from cache without first
|
|
|
|
* revalidating with the origin.
|
|
|
|
*
|
|
|
|
* @return integer The TTL in seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getTtl()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($maxAge = $this->getMaxAge()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $maxAge - $this->getAge();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the response's time-to-live for shared caches.
|
|
|
|
*
|
|
|
|
* This method adjusts the Cache-Control/s-maxage directive.
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @param integer $seconds Number of seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setTtl($seconds)
|
|
|
|
{
|
|
|
|
$this->setSharedMaxAge($this->getAge() + $seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the response's time-to-live for private/client caches.
|
|
|
|
*
|
|
|
|
* This method adjusts the Cache-Control/max-age directive.
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @param integer $seconds Number of seconds
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setClientTtl($seconds)
|
|
|
|
{
|
|
|
|
$this->setMaxAge($this->getAge() + $seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Last-Modified HTTP header as a DateTime instance.
|
|
|
|
*
|
|
|
|
* @return \DateTime A DateTime instance
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getLastModified()
|
|
|
|
{
|
2011-03-05 23:17:54 +00:00
|
|
|
return $this->headers->getDate('Last-Modified');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the Last-Modified HTTP header with a DateTime instance.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-11-23 08:42:19 +00:00
|
|
|
* If passed a null value, it removes the header.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param \DateTime $date A \DateTime instance
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setLastModified(\DateTime $date = null)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $date) {
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->headers->remove('Last-Modified');
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-10-27 09:56:56 +01:00
|
|
|
$date = clone $date;
|
|
|
|
$date->setTimezone(new \DateTimeZone('UTC'));
|
|
|
|
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Returns the literal value of the ETag HTTP header.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The ETag HTTP header
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getEtag()
|
2010-05-03 10:47:01 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->headers->get('ETag');
|
|
|
|
}
|
|
|
|
|
2010-11-12 18:46:22 +00:00
|
|
|
/**
|
|
|
|
* Sets the ETag value.
|
|
|
|
*
|
|
|
|
* @param string $etag The ETag unique identifier
|
|
|
|
* @param Boolean $weak Whether you want a weak ETag or not
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-12 18:46:22 +00:00
|
|
|
*/
|
2010-05-19 10:24:31 +01:00
|
|
|
public function setEtag($etag = null, $weak = false)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $etag) {
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->headers->remove('Etag');
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-19 10:24:31 +01:00
|
|
|
if (0 !== strpos($etag, '"')) {
|
|
|
|
$etag = '"'.$etag.'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 18:46:39 +00:00
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Sets the response's cache headers (validation and/or expiration).
|
2010-11-12 18:46:39 +00:00
|
|
|
*
|
2010-11-12 18:49:01 +00:00
|
|
|
* Available options are: etag, last_modified, max_age, s_maxage, private, and public.
|
2010-11-12 18:46:39 +00:00
|
|
|
*
|
|
|
|
* @param array $options An array of cache options
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-12 18:46:39 +00:00
|
|
|
*/
|
|
|
|
public function setCache(array $options)
|
|
|
|
{
|
2011-01-30 19:13:00 +00:00
|
|
|
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) {
|
2012-01-16 21:09:07 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff))));
|
2010-11-12 18:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['etag'])) {
|
|
|
|
$this->setEtag($options['etag']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['last_modified'])) {
|
|
|
|
$this->setLastModified($options['last_modified']);
|
|
|
|
}
|
|
|
|
|
2010-11-12 18:49:01 +00:00
|
|
|
if (isset($options['max_age'])) {
|
|
|
|
$this->setMaxAge($options['max_age']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['s_maxage'])) {
|
|
|
|
$this->setSharedMaxAge($options['s_maxage']);
|
|
|
|
}
|
|
|
|
|
2010-11-12 18:46:39 +00:00
|
|
|
if (isset($options['public'])) {
|
|
|
|
if ($options['public']) {
|
|
|
|
$this->setPublic();
|
|
|
|
} else {
|
|
|
|
$this->setPrivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['private'])) {
|
|
|
|
if ($options['private']) {
|
|
|
|
$this->setPrivate();
|
|
|
|
} else {
|
|
|
|
$this->setPublic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Modifies the response so that it conforms to the rules defined for a 304 status code.
|
|
|
|
*
|
|
|
|
* This sets the status, removes the body, and discards any headers
|
|
|
|
* that MUST NOT be included in 304 responses.
|
|
|
|
*
|
|
|
|
* @see http://tools.ietf.org/html/rfc2616#section-10.3.5
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setNotModified()
|
|
|
|
{
|
|
|
|
$this->setStatusCode(304);
|
|
|
|
$this->setContent(null);
|
|
|
|
|
|
|
|
// remove headers that MUST NOT be included with 304 Not Modified responses
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach (array('Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified') as $header) {
|
2010-11-23 08:42:19 +00:00
|
|
|
$this->headers->remove($header);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the response includes a Vary header.
|
|
|
|
*
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean true if the response includes a Vary header, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function hasVary()
|
|
|
|
{
|
|
|
|
return (Boolean) $this->headers->get('Vary');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of header names given in the Vary header.
|
|
|
|
*
|
|
|
|
* @return array An array of Vary names
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getVary()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$vary = $this->headers->get('Vary')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2010-10-30 15:37:18 +01:00
|
|
|
return is_array($vary) ? $vary : preg_split('/[\s,]+/', $vary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Vary header.
|
|
|
|
*
|
|
|
|
* @param string|array $headers
|
2012-05-18 18:37:58 +01:00
|
|
|
* @param Boolean $replace Whether to replace the actual value of not (true by default)
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-10-30 15:37:18 +01:00
|
|
|
*/
|
|
|
|
public function setVary($headers, $replace = true)
|
|
|
|
{
|
|
|
|
$this->headers->set('Vary', $headers, $replace);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 22:17:04 +00:00
|
|
|
* Determines if the Response validators (ETag, Last-Modified) match
|
2010-05-06 12:25:53 +01:00
|
|
|
* a conditional value specified in the Request.
|
|
|
|
*
|
|
|
|
* If the Response is not modified, it sets the status code to 304 and
|
2012-01-05 22:17:04 +00:00
|
|
|
* removes the actual content by calling the setNotModified() method.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param Request $request A Request instance
|
|
|
|
*
|
2012-01-05 22:17:04 +00:00
|
|
|
* @return Boolean true if the Response validators match the Request, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function isNotModified(Request $request)
|
|
|
|
{
|
|
|
|
$lastModified = $request->headers->get('If-Modified-Since');
|
|
|
|
$notModified = false;
|
2010-05-19 10:24:31 +01:00
|
|
|
if ($etags = $request->getEtags()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif ($lastModified) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$notModified = $lastModified == $this->headers->get('Last-Modified');
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($notModified) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->setNotModified();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isInvalid()
|
|
|
|
{
|
|
|
|
return $this->statusCode < 100 || $this->statusCode >= 600;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isInformational()
|
|
|
|
{
|
|
|
|
return $this->statusCode >= 100 && $this->statusCode < 200;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isSuccessful()
|
|
|
|
{
|
|
|
|
return $this->statusCode >= 200 && $this->statusCode < 300;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isRedirection()
|
|
|
|
{
|
|
|
|
return $this->statusCode >= 300 && $this->statusCode < 400;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isClientError()
|
|
|
|
{
|
|
|
|
return $this->statusCode >= 400 && $this->statusCode < 500;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isServerError()
|
|
|
|
{
|
|
|
|
return $this->statusCode >= 500 && $this->statusCode < 600;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isOk()
|
|
|
|
{
|
|
|
|
return 200 === $this->statusCode;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isForbidden()
|
|
|
|
{
|
|
|
|
return 403 === $this->statusCode;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isNotFound()
|
|
|
|
{
|
|
|
|
return 404 === $this->statusCode;
|
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @param string $location
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2011-05-29 14:33:37 +01:00
|
|
|
public function isRedirect($location = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-05-29 14:33:37 +01:00
|
|
|
return in_array($this->statusCode, array(201, 301, 302, 303, 307)) && (null === $location ?: $location == $this->headers->get('Location'));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-07-20 09:06:02 +01:00
|
|
|
/**
|
2012-02-12 01:04:15 +00:00
|
|
|
* @return Boolean
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return in_array($this->statusCode, array(201, 204, 304));
|
|
|
|
}
|
2010-02-17 13:53:18 +00:00
|
|
|
}
|