[TASK] Streamline HTTP Response codes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / Response.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Web;
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
17 use TYPO3\CMS\Core\Page\PageRenderer;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20
21 /**
22 * A web specific response implementation
23 *
24 * @api
25 */
26 class Response extends \TYPO3\CMS\Extbase\Mvc\Response
27 {
28 /**
29 * The HTTP headers which will be sent in the response
30 *
31 * @var array
32 */
33 protected $headers = [];
34
35 /**
36 * Additional header tags
37 *
38 * @var array
39 */
40 protected $additionalHeaderData = [];
41
42 /**
43 * The HTTP status code
44 *
45 * @var int
46 */
47 protected $statusCode;
48
49 /**
50 * The HTTP status message
51 *
52 * @var string
53 */
54 protected $statusMessage = 'OK';
55
56 /**
57 * The Request which generated the Response
58 *
59 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
60 */
61 protected $request;
62
63 /**
64 * The standardized and other important HTTP Status messages
65 *
66 * @var array
67 */
68 protected $statusMessages = [
69 // INFORMATIONAL CODES
70 100 => 'Continue',
71 101 => 'Switching Protocols',
72 102 => 'Processing',
73 103 => 'Early Hints',
74 // SUCCESS CODES
75 200 => 'OK',
76 201 => 'Created',
77 202 => 'Accepted',
78 203 => 'Non-Authoritative Information',
79 204 => 'No Content',
80 205 => 'Reset Content',
81 206 => 'Partial Content',
82 207 => 'Multi-status',
83 208 => 'Already Reported',
84 226 => 'IM Used',
85 // REDIRECTION CODES
86 300 => 'Multiple Choices',
87 301 => 'Moved Permanently',
88 302 => 'Found',
89 303 => 'See Other',
90 304 => 'Not Modified',
91 305 => 'Use Proxy',
92 306 => 'Switch Proxy', // Deprecated
93 307 => 'Temporary Redirect',
94 308 => 'Permanent Redirect',
95 // CLIENT ERROR
96 400 => 'Bad Request',
97 401 => 'Unauthorized',
98 402 => 'Payment Required',
99 403 => 'Forbidden',
100 404 => 'Not Found',
101 405 => 'Method Not Allowed',
102 406 => 'Not Acceptable',
103 407 => 'Proxy Authentication Required',
104 408 => 'Request Timeout',
105 409 => 'Conflict',
106 410 => 'Gone',
107 411 => 'Length Required',
108 412 => 'Precondition Failed',
109 413 => 'Request Entity Too Large',
110 414 => 'URI Too Long',
111 415 => 'Unsupported Media Type',
112 416 => 'Requested range not satisfiable',
113 417 => 'Expectation Failed',
114 418 => 'I\'m a teapot',
115 422 => 'Unprocessable Entity',
116 423 => 'Locked',
117 424 => 'Failed Dependency',
118 425 => 'Unordered Collection',
119 426 => 'Upgrade Required',
120 428 => 'Precondition Required',
121 429 => 'Too Many Requests',
122 431 => 'Request Header Fields Too Large',
123 451 => 'Unavailable For Legal Reasons',
124 // SERVER ERROR
125 500 => 'Internal Server Error',
126 501 => 'Not Implemented',
127 502 => 'Bad Gateway',
128 503 => 'Service Unavailable',
129 504 => 'Gateway Time-out',
130 505 => 'HTTP Version not supported',
131 506 => 'Variant Also Negotiates',
132 507 => 'Insufficient Storage',
133 508 => 'Loop Detected',
134 509 => 'Bandwidth Limit Exceeded',
135 511 => 'Network Authentication Required',
136 ];
137
138 /**
139 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
140 */
141 protected $environmentService;
142
143 /**
144 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
145 */
146 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
147 {
148 $this->environmentService = $environmentService;
149 }
150
151 /**
152 * Sets the HTTP status code and (optionally) a customized message.
153 *
154 * @param int $code The status code
155 * @param string $message If specified, this message is sent instead of the standard message
156 * @throws \InvalidArgumentException if the specified status code is not valid
157 * @api
158 */
159 public function setStatus($code, $message = null)
160 {
161 if (!is_int($code)) {
162 throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
163 }
164 if ($message === null && !isset($this->statusMessages[$code])) {
165 throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
166 }
167 $this->statusCode = $code;
168 $this->statusMessage = $message ?? $this->statusMessages[$code];
169 }
170
171 /**
172 * Returns status code and status message.
173 *
174 * @return string The status code and status message, eg. "404 Not Found
175 * @api
176 */
177 public function getStatus()
178 {
179 return $this->statusCode . ' ' . $this->statusMessage;
180 }
181
182 /**
183 * Returns the status code, if not set, uses the OK status code 200
184 *
185 * @return int
186 * @internal only use for backend module handling
187 */
188 public function getStatusCode()
189 {
190 return $this->statusCode ?: 200;
191 }
192
193 /**
194 * Sets the specified HTTP header
195 *
196 * @param string $name Name of the header, for example "Location", "Content-Description" etc.
197 * @param mixed $value The value of the given header
198 * @param bool $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
199 * @throws \InvalidArgumentException
200 * @api
201 */
202 public function setHeader($name, $value, $replaceExistingHeader = true)
203 {
204 if (strtoupper(substr($name, 0, 4)) === 'HTTP') {
205 throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
206 }
207 if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
208 $this->headers[$name] = [$value];
209 } else {
210 $this->headers[$name][] = $value;
211 }
212 }
213
214 /**
215 * Returns the HTTP headers - including the status header - of this web response
216 *
217 * @return string[] The HTTP headers
218 * @api
219 */
220 public function getHeaders()
221 {
222 $preparedHeaders = [];
223 if ($this->statusCode !== null) {
224 $protocolVersion = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
225 $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
226 $preparedHeaders[] = $statusHeader;
227 }
228 foreach ($this->headers as $name => $values) {
229 foreach ($values as $value) {
230 $preparedHeaders[] = $name . ': ' . $value;
231 }
232 }
233 return $preparedHeaders;
234 }
235
236 /**
237 * Returns the HTTP headers grouped by name without the status header
238 *
239 * @return array all headers set for this request
240 * @internal only used within TYPO3 Core to convert to PSR-7 response headers
241 */
242 public function getUnpreparedHeaders(): array
243 {
244 return $this->headers;
245 }
246
247 /**
248 * Sends the HTTP headers.
249 *
250 * If headers have already been sent, this method fails silently.
251 *
252 * @api
253 */
254 public function sendHeaders()
255 {
256 if (headers_sent() === true) {
257 return;
258 }
259 foreach ($this->getHeaders() as $header) {
260 header($header);
261 }
262 }
263
264 /**
265 * Renders and sends the whole web response
266 *
267 * @api
268 */
269 public function send()
270 {
271 $this->sendHeaders();
272 if ($this->content !== null) {
273 echo $this->getContent();
274 }
275 }
276
277 /**
278 * Adds an additional header data (something like
279 * '<script src="myext/Resources/JavaScript/my.js" type="text/javascript"></script>'
280 * )
281 *
282 * @TODO The workround and the $request member should be removed again, once the PageRender does support non-cached USER_INTs
283 * @param string $additionalHeaderData The value additional header
284 * @throws \InvalidArgumentException
285 * @api
286 */
287 public function addAdditionalHeaderData($additionalHeaderData)
288 {
289 if (!is_string($additionalHeaderData)) {
290 throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
291 }
292 if ($this->request->isCached()) {
293 /** @var PageRenderer $pageRenderer */
294 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
295 $pageRenderer->addHeaderData($additionalHeaderData);
296 } else {
297 $this->additionalHeaderData[] = $additionalHeaderData;
298 }
299 }
300
301 /**
302 * Returns the additional header data
303 *
304 * @return array The additional header data
305 * @api
306 */
307 public function getAdditionalHeaderData()
308 {
309 return $this->additionalHeaderData;
310 }
311
312 /**
313 * @param \TYPO3\CMS\Extbase\Mvc\Web\Request $request
314 */
315 public function setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
316 {
317 $this->request = $request;
318 }
319
320 /**
321 * @return \TYPO3\CMS\Extbase\Mvc\Web\Request
322 */
323 public function getRequest()
324 {
325 return $this->request;
326 }
327
328 /**
329 * Sends additional headers and returns the content
330 *
331 * @return string|null
332 */
333 public function shutdown()
334 {
335 if (!empty($this->getAdditionalHeaderData())) {
336 $this->getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $this->getAdditionalHeaderData());
337 }
338 $this->sendHeaders();
339 return parent::shutdown();
340 }
341
342 /**
343 * @return TypoScriptFrontendController
344 */
345 protected function getTypoScriptFrontendController()
346 {
347 return $GLOBALS['TSFE'];
348 }
349 }