Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation; Chris@0: Chris@0: /** Chris@0: * Response represents an HTTP response. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Response Chris@0: { Chris@0: const HTTP_CONTINUE = 100; Chris@0: const HTTP_SWITCHING_PROTOCOLS = 101; Chris@0: const HTTP_PROCESSING = 102; // RFC2518 Chris@16: const HTTP_EARLY_HINTS = 103; // RFC8297 Chris@0: const HTTP_OK = 200; Chris@0: const HTTP_CREATED = 201; Chris@0: const HTTP_ACCEPTED = 202; Chris@0: const HTTP_NON_AUTHORITATIVE_INFORMATION = 203; Chris@0: const HTTP_NO_CONTENT = 204; Chris@0: const HTTP_RESET_CONTENT = 205; Chris@0: const HTTP_PARTIAL_CONTENT = 206; Chris@0: const HTTP_MULTI_STATUS = 207; // RFC4918 Chris@0: const HTTP_ALREADY_REPORTED = 208; // RFC5842 Chris@0: const HTTP_IM_USED = 226; // RFC3229 Chris@0: const HTTP_MULTIPLE_CHOICES = 300; Chris@0: const HTTP_MOVED_PERMANENTLY = 301; Chris@0: const HTTP_FOUND = 302; Chris@0: const HTTP_SEE_OTHER = 303; Chris@0: const HTTP_NOT_MODIFIED = 304; Chris@0: const HTTP_USE_PROXY = 305; Chris@0: const HTTP_RESERVED = 306; Chris@0: const HTTP_TEMPORARY_REDIRECT = 307; Chris@0: const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238 Chris@0: const HTTP_BAD_REQUEST = 400; Chris@0: const HTTP_UNAUTHORIZED = 401; Chris@0: const HTTP_PAYMENT_REQUIRED = 402; Chris@0: const HTTP_FORBIDDEN = 403; Chris@0: const HTTP_NOT_FOUND = 404; Chris@0: const HTTP_METHOD_NOT_ALLOWED = 405; Chris@0: const HTTP_NOT_ACCEPTABLE = 406; Chris@0: const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407; Chris@0: const HTTP_REQUEST_TIMEOUT = 408; Chris@0: const HTTP_CONFLICT = 409; Chris@0: const HTTP_GONE = 410; Chris@0: const HTTP_LENGTH_REQUIRED = 411; Chris@0: const HTTP_PRECONDITION_FAILED = 412; Chris@0: const HTTP_REQUEST_ENTITY_TOO_LARGE = 413; Chris@0: const HTTP_REQUEST_URI_TOO_LONG = 414; Chris@0: const HTTP_UNSUPPORTED_MEDIA_TYPE = 415; Chris@0: const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; Chris@0: const HTTP_EXPECTATION_FAILED = 417; Chris@0: const HTTP_I_AM_A_TEAPOT = 418; // RFC2324 Chris@0: const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540 Chris@0: const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918 Chris@0: const HTTP_LOCKED = 423; // RFC4918 Chris@0: const HTTP_FAILED_DEPENDENCY = 424; // RFC4918 Chris@17: Chris@17: /** Chris@17: * @deprecated Chris@17: */ Chris@0: const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817 Chris@17: const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04 Chris@0: const HTTP_UPGRADE_REQUIRED = 426; // RFC2817 Chris@0: const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585 Chris@0: const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585 Chris@0: const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585 Chris@0: const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451; Chris@0: const HTTP_INTERNAL_SERVER_ERROR = 500; Chris@0: const HTTP_NOT_IMPLEMENTED = 501; Chris@0: const HTTP_BAD_GATEWAY = 502; Chris@0: const HTTP_SERVICE_UNAVAILABLE = 503; Chris@0: const HTTP_GATEWAY_TIMEOUT = 504; Chris@0: const HTTP_VERSION_NOT_SUPPORTED = 505; Chris@0: const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295 Chris@0: const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918 Chris@0: const HTTP_LOOP_DETECTED = 508; // RFC5842 Chris@0: const HTTP_NOT_EXTENDED = 510; // RFC2774 Chris@0: const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585 Chris@0: Chris@0: /** Chris@0: * @var \Symfony\Component\HttpFoundation\ResponseHeaderBag Chris@0: */ Chris@0: public $headers; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $content; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $version; Chris@0: Chris@0: /** Chris@0: * @var int Chris@0: */ Chris@0: protected $statusCode; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $statusText; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $charset; Chris@0: Chris@0: /** Chris@0: * Status codes translation table. Chris@0: * Chris@0: * The list of codes is complete according to the Chris@0: * {@link http://www.iana.org/assignments/http-status-codes/ Hypertext Transfer Protocol (HTTP) Status Code Registry} Chris@0: * (last updated 2016-03-01). Chris@0: * Chris@0: * Unless otherwise noted, the status code is defined in RFC2616. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@17: public static $statusTexts = [ Chris@0: 100 => 'Continue', Chris@0: 101 => 'Switching Protocols', Chris@0: 102 => 'Processing', // RFC2518 Chris@14: 103 => 'Early Hints', Chris@0: 200 => 'OK', Chris@0: 201 => 'Created', Chris@0: 202 => 'Accepted', Chris@0: 203 => 'Non-Authoritative Information', Chris@0: 204 => 'No Content', Chris@0: 205 => 'Reset Content', Chris@0: 206 => 'Partial Content', Chris@0: 207 => 'Multi-Status', // RFC4918 Chris@0: 208 => 'Already Reported', // RFC5842 Chris@0: 226 => 'IM Used', // RFC3229 Chris@0: 300 => 'Multiple Choices', Chris@0: 301 => 'Moved Permanently', Chris@0: 302 => 'Found', Chris@0: 303 => 'See Other', Chris@0: 304 => 'Not Modified', Chris@0: 305 => 'Use Proxy', Chris@0: 307 => 'Temporary Redirect', Chris@0: 308 => 'Permanent Redirect', // RFC7238 Chris@0: 400 => 'Bad Request', Chris@0: 401 => 'Unauthorized', Chris@0: 402 => 'Payment Required', Chris@0: 403 => 'Forbidden', Chris@0: 404 => 'Not Found', Chris@0: 405 => 'Method Not Allowed', Chris@0: 406 => 'Not Acceptable', Chris@0: 407 => 'Proxy Authentication Required', Chris@0: 408 => 'Request Timeout', Chris@0: 409 => 'Conflict', Chris@0: 410 => 'Gone', Chris@0: 411 => 'Length Required', Chris@0: 412 => 'Precondition Failed', Chris@0: 413 => 'Payload Too Large', Chris@0: 414 => 'URI Too Long', Chris@0: 415 => 'Unsupported Media Type', Chris@0: 416 => 'Range Not Satisfiable', Chris@0: 417 => 'Expectation Failed', Chris@0: 418 => 'I\'m a teapot', // RFC2324 Chris@0: 421 => 'Misdirected Request', // RFC7540 Chris@0: 422 => 'Unprocessable Entity', // RFC4918 Chris@0: 423 => 'Locked', // RFC4918 Chris@0: 424 => 'Failed Dependency', // RFC4918 Chris@17: 425 => 'Too Early', // RFC-ietf-httpbis-replay-04 Chris@0: 426 => 'Upgrade Required', // RFC2817 Chris@0: 428 => 'Precondition Required', // RFC6585 Chris@0: 429 => 'Too Many Requests', // RFC6585 Chris@0: 431 => 'Request Header Fields Too Large', // RFC6585 Chris@0: 451 => 'Unavailable For Legal Reasons', // RFC7725 Chris@0: 500 => 'Internal Server Error', Chris@0: 501 => 'Not Implemented', Chris@0: 502 => 'Bad Gateway', Chris@0: 503 => 'Service Unavailable', Chris@0: 504 => 'Gateway Timeout', Chris@0: 505 => 'HTTP Version Not Supported', Chris@0: 506 => 'Variant Also Negotiates', // RFC2295 Chris@0: 507 => 'Insufficient Storage', // RFC4918 Chris@0: 508 => 'Loop Detected', // RFC5842 Chris@0: 510 => 'Not Extended', // RFC2774 Chris@0: 511 => 'Network Authentication Required', // RFC6585 Chris@17: ]; Chris@0: Chris@0: /** Chris@0: * @param mixed $content The response content, see setContent() Chris@0: * @param int $status The response status code Chris@0: * @param array $headers An array of response headers Chris@0: * Chris@0: * @throws \InvalidArgumentException When the HTTP status code is not valid Chris@0: */ Chris@17: public function __construct($content = '', $status = 200, $headers = []) Chris@0: { Chris@0: $this->headers = new ResponseHeaderBag($headers); Chris@0: $this->setContent($content); Chris@0: $this->setStatusCode($status); Chris@0: $this->setProtocolVersion('1.0'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Factory method for chainability. Chris@0: * Chris@0: * Example: Chris@0: * Chris@0: * return Response::create($body, 200) Chris@0: * ->setSharedMaxAge(300); Chris@0: * Chris@0: * @param mixed $content The response content, see setContent() Chris@0: * @param int $status The response status code Chris@0: * @param array $headers An array of response headers Chris@0: * Chris@0: * @return static Chris@0: */ Chris@17: public static function create($content = '', $status = 200, $headers = []) Chris@0: { Chris@0: return new static($content, $status, $headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the Response as an HTTP string. Chris@0: * Chris@0: * The string representation of the Response is the same as the Chris@0: * one that will be sent to the client only if the prepare() method Chris@0: * has been called before. Chris@0: * Chris@0: * @return string The Response as an HTTP string Chris@0: * Chris@0: * @see prepare() Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@0: return Chris@0: sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". Chris@0: $this->headers."\r\n". Chris@0: $this->getContent(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clones the current Response instance. Chris@0: */ Chris@0: public function __clone() Chris@0: { Chris@0: $this->headers = clone $this->headers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares the Response before it is sent to the client. Chris@0: * Chris@0: * This method tweaks the Response to ensure that it is Chris@0: * compliant with RFC 2616. Most of the changes are based on Chris@0: * the Request that is "associated" with this Response. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function prepare(Request $request) Chris@0: { Chris@0: $headers = $this->headers; Chris@0: Chris@0: if ($this->isInformational() || $this->isEmpty()) { Chris@0: $this->setContent(null); Chris@0: $headers->remove('Content-Type'); Chris@0: $headers->remove('Content-Length'); Chris@0: } else { Chris@0: // Content-type based on the Request Chris@0: if (!$headers->has('Content-Type')) { Chris@0: $format = $request->getRequestFormat(); Chris@0: if (null !== $format && $mimeType = $request->getMimeType($format)) { Chris@0: $headers->set('Content-Type', $mimeType); Chris@0: } Chris@0: } Chris@0: Chris@0: // Fix Content-Type Chris@0: $charset = $this->charset ?: 'UTF-8'; Chris@0: if (!$headers->has('Content-Type')) { Chris@0: $headers->set('Content-Type', 'text/html; charset='.$charset); Chris@0: } elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) { Chris@0: // add the charset Chris@0: $headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset); Chris@0: } Chris@0: Chris@0: // Fix Content-Length Chris@0: if ($headers->has('Transfer-Encoding')) { Chris@0: $headers->remove('Content-Length'); Chris@0: } Chris@0: Chris@0: if ($request->isMethod('HEAD')) { Chris@0: // cf. RFC2616 14.13 Chris@0: $length = $headers->get('Content-Length'); Chris@0: $this->setContent(null); Chris@0: if ($length) { Chris@0: $headers->set('Content-Length', $length); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Fix protocol Chris@0: if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) { Chris@0: $this->setProtocolVersion('1.1'); Chris@0: } Chris@0: Chris@0: // Check if we need to send extra expire info headers Chris@18: if ('1.0' == $this->getProtocolVersion() && false !== strpos($headers->get('Cache-Control'), 'no-cache')) { Chris@18: $headers->set('pragma', 'no-cache'); Chris@18: $headers->set('expires', -1); Chris@0: } Chris@0: Chris@0: $this->ensureIEOverSSLCompatibility($request); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends HTTP headers. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function sendHeaders() Chris@0: { Chris@0: // headers have already been sent by the developer Chris@0: if (headers_sent()) { Chris@0: return $this; Chris@0: } Chris@0: Chris@0: // headers Chris@17: foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { Chris@17: $replace = 0 === strcasecmp($name, 'Content-Type'); Chris@0: foreach ($values as $value) { Chris@17: header($name.': '.$value, $replace, $this->statusCode); Chris@0: } Chris@0: } Chris@0: Chris@17: // cookies Chris@17: foreach ($this->headers->getCookies() as $cookie) { Chris@17: header('Set-Cookie: '.$cookie->getName().strstr($cookie, '='), false, $this->statusCode); Chris@17: } Chris@17: Chris@0: // status Chris@0: header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends content for the current web response. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function sendContent() Chris@0: { Chris@0: echo $this->content; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends HTTP headers and content. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function send() Chris@0: { Chris@0: $this->sendHeaders(); Chris@0: $this->sendContent(); Chris@0: Chris@17: if (\function_exists('fastcgi_finish_request')) { Chris@0: fastcgi_finish_request(); Chris@17: } elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) { Chris@0: static::closeOutputBuffers(0, true); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response content. Chris@0: * Chris@0: * Valid types are strings, numbers, null, and objects that implement a __toString() method. Chris@0: * Chris@0: * @param mixed $content Content that can be cast to string Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws \UnexpectedValueException Chris@0: */ Chris@0: public function setContent($content) Chris@0: { Chris@17: if (null !== $content && !\is_string($content) && !is_numeric($content) && !\is_callable([$content, '__toString'])) { Chris@17: throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', \gettype($content))); Chris@0: } Chris@0: Chris@0: $this->content = (string) $content; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current response content. Chris@0: * Chris@0: * @return string Content Chris@0: */ Chris@0: public function getContent() Chris@0: { Chris@0: return $this->content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP protocol version (1.0 or 1.1). Chris@0: * Chris@0: * @param string $version The HTTP protocol version Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setProtocolVersion($version) Chris@0: { Chris@0: $this->version = $version; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTP protocol version. Chris@0: * Chris@0: * @return string The HTTP protocol version Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getProtocolVersion() Chris@0: { Chris@0: return $this->version; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response status code. Chris@0: * Chris@14: * If the status text is null it will be automatically populated for the known Chris@14: * status codes and left empty otherwise. Chris@14: * Chris@0: * @param int $code HTTP status code Chris@0: * @param mixed $text HTTP status text Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws \InvalidArgumentException When the HTTP status code is not valid Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setStatusCode($code, $text = null) Chris@0: { Chris@0: $this->statusCode = $code = (int) $code; Chris@0: if ($this->isInvalid()) { Chris@0: throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code)); Chris@0: } Chris@0: Chris@0: if (null === $text) { Chris@0: $this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : 'unknown status'; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: if (false === $text) { Chris@0: $this->statusText = ''; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: $this->statusText = $text; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves the status code for the current web response. Chris@0: * Chris@0: * @return int Status code Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getStatusCode() Chris@0: { Chris@0: return $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response charset. Chris@0: * Chris@0: * @param string $charset Character set Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setCharset($charset) Chris@0: { Chris@0: $this->charset = $charset; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves the response charset. Chris@0: * Chris@0: * @return string Character set Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getCharset() Chris@0: { Chris@0: return $this->charset; Chris@0: } Chris@0: Chris@0: /** Chris@16: * Returns true if the response may safely be kept in a shared (surrogate) cache. Chris@0: * Chris@0: * Responses marked "private" with an explicit Cache-Control directive are Chris@0: * considered uncacheable. Chris@0: * Chris@0: * Responses with neither a freshness lifetime (Expires, max-age) nor cache Chris@16: * validator (Last-Modified, ETag) are considered uncacheable because there is Chris@16: * no way to tell when or how to remove them from the cache. Chris@16: * Chris@16: * Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation, Chris@16: * for example "status codes that are defined as cacheable by default [...] Chris@16: * can be reused by a cache with heuristic expiration unless otherwise indicated" Chris@16: * (https://tools.ietf.org/html/rfc7231#section-6.1) Chris@0: * Chris@0: * @return bool true if the response is worth caching, false otherwise Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isCacheable() Chris@0: { Chris@17: if (!\in_array($this->statusCode, [200, 203, 300, 301, 302, 404, 410])) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if ($this->headers->hasCacheControlDirective('no-store') || $this->headers->getCacheControlDirective('private')) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return $this->isValidateable() || $this->isFresh(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the response is "fresh". Chris@0: * Chris@0: * Fresh responses may be served from cache without any interaction with the Chris@0: * origin. A response is considered fresh when it includes a Cache-Control/max-age Chris@0: * indicator or Expires header and the calculated age is less than the freshness lifetime. Chris@0: * Chris@0: * @return bool true if the response is fresh, false otherwise Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isFresh() Chris@0: { Chris@0: return $this->getTtl() > 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the response includes headers that can be used to validate Chris@0: * the response with the origin server using a conditional GET request. Chris@0: * Chris@0: * @return bool true if the response is validateable, false otherwise Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isValidateable() Chris@0: { Chris@0: return $this->headers->has('Last-Modified') || $this->headers->has('ETag'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Marks the response as "private". Chris@0: * Chris@0: * It makes the response ineligible for serving other clients. Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setPrivate() Chris@0: { Chris@0: $this->headers->removeCacheControlDirective('public'); Chris@0: $this->headers->addCacheControlDirective('private'); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Marks the response as "public". Chris@0: * Chris@0: * It makes the response eligible for serving other clients. Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setPublic() Chris@0: { Chris@0: $this->headers->addCacheControlDirective('public'); Chris@0: $this->headers->removeCacheControlDirective('private'); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@14: * Marks the response as "immutable". Chris@14: * Chris@14: * @param bool $immutable enables or disables the immutable directive Chris@14: * Chris@14: * @return $this Chris@14: * Chris@14: * @final Chris@14: */ Chris@14: public function setImmutable($immutable = true) Chris@14: { Chris@14: if ($immutable) { Chris@14: $this->headers->addCacheControlDirective('immutable'); Chris@14: } else { Chris@14: $this->headers->removeCacheControlDirective('immutable'); Chris@14: } Chris@14: Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns true if the response is marked as "immutable". Chris@14: * Chris@14: * @return bool returns true if the response is marked as "immutable"; otherwise false Chris@14: * Chris@14: * @final Chris@14: */ Chris@14: public function isImmutable() Chris@14: { Chris@14: return $this->headers->hasCacheControlDirective('immutable'); Chris@14: } Chris@14: Chris@14: /** Chris@0: * Returns true if the response must be revalidated by caches. Chris@0: * Chris@0: * This method indicates that the response must not be served stale by a Chris@0: * cache in any circumstance without first revalidating with the origin. Chris@0: * When present, the TTL of the response should not be overridden to be Chris@0: * greater than the value provided by the origin. Chris@0: * Chris@0: * @return bool true if the response must be revalidated by a cache, false otherwise Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function mustRevalidate() Chris@0: { Chris@0: return $this->headers->hasCacheControlDirective('must-revalidate') || $this->headers->hasCacheControlDirective('proxy-revalidate'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the Date header as a DateTime instance. Chris@0: * Chris@0: * @return \DateTime A \DateTime instance Chris@0: * Chris@0: * @throws \RuntimeException When the header is not parseable Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getDate() Chris@0: { Chris@0: return $this->headers->getDate('Date'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the Date header. Chris@0: * Chris@14: * @return $this Chris@0: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setDate(\DateTime $date) Chris@0: { Chris@0: $date->setTimezone(new \DateTimeZone('UTC')); Chris@0: $this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT'); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the age of the response. Chris@0: * Chris@0: * @return int The age of the response in seconds Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getAge() Chris@0: { Chris@0: if (null !== $age = $this->headers->get('Age')) { Chris@0: return (int) $age; Chris@0: } Chris@0: Chris@0: return max(time() - $this->getDate()->format('U'), 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Marks the response stale by setting the Age header to be equal to the maximum age of the response. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function expire() Chris@0: { Chris@0: if ($this->isFresh()) { Chris@0: $this->headers->set('Age', $this->getMaxAge()); Chris@17: $this->headers->remove('Expires'); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the value of the Expires header as a DateTime instance. Chris@0: * Chris@0: * @return \DateTime|null A DateTime instance or null if the header does not exist Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getExpires() Chris@0: { Chris@0: try { Chris@0: return $this->headers->getDate('Expires'); Chris@0: } catch (\RuntimeException $e) { Chris@0: // according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past Chris@0: return \DateTime::createFromFormat(DATE_RFC2822, 'Sat, 01 Jan 00 00:00:00 +0000'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the Expires HTTP header with a DateTime instance. Chris@0: * Chris@0: * Passing null as value will remove the header. Chris@0: * Chris@0: * @param \DateTime|null $date A \DateTime instance or null to remove the header Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setExpires(\DateTime $date = null) Chris@0: { Chris@0: if (null === $date) { Chris@0: $this->headers->remove('Expires'); Chris@0: } else { Chris@0: $date = clone $date; Chris@0: $date->setTimezone(new \DateTimeZone('UTC')); Chris@0: $this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT'); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the number of seconds after the time specified in the response's Date Chris@0: * header when the response should no longer be considered fresh. Chris@0: * Chris@0: * First, it checks for a s-maxage directive, then a max-age directive, and then it falls Chris@0: * back on an expires header. It returns null when no maximum age can be established. Chris@0: * Chris@0: * @return int|null Number of seconds Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getMaxAge() Chris@0: { Chris@0: if ($this->headers->hasCacheControlDirective('s-maxage')) { Chris@0: return (int) $this->headers->getCacheControlDirective('s-maxage'); Chris@0: } Chris@0: Chris@0: if ($this->headers->hasCacheControlDirective('max-age')) { Chris@0: return (int) $this->headers->getCacheControlDirective('max-age'); Chris@0: } Chris@0: Chris@0: if (null !== $this->getExpires()) { Chris@0: return $this->getExpires()->format('U') - $this->getDate()->format('U'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the number of seconds after which the response should no longer be considered fresh. Chris@0: * Chris@0: * This methods sets the Cache-Control max-age directive. Chris@0: * Chris@0: * @param int $value Number of seconds Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setMaxAge($value) Chris@0: { Chris@0: $this->headers->addCacheControlDirective('max-age', $value); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the number of seconds after which the response should no longer be considered fresh by shared caches. Chris@0: * Chris@0: * This methods sets the Cache-Control s-maxage directive. Chris@0: * Chris@0: * @param int $value Number of seconds Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setSharedMaxAge($value) Chris@0: { Chris@0: $this->setPublic(); Chris@0: $this->headers->addCacheControlDirective('s-maxage', $value); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the response's time-to-live in seconds. Chris@0: * Chris@0: * It returns null when no freshness information is present in the response. Chris@0: * Chris@0: * When the responses TTL is <= 0, the response may not be served from cache without first Chris@0: * revalidating with the origin. Chris@0: * Chris@0: * @return int|null The TTL in seconds Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getTtl() Chris@0: { Chris@0: if (null !== $maxAge = $this->getMaxAge()) { Chris@0: return $maxAge - $this->getAge(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response's time-to-live for shared caches. Chris@0: * Chris@0: * This method adjusts the Cache-Control/s-maxage directive. Chris@0: * Chris@0: * @param int $seconds Number of seconds Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setTtl($seconds) Chris@0: { Chris@0: $this->setSharedMaxAge($this->getAge() + $seconds); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response's time-to-live for private/client caches. Chris@0: * Chris@0: * This method adjusts the Cache-Control/max-age directive. Chris@0: * Chris@0: * @param int $seconds Number of seconds Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setClientTtl($seconds) Chris@0: { Chris@0: $this->setMaxAge($this->getAge() + $seconds); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the Last-Modified HTTP header as a DateTime instance. Chris@0: * Chris@0: * @return \DateTime|null A DateTime instance or null if the header does not exist Chris@0: * Chris@0: * @throws \RuntimeException When the HTTP header is not parseable Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getLastModified() Chris@0: { Chris@0: return $this->headers->getDate('Last-Modified'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the Last-Modified HTTP header with a DateTime instance. Chris@0: * Chris@0: * Passing null as value will remove the header. Chris@0: * Chris@0: * @param \DateTime|null $date A \DateTime instance or null to remove the header Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setLastModified(\DateTime $date = null) Chris@0: { Chris@0: if (null === $date) { Chris@0: $this->headers->remove('Last-Modified'); Chris@0: } else { Chris@0: $date = clone $date; Chris@0: $date->setTimezone(new \DateTimeZone('UTC')); Chris@0: $this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT'); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the literal value of the ETag HTTP header. Chris@0: * Chris@0: * @return string|null The ETag HTTP header or null if it does not exist Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getEtag() Chris@0: { Chris@0: return $this->headers->get('ETag'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the ETag value. Chris@0: * Chris@0: * @param string|null $etag The ETag unique identifier or null to remove the header Chris@0: * @param bool $weak Whether you want a weak ETag or not Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setEtag($etag = null, $weak = false) Chris@0: { Chris@0: if (null === $etag) { Chris@0: $this->headers->remove('Etag'); Chris@0: } else { Chris@0: if (0 !== strpos($etag, '"')) { Chris@0: $etag = '"'.$etag.'"'; Chris@0: } Chris@0: Chris@0: $this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the response's cache headers (validation and/or expiration). Chris@0: * Chris@14: * Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable. Chris@0: * Chris@0: * @param array $options An array of cache options Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function setCache(array $options) Chris@0: { Chris@17: if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) { Chris@17: throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff))); Chris@0: } Chris@0: Chris@0: if (isset($options['etag'])) { Chris@0: $this->setEtag($options['etag']); Chris@0: } Chris@0: Chris@0: if (isset($options['last_modified'])) { Chris@0: $this->setLastModified($options['last_modified']); Chris@0: } Chris@0: Chris@0: if (isset($options['max_age'])) { Chris@0: $this->setMaxAge($options['max_age']); Chris@0: } Chris@0: Chris@0: if (isset($options['s_maxage'])) { Chris@0: $this->setSharedMaxAge($options['s_maxage']); Chris@0: } Chris@0: Chris@0: if (isset($options['public'])) { Chris@0: if ($options['public']) { Chris@0: $this->setPublic(); Chris@0: } else { Chris@0: $this->setPrivate(); Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($options['private'])) { Chris@0: if ($options['private']) { Chris@0: $this->setPrivate(); Chris@0: } else { Chris@0: $this->setPublic(); Chris@0: } Chris@0: } Chris@0: Chris@14: if (isset($options['immutable'])) { Chris@14: $this->setImmutable((bool) $options['immutable']); Chris@14: } Chris@14: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Modifies the response so that it conforms to the rules defined for a 304 status code. Chris@0: * Chris@0: * This sets the status, removes the body, and discards any headers Chris@0: * that MUST NOT be included in 304 responses. Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @see http://tools.ietf.org/html/rfc2616#section-10.3.5 Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function setNotModified() Chris@0: { Chris@0: $this->setStatusCode(304); Chris@0: $this->setContent(null); Chris@0: Chris@0: // remove headers that MUST NOT be included with 304 Not Modified responses Chris@17: foreach (['Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified'] as $header) { Chris@0: $this->headers->remove($header); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the response includes a Vary header. Chris@0: * Chris@0: * @return bool true if the response includes a Vary header, false otherwise Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function hasVary() Chris@0: { Chris@0: return null !== $this->headers->get('Vary'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of header names given in the Vary header. Chris@0: * Chris@0: * @return array An array of Vary names Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function getVary() Chris@0: { Chris@0: if (!$vary = $this->headers->get('Vary', null, false)) { Chris@17: return []; Chris@0: } Chris@0: Chris@17: $ret = []; Chris@0: foreach ($vary as $item) { Chris@0: $ret = array_merge($ret, preg_split('/[\s,]+/', $item)); Chris@0: } Chris@0: Chris@0: return $ret; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the Vary header. Chris@0: * Chris@0: * @param string|array $headers Chris@0: * @param bool $replace Whether to replace the actual value or not (true by default) Chris@0: * Chris@0: * @return $this Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function setVary($headers, $replace = true) Chris@0: { Chris@0: $this->headers->set('Vary', $headers, $replace); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if the Response validators (ETag, Last-Modified) match Chris@0: * a conditional value specified in the Request. Chris@0: * Chris@0: * If the Response is not modified, it sets the status code to 304 and Chris@0: * removes the actual content by calling the setNotModified() method. Chris@0: * Chris@14: * @return bool true if the Response validators match the Request, false otherwise Chris@0: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isNotModified(Request $request) Chris@0: { Chris@0: if (!$request->isMethodCacheable()) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $notModified = false; Chris@0: $lastModified = $this->headers->get('Last-Modified'); Chris@0: $modifiedSince = $request->headers->get('If-Modified-Since'); Chris@0: Chris@0: if ($etags = $request->getETags()) { Chris@17: $notModified = \in_array($this->getEtag(), $etags) || \in_array('*', $etags); Chris@0: } Chris@0: Chris@0: if ($modifiedSince && $lastModified) { Chris@0: $notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified); Chris@0: } Chris@0: Chris@0: if ($notModified) { Chris@0: $this->setNotModified(); Chris@0: } Chris@0: Chris@0: return $notModified; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is response invalid? Chris@0: * Chris@0: * @return bool Chris@0: * Chris@0: * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isInvalid() Chris@0: { Chris@0: return $this->statusCode < 100 || $this->statusCode >= 600; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is response informative? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isInformational() Chris@0: { Chris@0: return $this->statusCode >= 100 && $this->statusCode < 200; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is response successful? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isSuccessful() Chris@0: { Chris@0: return $this->statusCode >= 200 && $this->statusCode < 300; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response a redirect? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isRedirection() Chris@0: { Chris@0: return $this->statusCode >= 300 && $this->statusCode < 400; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is there a client error? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isClientError() Chris@0: { Chris@0: return $this->statusCode >= 400 && $this->statusCode < 500; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Was there a server side error? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public function isServerError() Chris@0: { Chris@0: return $this->statusCode >= 500 && $this->statusCode < 600; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response OK? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isOk() Chris@0: { Chris@0: return 200 === $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response forbidden? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isForbidden() Chris@0: { Chris@0: return 403 === $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response a not found error? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isNotFound() Chris@0: { Chris@0: return 404 === $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response a redirect of some form? Chris@0: * Chris@0: * @param string $location Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isRedirect($location = null) Chris@0: { Chris@17: return \in_array($this->statusCode, [201, 301, 302, 303, 307, 308]) && (null === $location ?: $location == $this->headers->get('Location')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the response empty? Chris@0: * Chris@0: * @return bool Chris@14: * Chris@14: * @final since version 3.2 Chris@0: */ Chris@0: public function isEmpty() Chris@0: { Chris@17: return \in_array($this->statusCode, [204, 304]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Cleans or flushes output buffers up to target level. Chris@0: * Chris@0: * Resulting level can be greater than target level if a non-removable buffer has been encountered. Chris@0: * Chris@0: * @param int $targetLevel The target output buffering level Chris@0: * @param bool $flush Whether to flush or clean the buffers Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: public static function closeOutputBuffers($targetLevel, $flush) Chris@0: { Chris@0: $status = ob_get_status(true); Chris@17: $level = \count($status); Chris@0: // PHP_OUTPUT_HANDLER_* are not defined on HHVM 3.3 Chris@17: $flags = \defined('PHP_OUTPUT_HANDLER_REMOVABLE') ? PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE) : -1; Chris@0: Chris@14: while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) { Chris@0: if ($flush) { Chris@0: ob_end_flush(); Chris@0: } else { Chris@0: ob_end_clean(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9. Chris@0: * Chris@0: * @see http://support.microsoft.com/kb/323308 Chris@14: * Chris@14: * @final since version 3.3 Chris@0: */ Chris@0: protected function ensureIEOverSSLCompatibility(Request $request) Chris@0: { Chris@14: if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && 1 == preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) && true === $request->isSecure()) { Chris@0: if ((int) preg_replace('/(MSIE )(.*?);/', '$2', $match[0]) < 9) { Chris@0: $this->headers->remove('Cache-Control'); Chris@0: } Chris@0: } Chris@0: } Chris@0: }