‪TYPO3CMS  10.4
Request.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Http;
17 
18 use Psr\Http\Message\RequestInterface;
19 use Psr\Http\Message\StreamInterface;
20 use Psr\Http\Message\UriInterface;
21 
32 class ‪Request extends ‪Message implements RequestInterface
33 {
38  protected ‪$requestTarget;
39 
45  protected ‪$method;
46 
52  protected ‪$supportedMethods = [
53  'CONNECT',
54  'DELETE',
55  'GET',
56  'HEAD',
57  'OPTIONS',
58  'PATCH',
59  'POST',
60  'PUT',
61  'TRACE',
62  // WebDAV methods
63  'COPY',
64  'LOCK',
65  'MKCOL',
66  'MOVE',
67  'PROPFIND',
68  'PROPPATCH',
69  'REPORT',
70  'UNLOCK',
71  // Custom methods
72  'PURGE',
73  'BAN'
74  ];
75 
80  protected ‪$uri;
81 
91  public function ‪__construct(‪$uri = null, ‪$method = null, ‪$body = 'php://input', array ‪$headers = [])
92  {
93 
94  // Build a streamable object for the body
95  if (‪$body !== null && !is_string(‪$body) && !is_resource(‪$body) && !‪$body instanceof StreamInterface) {
96  throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717271);
97  }
98 
99  if (‪$body !== null && !‪$body instanceof StreamInterface) {
100  ‪$body = new ‪Stream(‪$body);
101  }
102 
103  if (is_string(‪$uri)) {
104  ‪$uri = new ‪Uri(‪$uri);
105  }
106 
107  if (!‪$uri instanceof UriInterface && ‪$uri !== null) {
108  throw new \InvalidArgumentException('Invalid URI provided; must be null, a string, or a UriInterface instance', 1436717272);
109  }
110 
112 
113  $this->method = ‪$method;
114  $this->uri = ‪$uri;
115  $this->body = ‪$body;
118  $this->headers = ‪$headers;
119  }
120 
146  public function ‪getHeaders()
147  {
148  ‪$headers = parent::getHeaders();
149  if (!$this->‪hasHeader('host') && ($this->uri && $this->uri->getHost())) {
150  ‪$headers['host'] = [$this->‪getHostFromUri()];
151  }
152  return ‪$headers;
153  }
154 
169  public function ‪getHeader($header)
170  {
171  if (!$this->‪hasHeader($header) && strtolower($header) === 'host' && ($this->uri && $this->uri->getHost())) {
172  return [$this->‪getHostFromUri()];
173  }
174  return parent::getHeader($header);
175  }
176 
182  protected function ‪getHostFromUri()
183  {
184  $host = $this->uri->getHost();
185  $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
186  return $host;
187  }
188 
205  public function ‪getRequestTarget()
206  {
207  if ($this->requestTarget !== null) {
209  }
210  if (!$this->uri) {
211  return '/';
212  }
213  $target = $this->uri->getPath();
214 
215  if ($this->uri->getQuery()) {
216  $target .= '?' . $this->uri->getQuery();
217  }
218 
219  if (empty($target)) {
220  $target = '/';
221  }
222  return $target;
223  }
224 
243  public function ‪withRequestTarget(‪$requestTarget)
244  {
245  if (preg_match('#\s#', ‪$requestTarget)) {
246  throw new \InvalidArgumentException('Invalid request target provided which contains whitespaces.', 1436717273);
247  }
248  $clonedObject = clone $this;
249  $clonedObject->requestTarget = ‪$requestTarget;
250  return $clonedObject;
251  }
252 
258  public function ‪getMethod()
259  {
260  return !empty($this->method) ? $this->method : 'GET';
261  }
262 
278  public function ‪withMethod(‪$method)
279  {
280  $clonedObject = clone $this;
281  $clonedObject->method = ‪$method;
282  return $clonedObject;
283  }
284 
294  public function ‪getUri()
295  {
296  return ‪$this->uri;
297  }
298 
330  public function ‪withUri(UriInterface ‪$uri, $preserveHost = false)
331  {
332  $clonedObject = clone $this;
333  $clonedObject->uri = ‪$uri;
334 
335  if ($preserveHost) {
336  return $clonedObject;
337  }
338 
339  if (!‪$uri->getHost()) {
340  return $clonedObject;
341  }
342 
343  $host = ‪$uri->getHost();
344 
345  if (‪$uri->getPort()) {
346  $host .= ':' . ‪$uri->getPort();
347  }
348 
349  $clonedObject->lowercasedHeaderNames['host'] = 'Host';
350  $clonedObject->headers['Host'] = [$host];
351  return $clonedObject;
352  }
353 
360  protected function ‪validateMethod(‪$method)
361  {
362  if (‪$method !== null) {
363  if (!is_string(‪$method)) {
364  $methodAsString = is_object(‪$method) ? get_class(‪$method) : gettype(‪$method);
365  throw new \InvalidArgumentException('Unsupported HTTP method "' . $methodAsString . '".', 1436717274);
366  }
367  ‪$method = strtoupper(‪$method);
368  if (!in_array(‪$method, $this->supportedMethods, true)) {
369  throw new \InvalidArgumentException('Unsupported HTTP method "' . ‪$method . '".', 1436717275);
370  }
371  }
372  }
373 }
‪TYPO3\CMS\Core\Http\Request\$requestTarget
‪string null $requestTarget
Definition: Request.php:37
‪TYPO3\CMS\Core\Http\Request\getHeader
‪string[] getHeader($header)
Definition: Request.php:165
‪TYPO3\CMS\Core\Http\Request\withUri
‪static withUri(UriInterface $uri, $preserveHost=false)
Definition: Request.php:326
‪TYPO3\CMS\Core\Http\Message\assertHeaders
‪assertHeaders(array $headers)
Definition: Message.php:317
‪TYPO3\CMS\Core\Http\Request\getHostFromUri
‪string getHostFromUri()
Definition: Request.php:178
‪TYPO3\CMS\Core\Http\Request\withRequestTarget
‪static withRequestTarget($requestTarget)
Definition: Request.php:239
‪TYPO3\CMS\Core\Http\Request\$uri
‪UriInterface $uri
Definition: Request.php:76
‪TYPO3\CMS\Core\Http\Message\hasHeader
‪bool hasHeader($name)
Definition: Message.php:123
‪TYPO3\CMS\Core\Http\Message
Definition: Message.php:30
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Http\Request\$method
‪string $method
Definition: Request.php:43
‪TYPO3\CMS\Core\Http\Request\__construct
‪__construct($uri=null, $method=null, $body='php://input', array $headers=[])
Definition: Request.php:87
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Http\Request\getMethod
‪string getMethod()
Definition: Request.php:254
‪TYPO3\CMS\Core\Http\Request\withMethod
‪static withMethod($method)
Definition: Request.php:274
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:51
‪TYPO3\CMS\Core\Http\Request\getHeaders
‪array getHeaders()
Definition: Request.php:142
‪TYPO3\CMS\Core\Http\Request\getUri
‪Psr Http Message UriInterface getUri()
Definition: Request.php:290
‪TYPO3\CMS\Core\Http\Request\$supportedMethods
‪array $supportedMethods
Definition: Request.php:49
‪TYPO3\CMS\Core\Http\Request\validateMethod
‪validateMethod($method)
Definition: Request.php:356
‪TYPO3\CMS\Core\Http\Message\$lowercasedHeaderNames
‪array $lowercasedHeaderNames
Definition: Message.php:46
‪TYPO3\CMS\Core\Http\Message\filterHeaders
‪array filterHeaders(array $originalHeaders)
Definition: Message.php:338
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:33
‪TYPO3\CMS\Core\Http\Request\getRequestTarget
‪string getRequestTarget()
Definition: Request.php:201
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:40
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18