This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation/Response.php

1309 lines
37 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
/**
* Response represents an HTTP response.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Response
{
const HTTP_CONTINUE = 100;
const HTTP_SWITCHING_PROTOCOLS = 101;
const HTTP_PROCESSING = 102; // RFC2518
const HTTP_EARLY_HINTS = 103; // RFC8297
const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_ACCEPTED = 202;
const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
const HTTP_NO_CONTENT = 204;
const HTTP_RESET_CONTENT = 205;
const HTTP_PARTIAL_CONTENT = 206;
const HTTP_MULTI_STATUS = 207; // RFC4918
const HTTP_ALREADY_REPORTED = 208; // RFC5842
const HTTP_IM_USED = 226; // RFC3229
const HTTP_MULTIPLE_CHOICES = 300;
const HTTP_MOVED_PERMANENTLY = 301;
const HTTP_FOUND = 302;
const HTTP_SEE_OTHER = 303;
const HTTP_NOT_MODIFIED = 304;
const HTTP_USE_PROXY = 305;
const HTTP_RESERVED = 306;
const HTTP_TEMPORARY_REDIRECT = 307;
2014-06-07 12:26:56 +01:00
const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
const HTTP_BAD_REQUEST = 400;
const HTTP_UNAUTHORIZED = 401;
const HTTP_PAYMENT_REQUIRED = 402;
const HTTP_FORBIDDEN = 403;
const HTTP_NOT_FOUND = 404;
const HTTP_METHOD_NOT_ALLOWED = 405;
const HTTP_NOT_ACCEPTABLE = 406;
const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
const HTTP_REQUEST_TIMEOUT = 408;
const HTTP_CONFLICT = 409;
const HTTP_GONE = 410;
const HTTP_LENGTH_REQUIRED = 411;
const HTTP_PRECONDITION_FAILED = 412;
const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
const HTTP_REQUEST_URI_TOO_LONG = 414;
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
const HTTP_EXPECTATION_FAILED = 417;
const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
2016-05-13 16:31:27 +01:00
const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
const HTTP_LOCKED = 423; // RFC4918
const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
/**
* @deprecated
*/
const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817
const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04
const HTTP_UPGRADE_REQUIRED = 426; // RFC2817
const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585
const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585
const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
2016-01-25 11:39:25 +00:00
const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
const HTTP_INTERNAL_SERVER_ERROR = 500;
const HTTP_NOT_IMPLEMENTED = 501;
const HTTP_BAD_GATEWAY = 502;
const HTTP_SERVICE_UNAVAILABLE = 503;
const HTTP_GATEWAY_TIMEOUT = 504;
const HTTP_VERSION_NOT_SUPPORTED = 505;
const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918
const HTTP_LOOP_DETECTED = 508; // RFC5842
const HTTP_NOT_EXTENDED = 510; // RFC2774
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
2010-07-10 13:46:25 +01:00
/**
* @var \Symfony\Component\HttpFoundation\ResponseHeaderBag
2010-07-10 13:46:25 +01:00
*/
public $headers;
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
protected $content;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
protected $version;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
2014-04-16 11:30:19 +01:00
* @var int
2011-11-02 15:42:51 +00:00
*/
protected $statusCode;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
protected $statusText;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
protected $charset;
2011-11-02 15:42:51 +00:00
/**
* Status codes translation table.
*
* The list of codes is complete according to the
* {@link http://www.iana.org/assignments/http-status-codes/ Hypertext Transfer Protocol (HTTP) Status Code Registry}
2016-04-27 09:40:10 +01:00
* (last updated 2016-03-01).
*
* Unless otherwise noted, the status code is defined in RFC2616.
*
2011-11-02 15:42:51 +00:00
* @var array
*/
2019-01-16 09:39:14 +00:00
public static $statusTexts = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing', // RFC2518
103 => 'Early Hints',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status', // RFC4918
208 => 'Already Reported', // RFC5842
226 => 'IM Used', // RFC3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
2014-06-07 12:26:56 +01:00
308 => 'Permanent Redirect', // RFC7238
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 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
2015-11-18 09:45:48 +00:00
418 => 'I\'m a teapot', // RFC2324
2016-04-27 09:40:10 +01:00
421 => 'Misdirected Request', // RFC7540
422 => 'Unprocessable Entity', // RFC4918
423 => 'Locked', // RFC4918
424 => 'Failed Dependency', // RFC4918
425 => 'Too Early', // RFC-ietf-httpbis-replay-04
426 => 'Upgrade Required', // RFC2817
428 => 'Precondition Required', // RFC6585
429 => 'Too Many Requests', // RFC6585
431 => 'Request Header Fields Too Large', // RFC6585
2016-03-02 00:02:26 +00:00
451 => 'Unavailable For Legal Reasons', // RFC7725
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates', // RFC2295
507 => 'Insufficient Storage', // RFC4918
508 => 'Loop Detected', // RFC5842
510 => 'Not Extended', // RFC2774
511 => 'Network Authentication Required', // RFC6585
2019-01-16 09:39:14 +00:00
];
/**
Merge branch '2.3' into 2.5 * 2.3: Configure firewall's kernel exception listener with configured entry point or a default entry point PSR-2 fixes [DependencyInjection] make paths relative to __DIR__ in the generated container Fixed the syntax of a composer.json file Fixed the symfony/config version constraint Tweaked the password-compat version constraint Docblock fixes define constant only if it wasn't defined before Fix incorrect spanish translation Fixed typos Conflicts: composer.json src/Symfony/Bridge/Twig/TwigEngine.php src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php src/Symfony/Bundle/FrameworkBundle/Templating/Loader/FilesystemLoader.php src/Symfony/Bundle/FrameworkBundle/composer.json src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php src/Symfony/Component/Console/Helper/TableHelper.php src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php src/Symfony/Component/Debug/ErrorHandler.php src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/Finder/Tests/Iterator/RecursiveDirectoryIteratorTest.php src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php src/Symfony/Component/HttpFoundation/Response.php src/Symfony/Component/HttpFoundation/StreamedResponse.php src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/Fragment/RoutableFragmentRenderer.php src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php src/Symfony/Component/Process/Process.php src/Symfony/Component/Process/Tests/AbstractProcessTest.php src/Symfony/Component/PropertyAccess/PropertyAccessorBuilder.php src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php src/Symfony/Component/Security/composer.json src/Symfony/Component/Serializer/Encoder/XmlEncoder.php src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php src/Symfony/Component/Stopwatch/StopwatchEvent.php src/Symfony/Component/Stopwatch/StopwatchPeriod.php src/Symfony/Component/Templating/PhpEngine.php src/Symfony/Component/Templating/TemplateReference.php src/Symfony/Component/Templating/TemplateReferenceInterface.php src/Symfony/Component/Translation/TranslatorInterface.php src/Symfony/Component/Validator/ConstraintViolation.php src/Symfony/Component/Validator/ExecutionContextInterface.php src/Symfony/Component/Validator/Mapping/ClassMetadata.php src/Symfony/Component/Validator/MetadataFactoryInterface.php
2014-12-02 20:15:53 +00:00
* @param mixed $content The response content, see setContent()
* @param int $status The response status code
* @param array $headers An array of response headers
2011-07-20 09:06:02 +01:00
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*/
2019-01-16 09:39:14 +00:00
public function __construct($content = '', $status = 200, $headers = [])
{
$this->headers = new ResponseHeaderBag($headers);
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');
}
/**
2014-12-21 17:00:50 +00:00
* Factory method for chainability.
*
* Example:
*
* return Response::create($body, 200)
* ->setSharedMaxAge(300);
*
Merge branch '2.3' into 2.5 * 2.3: Configure firewall's kernel exception listener with configured entry point or a default entry point PSR-2 fixes [DependencyInjection] make paths relative to __DIR__ in the generated container Fixed the syntax of a composer.json file Fixed the symfony/config version constraint Tweaked the password-compat version constraint Docblock fixes define constant only if it wasn't defined before Fix incorrect spanish translation Fixed typos Conflicts: composer.json src/Symfony/Bridge/Twig/TwigEngine.php src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php src/Symfony/Bundle/FrameworkBundle/Templating/Loader/FilesystemLoader.php src/Symfony/Bundle/FrameworkBundle/composer.json src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php src/Symfony/Component/Console/Helper/TableHelper.php src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php src/Symfony/Component/Debug/ErrorHandler.php src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/Finder/Tests/Iterator/RecursiveDirectoryIteratorTest.php src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php src/Symfony/Component/HttpFoundation/Response.php src/Symfony/Component/HttpFoundation/StreamedResponse.php src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/Fragment/RoutableFragmentRenderer.php src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php src/Symfony/Component/Process/Process.php src/Symfony/Component/Process/Tests/AbstractProcessTest.php src/Symfony/Component/PropertyAccess/PropertyAccessorBuilder.php src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php src/Symfony/Component/Security/composer.json src/Symfony/Component/Serializer/Encoder/XmlEncoder.php src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php src/Symfony/Component/Stopwatch/StopwatchEvent.php src/Symfony/Component/Stopwatch/StopwatchPeriod.php src/Symfony/Component/Templating/PhpEngine.php src/Symfony/Component/Templating/TemplateReference.php src/Symfony/Component/Templating/TemplateReferenceInterface.php src/Symfony/Component/Translation/TranslatorInterface.php src/Symfony/Component/Validator/ConstraintViolation.php src/Symfony/Component/Validator/ExecutionContextInterface.php src/Symfony/Component/Validator/Mapping/ClassMetadata.php src/Symfony/Component/Validator/MetadataFactoryInterface.php
2014-12-02 20:15:53 +00:00
* @param mixed $content The response content, see setContent()
* @param int $status The response status code
* @param array $headers An array of response headers
2012-03-15 16:41:21 +00:00
*
* @return static
*/
2019-01-16 09:39:14 +00:00
public static function create($content = '', $status = 200, $headers = [])
{
return new static($content, $status, $headers);
}
/**
* Returns the Response as an HTTP string.
*
2012-04-18 21:38:08 +01:00
* The string representation of the Response is the same as the
* one that will be sent to the client only if the prepare() method
* has been called before.
*
* @return string The Response as an HTTP string
*
* @see prepare()
*/
public function __toString()
{
return
sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
$this->headers."\r\n".
$this->getContent();
}
/**
* Clones the current Response instance.
*/
public function __clone()
{
$this->headers = clone $this->headers;
}
/**
* Prepares the Response before it is sent to the client.
*
* This method tweaks the Response to ensure that it is
* compliant with RFC 2616. Most of the changes are based on
* the Request that is "associated" with this Response.
*
* @return $this
*/
public function prepare(Request $request)
{
$headers = $this->headers;
if ($this->isInformational() || $this->isEmpty()) {
$this->setContent(null);
$headers->remove('Content-Type');
$headers->remove('Content-Length');
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getRequestFormat();
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
}
// Fix Content-Type
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
2014-09-21 19:53:12 +01:00
$headers->set('Content-Type', 'text/html; charset='.$charset);
2014-07-09 10:04:31 +01:00
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
// add the charset
2014-09-21 19:53:12 +01:00
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
}
// Fix Content-Length
if ($headers->has('Transfer-Encoding')) {
$headers->remove('Content-Length');
}
if ($request->isMethod('HEAD')) {
// cf. RFC2616 14.13
$length = $headers->get('Content-Length');
$this->setContent(null);
if ($length) {
$headers->set('Content-Length', $length);
}
}
}
// Fix protocol
if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
$this->setProtocolVersion('1.1');
}
// Check if we need to send extra expire info headers
if ('1.0' == $this->getProtocolVersion() && false !== strpos($headers->get('Cache-Control'), 'no-cache')) {
$headers->set('pragma', 'no-cache');
$headers->set('expires', -1);
}
$this->ensureIEOverSSLCompatibility($request);
return $this;
}
/**
2010-06-23 15:24:24 +01:00
* Sends HTTP headers.
*
* @return $this
*/
public function sendHeaders()
{
// headers have already been sent by the developer
if (headers_sent()) {
return $this;
}
// headers
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
$replace = 0 === strcasecmp($name, 'Content-Type');
2010-06-23 15:24:24 +01:00
foreach ($values as $value) {
header($name.': '.$value, $replace, $this->statusCode);
2010-06-23 15:24:24 +01:00
}
}
// cookies
foreach ($this->headers->getCookies() as $cookie) {
header('Set-Cookie: '.$cookie->getName().strstr($cookie, '='), false, $this->statusCode);
}
// status
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);
return $this;
}
/**
* Sends content for the current web response.
*
* @return $this
*/
public function sendContent()
{
echo $this->content;
return $this;
}
/**
* Sends HTTP headers and content.
2011-07-20 09:06:02 +01:00
*
* @return $this
*/
public function send()
{
$this->sendHeaders();
$this->sendContent();
2011-06-13 17:54:20 +01:00
if (\function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
2019-01-16 09:39:14 +00:00
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
static::closeOutputBuffers(0, true);
}
return $this;
}
/**
* Sets the response content.
*
* Valid types are strings, numbers, null, and objects that implement a __toString() method.
*
* @param mixed $content Content that can be cast to string
2012-03-15 16:41:21 +00:00
*
* @return $this
2011-07-20 09:06:02 +01:00
*
* @throws \UnexpectedValueException
*/
public function setContent($content)
{
2019-01-16 09:39:14 +00:00
if (null !== $content && !\is_string($content) && !is_numeric($content) && !\is_callable([$content, '__toString'])) {
throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', \gettype($content)));
}
$this->content = (string) $content;
return $this;
}
/**
* Gets the current response content.
*
* @return string Content
*/
public function getContent()
{
return $this->content;
}
/**
* Sets the HTTP protocol version (1.0 or 1.1).
*
* @param string $version The HTTP protocol version
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setProtocolVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Gets the HTTP protocol version.
*
* @return string The HTTP protocol version
*
* @final since version 3.2
*/
public function getProtocolVersion()
{
return $this->version;
}
/**
* Sets the response status code.
*
* If the status text is null it will be automatically populated for the known
* status codes and left empty otherwise.
2012-03-15 16:41:21 +00:00
*
* @param int $code HTTP status code
* @param mixed $text HTTP status text
*
* @return $this
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*
* @final since version 3.2
*/
public function setStatusCode($code, $text = null)
{
$this->statusCode = $code = (int) $code;
if ($this->isInvalid()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
}
if (null === $text) {
$this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : 'unknown status';
2012-07-20 06:34:13 +01:00
return $this;
}
if (false === $text) {
$this->statusText = '';
2012-07-20 06:34:13 +01:00
return $this;
}
$this->statusText = $text;
return $this;
}
/**
* Retrieves the status code for the current web response.
*
2014-11-30 13:33:44 +00:00
* @return int Status code
*
* @final since version 3.2
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* Sets the response charset.
*
* @param string $charset Character set
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setCharset($charset)
{
$this->charset = $charset;
return $this;
}
/**
* Retrieves the response charset.
*
* @return string Character set
*
* @final since version 3.2
*/
public function getCharset()
{
return $this->charset;
}
/**
* Returns true if the response may safely be kept in a shared (surrogate) cache.
*
* 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 because there is
* no way to tell when or how to remove them from the cache.
*
* Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation,
* for example "status codes that are defined as cacheable by default [...]
* can be reused by a cache with heuristic expiration unless otherwise indicated"
* (https://tools.ietf.org/html/rfc7231#section-6.1)
*
2014-11-30 13:33:44 +00:00
* @return bool true if the response is worth caching, false otherwise
*
* @final since version 3.3
*/
public function isCacheable()
{
2019-01-16 09:39:14 +00:00
if (!\in_array($this->statusCode, [200, 203, 300, 301, 302, 404, 410])) {
return false;
}
if ($this->headers->hasCacheControlDirective('no-store') || $this->headers->getCacheControlDirective('private')) {
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 Expires header and the calculated age is less than the freshness lifetime.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the response is fresh, false otherwise
*
* @final since version 3.3
*/
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.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the response is validateable, false otherwise
*
* @final since version 3.3
*/
public function isValidateable()
{
return $this->headers->has('Last-Modified') || $this->headers->has('ETag');
}
/**
* Marks the response as "private".
*
* It makes the response ineligible for serving other clients.
2011-07-20 09:06:02 +01:00
*
* @return $this
*
* @final since version 3.2
*/
public function setPrivate()
{
$this->headers->removeCacheControlDirective('public');
$this->headers->addCacheControlDirective('private');
return $this;
}
/**
* Marks the response as "public".
*
* It makes the response eligible for serving other clients.
2011-07-20 09:06:02 +01:00
*
* @return $this
*
* @final since version 3.2
*/
public function setPublic()
{
$this->headers->addCacheControlDirective('public');
$this->headers->removeCacheControlDirective('private');
return $this;
}
/**
* Marks the response as "immutable".
*
2017-09-11 22:34:45 +01:00
* @param bool $immutable enables or disables the immutable directive
*
* @return $this
*
* @final
*/
public function setImmutable($immutable = true)
{
if ($immutable) {
$this->headers->addCacheControlDirective('immutable');
} else {
$this->headers->removeCacheControlDirective('immutable');
}
return $this;
}
/**
* Returns true if the response is marked as "immutable".
*
2017-09-11 22:34:45 +01:00
* @return bool returns true if the response is marked as "immutable"; otherwise false
*
* @final
*/
public function isImmutable()
{
return $this->headers->hasCacheControlDirective('immutable');
}
/**
* 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
* greater than the value provided by the origin.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the response must be revalidated by a cache, false otherwise
*
* @final since version 3.3
*/
public function mustRevalidate()
{
return $this->headers->hasCacheControlDirective('must-revalidate') || $this->headers->hasCacheControlDirective('proxy-revalidate');
}
/**
* Returns the Date header as a DateTime instance.
*
* @return \DateTime A \DateTime instance
*
* @throws \RuntimeException When the header is not parseable
*
* @final since version 3.2
*/
public function getDate()
{
return $this->headers->getDate('Date');
}
/**
* Sets the Date header.
*
* @return $this
*
* @final since version 3.2
*/
public function setDate(\DateTime $date)
{
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
return $this;
}
/**
* Returns the age of the response.
*
2014-11-30 13:33:44 +00:00
* @return int The age of the response in seconds
*
* @final since version 3.2
*/
public function getAge()
{
if (null !== $age = $this->headers->get('Age')) {
return (int) $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
*
* @return $this
*/
public function expire()
{
if ($this->isFresh()) {
$this->headers->set('Age', $this->getMaxAge());
$this->headers->remove('Expires');
}
return $this;
}
/**
* Returns the value of the Expires header as a DateTime instance.
*
* @return \DateTime|null A DateTime instance or null if the header does not exist
*
* @final since version 3.2
*/
public function getExpires()
{
try {
return $this->headers->getDate('Expires');
} catch (\RuntimeException $e) {
// according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past
return \DateTime::createFromFormat(DATE_RFC2822, 'Sat, 01 Jan 00 00:00:00 +0000');
}
}
/**
* Sets the Expires HTTP header with a DateTime instance.
*
* Passing null as value will remove the header.
*
* @param \DateTime|null $date A \DateTime instance or null to remove the header
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setExpires(\DateTime $date = null)
{
if (null === $date) {
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
$this->headers->remove('Expires');
} else {
$date = clone $date;
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
}
return $this;
}
/**
* Returns the number of seconds after the time specified in the response's Date
2013-04-21 14:23:54 +01:00
* header when 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.
*
2014-11-30 13:33:44 +00:00
* @return int|null Number of seconds
*
* @final since version 3.2
*/
public function getMaxAge()
{
if ($this->headers->hasCacheControlDirective('s-maxage')) {
return (int) $this->headers->getCacheControlDirective('s-maxage');
}
if ($this->headers->hasCacheControlDirective('max-age')) {
return (int) $this->headers->getCacheControlDirective('max-age');
}
if (null !== $this->getExpires()) {
return $this->getExpires()->format('U') - $this->getDate()->format('U');
}
}
/**
* Sets the number of seconds after which the response should no longer be considered fresh.
*
* This methods sets the Cache-Control max-age directive.
*
2014-11-30 13:33:44 +00:00
* @param int $value Number of seconds
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setMaxAge($value)
{
$this->headers->addCacheControlDirective('max-age', $value);
return $this;
}
/**
* 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.
*
2014-11-30 13:33:44 +00:00
* @param int $value Number of seconds
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setSharedMaxAge($value)
{
$this->setPublic();
$this->headers->addCacheControlDirective('s-maxage', $value);
return $this;
}
/**
* 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.
*
2014-11-30 13:33:44 +00:00
* @return int|null The TTL in seconds
*
* @final since version 3.2
*/
public function getTtl()
{
if (null !== $maxAge = $this->getMaxAge()) {
return $maxAge - $this->getAge();
}
}
/**
* Sets the response's time-to-live for shared caches.
*
* This method adjusts the Cache-Control/s-maxage directive.
*
2014-11-30 13:33:44 +00:00
* @param int $seconds Number of seconds
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setTtl($seconds)
{
$this->setSharedMaxAge($this->getAge() + $seconds);
return $this;
}
/**
* Sets the response's time-to-live for private/client caches.
*
* This method adjusts the Cache-Control/max-age directive.
*
2014-11-30 13:33:44 +00:00
* @param int $seconds Number of seconds
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setClientTtl($seconds)
{
$this->setMaxAge($this->getAge() + $seconds);
return $this;
}
/**
* Returns the Last-Modified HTTP header as a DateTime instance.
*
* @return \DateTime|null A DateTime instance or null if the header does not exist
*
* @throws \RuntimeException When the HTTP header is not parseable
*
* @final since version 3.2
*/
public function getLastModified()
{
return $this->headers->getDate('Last-Modified');
}
/**
* Sets the Last-Modified HTTP header with a DateTime instance.
*
* Passing null as value will remove the header.
*
* @param \DateTime|null $date A \DateTime instance or null to remove the header
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setLastModified(\DateTime $date = null)
{
if (null === $date) {
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
$this->headers->remove('Last-Modified');
} else {
$date = clone $date;
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
}
return $this;
}
/**
* Returns the literal value of the ETag HTTP header.
*
* @return string|null The ETag HTTP header or null if it does not exist
*
* @final since version 3.2
*/
public function getEtag()
{
return $this->headers->get('ETag');
}
/**
* Sets the ETag value.
*
* @param string|null $etag The ETag unique identifier or null to remove the header
* @param bool $weak Whether you want a weak ETag or not
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setEtag($etag = null, $weak = false)
{
if (null === $etag) {
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
$this->headers->remove('Etag');
} else {
if (0 !== strpos($etag, '"')) {
$etag = '"'.$etag.'"';
}
$this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag);
}
return $this;
}
/**
* Sets the response's cache headers (validation and/or expiration).
*
* Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable.
*
* @param array $options An array of cache options
2012-03-15 16:41:21 +00:00
*
* @return $this
2011-07-20 09:06:02 +01:00
*
* @throws \InvalidArgumentException
*
* @final since version 3.3
*/
public function setCache(array $options)
{
2019-01-16 09:39:14 +00:00
if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) {
2018-10-26 14:40:38 +01:00
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
}
if (isset($options['etag'])) {
$this->setEtag($options['etag']);
}
if (isset($options['last_modified'])) {
$this->setLastModified($options['last_modified']);
}
if (isset($options['max_age'])) {
$this->setMaxAge($options['max_age']);
}
if (isset($options['s_maxage'])) {
$this->setSharedMaxAge($options['s_maxage']);
}
if (isset($options['public'])) {
if ($options['public']) {
$this->setPublic();
} else {
$this->setPrivate();
}
}
if (isset($options['private'])) {
if ($options['private']) {
$this->setPrivate();
} else {
$this->setPublic();
}
}
if (isset($options['immutable'])) {
$this->setImmutable((bool) $options['immutable']);
}
return $this;
}
/**
* 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.
*
* @return $this
*
* @see http://tools.ietf.org/html/rfc2616#section-10.3.5
*
* @final since version 3.3
*/
public function setNotModified()
{
$this->setStatusCode(304);
$this->setContent(null);
// remove headers that MUST NOT be included with 304 Not Modified responses
2019-01-16 09:39:14 +00:00
foreach (['Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified'] as $header) {
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
$this->headers->remove($header);
}
return $this;
}
/**
* Returns true if the response includes a Vary header.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the response includes a Vary header, false otherwise
*
* @final since version 3.2
*/
public function hasVary()
{
return null !== $this->headers->get('Vary');
}
/**
* Returns an array of header names given in the Vary header.
*
* @return array An array of Vary names
*
* @final since version 3.2
*/
public function getVary()
{
2014-03-04 20:15:32 +00:00
if (!$vary = $this->headers->get('Vary', null, false)) {
2019-01-16 09:39:14 +00:00
return [];
}
2019-01-16 09:39:14 +00:00
$ret = [];
2014-03-04 20:15:32 +00:00
foreach ($vary as $item) {
2014-03-05 07:16:11 +00:00
$ret = array_merge($ret, preg_split('/[\s,]+/', $item));
2014-03-04 20:15:32 +00:00
}
return $ret;
}
/**
* Sets the Vary header.
*
* @param string|array $headers
2015-12-03 05:49:53 +00:00
* @param bool $replace Whether to replace the actual value or not (true by default)
2012-03-15 16:41:21 +00:00
*
* @return $this
*
* @final since version 3.2
*/
public function setVary($headers, $replace = true)
{
$this->headers->set('Vary', $headers, $replace);
return $this;
}
/**
* Determines if the Response validators (ETag, Last-Modified) match
* a conditional value specified in the Request.
*
* If the Response is not modified, it sets the status code to 304 and
* removes the actual content by calling the setNotModified() method.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the Response validators match the Request, false otherwise
*
* @final since version 3.3
*/
public function isNotModified(Request $request)
{
if (!$request->isMethodCacheable()) {
return false;
}
2014-10-22 19:27:13 +01:00
$notModified = false;
$lastModified = $this->headers->get('Last-Modified');
$modifiedSince = $request->headers->get('If-Modified-Since');
if ($etags = $request->getETags()) {
$notModified = \in_array($this->getEtag(), $etags) || \in_array('*', $etags);
}
if ($modifiedSince && $lastModified) {
$notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified);
}
if ($notModified) {
$this->setNotModified();
}
return $notModified;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-03 05:37:03 +00:00
* Is response invalid?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
2016-01-25 07:27:56 +00:00
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isInvalid()
{
return $this->statusCode < 100 || $this->statusCode >= 600;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is response informative?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.3
2011-07-20 09:06:02 +01:00
*/
public function isInformational()
{
return $this->statusCode >= 100 && $this->statusCode < 200;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is response successful?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isSuccessful()
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is the response a redirect?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isRedirection()
{
return $this->statusCode >= 300 && $this->statusCode < 400;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is there a client error?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isClientError()
{
return $this->statusCode >= 400 && $this->statusCode < 500;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Was there a server side error?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.3
2011-07-20 09:06:02 +01:00
*/
public function isServerError()
{
return $this->statusCode >= 500 && $this->statusCode < 600;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is the response OK?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isOk()
{
return 200 === $this->statusCode;
}
2011-07-20 09:06:02 +01:00
/**
2012-10-28 23:25:34 +00:00
* Is the response forbidden?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isForbidden()
{
return 403 === $this->statusCode;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is the response a not found error?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isNotFound()
{
return 404 === $this->statusCode;
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is the response a redirect of some form?
2011-12-18 13:33:54 +00:00
*
* @param string $location
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isRedirect($location = null)
{
2019-01-16 09:39:14 +00:00
return \in_array($this->statusCode, [201, 301, 302, 303, 307, 308]) && (null === $location ?: $location == $this->headers->get('Location'));
}
2011-07-20 09:06:02 +01:00
/**
2011-11-02 15:42:51 +00:00
* Is the response empty?
2011-12-18 13:33:54 +00:00
*
2014-04-16 11:30:19 +01:00
* @return bool
*
* @final since version 3.2
2011-07-20 09:06:02 +01:00
*/
public function isEmpty()
{
2019-01-16 09:39:14 +00:00
return \in_array($this->statusCode, [204, 304]);
}
/**
* Cleans or flushes output buffers up to target level.
*
* Resulting level can be greater than target level if a non-removable buffer has been encountered.
*
* @param int $targetLevel The target output buffering level
* @param bool $flush Whether to flush or clean the buffers
*
* @final since version 3.3
*/
public static function closeOutputBuffers($targetLevel, $flush)
{
$status = ob_get_status(true);
$level = \count($status);
2015-08-01 08:16:55 +01:00
// PHP_OUTPUT_HANDLER_* are not defined on HHVM 3.3
$flags = \defined('PHP_OUTPUT_HANDLER_REMOVABLE') ? PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE) : -1;
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
if ($flush) {
ob_end_flush();
} else {
ob_end_clean();
}
}
}
/**
Merge branch '2.3' into 2.5 * 2.3: [2.3] CS And DocBlock Fixes [2.3] CS Fixes Conflicts: src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php src/Symfony/Component/Config/Definition/ReferenceDumper.php src/Symfony/Component/Console/Application.php src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/Filesystem/Tests/FilesystemTest.php src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php src/Symfony/Component/Form/FormError.php src/Symfony/Component/HttpFoundation/Request.php src/Symfony/Component/HttpFoundation/Response.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Process/ProcessUtils.php src/Symfony/Component/PropertyAccess/PropertyAccessor.php src/Symfony/Component/PropertyAccess/PropertyAccessorInterface.php src/Symfony/Component/Serializer/Encoder/XmlEncoder.php src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php src/Symfony/Component/Validator/Constraints/GroupSequence.php src/Symfony/Component/Validator/Mapping/ClassMetadata.php src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php src/Symfony/Component/Validator/Mapping/MemberMetadata.php src/Symfony/Component/Validator/Tests/Fixtures/StubGlobalExecutionContext.php
2014-12-22 16:29:52 +00:00
* Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.
*
2016-11-13 17:41:36 +00:00
* @see http://support.microsoft.com/kb/323308
*
* @final since version 3.3
*/
protected function ensureIEOverSSLCompatibility(Request $request)
{
if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && 1 == preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) && true === $request->isSecure()) {
if ((int) preg_replace('/(MSIE )(.*?);/', '$2', $match[0]) < 9) {
$this->headers->remove('Cache-Control');
}
}
}
2011-12-18 13:33:54 +00:00
}