← All changes
|
vendor_prefixed/guzzlehttp/psr7/src/MessageTrait.php
+69
-6
27.7
→
trunk
View file →
| @@ -21,10 +21,17 @@ | ||
| 21 | 21 | public function getProtocolVersion() : string |
| 22 | 22 | { |
| 23 | 23 | return $this->protocol; |
| 24 | 24 | } |
| 25 | + /** | |
| 26 | + * @return static | |
| 27 | + */ | |
| 25 | 28 | public function withProtocolVersion($version) : \YoastSEO_Vendor\Psr\Http\Message\MessageInterface |
| 26 | 29 | { |
| 30 | + if (!\is_string($version)) { | |
| 31 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to MessageInterface::withProtocolVersion() is deprecated; guzzlehttp/psr7 3.0 requires string.', \get_debug_type($version)); | |
| 32 | + } | |
| 33 | + $this->assertProtocolVersion($version); | |
| 27 | 34 | if ($this->protocol === $version) { |
| 28 | 35 | return $this; |
| 29 | 36 | } |
| 30 | 37 | $new = clone $this; |
| @@ -36,13 +43,13 @@ | ||
| 36 | 43 | return $this->headers; |
| 37 | 44 | } |
| 38 | 45 | public function hasHeader($header) : bool |
| 39 | 46 | { |
| 40 | - return isset($this->headerNames[\strtolower($header)]); | |
| 47 | + return isset($this->headerNames[\YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header)]); | |
| 41 | 48 | } |
| 42 | 49 | public function getHeader($header) : array |
| 43 | 50 | { |
| 44 | - $header = \strtolower($header); | |
| 51 | + $header = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header); | |
| 45 | 52 | if (!isset($this->headerNames[$header])) { |
| 46 | 53 | return []; |
| 47 | 54 | } |
| 48 | 55 | $header = $this->headerNames[$header]; |
| @@ -51,13 +58,23 @@ | ||
| 51 | 58 | public function getHeaderLine($header) : string |
| 52 | 59 | { |
| 53 | 60 | return \implode(', ', $this->getHeader($header)); |
| 54 | 61 | } |
| 62 | + /** | |
| 63 | + * @return static | |
| 64 | + */ | |
| 55 | 65 | public function withHeader($header, $value) : \YoastSEO_Vendor\Psr\Http\Message\MessageInterface |
| 56 | 66 | { |
| 57 | 67 | $this->assertHeader($header); |
| 68 | + $values = \is_array($value) ? $value : [$value]; | |
| 69 | + foreach ($values as $item) { | |
| 70 | + if (!\is_string($item) && (\is_scalar($item) || $item === null)) { | |
| 71 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to MessageInterface::withHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', \get_debug_type($item)); | |
| 72 | + break; | |
| 73 | + } | |
| 74 | + } | |
| 58 | 75 | $value = $this->normalizeHeaderValue($value); |
| 59 | - $normalized = \strtolower($header); | |
| 76 | + $normalized = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header); | |
| 60 | 77 | $new = clone $this; |
| 61 | 78 | if (isset($new->headerNames[$normalized])) { |
| 62 | 79 | unset($new->headers[$new->headerNames[$normalized]]); |
| 63 | 80 | } |
| @@ -64,13 +81,23 @@ | ||
| 64 | 81 | $new->headerNames[$normalized] = $header; |
| 65 | 82 | $new->headers[$header] = $value; |
| 66 | 83 | return $new; |
| 67 | 84 | } |
| 85 | + /** | |
| 86 | + * @return static | |
| 87 | + */ | |
| 68 | 88 | public function withAddedHeader($header, $value) : \YoastSEO_Vendor\Psr\Http\Message\MessageInterface |
| 69 | 89 | { |
| 70 | 90 | $this->assertHeader($header); |
| 91 | + $values = \is_array($value) ? $value : [$value]; | |
| 92 | + foreach ($values as $item) { | |
| 93 | + if (!\is_string($item) && (\is_scalar($item) || $item === null)) { | |
| 94 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to MessageInterface::withAddedHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', \get_debug_type($item)); | |
| 95 | + break; | |
| 96 | + } | |
| 97 | + } | |
| 71 | 98 | $value = $this->normalizeHeaderValue($value); |
| 72 | - $normalized = \strtolower($header); | |
| 99 | + $normalized = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header); | |
| 73 | 100 | $new = clone $this; |
| 74 | 101 | if (isset($new->headerNames[$normalized])) { |
| 75 | 102 | $header = $this->headerNames[$normalized]; |
| 76 | 103 | $new->headers[$header] = \array_merge($this->headers[$header], $value); |
| @@ -79,11 +106,14 @@ | ||
| 79 | 106 | $new->headers[$header] = $value; |
| 80 | 107 | } |
| 81 | 108 | return $new; |
| 82 | 109 | } |
| 110 | + /** | |
| 111 | + * @return static | |
| 112 | + */ | |
| 83 | 113 | public function withoutHeader($header) : \YoastSEO_Vendor\Psr\Http\Message\MessageInterface |
| 84 | 114 | { |
| 85 | - $normalized = \strtolower($header); | |
| 115 | + $normalized = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header); | |
| 86 | 116 | if (!isset($this->headerNames[$normalized])) { |
| 87 | 117 | return $this; |
| 88 | 118 | } |
| 89 | 119 | $header = $this->headerNames[$normalized]; |
| @@ -97,8 +127,11 @@ | ||
| 97 | 127 | $this->stream = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::streamFor(''); |
| 98 | 128 | } |
| 99 | 129 | return $this->stream; |
| 100 | 130 | } |
| 131 | + /** | |
| 132 | + * @return static | |
| 133 | + */ | |
| 101 | 134 | public function withBody(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $body) : \YoastSEO_Vendor\Psr\Http\Message\MessageInterface |
| 102 | 135 | { |
| 103 | 136 | if ($body === $this->stream) { |
| 104 | 137 | return $this; |
| @@ -116,10 +149,17 @@ | ||
| 116 | 149 | foreach ($headers as $header => $value) { |
| 117 | 150 | // Numeric array keys are converted to int by PHP. |
| 118 | 151 | $header = (string) $header; |
| 119 | 152 | $this->assertHeader($header); |
| 153 | + $values = \is_array($value) ? $value : [$value]; | |
| 154 | + foreach ($values as $item) { | |
| 155 | + if (!\is_string($item) && (\is_scalar($item) || $item === null)) { | |
| 156 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to %s::__construct() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].', \get_debug_type($item), static::class); | |
| 157 | + break; | |
| 158 | + } | |
| 159 | + } | |
| 120 | 160 | $value = $this->normalizeHeaderValue($value); |
| 121 | - $normalized = \strtolower($header); | |
| 161 | + $normalized = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($header); | |
| 122 | 162 | if (isset($this->headerNames[$normalized])) { |
| 123 | 163 | $header = $this->headerNames[$normalized]; |
| 124 | 164 | $this->headers[$header] = \array_merge($this->headers[$header], $value); |
| 125 | 165 | } else { |
| @@ -134,8 +174,11 @@ | ||
| 134 | 174 | * @return string[] |
| 135 | 175 | */ |
| 136 | 176 | private function normalizeHeaderValue($value) : array |
| 137 | 177 | { |
| 178 | + if (\is_array($value) && $value === []) { | |
| 179 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing an empty array as a header value is deprecated; guzzlehttp/psr7 3.0 rejects empty header value arrays.'); | |
| 180 | + } | |
| 138 | 181 | if (!\is_array($value)) { |
| 139 | 182 | return $this->trimAndValidateHeaderValues([$value]); |
| 140 | 183 | } |
| 141 | 184 | return $this->trimAndValidateHeaderValues($value); |
| @@ -159,8 +202,13 @@ | ||
| 159 | 202 | return \array_map(function ($value) { |
| 160 | 203 | if (!\is_scalar($value) && null !== $value) { |
| 161 | 204 | throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value))); |
| 162 | 205 | } |
| 206 | + // Convert non-finite floats explicitly, as implicit coercion of | |
| 207 | + // NAN emits a warning on PHP 8.5. | |
| 208 | + if (\is_float($value) && !\is_finite($value)) { | |
| 209 | + $value = \is_nan($value) ? 'NAN' : ($value > 0 ? 'INF' : '-INF'); | |
| 210 | + } | |
| 163 | 211 | $trimmed = \trim((string) $value, " \t"); |
| 164 | 212 | $this->assertValue($trimmed); |
| 165 | 213 | return $trimmed; |
| 166 | 214 | }, \array_values($values)); |
| @@ -176,8 +224,23 @@ | ||
| 176 | 224 | throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header))); |
| 177 | 225 | } |
| 178 | 226 | if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) { |
| 179 | 227 | throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name.', $header)); |
| 228 | + } | |
| 229 | + } | |
| 230 | + /** | |
| 231 | + * @param mixed $version | |
| 232 | + */ | |
| 233 | + private function assertProtocolVersion($version) : void | |
| 234 | + { | |
| 235 | + if (\is_string($version)) { | |
| 236 | + $this->assertNoLineSeparators($version, 'Protocol version'); | |
| 237 | + } | |
| 238 | + } | |
| 239 | + private function assertNoLineSeparators(string $value, string $field) : void | |
| 240 | + { | |
| 241 | + if (\strpbrk($value, "\r\n") !== \false) { | |
| 242 | + throw new \InvalidArgumentException($field . ' must not contain CR or LF characters.'); | |
| 180 | 243 | } |
| 181 | 244 | } |
| 182 | 245 | /** |
| 183 | 246 | * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |