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