1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation;
13:
14: 15: 16: 17: 18: 19: 20:
21: class Response
22: {
23: const HTTP_CONTINUE = 100;
24: const HTTP_SWITCHING_PROTOCOLS = 101;
25: const HTTP_PROCESSING = 102;
26: const HTTP_OK = 200;
27: const HTTP_CREATED = 201;
28: const HTTP_ACCEPTED = 202;
29: const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
30: const HTTP_NO_CONTENT = 204;
31: const HTTP_RESET_CONTENT = 205;
32: const HTTP_PARTIAL_CONTENT = 206;
33: const HTTP_MULTI_STATUS = 207;
34: const HTTP_ALREADY_REPORTED = 208;
35: const HTTP_IM_USED = 226;
36: const HTTP_MULTIPLE_CHOICES = 300;
37: const HTTP_MOVED_PERMANENTLY = 301;
38: const HTTP_FOUND = 302;
39: const HTTP_SEE_OTHER = 303;
40: const HTTP_NOT_MODIFIED = 304;
41: const HTTP_USE_PROXY = 305;
42: const HTTP_RESERVED = 306;
43: const HTTP_TEMPORARY_REDIRECT = 307;
44: const HTTP_PERMANENTLY_REDIRECT = 308;
45: const HTTP_BAD_REQUEST = 400;
46: const HTTP_UNAUTHORIZED = 401;
47: const HTTP_PAYMENT_REQUIRED = 402;
48: const HTTP_FORBIDDEN = 403;
49: const HTTP_NOT_FOUND = 404;
50: const HTTP_METHOD_NOT_ALLOWED = 405;
51: const HTTP_NOT_ACCEPTABLE = 406;
52: const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
53: const HTTP_REQUEST_TIMEOUT = 408;
54: const HTTP_CONFLICT = 409;
55: const HTTP_GONE = 410;
56: const HTTP_LENGTH_REQUIRED = 411;
57: const HTTP_PRECONDITION_FAILED = 412;
58: const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
59: const HTTP_REQUEST_URI_TOO_LONG = 414;
60: const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
61: const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
62: const HTTP_EXPECTATION_FAILED = 417;
63: const HTTP_I_AM_A_TEAPOT = 418;
64: const HTTP_UNPROCESSABLE_ENTITY = 422;
65: const HTTP_LOCKED = 423;
66: const HTTP_FAILED_DEPENDENCY = 424;
67: const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425;
68: const HTTP_UPGRADE_REQUIRED = 426;
69: const HTTP_PRECONDITION_REQUIRED = 428;
70: const HTTP_TOO_MANY_REQUESTS = 429;
71: const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
72: const HTTP_INTERNAL_SERVER_ERROR = 500;
73: const HTTP_NOT_IMPLEMENTED = 501;
74: const HTTP_BAD_GATEWAY = 502;
75: const HTTP_SERVICE_UNAVAILABLE = 503;
76: const HTTP_GATEWAY_TIMEOUT = 504;
77: const HTTP_VERSION_NOT_SUPPORTED = 505;
78: const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;
79: const HTTP_INSUFFICIENT_STORAGE = 507;
80: const HTTP_LOOP_DETECTED = 508;
81: const HTTP_NOT_EXTENDED = 510;
82: const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
83:
84: 85: 86:
87: public $headers;
88:
89: 90: 91:
92: protected $content;
93:
94: 95: 96:
97: protected $version;
98:
99: 100: 101:
102: protected $statusCode;
103:
104: 105: 106:
107: protected $statusText;
108:
109: 110: 111:
112: protected $charset;
113:
114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
125: public static $statusTexts = array(
126: 100 => 'Continue',
127: 101 => 'Switching Protocols',
128: 102 => 'Processing',
129: 200 => 'OK',
130: 201 => 'Created',
131: 202 => 'Accepted',
132: 203 => 'Non-Authoritative Information',
133: 204 => 'No Content',
134: 205 => 'Reset Content',
135: 206 => 'Partial Content',
136: 207 => 'Multi-Status',
137: 208 => 'Already Reported',
138: 226 => 'IM Used',
139: 300 => 'Multiple Choices',
140: 301 => 'Moved Permanently',
141: 302 => 'Found',
142: 303 => 'See Other',
143: 304 => 'Not Modified',
144: 305 => 'Use Proxy',
145: 306 => 'Reserved',
146: 307 => 'Temporary Redirect',
147: 308 => 'Permanent Redirect',
148: 400 => 'Bad Request',
149: 401 => 'Unauthorized',
150: 402 => 'Payment Required',
151: 403 => 'Forbidden',
152: 404 => 'Not Found',
153: 405 => 'Method Not Allowed',
154: 406 => 'Not Acceptable',
155: 407 => 'Proxy Authentication Required',
156: 408 => 'Request Timeout',
157: 409 => 'Conflict',
158: 410 => 'Gone',
159: 411 => 'Length Required',
160: 412 => 'Precondition Failed',
161: 413 => 'Request Entity Too Large',
162: 414 => 'Request-URI Too Long',
163: 415 => 'Unsupported Media Type',
164: 416 => 'Requested Range Not Satisfiable',
165: 417 => 'Expectation Failed',
166: 418 => 'I\'m a teapot',
167: 422 => 'Unprocessable Entity',
168: 423 => 'Locked',
169: 424 => 'Failed Dependency',
170: 425 => 'Reserved for WebDAV advanced collections expired proposal',
171: 426 => 'Upgrade Required',
172: 428 => 'Precondition Required',
173: 429 => 'Too Many Requests',
174: 431 => 'Request Header Fields Too Large',
175: 500 => 'Internal Server Error',
176: 501 => 'Not Implemented',
177: 502 => 'Bad Gateway',
178: 503 => 'Service Unavailable',
179: 504 => 'Gateway Timeout',
180: 505 => 'HTTP Version Not Supported',
181: 506 => 'Variant Also Negotiates (Experimental)',
182: 507 => 'Insufficient Storage',
183: 508 => 'Loop Detected',
184: 510 => 'Not Extended',
185: 511 => 'Network Authentication Required',
186: );
187:
188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
199: public function __construct($content = '', $status = 200, $headers = array())
200: {
201: $this->headers = new ResponseHeaderBag($headers);
202: $this->setContent($content);
203: $this->setStatusCode($status);
204: $this->setProtocolVersion('1.0');
205: if (!$this->headers->has('Date')) {
206: $this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
207: }
208: }
209:
210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
224: public static function create($content = '', $status = 200, $headers = array())
225: {
226: return new static($content, $status, $headers);
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
240: public function __toString()
241: {
242: return
243: sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
244: $this->headers."\r\n".
245: $this->getContent();
246: }
247:
248: 249: 250:
251: public function __clone()
252: {
253: $this->headers = clone $this->headers;
254: }
255:
256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
267: public function prepare(Request $request)
268: {
269: $headers = $this->headers;
270:
271: if ($this->isInformational() || $this->isEmpty()) {
272: $this->setContent(null);
273: $headers->remove('Content-Type');
274: $headers->remove('Content-Length');
275: } else {
276:
277: if (!$headers->has('Content-Type')) {
278: $format = $request->getRequestFormat();
279: if (null !== $format && $mimeType = $request->getMimeType($format)) {
280: $headers->set('Content-Type', $mimeType);
281: }
282: }
283:
284:
285: $charset = $this->charset ?: 'UTF-8';
286: if (!$headers->has('Content-Type')) {
287: $headers->set('Content-Type', 'text/html; charset='.$charset);
288: } elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
289:
290: $headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
291: }
292:
293:
294: if ($headers->has('Transfer-Encoding')) {
295: $headers->remove('Content-Length');
296: }
297:
298: if ($request->isMethod('HEAD')) {
299:
300: $length = $headers->get('Content-Length');
301: $this->setContent(null);
302: if ($length) {
303: $headers->set('Content-Length', $length);
304: }
305: }
306: }
307:
308:
309: if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
310: $this->setProtocolVersion('1.1');
311: }
312:
313:
314: if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) {
315: $this->headers->set('pragma', 'no-cache');
316: $this->headers->set('expires', -1);
317: }
318:
319: $this->ensureIEOverSSLCompatibility($request);
320:
321: return $this;
322: }
323:
324: 325: 326: 327: 328:
329: public function sendHeaders()
330: {
331:
332: if (headers_sent()) {
333: return $this;
334: }
335:
336:
337: header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);
338:
339:
340: foreach ($this->headers->allPreserveCase() as $name => $values) {
341: foreach ($values as $value) {
342: header($name.': '.$value, false, $this->statusCode);
343: }
344: }
345:
346:
347: foreach ($this->headers->getCookies() as $cookie) {
348: setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
349: }
350:
351: return $this;
352: }
353:
354: 355: 356: 357: 358:
359: public function sendContent()
360: {
361: echo $this->content;
362:
363: return $this;
364: }
365:
366: 367: 368: 369: 370: 371: 372:
373: public function send()
374: {
375: $this->sendHeaders();
376: $this->sendContent();
377:
378: if (function_exists('fastcgi_finish_request')) {
379: fastcgi_finish_request();
380: } elseif ('cli' !== PHP_SAPI) {
381: static::closeOutputBuffers(0, true);
382: }
383:
384: return $this;
385: }
386:
387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399:
400: public function setContent($content)
401: {
402: if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
403: throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content)));
404: }
405:
406: $this->content = (string) $content;
407:
408: return $this;
409: }
410:
411: 412: 413: 414: 415: 416: 417:
418: public function getContent()
419: {
420: return $this->content;
421: }
422:
423: 424: 425: 426: 427: 428: 429: 430: 431:
432: public function setProtocolVersion($version)
433: {
434: $this->version = $version;
435:
436: return $this;
437: }
438:
439: 440: 441: 442: 443: 444: 445:
446: public function getProtocolVersion()
447: {
448: return $this->version;
449: }
450:
451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465:
466: public function setStatusCode($code, $text = null)
467: {
468: $this->statusCode = $code = (int) $code;
469: if ($this->isInvalid()) {
470: throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
471: }
472:
473: if (null === $text) {
474: $this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : '';
475:
476: return $this;
477: }
478:
479: if (false === $text) {
480: $this->statusText = '';
481:
482: return $this;
483: }
484:
485: $this->statusText = $text;
486:
487: return $this;
488: }
489:
490: 491: 492: 493: 494: 495: 496:
497: public function getStatusCode()
498: {
499: return $this->statusCode;
500: }
501:
502: 503: 504: 505: 506: 507: 508: 509: 510:
511: public function setCharset($charset)
512: {
513: $this->charset = $charset;
514:
515: return $this;
516: }
517:
518: 519: 520: 521: 522: 523: 524:
525: public function getCharset()
526: {
527: return $this->charset;
528: }
529:
530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542:
543: public function isCacheable()
544: {
545: if (!in_array($this->statusCode, array(200, 203, 300, 301, 302, 404, 410))) {
546: return false;
547: }
548:
549: if ($this->headers->hasCacheControlDirective('no-store') || $this->headers->getCacheControlDirective('private')) {
550: return false;
551: }
552:
553: return $this->isValidateable() || $this->isFresh();
554: }
555:
556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566:
567: public function isFresh()
568: {
569: return $this->getTtl() > 0;
570: }
571:
572: 573: 574: 575: 576: 577: 578: 579:
580: public function isValidateable()
581: {
582: return $this->headers->has('Last-Modified') || $this->headers->has('ETag');
583: }
584:
585: 586: 587: 588: 589: 590: 591: 592: 593:
594: public function setPrivate()
595: {
596: $this->headers->removeCacheControlDirective('public');
597: $this->headers->addCacheControlDirective('private');
598:
599: return $this;
600: }
601:
602: 603: 604: 605: 606: 607: 608: 609: 610:
611: public function setPublic()
612: {
613: $this->headers->addCacheControlDirective('public');
614: $this->headers->removeCacheControlDirective('private');
615:
616: return $this;
617: }
618:
619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630:
631: public function mustRevalidate()
632: {
633: return $this->headers->hasCacheControlDirective('must-revalidate') || $this->headers->has('proxy-revalidate');
634: }
635:
636: 637: 638: 639: 640: 641: 642: 643: 644:
645: public function getDate()
646: {
647: return $this->headers->getDate('Date', new \DateTime());
648: }
649:
650: 651: 652: 653: 654: 655: 656: 657: 658:
659: public function setDate(\DateTime $date)
660: {
661: $date->setTimezone(new \DateTimeZone('UTC'));
662: $this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
663:
664: return $this;
665: }
666:
667: 668: 669: 670: 671:
672: public function getAge()
673: {
674: if (null !== $age = $this->headers->get('Age')) {
675: return (int) $age;
676: }
677:
678: return max(time() - $this->getDate()->format('U'), 0);
679: }
680:
681: 682: 683: 684: 685: 686: 687:
688: public function expire()
689: {
690: if ($this->isFresh()) {
691: $this->headers->set('Age', $this->getMaxAge());
692: }
693:
694: return $this;
695: }
696:
697: 698: 699: 700: 701: 702: 703:
704: public function getExpires()
705: {
706: try {
707: return $this->headers->getDate('Expires');
708: } catch (\RuntimeException $e) {
709:
710: return \DateTime::createFromFormat(DATE_RFC2822, 'Sat, 01 Jan 00 00:00:00 +0000');
711: }
712: }
713:
714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724:
725: public function setExpires(\DateTime $date = null)
726: {
727: if (null === $date) {
728: $this->headers->remove('Expires');
729: } else {
730: $date = clone $date;
731: $date->setTimezone(new \DateTimeZone('UTC'));
732: $this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
733: }
734:
735: return $this;
736: }
737:
738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748:
749: public function getMaxAge()
750: {
751: if ($this->headers->hasCacheControlDirective('s-maxage')) {
752: return (int) $this->headers->getCacheControlDirective('s-maxage');
753: }
754:
755: if ($this->headers->hasCacheControlDirective('max-age')) {
756: return (int) $this->headers->getCacheControlDirective('max-age');
757: }
758:
759: if (null !== $this->getExpires()) {
760: return $this->getExpires()->format('U') - $this->getDate()->format('U');
761: }
762: }
763:
764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774:
775: public function setMaxAge($value)
776: {
777: $this->headers->addCacheControlDirective('max-age', $value);
778:
779: return $this;
780: }
781:
782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792:
793: public function setSharedMaxAge($value)
794: {
795: $this->setPublic();
796: $this->headers->addCacheControlDirective('s-maxage', $value);
797:
798: return $this;
799: }
800:
801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812:
813: public function getTtl()
814: {
815: if (null !== $maxAge = $this->getMaxAge()) {
816: return $maxAge - $this->getAge();
817: }
818: }
819:
820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830:
831: public function setTtl($seconds)
832: {
833: $this->setSharedMaxAge($this->getAge() + $seconds);
834:
835: return $this;
836: }
837:
838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848:
849: public function setClientTtl($seconds)
850: {
851: $this->setMaxAge($this->getAge() + $seconds);
852:
853: return $this;
854: }
855:
856: 857: 858: 859: 860: 861: 862: 863: 864:
865: public function getLastModified()
866: {
867: return $this->headers->getDate('Last-Modified');
868: }
869:
870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880:
881: public function setLastModified(\DateTime $date = null)
882: {
883: if (null === $date) {
884: $this->headers->remove('Last-Modified');
885: } else {
886: $date = clone $date;
887: $date->setTimezone(new \DateTimeZone('UTC'));
888: $this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
889: }
890:
891: return $this;
892: }
893:
894: 895: 896: 897: 898: 899: 900:
901: public function getEtag()
902: {
903: return $this->headers->get('ETag');
904: }
905:
906: 907: 908: 909: 910: 911: 912: 913: 914: 915:
916: public function setEtag($etag = null, $weak = false)
917: {
918: if (null === $etag) {
919: $this->headers->remove('Etag');
920: } else {
921: if (0 !== strpos($etag, '"')) {
922: $etag = '"'.$etag.'"';
923: }
924:
925: $this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag);
926: }
927:
928: return $this;
929: }
930:
931: 932: 933: 934: 935: 936: 937: 938: 939: 940: 941: 942: 943:
944: public function setCache(array $options)
945: {
946: if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) {
947: throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff))));
948: }
949:
950: if (isset($options['etag'])) {
951: $this->setEtag($options['etag']);
952: }
953:
954: if (isset($options['last_modified'])) {
955: $this->setLastModified($options['last_modified']);
956: }
957:
958: if (isset($options['max_age'])) {
959: $this->setMaxAge($options['max_age']);
960: }
961:
962: if (isset($options['s_maxage'])) {
963: $this->setSharedMaxAge($options['s_maxage']);
964: }
965:
966: if (isset($options['public'])) {
967: if ($options['public']) {
968: $this->setPublic();
969: } else {
970: $this->setPrivate();
971: }
972: }
973:
974: if (isset($options['private'])) {
975: if ($options['private']) {
976: $this->setPrivate();
977: } else {
978: $this->setPublic();
979: }
980: }
981:
982: return $this;
983: }
984:
985: 986: 987: 988: 989: 990: 991: 992: 993: 994: 995: 996:
997: public function setNotModified()
998: {
999: $this->setStatusCode(304);
1000: $this->setContent(null);
1001:
1002:
1003: foreach (array('Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified') as $header) {
1004: $this->headers->remove($header);
1005: }
1006:
1007: return $this;
1008: }
1009:
1010: 1011: 1012: 1013: 1014: 1015: 1016:
1017: public function hasVary()
1018: {
1019: return null !== $this->headers->get('Vary');
1020: }
1021:
1022: 1023: 1024: 1025: 1026: 1027: 1028:
1029: public function getVary()
1030: {
1031: if (!$vary = $this->headers->get('Vary', null, false)) {
1032: return array();
1033: }
1034:
1035: $ret = array();
1036: foreach ($vary as $item) {
1037: $ret = array_merge($ret, preg_split('/[\s,]+/', $item));
1038: }
1039:
1040: return $ret;
1041: }
1042:
1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052:
1053: public function setVary($headers, $replace = true)
1054: {
1055: $this->headers->set('Vary', $headers, $replace);
1056:
1057: return $this;
1058: }
1059:
1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072:
1073: public function isNotModified(Request $request)
1074: {
1075: if (!$request->isMethodSafe()) {
1076: return false;
1077: }
1078:
1079: $notModified = false;
1080: $lastModified = $this->headers->get('Last-Modified');
1081: $modifiedSince = $request->headers->get('If-Modified-Since');
1082:
1083: if ($etags = $request->getEtags()) {
1084: $notModified = in_array($this->getEtag(), $etags) || in_array('*', $etags);
1085: }
1086:
1087: if ($modifiedSince && $lastModified) {
1088: $notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified);
1089: }
1090:
1091: if ($notModified) {
1092: $this->setNotModified();
1093: }
1094:
1095: return $notModified;
1096: }
1097:
1098:
1099: 1100: 1101: 1102: 1103: 1104: 1105:
1106: public function isInvalid()
1107: {
1108: return $this->statusCode < 100 || $this->statusCode >= 600;
1109: }
1110:
1111: 1112: 1113: 1114: 1115: 1116: 1117:
1118: public function isInformational()
1119: {
1120: return $this->statusCode >= 100 && $this->statusCode < 200;
1121: }
1122:
1123: 1124: 1125: 1126: 1127: 1128: 1129:
1130: public function isSuccessful()
1131: {
1132: return $this->statusCode >= 200 && $this->statusCode < 300;
1133: }
1134:
1135: 1136: 1137: 1138: 1139: 1140: 1141:
1142: public function isRedirection()
1143: {
1144: return $this->statusCode >= 300 && $this->statusCode < 400;
1145: }
1146:
1147: 1148: 1149: 1150: 1151: 1152: 1153:
1154: public function isClientError()
1155: {
1156: return $this->statusCode >= 400 && $this->statusCode < 500;
1157: }
1158:
1159: 1160: 1161: 1162: 1163: 1164: 1165:
1166: public function isServerError()
1167: {
1168: return $this->statusCode >= 500 && $this->statusCode < 600;
1169: }
1170:
1171: 1172: 1173: 1174: 1175: 1176: 1177:
1178: public function isOk()
1179: {
1180: return 200 === $this->statusCode;
1181: }
1182:
1183: 1184: 1185: 1186: 1187: 1188: 1189:
1190: public function isForbidden()
1191: {
1192: return 403 === $this->statusCode;
1193: }
1194:
1195: 1196: 1197: 1198: 1199: 1200: 1201:
1202: public function isNotFound()
1203: {
1204: return 404 === $this->statusCode;
1205: }
1206:
1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215:
1216: public function isRedirect($location = null)
1217: {
1218: return in_array($this->statusCode, array(201, 301, 302, 303, 307, 308)) && (null === $location ?: $location == $this->headers->get('Location'));
1219: }
1220:
1221: 1222: 1223: 1224: 1225: 1226: 1227:
1228: public function isEmpty()
1229: {
1230: return in_array($this->statusCode, array(204, 304));
1231: }
1232:
1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240:
1241: public static function closeOutputBuffers($targetLevel, $flush)
1242: {
1243: $status = ob_get_status(true);
1244: $level = count($status);
1245:
1246: while ($level-- > $targetLevel
1247: && (!empty($status[$level]['del'])
1248: || (isset($status[$level]['flags'])
1249: && ($status[$level]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)
1250: && ($status[$level]['flags'] & ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE))
1251: )
1252: )
1253: ) {
1254: if ($flush) {
1255: ob_end_flush();
1256: } else {
1257: ob_end_clean();
1258: }
1259: }
1260: }
1261:
1262: 1263: 1264: 1265: 1266:
1267: protected function ensureIEOverSSLCompatibility(Request $request)
1268: {
1269: if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) == 1 && true === $request->isSecure()) {
1270: if (intval(preg_replace("/(MSIE )(.*?);/", "$2", $match[0])) < 9) {
1271: $this->headers->remove('Cache-Control');
1272: }
1273: }
1274: }
1275: }
1276: