TYPO3  7.6
typo3/sysext/core/Classes/Http/Response.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Http;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
27 class Response extends Message implements ResponseInterface
28 {
33  protected $statusCode;
34 
39  protected $reasonPhrase = '';
40 
45  protected $availableStatusCodes = array(
46  // INFORMATIONAL CODES
47  100 => 'Continue',
48  101 => 'Switching Protocols',
49  102 => 'Processing',
50  // SUCCESS CODES
51  200 => 'OK',
52  201 => 'Created',
53  202 => 'Accepted',
54  203 => 'Non-Authoritative Information',
55  204 => 'No Content',
56  205 => 'Reset Content',
57  206 => 'Partial Content',
58  207 => 'Multi-status',
59  208 => 'Already Reported',
60  // REDIRECTION CODES
61  300 => 'Multiple Choices',
62  301 => 'Moved Permanently',
63  302 => 'Found',
64  303 => 'See Other',
65  304 => 'Not Modified',
66  305 => 'Use Proxy',
67  306 => 'Switch Proxy', // Deprecated
68  307 => 'Temporary Redirect',
69  // CLIENT ERROR
70  400 => 'Bad Request',
71  401 => 'Unauthorized',
72  402 => 'Payment Required',
73  403 => 'Forbidden',
74  404 => 'Not Found',
75  405 => 'Method Not Allowed',
76  406 => 'Not Acceptable',
77  407 => 'Proxy Authentication Required',
78  408 => 'Request Time-out',
79  409 => 'Conflict',
80  410 => 'Gone',
81  411 => 'Length Required',
82  412 => 'Precondition Failed',
83  413 => 'Request Entity Too Large',
84  414 => 'Request-URI Too Large',
85  415 => 'Unsupported Media Type',
86  416 => 'Requested range not satisfiable',
87  417 => 'Expectation Failed',
88  418 => 'I\'m a teapot',
89  422 => 'Unprocessable Entity',
90  423 => 'Locked',
91  424 => 'Failed Dependency',
92  425 => 'Unordered Collection',
93  426 => 'Upgrade Required',
94  428 => 'Precondition Required',
95  429 => 'Too Many Requests',
96  431 => 'Request Header Fields Too Large',
97  // SERVER ERROR
98  500 => 'Internal Server Error',
99  501 => 'Not Implemented',
100  502 => 'Bad Gateway',
101  503 => 'Service Unavailable',
102  504 => 'Gateway Time-out',
103  505 => 'HTTP Version not supported',
104  506 => 'Variant Also Negotiates',
105  507 => 'Insufficient Storage',
106  508 => 'Loop Detected',
107  509 => 'Bandwidth Limit Exceeded',
108  511 => 'Network Authentication Required'
109  );
110 
119  public function __construct($body = 'php://temp', $statusCode = 200, $headers = array())
120  {
121  // Build a streamable object for the body
122  if (!is_string($body) && !is_resource($body) && !$body instanceof StreamInterface) {
123  throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717277);
124  }
125 
126  if (!$body instanceof StreamInterface) {
127  $body = new Stream($body, 'rw');
128  }
129  $this->body = $body;
130 
131  if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($statusCode) === false || !array_key_exists((int)$statusCode, $this->availableStatusCodes)) {
132  throw new \InvalidArgumentException('The given status code is not a valid HTTP status code.', 1436717278);
133  }
134  $this->statusCode = (int)$statusCode;
135 
136  $this->reasonPhrase = $this->availableStatusCodes[$this->statusCode];
137  list($this->headerNames, $headers) = $this->filterHeaders($headers);
138  $this->assertHeaders($headers);
139  $this->headers = $headers;
140  }
141 
150  public function getStatusCode()
151  {
152  return $this->statusCode;
153  }
154 
176  public function withStatus($code, $reasonPhrase = '')
177  {
178  if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($code) === false || !array_key_exists((int)$code, $this->availableStatusCodes)) {
179  throw new \InvalidArgumentException('The given status code is not a valid HTTP status code', 1436717279);
180  }
181  $clonedObject = clone $this;
182  $clonedObject->statusCode = $code;
183  $clonedObject->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : $this->availableStatusCodes[$code];
184  return $clonedObject;
185  }
186 
200  public function getReasonPhrase()
201  {
202  return $this->reasonPhrase;
203  }
204 }