‪TYPO3CMS  ‪main
Request.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Http\Message\RequestInterface;
21 use Psr\Http\Message\StreamInterface;
22 use Psr\Http\Message\UriInterface;
23 
34 class ‪Request extends ‪Message implements RequestInterface
35 {
39  protected ?string ‪$requestTarget = null;
40 
44  protected string ‪$method = 'GET';
45 
51  protected array ‪$supportedMethods = [
52  'CONNECT',
53  'DELETE',
54  'GET',
55  'HEAD',
56  'OPTIONS',
57  'PATCH',
58  'POST',
59  'PUT',
60  'TRACE',
61  // WebDAV methods
62  'COPY',
63  'LOCK',
64  'MKCOL',
65  'MOVE',
66  'PROPFIND',
67  'PROPPATCH',
68  'REPORT',
69  'UNLOCK',
70  // Custom methods
71  'PURGE',
72  'BAN',
73  ];
74 
80  protected ?UriInterface ‪$uri;
81 
91  public function ‪__construct(UriInterface|string|null ‪$uri = null, string ‪$method = 'GET', ‪$body = 'php://input', array ‪$headers = [])
92  {
93  // Upcast the body to a streamable object, or error
94  // if it's an invalid type.
95  if (‪$body instanceof StreamInterface) {
96  $this->body = ‪$body;
97  } else {
98  $this->body = match (get_debug_type(‪$body)) {
99  'string', 'resource (stream)' => new ‪Stream(‪$body),
100  'null' => null,
101  default => throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717271),
102  };
103  }
104 
105  if (is_string(‪$uri)) {
106  ‪$uri = new ‪Uri(‪$uri);
107  }
108 
109  $this->‪validateMethod($method);
110 
111  $this->method = ‪$method;
112  $this->uri = ‪$uri;
115  $this->headers = ‪$headers;
116  }
117 
145  public function ‪getHeaders(): array
146  {
147  ‪$headers = parent::getHeaders();
148  if (!$this->‪hasHeader('host') && ($this->uri?->getHost())) {
149  ‪$headers['host'] = [$this->‪getHostFromUri()];
150  }
151  return ‪$headers;
152  }
153 
168  public function ‪getHeader(string $name): array
169  {
170  if (!$this->‪hasHeader($name) && strtolower($name) === 'host' && ($this->uri?->getHost())) {
171  return [$this->‪getHostFromUri()];
172  }
173  return parent::getHeader($name);
174  }
175 
179  protected function ‪getHostFromUri(): string
180  {
181  $host = $this->uri->getHost();
182  $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
183  return $host;
184  }
185 
200  public function ‪getRequestTarget(): string
201  {
202  if ($this->requestTarget !== null) {
204  }
205 
206  if (!$this->uri) {
207  return '/';
208  }
209 
210  $target = $this->uri->getPath();
211 
212  if ($this->uri->getQuery()) {
213  $target .= '?' . $this->uri->getQuery();
214  }
215 
216  if (empty($target)) {
217  $target = '/';
218  }
219  return $target;
220  }
221 
237  public function ‪withRequestTarget(mixed ‪$requestTarget): static
238  {
239  if (preg_match('#\s#', ‪$requestTarget)) {
240  throw new \InvalidArgumentException('Invalid request target provided which contains whitespaces.', 1436717273);
241  }
242  $clonedObject = clone $this;
243  $clonedObject->requestTarget = ‪$requestTarget;
244  return $clonedObject;
245  }
246 
250  public function ‪getMethod(): string
251  {
252  return ‪$this->method;
253  }
254 
269  public function ‪withMethod(string ‪$method): static
270  {
271  $clonedObject = clone $this;
272  $clonedObject->method = ‪$method;
273  return $clonedObject;
274  }
275 
285  public function ‪getUri(): UriInterface
286  {
287  return ‪$this->uri;
288  }
289 
320  public function ‪withUri(UriInterface ‪$uri, bool $preserveHost = false): static
321  {
322  $clonedObject = clone $this;
323  $clonedObject->uri = ‪$uri;
324 
325  if ($preserveHost) {
326  return $clonedObject;
327  }
328 
329  if (!‪$uri->getHost()) {
330  return $clonedObject;
331  }
332 
333  $host = ‪$uri->getHost();
334 
335  if (‪$uri->getPort()) {
336  $host .= ':' . ‪$uri->getPort();
337  }
338 
339  $clonedObject->lowercasedHeaderNames['host'] = 'Host';
340  $clonedObject->headers['Host'] = [$host];
341  return $clonedObject;
342  }
343 
349  protected function ‪validateMethod(?string ‪$method): void
350  {
351  if (is_null(‪$method)) {
352  return;
353  }
354 
355  ‪$method = strtoupper(‪$method);
356  if (!in_array(‪$method, $this->supportedMethods, true)) {
357  throw new \InvalidArgumentException('Unsupported HTTP method "' . ‪$method . '".', 1436717275);
358  }
359  }
360 }
‪TYPO3\CMS\Core\Http\Request\getRequestTarget
‪getRequestTarget()
Definition: Request.php:200
‪TYPO3\CMS\Core\Http\Request\__construct
‪__construct(UriInterface|string|null $uri=null, string $method='GET', $body='php://input', array $headers=[])
Definition: Request.php:91
‪TYPO3\CMS\Core\Http\Message\assertHeaders
‪assertHeaders(array $headers)
Definition: Message.php:319
‪TYPO3\CMS\Core\Http\Request\getUri
‪UriInterface getUri()
Definition: Request.php:285
‪TYPO3\CMS\Core\Http\Request\$uri
‪UriInterface $uri
Definition: Request.php:80
‪TYPO3\CMS\Core\Http\Request\withUri
‪withUri(UriInterface $uri, bool $preserveHost=false)
Definition: Request.php:320
‪TYPO3\CMS\Core\Http\Request\getMethod
‪getMethod()
Definition: Request.php:250
‪TYPO3\CMS\Core\Http\Message
Definition: Message.php:32
‪TYPO3\CMS\Core\Http\Request\getHostFromUri
‪getHostFromUri()
Definition: Request.php:179
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Core\Http\Request\$requestTarget
‪string $requestTarget
Definition: Request.php:39
‪TYPO3\CMS\Core\Http\Request\$method
‪string $method
Definition: Request.php:44
‪TYPO3\CMS\Core\Http\Request\withRequestTarget
‪withRequestTarget(mixed $requestTarget)
Definition: Request.php:237
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Http\Request\validateMethod
‪validateMethod(?string $method)
Definition: Request.php:349
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:53
‪TYPO3\CMS\Core\Http\Request\getHeaders
‪array getHeaders()
Definition: Request.php:145
‪TYPO3\CMS\Core\Http\Request\$supportedMethods
‪array $supportedMethods
Definition: Request.php:51
‪TYPO3\CMS\Core\Http\Request\withMethod
‪withMethod(string $method)
Definition: Request.php:269
‪TYPO3\CMS\Core\Http\Request\getHeader
‪string[] getHeader(string $name)
Definition: Request.php:168
‪TYPO3\CMS\Core\Http\Message\$lowercasedHeaderNames
‪array $lowercasedHeaderNames
Definition: Message.php:48
‪TYPO3\CMS\Core\Http\Message\filterHeaders
‪array filterHeaders(array $originalHeaders)
Definition: Message.php:340
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:35
‪TYPO3\CMS\Core\Http\Message\hasHeader
‪bool hasHeader(string $name)
Definition: Message.php:127
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:42
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18