krz/vox-populi

A Tumblr web client.

clone: git clone https://gitbay.org/krz/vox-populi.git

main: vendor/guzzlehttp/psr7/src/Request.php · raw

  1<?php
  2namespace GuzzleHttp\Psr7;
  3
  4use InvalidArgumentException;
  5use Psr\Http\Message\RequestInterface;
  6use Psr\Http\Message\StreamInterface;
  7use Psr\Http\Message\UriInterface;
  8
  9/**
 10 * PSR-7 request implementation.
 11 */
 12class Request implements RequestInterface
 13{
 14    use MessageTrait;
 15
 16    /** @var string */
 17    private $method;
 18
 19    /** @var null|string */
 20    private $requestTarget;
 21
 22    /** @var UriInterface */
 23    private $uri;
 24
 25    /**
 26     * @param string                               $method  HTTP method
 27     * @param string|UriInterface                  $uri     URI
 28     * @param array                                $headers Request headers
 29     * @param string|null|resource|StreamInterface $body    Request body
 30     * @param string                               $version Protocol version
 31     */
 32    public function __construct(
 33        $method,
 34        $uri,
 35        array $headers = [],
 36        $body = null,
 37        $version = '1.1'
 38    ) {
 39        $this->assertMethod($method);
 40        if (!($uri instanceof UriInterface)) {
 41            $uri = new Uri($uri);
 42        }
 43
 44        $this->method = strtoupper($method);
 45        $this->uri = $uri;
 46        $this->setHeaders($headers);
 47        $this->protocol = $version;
 48
 49        if (!isset($this->headerNames['host'])) {
 50            $this->updateHostFromUri();
 51        }
 52
 53        if ($body !== '' && $body !== null) {
 54            $this->stream = stream_for($body);
 55        }
 56    }
 57
 58    public function getRequestTarget()
 59    {
 60        if ($this->requestTarget !== null) {
 61            return $this->requestTarget;
 62        }
 63
 64        $target = $this->uri->getPath();
 65        if ($target == '') {
 66            $target = '/';
 67        }
 68        if ($this->uri->getQuery() != '') {
 69            $target .= '?' . $this->uri->getQuery();
 70        }
 71
 72        return $target;
 73    }
 74
 75    public function withRequestTarget($requestTarget)
 76    {
 77        if (preg_match('#\s#', $requestTarget)) {
 78            throw new InvalidArgumentException(
 79                'Invalid request target provided; cannot contain whitespace'
 80            );
 81        }
 82
 83        $new = clone $this;
 84        $new->requestTarget = $requestTarget;
 85        return $new;
 86    }
 87
 88    public function getMethod()
 89    {
 90        return $this->method;
 91    }
 92
 93    public function withMethod($method)
 94    {
 95        $this->assertMethod($method);
 96        $new = clone $this;
 97        $new->method = strtoupper($method);
 98        return $new;
 99    }
100
101    public function getUri()
102    {
103        return $this->uri;
104    }
105
106    public function withUri(UriInterface $uri, $preserveHost = false)
107    {
108        if ($uri === $this->uri) {
109            return $this;
110        }
111
112        $new = clone $this;
113        $new->uri = $uri;
114
115        if (!$preserveHost || !isset($this->headerNames['host'])) {
116            $new->updateHostFromUri();
117        }
118
119        return $new;
120    }
121
122    private function updateHostFromUri()
123    {
124        $host = $this->uri->getHost();
125
126        if ($host == '') {
127            return;
128        }
129
130        if (($port = $this->uri->getPort()) !== null) {
131            $host .= ':' . $port;
132        }
133
134        if (isset($this->headerNames['host'])) {
135            $header = $this->headerNames['host'];
136        } else {
137            $header = 'Host';
138            $this->headerNames['host'] = 'Host';
139        }
140        // Ensure Host is the first header.
141        // See: http://tools.ietf.org/html/rfc7230#section-5.4
142        $this->headers = [$header => [$host]] + $this->headers;
143    }
144
145    private function assertMethod($method)
146    {
147        if (!is_string($method) || $method === '') {
148            throw new \InvalidArgumentException('Method must be a non-empty string.');
149        }
150    }
151}