‪TYPO3CMS  9.5
Response.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
31  protected ‪$headers = [];
32 
38  protected ‪$additionalHeaderData = [];
39 
45  protected ‪$statusCode;
46 
52  protected ‪$statusMessage = 'OK';
53 
59  protected ‪$request;
60 
66  protected ‪$statusMessages = [
67  // INFORMATIONAL CODES
68  100 => 'Continue',
69  101 => 'Switching Protocols',
70  102 => 'Processing',
71  103 => 'Early Hints',
72  // SUCCESS CODES
73  200 => 'OK',
74  201 => 'Created',
75  202 => 'Accepted',
76  203 => 'Non-Authoritative Information',
77  204 => 'No Content',
78  205 => 'Reset Content',
79  206 => 'Partial Content',
80  207 => 'Multi-status',
81  208 => 'Already Reported',
82  226 => 'IM Used',
83  // REDIRECTION CODES
84  300 => 'Multiple Choices',
85  301 => 'Moved Permanently',
86  302 => 'Found',
87  303 => 'See Other',
88  304 => 'Not Modified',
89  305 => 'Use Proxy',
90  306 => 'Switch Proxy', // Deprecated
91  307 => 'Temporary Redirect',
92  308 => 'Permanent Redirect',
93  // CLIENT ERROR
94  400 => 'Bad Request',
95  401 => 'Unauthorized',
96  402 => 'Payment Required',
97  403 => 'Forbidden',
98  404 => 'Not Found',
99  405 => 'Method Not Allowed',
100  406 => 'Not Acceptable',
101  407 => 'Proxy Authentication Required',
102  408 => 'Request Timeout',
103  409 => 'Conflict',
104  410 => 'Gone',
105  411 => 'Length Required',
106  412 => 'Precondition Failed',
107  413 => 'Request Entity Too Large',
108  414 => 'URI Too Long',
109  415 => 'Unsupported Media Type',
110  416 => 'Requested range not satisfiable',
111  417 => 'Expectation Failed',
112  418 => 'I\'m a teapot',
113  422 => 'Unprocessable Entity',
114  423 => 'Locked',
115  424 => 'Failed Dependency',
116  425 => 'Unordered Collection',
117  426 => 'Upgrade Required',
118  428 => 'Precondition Required',
119  429 => 'Too Many Requests',
120  431 => 'Request Header Fields Too Large',
121  451 => 'Unavailable For Legal Reasons',
122  // SERVER ERROR
123  500 => 'Internal Server Error',
124  501 => 'Not Implemented',
125  502 => 'Bad Gateway',
126  503 => 'Service Unavailable',
127  504 => 'Gateway Time-out',
128  505 => 'HTTP Version not supported',
129  506 => 'Variant Also Negotiates',
130  507 => 'Insufficient Storage',
131  508 => 'Loop Detected',
132  509 => 'Bandwidth Limit Exceeded',
133  511 => 'Network Authentication Required',
134  ];
135 
139  protected ‪$environmentService;
140 
145  public function ‪injectEnvironmentService(\‪TYPO3\CMS\‪Extbase\Service\EnvironmentService ‪$environmentService)
146  {
147  $this->environmentService = ‪$environmentService;
148  }
149 
157  public function ‪setStatus($code, $message = null)
158  {
159  if (!is_int($code)) {
160  throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
161  }
162  if ($message === null && !isset($this->statusMessages[$code])) {
163  throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
164  }
165  $this->statusCode = $code;
166  $this->statusMessage = $message ?? $this->statusMessages[$code];
167  }
168 
174  public function ‪getStatus()
175  {
176  return $this->statusCode . ' ' . ‪$this->statusMessage;
177  }
178 
185  public function ‪getStatusCode()
186  {
187  return $this->statusCode ?: 200;
188  }
189 
198  public function ‪setHeader($name, $value, $replaceExistingHeader = true)
199  {
200  if (stripos($name, 'HTTP') === 0) {
201  throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
202  }
203  if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
204  $this->headers[$name] = [$value];
205  } else {
206  $this->headers[$name][] = $value;
207  }
208  }
209 
215  public function ‪getHeaders()
216  {
217  $preparedHeaders = [];
218  if ($this->statusCode !== null) {
219  $protocolVersion = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
220  $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . ‪$this->statusMessage;
221  $preparedHeaders[] = $statusHeader;
222  }
223  foreach ($this->headers as $name => $values) {
224  foreach ($values as $value) {
225  $preparedHeaders[] = $name . ': ' . $value;
226  }
227  }
228  return $preparedHeaders;
229  }
230 
237  public function ‪getUnpreparedHeaders(): array
238  {
239  return ‪$this->headers;
240  }
241 
247  public function ‪sendHeaders()
248  {
249  if (headers_sent() === true) {
250  return;
251  }
252  foreach ($this->‪getHeaders() as $header) {
253  header($header);
254  }
255  }
256 
260  public function ‪send()
261  {
262  $this->‪sendHeaders();
263  if ($this->content !== null) {
264  echo $this->‪getContent();
265  }
266  }
267 
278  {
279  if (!is_string(‪$additionalHeaderData)) {
280  throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype(‪$additionalHeaderData) . ' given.', 1237370877);
281  }
282  if ($this->request->isCached()) {
284  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
285  $pageRenderer->addHeaderData(‪$additionalHeaderData);
286  } else {
287  $this->additionalHeaderData[] = ‪$additionalHeaderData;
288  }
289  }
290 
296  public function ‪getAdditionalHeaderData()
297  {
299  }
300 
305  public function ‪setRequest(\‪TYPO3\CMS\‪Extbase\Mvc\Web\‪Request ‪$request)
306  {
307  $this->request = ‪$request;
308  }
309 
314  public function ‪getRequest()
315  {
316  return ‪$this->request;
317  }
318 
325  public function ‪shutdown()
326  {
327  if (!empty($this->‪getAdditionalHeaderData())) {
328  $this->‪getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $this->‪getAdditionalHeaderData());
329  }
330  $this->‪sendHeaders();
331  return parent::shutdown();
332  }
333 
337  protected function ‪getTypoScriptFrontendController()
338  {
339  return ‪$GLOBALS['TSFE'];
340  }
341 }
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$statusCode
‪int $statusCode
Definition: Response.php:42
‪TYPO3\CMS\Extbase\Mvc\Web\Response\setStatus
‪setStatus($code, $message=null)
Definition: Response.php:150
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Web\Response\shutdown
‪string null shutdown()
Definition: Response.php:318
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getHeaders
‪string[] getHeaders()
Definition: Response.php:208
‪TYPO3\CMS\Extbase\Mvc\Response\getContent
‪string getContent()
Definition: Response.php:51
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Web\Response\injectEnvironmentService
‪injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
Definition: Response.php:138
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getStatusCode
‪int getStatusCode()
Definition: Response.php:178
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getUnpreparedHeaders
‪array getUnpreparedHeaders()
Definition: Response.php:230
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getStatus
‪string getStatus()
Definition: Response.php:167
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$headers
‪array $headers
Definition: Response.php:30
‪TYPO3\CMS\Extbase\Mvc\Response
Definition: Response.php:21
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$statusMessage
‪string $statusMessage
Definition: Response.php:48
‪TYPO3\CMS\Extbase\Mvc\Web\Response\setHeader
‪setHeader($name, $value, $replaceExistingHeader=true)
Definition: Response.php:191
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$request
‪TYPO3 CMS Extbase Mvc Web Request $request
Definition: Response.php:54
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$statusMessages
‪array $statusMessages
Definition: Response.php:60
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:25
‪TYPO3\CMS\Extbase\Mvc\Web\Response\send
‪send()
Definition: Response.php:253
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:21
‪TYPO3\CMS\Extbase\Mvc\Web\Response\addAdditionalHeaderData
‪addAdditionalHeaderData($additionalHeaderData)
Definition: Response.php:270
‪TYPO3\CMS\Extbase\Mvc\Web
Definition: AbstractRequestHandler.php:2
‪TYPO3\CMS\Extbase\Mvc\Web\Response\sendHeaders
‪sendHeaders()
Definition: Response.php:240
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getAdditionalHeaderData
‪array getAdditionalHeaderData()
Definition: Response.php:289
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$environmentService
‪TYPO3 CMS Extbase Service EnvironmentService $environmentService
Definition: Response.php:132
‪TYPO3\CMS\Extbase\Mvc\Web\Response\$additionalHeaderData
‪array $additionalHeaderData
Definition: Response.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getRequest
‪TYPO3 CMS Extbase Mvc Web Request getRequest()
Definition: Response.php:307
‪TYPO3\CMS\Extbase\Mvc\Web\Response\setRequest
‪setRequest(\TYPO3\CMS\Extbase\Mvc\Web\Request $request)
Definition: Response.php:298
‪TYPO3\CMS\Extbase\Mvc\Web\Response\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: Response.php:330