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