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 >
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
{
2013-08-22 05:10:30 +01:00
const HTTP_CONTINUE = 100 ;
const HTTP_SWITCHING_PROTOCOLS = 101 ;
const HTTP_PROCESSING = 102 ; // RFC2518
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
2013-08-22 05:10:30 +01:00
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
2013-08-22 05:10:30 +01:00
const HTTP_UNPROCESSABLE_ENTITY = 422 ; // RFC4918
const HTTP_LOCKED = 423 ; // RFC4918
const HTTP_FAILED_DEPENDENCY = 424 ; // RFC4918
const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425 ; // RFC2817
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 ;
2013-08-22 05:10:30 +01:00
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
/**
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 ;
2011-11-02 15:42:51 +00:00
/**
* @ var string
*/
2010-05-06 12:25:53 +01:00
protected $content ;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @ var string
*/
2010-05-06 12:25:53 +01:00
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
*/
2010-05-06 12:25:53 +01:00
protected $statusCode ;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @ var string
*/
2010-05-06 12:25:53 +01:00
protected $statusText ;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @ var string
*/
2011-02-21 15:56:42 +00:00
protected $charset ;
2010-05-06 12:25:53 +01:00
2011-11-02 15:42:51 +00:00
/**
* Status codes translation table .
*
2012-03-10 12:09:53 +00:00
* 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 ) .
2012-03-10 12:09:53 +00:00
*
* Unless otherwise noted , the status code is defined in RFC2616 .
*
2011-11-02 15:42:51 +00:00
* @ var array
*/
2012-07-09 13:50:58 +01:00
public static $statusTexts = array (
2010-05-06 12:25:53 +01:00
100 => 'Continue' ,
101 => 'Switching Protocols' ,
2012-03-10 12:09:53 +00:00
102 => 'Processing' , // RFC2518
2010-05-06 12:25:53 +01:00
200 => 'OK' ,
201 => 'Created' ,
202 => 'Accepted' ,
203 => 'Non-Authoritative Information' ,
204 => 'No Content' ,
205 => 'Reset Content' ,
206 => 'Partial Content' ,
2012-03-10 12:09:53 +00:00
207 => 'Multi-Status' , // RFC4918
208 => 'Already Reported' , // RFC5842
226 => 'IM Used' , // RFC3229
2010-05-06 12:25:53 +01:00
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
2010-05-06 12:25:53 +01:00
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' ,
2015-09-14 19:59:07 +01:00
413 => 'Payload Too Large' ,
414 => 'URI Too Long' ,
2010-05-06 12:25:53 +01:00
415 => 'Unsupported Media Type' ,
2015-09-14 19:59:07 +01:00
416 => 'Range Not Satisfiable' ,
2010-05-06 12:25:53 +01:00
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
2012-05-05 07:22:03 +01:00
422 => 'Unprocessable Entity' , // RFC4918
423 => 'Locked' , // RFC4918
424 => 'Failed Dependency' , // RFC4918
2012-03-10 12:09:53 +00:00
425 => 'Reserved for WebDAV advanced collections expired proposal' , // RFC2817
2012-05-05 07:22:03 +01:00
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
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' ,
2012-05-05 07:22:03 +01:00
506 => 'Variant Also Negotiates (Experimental)' , // RFC2295
507 => 'Insufficient Storage' , // RFC4918
508 => 'Loop Detected' , // RFC5842
510 => 'Not Extended' , // RFC2774
511 => 'Network Authentication Required' , // RFC6585
2010-05-06 12:25:53 +01:00
);
2016-08-25 13:14:47 +01:00
private static $deprecatedMethods = array (
'setDate' , 'getDate' ,
'setExpires' , 'getExpires' ,
'setLastModified' , 'getLastModified' ,
'setProtocolVersion' , 'getProtocolVersion' ,
'setStatusCode' , 'getStatusCode' ,
'setCharset' , 'getCharset' ,
'setPrivate' , 'setPublic' ,
'getAge' , 'getMaxAge' , 'setMaxAge' , 'setSharedMaxAge' ,
'getTtl' , 'setTtl' , 'setClientTtl' ,
'getEtag' , 'setEtag' ,
'hasVary' , 'getVary' , 'setVary' ,
'isInvalid' , 'isSuccessful' , 'isRedirection' ,
'isClientError' , 'isOk' , 'isForbidden' ,
'isNotFound' , 'isRedirect' , 'isEmpty' ,
);
private static $deprecationsTriggered = array (
__CLASS__ => true ,
BinaryFileResponse :: class => true ,
JsonResponse :: class => true ,
RedirectResponse :: class => true ,
StreamedResponse :: class => true ,
);
2010-05-06 12:25:53 +01:00
/**
* Constructor .
*
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
*
2013-01-02 19:47:18 +00:00
* @ throws \InvalidArgumentException When the HTTP status code is not valid
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' );
2016-08-25 13:14:47 +01:00
// Deprecations
$class = get_class ( $this );
if ( $this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface ) {
$class = get_parent_class ( $class );
}
if ( isset ( self :: $deprecationsTriggered [ $class ])) {
return ;
}
self :: $deprecationsTriggered [ $class ] = true ;
foreach ( self :: $deprecatedMethods as $method ) {
$r = new \ReflectionMethod ( $class , $method );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName ()) {
@ trigger_error ( sprintf ( 'Extending %s::%s() in %s is deprecated since version 3.2 and won\'t be supported anymore in 4.0 as it will be final.' , __CLASS__ , $method , $class ), E_USER_DEPRECATED );
}
}
2010-05-06 12:25:53 +01:00
}
2012-03-15 15:22:55 +00:00
/**
2014-12-21 17:00:50 +00:00
* Factory method for chainability .
2012-03-15 15:22:55 +00:00
*
* Example :
*
* return Response :: create ( $body , 200 )
* -> setSharedMaxAge ( 300 );
*
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
*
2016-12-26 07:50:27 +00:00
* @ return static
2012-03-15 15:22:55 +00:00
*/
2012-07-09 13:50:58 +01:00
public static function create ( $content = '' , $status = 200 , $headers = array ())
2012-03-15 15:22:55 +00:00
{
return new static ( $content , $status , $headers );
}
2010-05-06 12:25:53 +01:00
/**
2011-10-17 16:16:22 +01:00
* Returns the Response as an HTTP string .
2010-05-06 12:25:53 +01:00
*
2012-04-18 21:38:08 +01:00
* The string representation of the Response is the same as the
2011-10-17 16:16:22 +01:00
* 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 ()
2010-05-06 12:25:53 +01:00
*/
public function __toString ()
{
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
2011-10-17 16:16:22 +01:00
* compliant with RFC 2616. Most of the changes are based on
* the Request that is " associated " with this Response .
*
* @ param Request $request A Request instance
2012-04-16 00:30:07 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2011-07-11 10:21:18 +01:00
*/
2011-10-17 16:16:22 +01:00
public function prepare ( Request $request )
2011-07-11 10:21:18 +01:00
{
2011-10-17 16:16:22 +01:00
$headers = $this -> headers ;
2015-01-11 13:22:08 +00:00
if ( $this -> isInformational () || $this -> isEmpty ()) {
2012-06-13 15:20:50 +01:00
$this -> setContent ( null );
2014-06-27 08:57:31 +01:00
$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 );
}
2011-10-17 16:16:22 +01:00
}
2014-06-27 08:57:31 +01:00
// 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' )) {
2014-06-27 08:57:31 +01:00
// add the charset
2014-09-21 19:53:12 +01:00
$headers -> set ( 'Content-Type' , $headers -> get ( 'Content-Type' ) . '; charset=' . $charset );
2014-06-27 08:57:31 +01:00
}
2011-07-11 10:21:18 +01:00
2014-06-27 08:57:31 +01:00
// Fix Content-Length
if ( $headers -> has ( 'Transfer-Encoding' )) {
$headers -> remove ( 'Content-Length' );
}
2011-10-17 16:16:22 +01:00
2014-06-27 08:57:31 +01:00
if ( $request -> isMethod ( 'HEAD' )) {
// cf. RFC2616 14.13
$length = $headers -> get ( 'Content-Length' );
$this -> setContent ( null );
if ( $length ) {
$headers -> set ( 'Content-Length' , $length );
}
2011-10-17 16:16:22 +01:00
}
2011-07-11 10:21:18 +01:00
}
2012-04-16 00:30:07 +01:00
2012-11-09 07:57:59 +00:00
// Fix protocol
if ( 'HTTP/1.0' != $request -> server -> get ( 'SERVER_PROTOCOL' )) {
$this -> setProtocolVersion ( '1.1' );
}
2012-11-17 10:57:00 +00:00
// Check if we need to send extra expire info headers
2016-06-22 14:14:54 +01:00
if ( '1.0' == $this -> getProtocolVersion () && false !== strpos ( $this -> headers -> get ( 'Cache-Control' ), 'no-cache' )) {
2012-11-17 10:57:00 +00:00
$this -> headers -> set ( 'pragma' , 'no-cache' );
$this -> headers -> set ( 'expires' , - 1 );
}
2013-04-12 14:45:42 +01:00
$this -> ensureIEOverSSLCompatibility ( $request );
2013-02-22 07:28:58 +00:00
2012-04-16 00:30:07 +01:00
return $this ;
2011-07-11 10:21:18 +01:00
}
2010-05-06 12:25:53 +01:00
/**
2010-06-23 15:24:24 +01:00
* Sends HTTP headers .
2012-03-15 15:22:55 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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 ()) {
2012-03-15 15:22:55 +00:00
return $this ;
2011-07-07 06:31:02 +01:00
}
2015-06-08 19:37:36 +01:00
if ( ! $this -> headers -> has ( 'Date' )) {
2015-10-13 17:11:28 +01:00
$this -> setDate ( \DateTime :: createFromFormat ( 'U' , time ()));
2015-06-08 19:37:36 +01:00
}
2010-05-06 12:25:53 +01:00
// headers
2016-11-19 14:10:17 +00:00
foreach ( $this -> headers -> allPreserveCaseWithoutCookies () as $name => $values ) {
2010-06-23 15:24:24 +01:00
foreach ( $values as $value ) {
2013-10-23 14:49:48 +01:00
header ( $name . ': ' . $value , false , $this -> statusCode );
2010-06-23 15:24:24 +01:00
}
2010-02-17 13:53:18 +00:00
}
2011-01-15 17:53:30 +00:00
2015-11-26 06:58:58 +00:00
// status
header ( sprintf ( 'HTTP/%s %s %s' , $this -> version , $this -> statusCode , $this -> statusText ), true , $this -> statusCode );
2011-01-15 17:53:30 +00:00
// cookies
foreach ( $this -> headers -> getCookies () as $cookie ) {
2016-03-11 09:58:00 +00:00
if ( $cookie -> isRaw ()) {
setrawcookie ( $cookie -> getName (), $cookie -> getValue (), $cookie -> getExpiresTime (), $cookie -> getPath (), $cookie -> getDomain (), $cookie -> isSecure (), $cookie -> isHttpOnly ());
} else {
setcookie ( $cookie -> getName (), $cookie -> getValue (), $cookie -> getExpiresTime (), $cookie -> getPath (), $cookie -> getDomain (), $cookie -> isSecure (), $cookie -> isHttpOnly ());
}
2011-01-15 17:53:30 +00:00
}
2012-03-15 15:22:55 +00:00
return $this ;
2010-02-17 13:53:18 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Sends content for the current web response .
2012-03-15 15:22:55 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*/
public function sendContent ()
{
echo $this -> content ;
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* Sends HTTP headers and content .
2011-07-20 09:06:02 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*/
public function send ()
{
$this -> sendHeaders ();
$this -> sendContent ();
2011-06-13 17:54:20 +01:00
2016-07-13 11:53:11 +01:00
if ( function_exists ( 'fastcgi_finish_request' )) {
fastcgi_finish_request ();
} elseif ( 'cli' !== PHP_SAPI ) {
static :: closeOutputBuffers ( 0 , true );
}
2012-03-15 15:22:55 +00:00
return $this ;
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
*
2013-11-20 02:35:05 +00:00
* Valid types are strings , numbers , null , and objects that implement a __toString () method .
2011-06-21 12:06:36 +01:00
*
2013-11-20 02:35:05 +00:00
* @ param mixed $content Content that can be cast to string
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2011-07-20 09:06:02 +01:00
*
2012-12-16 12:02:54 +00:00
* @ throws \UnexpectedValueException
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' ))) {
2013-04-10 11:24:37 +01:00
throw new \UnexpectedValueException ( sprintf ( 'The Response content must be a string or object implementing __toString(), "%s" given.' , gettype ( $content )));
2011-06-21 10:59:20 +01:00
}
2011-06-22 14:22:06 +01:00
$this -> content = ( string ) $content ;
2012-03-15 15:22:55 +00:00
return $this ;
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
*/
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
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*/
public function setProtocolVersion ( $version )
{
$this -> version = $version ;
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* Gets the HTTP protocol version .
*
* @ return string The HTTP protocol version
*/
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
*
2014-11-30 13:33:44 +00:00
* @ param int $code HTTP status code
* @ param mixed $text HTTP status text
2012-07-19 16:48:12 +01:00
*
* 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
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*
* @ throws \InvalidArgumentException When the HTTP status code is not valid
*/
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 ) {
2015-11-18 10:39:29 +00:00
$this -> statusText = isset ( self :: $statusTexts [ $code ]) ? self :: $statusTexts [ $code ] : 'unknown status' ;
2012-07-19 16:48:12 +01:00
2012-07-20 06:34:13 +01:00
return $this ;
2012-07-19 16:48:12 +01:00
}
if ( false === $text ) {
$this -> statusText = '' ;
2012-07-20 06:34:13 +01:00
return $this ;
2012-07-19 16:48:12 +01:00
}
$this -> statusText = $text ;
2012-03-15 15:22:55 +00:00
return $this ;
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
*
2014-11-30 13:33:44 +00:00
* @ return int Status code
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
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-12-15 11:10:29 +00:00
*/
public function setCharset ( $charset )
{
$this -> charset = $charset ;
2012-03-15 15:22:55 +00:00
return $this ;
2010-12-15 11:10:29 +00:00
}
/**
* Retrieves the response charset .
*
* @ return string Character set
*/
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 .
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the response is worth caching , false otherwise
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
2013-01-02 19:47:18 +00:00
* indicator or Expires header and the calculated age is less than the freshness lifetime .
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the response is fresh , false otherwise
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 .
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the response is validateable , false otherwise
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
*
2016-12-26 07:50:27 +00:00
* @ return $this
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' );
2012-03-15 15:22:55 +00:00
return $this ;
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
*
2016-12-26 07:50:27 +00:00
* @ return $this
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' );
2012-03-15 15:22:55 +00:00
return $this ;
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 .
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the response must be revalidated by a cache , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function mustRevalidate ()
{
2015-07-11 22:06:45 +01:00
return $this -> headers -> hasCacheControlDirective ( 'must-revalidate' ) || $this -> headers -> hasCacheControlDirective ( '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
2010-05-06 12:25:53 +01:00
*/
public function getDate ()
{
2015-06-08 19:37:36 +01:00
if ( ! $this -> headers -> has ( 'Date' )) {
2015-10-13 17:11:28 +01:00
$this -> setDate ( \DateTime :: createFromFormat ( 'U' , time ()));
2015-06-08 19:37:36 +01:00
}
return $this -> headers -> getDate ( 'Date' );
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
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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' );
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* Returns the age of the response .
*
2014-11-30 13:33:44 +00:00
* @ return int The age of the response in seconds
2010-05-06 12:25:53 +01:00
*/
public function getAge ()
{
2013-01-02 21:06:26 +00:00
if ( null !== $age = $this -> headers -> get ( 'Age' )) {
return ( int ) $age ;
2010-05-06 12:25:53 +01:00
}
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
*
2016-12-26 07:50:27 +00:00
* @ return $this
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 ());
}
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* Returns the value of the Expires header as a DateTime instance .
*
2013-01-02 19:47:18 +00:00
* @ return \DateTime | null A DateTime instance or null if the header does not exist
2010-05-06 12:25:53 +01:00
*/
public function getExpires ()
{
2013-01-02 19:47:18 +00:00
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' );
}
2010-05-06 12:25:53 +01:00
}
/**
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
*
2013-01-02 19:47:18 +00:00
* Passing null as value will remove the header .
2010-05-06 12:25:53 +01:00
*
2013-01-02 19:47:18 +00:00
* @ param \DateTime | null $date A \DateTime instance or null to remove the header
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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
}
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
2012-12-31 09:21:48 +00:00
* 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 .
2010-05-06 12:25:53 +01:00
*
* 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
2010-05-06 12:25:53 +01:00
*/
public function getMaxAge ()
{
2013-01-02 21:06:26 +00:00
if ( $this -> headers -> hasCacheControlDirective ( 's-maxage' )) {
return ( int ) $this -> headers -> getCacheControlDirective ( 's-maxage' );
2010-05-06 12:25:53 +01:00
}
2013-01-02 21:06:26 +00:00
if ( $this -> headers -> hasCacheControlDirective ( 'max-age' )) {
return ( int ) $this -> headers -> getCacheControlDirective ( 'max-age' );
2010-05-06 12:25:53 +01:00
}
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' );
}
}
/**
* 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
*
2016-12-26 07:50:27 +00:00
* @ return $this
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 );
2012-03-15 15:22:55 +00:00
return $this ;
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 .
*
2014-11-30 13:33:44 +00:00
* @ param int $value Number of seconds
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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 );
2012-03-15 15:22:55 +00:00
return $this ;
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 .
*
2014-11-30 13:33:44 +00:00
* @ return int | null The TTL in seconds
2010-05-06 12:25:53 +01:00
*/
public function getTtl ()
{
2013-01-02 21:06:26 +00:00
if ( null !== $maxAge = $this -> getMaxAge ()) {
2010-05-06 12:25:53 +01:00
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
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*/
public function setTtl ( $seconds )
{
$this -> setSharedMaxAge ( $this -> getAge () + $seconds );
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* 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
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-05-06 12:25:53 +01:00
*/
public function setClientTtl ( $seconds )
{
$this -> setMaxAge ( $this -> getAge () + $seconds );
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* Returns the Last - Modified HTTP header as a DateTime instance .
*
2013-01-02 19:47:18 +00:00
* @ return \DateTime | null A DateTime instance or null if the header does not exist
*
* @ throws \RuntimeException When the HTTP header is not parseable
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
*
2013-01-02 19:47:18 +00:00
* Passing null as value will remove the header .
2010-05-06 12:25:53 +01:00
*
2013-01-02 19:47:18 +00:00
* @ param \DateTime | null $date A \DateTime instance or null to remove the header
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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-03-15 15:22:55 +00:00
return $this ;
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
*
2013-01-02 19:47:18 +00:00
* @ return string | null The ETag HTTP header or null if it does not exist
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 .
*
2013-01-02 19:47:18 +00:00
* @ param string | null $etag The ETag unique identifier or null to remove the header
2014-04-12 18:54:57 +01:00
* @ param bool $weak Whether you want a weak ETag or not
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
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
}
2012-03-15 15:22:55 +00:00
return $this ;
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
2012-03-15 16:41:21 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2011-07-20 09:06:02 +01:00
*
2012-12-16 12:02:54 +00:00
* @ throws \InvalidArgumentException
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 ();
}
}
2012-03-15 15:22:55 +00:00
return $this ;
2010-11-12 18:46:39 +00:00
}
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 .
*
2016-12-26 07:50:27 +00:00
* @ return $this
2012-03-15 15:22:55 +00:00
*
2010-05-06 12:25:53 +01:00
* @ see http :// tools . ietf . org / html / rfc2616 #section-10.3.5
*/
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
}
2012-03-15 15:22:55 +00:00
return $this ;
2010-05-06 12:25:53 +01:00
}
/**
* 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
2010-05-06 12:25:53 +01:00
*/
public function hasVary ()
{
2013-01-02 21:06:26 +00:00
return null !== $this -> headers -> get ( 'Vary' );
2010-05-06 12:25:53 +01:00
}
/**
* Returns an array of header names given in the Vary header .
*
* @ return array An array of Vary names
*/
public function getVary ()
{
2014-03-04 20:15:32 +00:00
if ( ! $vary = $this -> headers -> get ( 'Vary' , null , false )) {
2010-05-06 12:25:53 +01:00
return array ();
}
2014-03-04 20:15:32 +00:00
$ret = array ();
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 ;
2010-10-30 15:37:18 +01:00
}
/**
* 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
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-10-30 15:37:18 +01:00
*/
public function setVary ( $headers , $replace = true )
{
$this -> headers -> set ( 'Vary' , $headers , $replace );
2012-03-15 15:22:55 +00:00
return $this ;
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
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the Response validators match the Request , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function isNotModified ( Request $request )
{
2016-10-14 12:10:27 +01:00
if ( ! $request -> isMethodCacheable ()) {
2012-07-21 20:52:19 +01:00
return false ;
}
2014-10-22 19:27:13 +01:00
$notModified = false ;
$lastModified = $this -> headers -> get ( 'Last-Modified' );
2014-06-18 21:51:10 +01:00
$modifiedSince = $request -> headers -> get ( 'If-Modified-Since' );
2015-05-29 18:14:48 +01:00
if ( $etags = $request -> getETags ()) {
2014-06-18 21:51:10 +01:00
$notModified = in_array ( $this -> getEtag (), $etags ) || in_array ( '*' , $etags );
}
if ( $modifiedSince && $lastModified ) {
$notModified = strtotime ( $modifiedSince ) >= strtotime ( $lastModified ) && ( ! $etags || $notModified );
2010-05-06 12:25:53 +01:00
}
2010-05-07 15:09:11 +01:00
if ( $notModified ) {
2010-05-06 12:25:53 +01:00
$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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
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
/**
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
2011-07-20 09:06:02 +01:00
*/
2010-05-06 12:25:53 +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
2011-07-20 09:06:02 +01:00
*/
2010-05-06 12:25:53 +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
2011-07-20 09:06:02 +01:00
*/
2010-05-06 12:25:53 +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
*
2012-02-12 01:04:15 +00:00
* @ param string $location
*
2014-04-16 11:30:19 +01:00
* @ return bool
2011-07-20 09:06:02 +01:00
*/
2011-05-29 14:33:37 +01:00
public function isRedirect ( $location = null )
2010-05-06 12:25:53 +01:00
{
2012-06-09 23:20:06 +01:00
return in_array ( $this -> statusCode , array ( 201 , 301 , 302 , 303 , 307 , 308 )) && ( null === $location ? : $location == $this -> headers -> get ( 'Location' ));
2010-05-06 12:25:53 +01:00
}
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
2011-07-20 09:06:02 +01:00
*/
2010-05-06 12:25:53 +01:00
public function isEmpty ()
{
2014-04-16 16:58:23 +01:00
return in_array ( $this -> statusCode , array ( 204 , 304 ));
2010-05-06 12:25:53 +01:00
}
2013-04-12 14:45:42 +01:00
/**
2014-04-17 08:51:05 +01:00
* 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
*/
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
2015-07-16 09:43:55 +01:00
$flags = defined ( 'PHP_OUTPUT_HANDLER_REMOVABLE' ) ? PHP_OUTPUT_HANDLER_REMOVABLE | ( $flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE ) : - 1 ;
2014-04-17 08:51:05 +01:00
2015-06-30 18:32:40 +01:00
while ( $level -- > $targetLevel && ( $s = $status [ $level ]) && ( ! isset ( $s [ 'del' ]) ? ! isset ( $s [ 'flags' ]) || $flags === ( $s [ 'flags' ] & $flags ) : $s [ 'del' ])) {
2014-04-17 08:51:05 +01:00
if ( $flush ) {
ob_end_flush ();
} else {
ob_end_clean ();
}
}
}
/**
2014-12-22 16:29:52 +00:00
* Checks if we need to remove Cache - Control for SSL encrypted downloads when using IE < 9.
2013-04-12 14:45:42 +01:00
*
2016-11-13 17:41:36 +00:00
* @ see http :// support . microsoft . com / kb / 323308
2013-04-12 14:45:42 +01:00
*/
protected function ensureIEOverSSLCompatibility ( Request $request )
{
if ( false !== stripos ( $this -> headers -> get ( 'Content-Disposition' ), 'attachment' ) && preg_match ( '/MSIE (.*?);/i' , $request -> server -> get ( 'HTTP_USER_AGENT' ), $match ) == 1 && true === $request -> isSecure ()) {
2015-03-21 10:51:07 +00:00
if (( int ) preg_replace ( '/(MSIE )(.*?);/' , '$2' , $match [ 0 ]) < 9 ) {
2013-04-12 14:45:42 +01:00
$this -> headers -> remove ( 'Cache-Control' );
}
}
}
2011-12-18 13:33:54 +00:00
}