‪TYPO3CMS  9.5
Request.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 
17 use Psr\Http\Message\RequestInterface;
18 use Psr\Http\Message\StreamInterface;
19 use Psr\Http\Message\UriInterface;
20 
31 class ‪Request extends ‪Message implements RequestInterface
32 {
37  protected ‪$requestTarget;
38 
44  protected ‪$method;
45 
51  protected ‪$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 
79  protected ‪$uri;
80 
90  public function ‪__construct(‪$uri = null, ‪$method = null, ‪$body = 'php://input', array ‪$headers = [])
91  {
92 
93  // Build a streamable object for the body
94  if (!is_string(‪$body) && !is_resource(‪$body) && !‪$body instanceof StreamInterface) {
95  throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717271);
96  }
97 
98  if (!‪$body instanceof StreamInterface) {
100  }
101 
102  if (is_string(‪$uri)) {
103  ‪$uri = new ‪Uri(‪$uri);
104  }
105 
106  if (!‪$uri instanceof UriInterface && ‪$uri !== null) {
107  throw new \InvalidArgumentException('Invalid URI provided; must be null, a string, or a UriInterface instance', 1436717272);
108  }
109 
111 
112  $this->method = ‪$method;
113  $this->uri = ‪$uri;
114  $this->body = ‪$body;
115  list($this->lowercasedHeaderNames, ‪$headers) = $this->‪filterHeaders(‪$headers);
117  $this->headers = ‪$headers;
118  }
119 
145  public function ‪getHeaders()
146  {
147  ‪$headers = parent::getHeaders();
148  if (!$this->‪hasHeader('host') && ($this->uri && $this->uri->getHost())) {
149  ‪$headers['host'] = [$this->‪getHostFromUri()];
150  }
151  return ‪$headers;
152  }
153 
168  public function ‪getHeader($header)
169  {
170  if (!$this->‪hasHeader($header) && strtolower($header) === 'host' && ($this->uri && $this->uri->getHost())) {
171  return [$this->‪getHostFromUri()];
172  }
173  return parent::getHeader($header);
174  }
175 
181  protected function ‪getHostFromUri()
182  {
183  $host = $this->uri->getHost();
184  $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
185  return $host;
186  }
187 
204  public function ‪getRequestTarget()
205  {
206  if ($this->requestTarget !== null) {
208  }
209  if (!$this->uri) {
210  return '/';
211  }
212  $target = $this->uri->getPath();
213 
214  if ($this->uri->getQuery()) {
215  $target .= '?' . $this->uri->getQuery();
216  }
217 
218  if (empty($target)) {
219  $target = '/';
220  }
221  return $target;
222  }
223 
242  public function ‪withRequestTarget(‪$requestTarget)
243  {
244  if (preg_match('#\s#', ‪$requestTarget)) {
245  throw new \InvalidArgumentException('Invalid request target provided which contains whitespaces.', 1436717273);
246  }
247  $clonedObject = clone $this;
248  $clonedObject->requestTarget = ‪$requestTarget;
249  return $clonedObject;
250  }
251 
257  public function ‪getMethod()
258  {
259  return !empty($this->method) ? $this->method : 'GET';
260  }
261 
277  public function ‪withMethod(‪$method)
278  {
279  $clonedObject = clone $this;
280  $clonedObject->method = ‪$method;
281  return $clonedObject;
282  }
283 
293  public function ‪getUri()
294  {
295  return ‪$this->uri;
296  }
297 
329  public function ‪withUri(UriInterface ‪$uri, $preserveHost = false)
330  {
331  $clonedObject = clone $this;
332  $clonedObject->uri = ‪$uri;
333 
334  if ($preserveHost) {
335  return $clonedObject;
336  }
337 
338  if (!‪$uri->getHost()) {
339  return $clonedObject;
340  }
341 
342  $host = ‪$uri->getHost();
343 
344  if (‪$uri->getPort()) {
345  $host .= ':' . ‪$uri->getPort();
346  }
347 
348  $clonedObject->lowercasedHeaderNames['host'] = 'Host';
349  $clonedObject->headers['Host'] = [$host];
350  return $clonedObject;
351  }
352 
359  protected function ‪validateMethod(‪$method)
360  {
361  if (‪$method !== null) {
362  if (!is_string(‪$method)) {
363  $methodAsString = is_object(‪$method) ? get_class(‪$method) : gettype(‪$method);
364  throw new \InvalidArgumentException('Unsupported HTTP method "' . $methodAsString . '".', 1436717274);
365  }
366  ‪$method = strtoupper(‪$method);
367  if (!in_array(‪$method, $this->supportedMethods, true)) {
368  throw new \InvalidArgumentException('Unsupported HTTP method "' . ‪$method . '".', 1436717275);
369  }
370  }
371  }
372 }
‪TYPO3\CMS\Core\Http\Request\$requestTarget
‪string null $requestTarget
Definition: Request.php:36
‪TYPO3\CMS\Core\Http\Request\getHeader
‪string[] getHeader($header)
Definition: Request.php:164
‪TYPO3\CMS\Core\Http\Request\withUri
‪static withUri(UriInterface $uri, $preserveHost=false)
Definition: Request.php:325
‪TYPO3\CMS\Core\Http\Message\assertHeaders
‪assertHeaders(array $headers)
Definition: Message.php:313
‪TYPO3\CMS\Core\Http\Request\getHostFromUri
‪string getHostFromUri()
Definition: Request.php:177
‪TYPO3\CMS\Core\Http\Request\withRequestTarget
‪static withRequestTarget($requestTarget)
Definition: Request.php:238
‪TYPO3\CMS\Core\Http\Request\$uri
‪UriInterface $uri
Definition: Request.php:75
‪TYPO3\CMS\Core\Http\Message\hasHeader
‪bool hasHeader($name)
Definition: Message.php:122
‪TYPO3\CMS\Core\Http\Message
Definition: Message.php:29
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:27
‪TYPO3\CMS\Core\Http\Request\$method
‪string $method
Definition: Request.php:42
‪TYPO3\CMS\Core\Http\Request\__construct
‪__construct($uri=null, $method=null, $body='php://input', array $headers=[])
Definition: Request.php:86
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:28
‪TYPO3\CMS\Core\Http\Request\getMethod
‪string getMethod()
Definition: Request.php:253
‪TYPO3\CMS\Core\Http\Request\withMethod
‪static withMethod($method)
Definition: Request.php:273
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:50
‪TYPO3\CMS\Core\Http\Request\getHeaders
‪array getHeaders()
Definition: Request.php:141
‪TYPO3\CMS\Core\Http\Request\getUri
‪Psr Http Message UriInterface getUri()
Definition: Request.php:289
‪TYPO3\CMS\Core\Http\Request\$supportedMethods
‪array $supportedMethods
Definition: Request.php:48
‪TYPO3\CMS\Core\Http\Request\validateMethod
‪validateMethod($method)
Definition: Request.php:355
‪TYPO3\CMS\Core\Http\Message\filterHeaders
‪array filterHeaders(array $originalHeaders)
Definition: Message.php:334
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:32
‪TYPO3\CMS\Core\Http\Request\getRequestTarget
‪string getRequestTarget()
Definition: Request.php:200
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:39
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3