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