TYPO3 CMS  TYPO3_7-6
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 
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  'UNLOCK'
69  ];
70 
75  protected $uri;
76 
86  public function __construct($uri = null, $method = null, $body = 'php://input', array $headers = [])
87  {
88 
89  // Build a streamable object for the body
90  if (!is_string($body) && !is_resource($body) && !$body instanceof StreamInterface) {
91  throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717271);
92  }
93 
94  if (!$body instanceof StreamInterface) {
95  $body = new Stream($body);
96  }
97 
98  if (is_string($uri)) {
99  $uri = new Uri($uri);
100  }
101 
102  if (!$uri instanceof UriInterface && $uri !== null) {
103  throw new \InvalidArgumentException('Invalid URI provided; must be null, a string, or a UriInterface instance', 1436717272);
104  }
105 
106  $this->validateMethod($method);
107 
108  $this->method = $method;
109  $this->uri = $uri;
110  $this->body = $body;
111  list($this->lowercasedHeaderNames, $headers) = $this->filterHeaders($headers);
112  $this->assertHeaders($headers);
113  $this->headers = $headers;
114  }
115 
141  public function getHeaders()
142  {
143  $headers = parent::getHeaders();
144  if (!$this->hasHeader('host') && ($this->uri && $this->uri->getHost())) {
145  $headers['host'] = [$this->getHostFromUri()];
146  }
147  return $headers;
148  }
149 
164  public function getHeader($header)
165  {
166  if (!$this->hasHeader($header) && strtolower($header) === 'host' && ($this->uri && $this->uri->getHost())) {
167  return [$this->getHostFromUri()];
168  }
169  return parent::getHeader($header);
170  }
171 
177  protected function getHostFromUri()
178  {
179  $host = $this->uri->getHost();
180  $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
181  return $host;
182  }
183 
200  public function getRequestTarget()
201  {
202  if ($this->requestTarget !== null) {
203  return $this->requestTarget;
204  }
205  if (!$this->uri) {
206  return '/';
207  }
208  $target = $this->uri->getPath();
209 
210  if ($this->uri->getQuery()) {
211  $target .= '?' . $this->uri->getQuery();
212  }
213 
214  if (empty($target)) {
215  $target = '/';
216  }
217  return $target;
218  }
219 
239  {
240  if (preg_match('#\s#', $requestTarget)) {
241  throw new \InvalidArgumentException('Invalid request target provided which contains whitespaces.', 1436717273);
242  }
243  $clonedObject = clone $this;
244  $clonedObject->requestTarget = $requestTarget;
245  return $clonedObject;
246  }
247 
253  public function getMethod()
254  {
255  return !empty($this->method) ? $this->method : 'GET';
256  }
257 
273  public function withMethod($method)
274  {
275  $clonedObject = clone $this;
276  $clonedObject->method = $method;
277  return $clonedObject;
278  }
279 
289  public function getUri()
290  {
291  return $this->uri;
292  }
293 
325  public function withUri(UriInterface $uri, $preserveHost = false)
326  {
327  $clonedObject = clone $this;
328  $clonedObject->uri = $uri;
329 
330  if ($preserveHost) {
331  return $clonedObject;
332  }
333 
334  if (!$uri->getHost()) {
335  return $clonedObject;
336  }
337 
338  $host = $uri->getHost();
339 
340  if ($uri->getPort()) {
341  $host .= ':' . $uri->getPort();
342  }
343 
344  $clonedObject->lowercasedHeaderNames['host'] = 'Host';
345  $clonedObject->headers['Host'] = [$host];
346  return $clonedObject;
347  }
348 
355  protected function validateMethod($method)
356  {
357  if ($method !== null) {
358  if (!is_string($method)) {
359  $methodAsString = is_object($method) ? get_class($method) : gettype($method);
360  throw new \InvalidArgumentException('Unsupported HTTP method "' . $methodAsString . '".', 1436717274);
361  }
362  $method = strtoupper($method);
363  if (!in_array($method, $this->supportedMethods, true)) {
364  throw new \InvalidArgumentException('Unsupported HTTP method "' . $method . '".', 1436717275);
365  }
366  }
367  }
368 }
assertHeaders(array $headers)
Definition: Message.php:318
withUri(UriInterface $uri, $preserveHost=false)
Definition: Request.php:325
withRequestTarget($requestTarget)
Definition: Request.php:238
__construct($uri=null, $method=null, $body='php://input', array $headers=[])
Definition: Request.php:86
$host
Definition: server.php:37
filterHeaders(array $originalHeaders)
Definition: Message.php:339