Response.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. // Help opcache.preload discover always-needed symbols
  12. class_exists(ResponseHeaderBag::class);
  13. /**
  14. * Response represents an HTTP response.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class Response
  19. {
  20. public const HTTP_CONTINUE = 100;
  21. public const HTTP_SWITCHING_PROTOCOLS = 101;
  22. public const HTTP_PROCESSING = 102; // RFC2518
  23. public const HTTP_EARLY_HINTS = 103; // RFC8297
  24. public const HTTP_OK = 200;
  25. public const HTTP_CREATED = 201;
  26. public const HTTP_ACCEPTED = 202;
  27. public const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
  28. public const HTTP_NO_CONTENT = 204;
  29. public const HTTP_RESET_CONTENT = 205;
  30. public const HTTP_PARTIAL_CONTENT = 206;
  31. public const HTTP_MULTI_STATUS = 207; // RFC4918
  32. public const HTTP_ALREADY_REPORTED = 208; // RFC5842
  33. public const HTTP_IM_USED = 226; // RFC3229
  34. public const HTTP_MULTIPLE_CHOICES = 300;
  35. public const HTTP_MOVED_PERMANENTLY = 301;
  36. public const HTTP_FOUND = 302;
  37. public const HTTP_SEE_OTHER = 303;
  38. public const HTTP_NOT_MODIFIED = 304;
  39. public const HTTP_USE_PROXY = 305;
  40. public const HTTP_RESERVED = 306;
  41. public const HTTP_TEMPORARY_REDIRECT = 307;
  42. public const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
  43. public const HTTP_BAD_REQUEST = 400;
  44. public const HTTP_UNAUTHORIZED = 401;
  45. public const HTTP_PAYMENT_REQUIRED = 402;
  46. public const HTTP_FORBIDDEN = 403;
  47. public const HTTP_NOT_FOUND = 404;
  48. public const HTTP_METHOD_NOT_ALLOWED = 405;
  49. public const HTTP_NOT_ACCEPTABLE = 406;
  50. public const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
  51. public const HTTP_REQUEST_TIMEOUT = 408;
  52. public const HTTP_CONFLICT = 409;
  53. public const HTTP_GONE = 410;
  54. public const HTTP_LENGTH_REQUIRED = 411;
  55. public const HTTP_PRECONDITION_FAILED = 412;
  56. public const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
  57. public const HTTP_REQUEST_URI_TOO_LONG = 414;
  58. public const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
  59. public const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
  60. public const HTTP_EXPECTATION_FAILED = 417;
  61. public const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
  62. public const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
  63. public const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
  64. public const HTTP_LOCKED = 423; // RFC4918
  65. public const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
  66. public const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04
  67. public const HTTP_UPGRADE_REQUIRED = 426; // RFC2817
  68. public const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585
  69. public const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585
  70. public const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
  71. public const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451; // RFC7725
  72. public const HTTP_INTERNAL_SERVER_ERROR = 500;
  73. public const HTTP_NOT_IMPLEMENTED = 501;
  74. public const HTTP_BAD_GATEWAY = 502;
  75. public const HTTP_SERVICE_UNAVAILABLE = 503;
  76. public const HTTP_GATEWAY_TIMEOUT = 504;
  77. public const HTTP_VERSION_NOT_SUPPORTED = 505;
  78. public const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
  79. public const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918
  80. public const HTTP_LOOP_DETECTED = 508; // RFC5842
  81. public const HTTP_NOT_EXTENDED = 510; // RFC2774
  82. public const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
  83. /**
  84. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
  85. */
  86. private const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
  87. 'must_revalidate' => false,
  88. 'no_cache' => false,
  89. 'no_store' => false,
  90. 'no_transform' => false,
  91. 'public' => false,
  92. 'private' => false,
  93. 'proxy_revalidate' => false,
  94. 'max_age' => true,
  95. 's_maxage' => true,
  96. 'immutable' => false,
  97. 'last_modified' => true,
  98. 'etag' => true,
  99. ];
  100. /**
  101. * @var ResponseHeaderBag
  102. */
  103. public $headers;
  104. /**
  105. * @var string
  106. */
  107. protected $content;
  108. /**
  109. * @var string
  110. */
  111. protected $version;
  112. /**
  113. * @var int
  114. */
  115. protected $statusCode;
  116. /**
  117. * @var string
  118. */
  119. protected $statusText;
  120. /**
  121. * @var string
  122. */
  123. protected $charset;
  124. /**
  125. * Status codes translation table.
  126. *
  127. * The list of codes is complete according to the
  128. * {@link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml Hypertext Transfer Protocol (HTTP) Status Code Registry}
  129. * (last updated 2021-10-01).
  130. *
  131. * Unless otherwise noted, the status code is defined in RFC2616.
  132. *
  133. * @var array
  134. */
  135. public static $statusTexts = [
  136. 100 => 'Continue',
  137. 101 => 'Switching Protocols',
  138. 102 => 'Processing', // RFC2518
  139. 103 => 'Early Hints',
  140. 200 => 'OK',
  141. 201 => 'Created',
  142. 202 => 'Accepted',
  143. 203 => 'Non-Authoritative Information',
  144. 204 => 'No Content',
  145. 205 => 'Reset Content',
  146. 206 => 'Partial Content',
  147. 207 => 'Multi-Status', // RFC4918
  148. 208 => 'Already Reported', // RFC5842
  149. 226 => 'IM Used', // RFC3229
  150. 300 => 'Multiple Choices',
  151. 301 => 'Moved Permanently',
  152. 302 => 'Found',
  153. 303 => 'See Other',
  154. 304 => 'Not Modified',
  155. 305 => 'Use Proxy',
  156. 307 => 'Temporary Redirect',
  157. 308 => 'Permanent Redirect', // RFC7238
  158. 400 => 'Bad Request',
  159. 401 => 'Unauthorized',
  160. 402 => 'Payment Required',
  161. 403 => 'Forbidden',
  162. 404 => 'Not Found',
  163. 405 => 'Method Not Allowed',
  164. 406 => 'Not Acceptable',
  165. 407 => 'Proxy Authentication Required',
  166. 408 => 'Request Timeout',
  167. 409 => 'Conflict',
  168. 410 => 'Gone',
  169. 411 => 'Length Required',
  170. 412 => 'Precondition Failed',
  171. 413 => 'Content Too Large', // RFC-ietf-httpbis-semantics
  172. 414 => 'URI Too Long',
  173. 415 => 'Unsupported Media Type',
  174. 416 => 'Range Not Satisfiable',
  175. 417 => 'Expectation Failed',
  176. 418 => 'I\'m a teapot', // RFC2324
  177. 421 => 'Misdirected Request', // RFC7540
  178. 422 => 'Unprocessable Content', // RFC-ietf-httpbis-semantics
  179. 423 => 'Locked', // RFC4918
  180. 424 => 'Failed Dependency', // RFC4918
  181. 425 => 'Too Early', // RFC-ietf-httpbis-replay-04
  182. 426 => 'Upgrade Required', // RFC2817
  183. 428 => 'Precondition Required', // RFC6585
  184. 429 => 'Too Many Requests', // RFC6585
  185. 431 => 'Request Header Fields Too Large', // RFC6585
  186. 451 => 'Unavailable For Legal Reasons', // RFC7725
  187. 500 => 'Internal Server Error',
  188. 501 => 'Not Implemented',
  189. 502 => 'Bad Gateway',
  190. 503 => 'Service Unavailable',
  191. 504 => 'Gateway Timeout',
  192. 505 => 'HTTP Version Not Supported',
  193. 506 => 'Variant Also Negotiates', // RFC2295
  194. 507 => 'Insufficient Storage', // RFC4918
  195. 508 => 'Loop Detected', // RFC5842
  196. 510 => 'Not Extended', // RFC2774
  197. 511 => 'Network Authentication Required', // RFC6585
  198. ];
  199. /**
  200. * @throws \InvalidArgumentException When the HTTP status code is not valid
  201. */
  202. public function __construct(?string $content = '', int $status = 200, array $headers = [])
  203. {
  204. $this->headers = new ResponseHeaderBag($headers);
  205. $this->setContent($content);
  206. $this->setStatusCode($status);
  207. $this->setProtocolVersion('1.0');
  208. }
  209. /**
  210. * Factory method for chainability.
  211. *
  212. * Example:
  213. *
  214. * return Response::create($body, 200)
  215. * ->setSharedMaxAge(300);
  216. *
  217. * @return static
  218. *
  219. * @deprecated since Symfony 5.1, use __construct() instead.
  220. */
  221. public static function create(?string $content = '', int $status = 200, array $headers = [])
  222. {
  223. trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
  224. return new static($content, $status, $headers);
  225. }
  226. /**
  227. * Returns the Response as an HTTP string.
  228. *
  229. * The string representation of the Response is the same as the
  230. * one that will be sent to the client only if the prepare() method
  231. * has been called before.
  232. *
  233. * @return string
  234. *
  235. * @see prepare()
  236. */
  237. public function __toString()
  238. {
  239. return
  240. sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
  241. $this->headers."\r\n".
  242. $this->getContent();
  243. }
  244. /**
  245. * Clones the current Response instance.
  246. */
  247. public function __clone()
  248. {
  249. $this->headers = clone $this->headers;
  250. }
  251. /**
  252. * Prepares the Response before it is sent to the client.
  253. *
  254. * This method tweaks the Response to ensure that it is
  255. * compliant with RFC 2616. Most of the changes are based on
  256. * the Request that is "associated" with this Response.
  257. *
  258. * @return $this
  259. */
  260. public function prepare(Request $request)
  261. {
  262. $headers = $this->headers;
  263. if ($this->isInformational() || $this->isEmpty()) {
  264. $this->setContent(null);
  265. $headers->remove('Content-Type');
  266. $headers->remove('Content-Length');
  267. // prevent PHP from sending the Content-Type header based on default_mimetype
  268. ini_set('default_mimetype', '');
  269. } else {
  270. // Content-type based on the Request
  271. if (!$headers->has('Content-Type')) {
  272. $format = $request->getRequestFormat(null);
  273. if (null !== $format && $mimeType = $request->getMimeType($format)) {
  274. $headers->set('Content-Type', $mimeType);
  275. }
  276. }
  277. // Fix Content-Type
  278. $charset = $this->charset ?: 'UTF-8';
  279. if (!$headers->has('Content-Type')) {
  280. $headers->set('Content-Type', 'text/html; charset='.$charset);
  281. } elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
  282. // add the charset
  283. $headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
  284. }
  285. // Fix Content-Length
  286. if ($headers->has('Transfer-Encoding')) {
  287. $headers->remove('Content-Length');
  288. }
  289. if ($request->isMethod('HEAD')) {
  290. // cf. RFC2616 14.13
  291. $length = $headers->get('Content-Length');
  292. $this->setContent(null);
  293. if ($length) {
  294. $headers->set('Content-Length', $length);
  295. }
  296. }
  297. }
  298. // Fix protocol
  299. if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
  300. $this->setProtocolVersion('1.1');
  301. }
  302. // Check if we need to send extra expire info headers
  303. if ('1.0' == $this->getProtocolVersion() && str_contains($headers->get('Cache-Control', ''), 'no-cache')) {
  304. $headers->set('pragma', 'no-cache');
  305. $headers->set('expires', -1);
  306. }
  307. $this->ensureIEOverSSLCompatibility($request);
  308. if ($request->isSecure()) {
  309. foreach ($headers->getCookies() as $cookie) {
  310. $cookie->setSecureDefault(true);
  311. }
  312. }
  313. return $this;
  314. }
  315. /**
  316. * Sends HTTP headers.
  317. *
  318. * @return $this
  319. */
  320. public function sendHeaders()
  321. {
  322. // headers have already been sent by the developer
  323. if (headers_sent()) {
  324. return $this;
  325. }
  326. // headers
  327. foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
  328. $replace = 0 === strcasecmp($name, 'Content-Type');
  329. foreach ($values as $value) {
  330. header($name.': '.$value, $replace, $this->statusCode);
  331. }
  332. }
  333. // cookies
  334. foreach ($this->headers->getCookies() as $cookie) {
  335. header('Set-Cookie: '.$cookie, false, $this->statusCode);
  336. }
  337. // status
  338. header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);
  339. return $this;
  340. }
  341. /**
  342. * Sends content for the current web response.
  343. *
  344. * @return $this
  345. */
  346. public function sendContent()
  347. {
  348. echo $this->content;
  349. return $this;
  350. }
  351. /**
  352. * Sends HTTP headers and content.
  353. *
  354. * @return $this
  355. */
  356. public function send()
  357. {
  358. $this->sendHeaders();
  359. $this->sendContent();
  360. if (\function_exists('fastcgi_finish_request')) {
  361. fastcgi_finish_request();
  362. } elseif (\function_exists('litespeed_finish_request')) {
  363. litespeed_finish_request();
  364. } elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
  365. static::closeOutputBuffers(0, true);
  366. }
  367. return $this;
  368. }
  369. /**
  370. * Sets the response content.
  371. *
  372. * @return $this
  373. */
  374. public function setContent(?string $content)
  375. {
  376. $this->content = $content ?? '';
  377. return $this;
  378. }
  379. /**
  380. * Gets the current response content.
  381. *
  382. * @return string|false
  383. */
  384. public function getContent()
  385. {
  386. return $this->content;
  387. }
  388. /**
  389. * Sets the HTTP protocol version (1.0 or 1.1).
  390. *
  391. * @return $this
  392. *
  393. * @final
  394. */
  395. public function setProtocolVersion(string $version): object
  396. {
  397. $this->version = $version;
  398. return $this;
  399. }
  400. /**
  401. * Gets the HTTP protocol version.
  402. *
  403. * @final
  404. */
  405. public function getProtocolVersion(): string
  406. {
  407. return $this->version;
  408. }
  409. /**
  410. * Sets the response status code.
  411. *
  412. * If the status text is null it will be automatically populated for the known
  413. * status codes and left empty otherwise.
  414. *
  415. * @return $this
  416. *
  417. * @throws \InvalidArgumentException When the HTTP status code is not valid
  418. *
  419. * @final
  420. */
  421. public function setStatusCode(int $code, string $text = null): object
  422. {
  423. $this->statusCode = $code;
  424. if ($this->isInvalid()) {
  425. throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
  426. }
  427. if (null === $text) {
  428. $this->statusText = self::$statusTexts[$code] ?? 'unknown status';
  429. return $this;
  430. }
  431. if (false === $text) {
  432. $this->statusText = '';
  433. return $this;
  434. }
  435. $this->statusText = $text;
  436. return $this;
  437. }
  438. /**
  439. * Retrieves the status code for the current web response.
  440. *
  441. * @final
  442. */
  443. public function getStatusCode(): int
  444. {
  445. return $this->statusCode;
  446. }
  447. /**
  448. * Sets the response charset.
  449. *
  450. * @return $this
  451. *
  452. * @final
  453. */
  454. public function setCharset(string $charset): object
  455. {
  456. $this->charset = $charset;
  457. return $this;
  458. }
  459. /**
  460. * Retrieves the response charset.
  461. *
  462. * @final
  463. */
  464. public function getCharset(): ?string
  465. {
  466. return $this->charset;
  467. }
  468. /**
  469. * Returns true if the response may safely be kept in a shared (surrogate) cache.
  470. *
  471. * Responses marked "private" with an explicit Cache-Control directive are
  472. * considered uncacheable.
  473. *
  474. * Responses with neither a freshness lifetime (Expires, max-age) nor cache
  475. * validator (Last-Modified, ETag) are considered uncacheable because there is
  476. * no way to tell when or how to remove them from the cache.
  477. *
  478. * Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation,
  479. * for example "status codes that are defined as cacheable by default [...]
  480. * can be reused by a cache with heuristic expiration unless otherwise indicated"
  481. * (https://tools.ietf.org/html/rfc7231#section-6.1)
  482. *
  483. * @final
  484. */
  485. public function isCacheable(): bool
  486. {
  487. if (!\in_array($this->statusCode, [200, 203, 300, 301, 302, 404, 410])) {
  488. return false;
  489. }
  490. if ($this->headers->hasCacheControlDirective('no-store') || $this->headers->getCacheControlDirective('private')) {
  491. return false;
  492. }
  493. return $this->isValidateable() || $this->isFresh();
  494. }
  495. /**
  496. * Returns true if the response is "fresh".
  497. *
  498. * Fresh responses may be served from cache without any interaction with the
  499. * origin. A response is considered fresh when it includes a Cache-Control/max-age
  500. * indicator or Expires header and the calculated age is less than the freshness lifetime.
  501. *
  502. * @final
  503. */
  504. public function isFresh(): bool
  505. {
  506. return $this->getTtl() > 0;
  507. }
  508. /**
  509. * Returns true if the response includes headers that can be used to validate
  510. * the response with the origin server using a conditional GET request.
  511. *
  512. * @final
  513. */
  514. public function isValidateable(): bool
  515. {
  516. return $this->headers->has('Last-Modified') || $this->headers->has('ETag');
  517. }
  518. /**
  519. * Marks the response as "private".
  520. *
  521. * It makes the response ineligible for serving other clients.
  522. *
  523. * @return $this
  524. *
  525. * @final
  526. */
  527. public function setPrivate(): object
  528. {
  529. $this->headers->removeCacheControlDirective('public');
  530. $this->headers->addCacheControlDirective('private');
  531. return $this;
  532. }
  533. /**
  534. * Marks the response as "public".
  535. *
  536. * It makes the response eligible for serving other clients.
  537. *
  538. * @return $this
  539. *
  540. * @final
  541. */
  542. public function setPublic(): object
  543. {
  544. $this->headers->addCacheControlDirective('public');
  545. $this->headers->removeCacheControlDirective('private');
  546. return $this;
  547. }
  548. /**
  549. * Marks the response as "immutable".
  550. *
  551. * @return $this
  552. *
  553. * @final
  554. */
  555. public function setImmutable(bool $immutable = true): object
  556. {
  557. if ($immutable) {
  558. $this->headers->addCacheControlDirective('immutable');
  559. } else {
  560. $this->headers->removeCacheControlDirective('immutable');
  561. }
  562. return $this;
  563. }
  564. /**
  565. * Returns true if the response is marked as "immutable".
  566. *
  567. * @final
  568. */
  569. public function isImmutable(): bool
  570. {
  571. return $this->headers->hasCacheControlDirective('immutable');
  572. }
  573. /**
  574. * Returns true if the response must be revalidated by shared caches once it has become stale.
  575. *
  576. * This method indicates that the response must not be served stale by a
  577. * cache in any circumstance without first revalidating with the origin.
  578. * When present, the TTL of the response should not be overridden to be
  579. * greater than the value provided by the origin.
  580. *
  581. * @final
  582. */
  583. public function mustRevalidate(): bool
  584. {
  585. return $this->headers->hasCacheControlDirective('must-revalidate') || $this->headers->hasCacheControlDirective('proxy-revalidate');
  586. }
  587. /**
  588. * Returns the Date header as a DateTime instance.
  589. *
  590. * @throws \RuntimeException When the header is not parseable
  591. *
  592. * @final
  593. */
  594. public function getDate(): ?\DateTimeInterface
  595. {
  596. return $this->headers->getDate('Date');
  597. }
  598. /**
  599. * Sets the Date header.
  600. *
  601. * @return $this
  602. *
  603. * @final
  604. */
  605. public function setDate(\DateTimeInterface $date): object
  606. {
  607. if ($date instanceof \DateTime) {
  608. $date = \DateTimeImmutable::createFromMutable($date);
  609. }
  610. $date = $date->setTimezone(new \DateTimeZone('UTC'));
  611. $this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
  612. return $this;
  613. }
  614. /**
  615. * Returns the age of the response in seconds.
  616. *
  617. * @final
  618. */
  619. public function getAge(): int
  620. {
  621. if (null !== $age = $this->headers->get('Age')) {
  622. return (int) $age;
  623. }
  624. return max(time() - (int) $this->getDate()->format('U'), 0);
  625. }
  626. /**
  627. * Marks the response stale by setting the Age header to be equal to the maximum age of the response.
  628. *
  629. * @return $this
  630. */
  631. public function expire()
  632. {
  633. if ($this->isFresh()) {
  634. $this->headers->set('Age', $this->getMaxAge());
  635. $this->headers->remove('Expires');
  636. }
  637. return $this;
  638. }
  639. /**
  640. * Returns the value of the Expires header as a DateTime instance.
  641. *
  642. * @final
  643. */
  644. public function getExpires(): ?\DateTimeInterface
  645. {
  646. try {
  647. return $this->headers->getDate('Expires');
  648. } catch (\RuntimeException $e) {
  649. // according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past
  650. return \DateTime::createFromFormat('U', time() - 172800);
  651. }
  652. }
  653. /**
  654. * Sets the Expires HTTP header with a DateTime instance.
  655. *
  656. * Passing null as value will remove the header.
  657. *
  658. * @return $this
  659. *
  660. * @final
  661. */
  662. public function setExpires(\DateTimeInterface $date = null): object
  663. {
  664. if (null === $date) {
  665. $this->headers->remove('Expires');
  666. return $this;
  667. }
  668. if ($date instanceof \DateTime) {
  669. $date = \DateTimeImmutable::createFromMutable($date);
  670. }
  671. $date = $date->setTimezone(new \DateTimeZone('UTC'));
  672. $this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
  673. return $this;
  674. }
  675. /**
  676. * Returns the number of seconds after the time specified in the response's Date
  677. * header when the response should no longer be considered fresh.
  678. *
  679. * First, it checks for a s-maxage directive, then a max-age directive, and then it falls
  680. * back on an expires header. It returns null when no maximum age can be established.
  681. *
  682. * @final
  683. */
  684. public function getMaxAge(): ?int
  685. {
  686. if ($this->headers->hasCacheControlDirective('s-maxage')) {
  687. return (int) $this->headers->getCacheControlDirective('s-maxage');
  688. }
  689. if ($this->headers->hasCacheControlDirective('max-age')) {
  690. return (int) $this->headers->getCacheControlDirective('max-age');
  691. }
  692. if (null !== $this->getExpires()) {
  693. return (int) $this->getExpires()->format('U') - (int) $this->getDate()->format('U');
  694. }
  695. return null;
  696. }
  697. /**
  698. * Sets the number of seconds after which the response should no longer be considered fresh.
  699. *
  700. * This methods sets the Cache-Control max-age directive.
  701. *
  702. * @return $this
  703. *
  704. * @final
  705. */
  706. public function setMaxAge(int $value): object
  707. {
  708. $this->headers->addCacheControlDirective('max-age', $value);
  709. return $this;
  710. }
  711. /**
  712. * Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
  713. *
  714. * This methods sets the Cache-Control s-maxage directive.
  715. *
  716. * @return $this
  717. *
  718. * @final
  719. */
  720. public function setSharedMaxAge(int $value): object
  721. {
  722. $this->setPublic();
  723. $this->headers->addCacheControlDirective('s-maxage', $value);
  724. return $this;
  725. }
  726. /**
  727. * Returns the response's time-to-live in seconds.
  728. *
  729. * It returns null when no freshness information is present in the response.
  730. *
  731. * When the responses TTL is <= 0, the response may not be served from cache without first
  732. * revalidating with the origin.
  733. *
  734. * @final
  735. */
  736. public function getTtl(): ?int
  737. {
  738. $maxAge = $this->getMaxAge();
  739. return null !== $maxAge ? $maxAge - $this->getAge() : null;
  740. }
  741. /**
  742. * Sets the response's time-to-live for shared caches in seconds.
  743. *
  744. * This method adjusts the Cache-Control/s-maxage directive.
  745. *
  746. * @return $this
  747. *
  748. * @final
  749. */
  750. public function setTtl(int $seconds): object
  751. {
  752. $this->setSharedMaxAge($this->getAge() + $seconds);
  753. return $this;
  754. }
  755. /**
  756. * Sets the response's time-to-live for private/client caches in seconds.
  757. *
  758. * This method adjusts the Cache-Control/max-age directive.
  759. *
  760. * @return $this
  761. *
  762. * @final
  763. */
  764. public function setClientTtl(int $seconds): object
  765. {
  766. $this->setMaxAge($this->getAge() + $seconds);
  767. return $this;
  768. }
  769. /**
  770. * Returns the Last-Modified HTTP header as a DateTime instance.
  771. *
  772. * @throws \RuntimeException When the HTTP header is not parseable
  773. *
  774. * @final
  775. */
  776. public function getLastModified(): ?\DateTimeInterface
  777. {
  778. return $this->headers->getDate('Last-Modified');
  779. }
  780. /**
  781. * Sets the Last-Modified HTTP header with a DateTime instance.
  782. *
  783. * Passing null as value will remove the header.
  784. *
  785. * @return $this
  786. *
  787. * @final
  788. */
  789. public function setLastModified(\DateTimeInterface $date = null): object
  790. {
  791. if (null === $date) {
  792. $this->headers->remove('Last-Modified');
  793. return $this;
  794. }
  795. if ($date instanceof \DateTime) {
  796. $date = \DateTimeImmutable::createFromMutable($date);
  797. }
  798. $date = $date->setTimezone(new \DateTimeZone('UTC'));
  799. $this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
  800. return $this;
  801. }
  802. /**
  803. * Returns the literal value of the ETag HTTP header.
  804. *
  805. * @final
  806. */
  807. public function getEtag(): ?string
  808. {
  809. return $this->headers->get('ETag');
  810. }
  811. /**
  812. * Sets the ETag value.
  813. *
  814. * @param string|null $etag The ETag unique identifier or null to remove the header
  815. * @param bool $weak Whether you want a weak ETag or not
  816. *
  817. * @return $this
  818. *
  819. * @final
  820. */
  821. public function setEtag(string $etag = null, bool $weak = false): object
  822. {
  823. if (null === $etag) {
  824. $this->headers->remove('Etag');
  825. } else {
  826. if (!str_starts_with($etag, '"')) {
  827. $etag = '"'.$etag.'"';
  828. }
  829. $this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag);
  830. }
  831. return $this;
  832. }
  833. /**
  834. * Sets the response's cache headers (validation and/or expiration).
  835. *
  836. * Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
  837. *
  838. * @return $this
  839. *
  840. * @throws \InvalidArgumentException
  841. *
  842. * @final
  843. */
  844. public function setCache(array $options): object
  845. {
  846. if ($diff = array_diff(array_keys($options), array_keys(self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES))) {
  847. throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
  848. }
  849. if (isset($options['etag'])) {
  850. $this->setEtag($options['etag']);
  851. }
  852. if (isset($options['last_modified'])) {
  853. $this->setLastModified($options['last_modified']);
  854. }
  855. if (isset($options['max_age'])) {
  856. $this->setMaxAge($options['max_age']);
  857. }
  858. if (isset($options['s_maxage'])) {
  859. $this->setSharedMaxAge($options['s_maxage']);
  860. }
  861. foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
  862. if (!$hasValue && isset($options[$directive])) {
  863. if ($options[$directive]) {
  864. $this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
  865. } else {
  866. $this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
  867. }
  868. }
  869. }
  870. if (isset($options['public'])) {
  871. if ($options['public']) {
  872. $this->setPublic();
  873. } else {
  874. $this->setPrivate();
  875. }
  876. }
  877. if (isset($options['private'])) {
  878. if ($options['private']) {
  879. $this->setPrivate();
  880. } else {
  881. $this->setPublic();
  882. }
  883. }
  884. return $this;
  885. }
  886. /**
  887. * Modifies the response so that it conforms to the rules defined for a 304 status code.
  888. *
  889. * This sets the status, removes the body, and discards any headers
  890. * that MUST NOT be included in 304 responses.
  891. *
  892. * @return $this
  893. *
  894. * @see https://tools.ietf.org/html/rfc2616#section-10.3.5
  895. *
  896. * @final
  897. */
  898. public function setNotModified(): object
  899. {
  900. $this->setStatusCode(304);
  901. $this->setContent(null);
  902. // remove headers that MUST NOT be included with 304 Not Modified responses
  903. foreach (['Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified'] as $header) {
  904. $this->headers->remove($header);
  905. }
  906. return $this;
  907. }
  908. /**
  909. * Returns true if the response includes a Vary header.
  910. *
  911. * @final
  912. */
  913. public function hasVary(): bool
  914. {
  915. return null !== $this->headers->get('Vary');
  916. }
  917. /**
  918. * Returns an array of header names given in the Vary header.
  919. *
  920. * @final
  921. */
  922. public function getVary(): array
  923. {
  924. if (!$vary = $this->headers->all('Vary')) {
  925. return [];
  926. }
  927. $ret = [];
  928. foreach ($vary as $item) {
  929. $ret[] = preg_split('/[\s,]+/', $item);
  930. }
  931. return array_merge([], ...$ret);
  932. }
  933. /**
  934. * Sets the Vary header.
  935. *
  936. * @param string|array $headers
  937. * @param bool $replace Whether to replace the actual value or not (true by default)
  938. *
  939. * @return $this
  940. *
  941. * @final
  942. */
  943. public function setVary($headers, bool $replace = true): object
  944. {
  945. $this->headers->set('Vary', $headers, $replace);
  946. return $this;
  947. }
  948. /**
  949. * Determines if the Response validators (ETag, Last-Modified) match
  950. * a conditional value specified in the Request.
  951. *
  952. * If the Response is not modified, it sets the status code to 304 and
  953. * removes the actual content by calling the setNotModified() method.
  954. *
  955. * @final
  956. */
  957. public function isNotModified(Request $request): bool
  958. {
  959. if (!$request->isMethodCacheable()) {
  960. return false;
  961. }
  962. $notModified = false;
  963. $lastModified = $this->headers->get('Last-Modified');
  964. $modifiedSince = $request->headers->get('If-Modified-Since');
  965. if (($ifNoneMatchEtags = $request->getETags()) && (null !== $etag = $this->getEtag())) {
  966. if (0 == strncmp($etag, 'W/', 2)) {
  967. $etag = substr($etag, 2);
  968. }
  969. // Use weak comparison as per https://tools.ietf.org/html/rfc7232#section-3.2.
  970. foreach ($ifNoneMatchEtags as $ifNoneMatchEtag) {
  971. if (0 == strncmp($ifNoneMatchEtag, 'W/', 2)) {
  972. $ifNoneMatchEtag = substr($ifNoneMatchEtag, 2);
  973. }
  974. if ($ifNoneMatchEtag === $etag || '*' === $ifNoneMatchEtag) {
  975. $notModified = true;
  976. break;
  977. }
  978. }
  979. }
  980. // Only do If-Modified-Since date comparison when If-None-Match is not present as per https://tools.ietf.org/html/rfc7232#section-3.3.
  981. elseif ($modifiedSince && $lastModified) {
  982. $notModified = strtotime($modifiedSince) >= strtotime($lastModified);
  983. }
  984. if ($notModified) {
  985. $this->setNotModified();
  986. }
  987. return $notModified;
  988. }
  989. /**
  990. * Is response invalid?
  991. *
  992. * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  993. *
  994. * @final
  995. */
  996. public function isInvalid(): bool
  997. {
  998. return $this->statusCode < 100 || $this->statusCode >= 600;
  999. }
  1000. /**
  1001. * Is response informative?
  1002. *
  1003. * @final
  1004. */
  1005. public function isInformational(): bool
  1006. {
  1007. return $this->statusCode >= 100 && $this->statusCode < 200;
  1008. }
  1009. /**
  1010. * Is response successful?
  1011. *
  1012. * @final
  1013. */
  1014. public function isSuccessful(): bool
  1015. {
  1016. return $this->statusCode >= 200 && $this->statusCode < 300;
  1017. }
  1018. /**
  1019. * Is the response a redirect?
  1020. *
  1021. * @final
  1022. */
  1023. public function isRedirection(): bool
  1024. {
  1025. return $this->statusCode >= 300 && $this->statusCode < 400;
  1026. }
  1027. /**
  1028. * Is there a client error?
  1029. *
  1030. * @final
  1031. */
  1032. public function isClientError(): bool
  1033. {
  1034. return $this->statusCode >= 400 && $this->statusCode < 500;
  1035. }
  1036. /**
  1037. * Was there a server side error?
  1038. *
  1039. * @final
  1040. */
  1041. public function isServerError(): bool
  1042. {
  1043. return $this->statusCode >= 500 && $this->statusCode < 600;
  1044. }
  1045. /**
  1046. * Is the response OK?
  1047. *
  1048. * @final
  1049. */
  1050. public function isOk(): bool
  1051. {
  1052. return 200 === $this->statusCode;
  1053. }
  1054. /**
  1055. * Is the response forbidden?
  1056. *
  1057. * @final
  1058. */
  1059. public function isForbidden(): bool
  1060. {
  1061. return 403 === $this->statusCode;
  1062. }
  1063. /**
  1064. * Is the response a not found error?
  1065. *
  1066. * @final
  1067. */
  1068. public function isNotFound(): bool
  1069. {
  1070. return 404 === $this->statusCode;
  1071. }
  1072. /**
  1073. * Is the response a redirect of some form?
  1074. *
  1075. * @final
  1076. */
  1077. public function isRedirect(string $location = null): bool
  1078. {
  1079. return \in_array($this->statusCode, [201, 301, 302, 303, 307, 308]) && (null === $location ?: $location == $this->headers->get('Location'));
  1080. }
  1081. /**
  1082. * Is the response empty?
  1083. *
  1084. * @final
  1085. */
  1086. public function isEmpty(): bool
  1087. {
  1088. return \in_array($this->statusCode, [204, 304]);
  1089. }
  1090. /**
  1091. * Cleans or flushes output buffers up to target level.
  1092. *
  1093. * Resulting level can be greater than target level if a non-removable buffer has been encountered.
  1094. *
  1095. * @final
  1096. */
  1097. public static function closeOutputBuffers(int $targetLevel, bool $flush): void
  1098. {
  1099. $status = ob_get_status(true);
  1100. $level = \count($status);
  1101. $flags = \PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? \PHP_OUTPUT_HANDLER_FLUSHABLE : \PHP_OUTPUT_HANDLER_CLEANABLE);
  1102. while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
  1103. if ($flush) {
  1104. ob_end_flush();
  1105. flush();
  1106. } else {
  1107. ob_end_clean();
  1108. }
  1109. }
  1110. }
  1111. /**
  1112. * Marks a response as safe according to RFC8674.
  1113. *
  1114. * @see https://tools.ietf.org/html/rfc8674
  1115. */
  1116. public function setContentSafe(bool $safe = true): void
  1117. {
  1118. if ($safe) {
  1119. $this->headers->set('Preference-Applied', 'safe');
  1120. } elseif ('safe' === $this->headers->get('Preference-Applied')) {
  1121. $this->headers->remove('Preference-Applied');
  1122. }
  1123. $this->setVary('Prefer', false);
  1124. }
  1125. /**
  1126. * Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.
  1127. *
  1128. * @see http://support.microsoft.com/kb/323308
  1129. *
  1130. * @final
  1131. */
  1132. protected function ensureIEOverSSLCompatibility(Request $request): void
  1133. {
  1134. if (false !== stripos($this->headers->get('Content-Disposition') ?? '', 'attachment') && 1 == preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT') ?? '', $match) && true === $request->isSecure()) {
  1135. if ((int) preg_replace('/(MSIE )(.*?);/', '$2', $match[0]) < 9) {
  1136. $this->headers->remove('Cache-Control');
  1137. }
  1138. }
  1139. }
  1140. }