← All changes
|
vendor_prefixed/guzzlehttp/psr7/src/ServerRequest.php
+124
-87
18.7
→
trunk
View file →
| @@ -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\ServerRequestInterface; |
| @@ -49,14 +50,14 @@ | ||
| 49 | 50 | private $uploadedFiles = []; |
| 50 | 51 | /** |
| 51 | 52 | * @param string $method HTTP method |
| 52 | 53 | * @param string|UriInterface $uri URI |
| 53 | - * @param array $headers Request headers | |
| 54 | + * @param (string|string[])[] $headers Request headers | |
| 54 | 55 | * @param string|resource|StreamInterface|null $body Request body |
| 55 | 56 | * @param string $version Protocol version |
| 56 | 57 | * @param array $serverParams Typically the $_SERVER superglobal |
| 57 | 58 | */ |
| 58 | - public function __construct($method, $uri, array $headers = [], $body = null, $version = '1.1', array $serverParams = []) | |
| 59 | + public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = []) | |
| 59 | 60 | { |
| 60 | 61 | $this->serverParams = $serverParams; |
| 61 | 62 | parent::__construct($method, $uri, $headers, $body, $version); |
| 62 | 63 | } |
| @@ -62,15 +63,13 @@ | ||
| 62 | 63 | } |
| 63 | 64 | /** |
| 64 | 65 | * Return an UploadedFile instance array. |
| 65 | 66 | * |
| 66 | - * @param array $files A array which respect $_FILES structure | |
| 67 | + * @param array $files An array which respect $_FILES structure | |
| 67 | 68 | * |
| 68 | - * @return array | |
| 69 | - * | |
| 70 | 69 | * @throws InvalidArgumentException for unrecognized values |
| 71 | 70 | */ |
| 72 | - public static function normalizeFiles(array $files) | |
| 71 | + public static function normalizeFiles(array $files) : array | |
| 73 | 72 | { |
| 74 | 73 | $normalized = []; |
| 75 | 74 | foreach ($files as $key => $value) { |
| 76 | 75 | if ($value instanceof \YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface) { |
| @@ -93,9 +92,9 @@ | ||
| 93 | 92 | * delegate to normalizeNestedFileSpec() and return that return value. |
| 94 | 93 | * |
| 95 | 94 | * @param array $value $_FILES struct |
| 96 | 95 | * |
| 97 | - * @return array|UploadedFileInterface | |
| 96 | + * @return UploadedFileInterface|UploadedFileInterface[] | |
| 98 | 97 | */ |
| 99 | 98 | private static function createUploadedFileFromSpec(array $value) |
| 100 | 99 | { |
| 101 | 100 | if (\is_array($value['tmp_name'])) { |
| @@ -108,17 +107,15 @@ | ||
| 108 | 107 | * |
| 109 | 108 | * Loops through all nested files and returns a normalized array of |
| 110 | 109 | * UploadedFileInterface instances. |
| 111 | 110 | * |
| 112 | - * @param array $files | |
| 113 | - * | |
| 114 | 111 | * @return UploadedFileInterface[] |
| 115 | 112 | */ |
| 116 | - private static function normalizeNestedFileSpec(array $files = []) | |
| 113 | + private static function normalizeNestedFileSpec(array $files = []) : array | |
| 117 | 114 | { |
| 118 | 115 | $normalizedFiles = []; |
| 119 | 116 | foreach (\array_keys($files['tmp_name']) as $key) { |
| 120 | - $spec = ['tmp_name' => $files['tmp_name'][$key], 'size' => $files['size'][$key], 'error' => $files['error'][$key], 'name' => $files['name'][$key], 'type' => $files['type'][$key]]; | |
| 117 | + $spec = ['tmp_name' => $files['tmp_name'][$key], 'size' => $files['size'][$key] ?? null, 'error' => $files['error'][$key] ?? null, 'name' => $files['name'][$key] ?? null, 'type' => $files['type'][$key] ?? null]; | |
| 121 | 118 | $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); |
| 122 | 119 | } |
| 123 | 120 | return $normalizedFiles; |
| 124 | 121 | } |
| @@ -128,44 +125,82 @@ | ||
| 128 | 125 | * $_POST |
| 129 | 126 | * $_COOKIE |
| 130 | 127 | * $_FILES |
| 131 | 128 | * $_SERVER |
| 132 | - * | |
| 133 | - * @return ServerRequestInterface | |
| 134 | 129 | */ |
| 135 | - public static function fromGlobals() | |
| 130 | + public static function fromGlobals() : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 136 | 131 | { |
| 137 | - $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; | |
| 138 | - $headers = \getallheaders(); | |
| 132 | + $method = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToUpper(self::getServerParam('REQUEST_METHOD') ?? 'GET'); | |
| 133 | + $headers = self::removeInvalidHostHeader(self::getAllHeaders()); | |
| 139 | 134 | $uri = self::getUriFromGlobals(); |
| 140 | 135 | $body = new \YoastSEO_Vendor\GuzzleHttp\Psr7\CachingStream(new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream('php://input', 'r+')); |
| 141 | - $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1'; | |
| 136 | + $serverProtocol = self::getServerParam('SERVER_PROTOCOL'); | |
| 137 | + $protocol = $serverProtocol !== null ? \str_replace('HTTP/', '', $serverProtocol) : '1.1'; | |
| 142 | 138 | $serverRequest = new \YoastSEO_Vendor\GuzzleHttp\Psr7\ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER); |
| 143 | 139 | return $serverRequest->withCookieParams($_COOKIE)->withQueryParams($_GET)->withParsedBody($_POST)->withUploadedFiles(self::normalizeFiles($_FILES)); |
| 144 | 140 | } |
| 145 | - private static function extractHostAndPortFromAuthority($authority) | |
| 141 | + /** | |
| 142 | + * @return array<array-key, string> | |
| 143 | + */ | |
| 144 | + private static function getAllHeaders() : array | |
| 146 | 145 | { |
| 147 | - $uri = 'http://' . $authority; | |
| 148 | - $parts = \parse_url($uri); | |
| 149 | - if (\false === $parts) { | |
| 150 | - return [null, null]; | |
| 146 | + return self::normalizeHeaderValues(\getallheaders()); | |
| 147 | + } | |
| 148 | + /** | |
| 149 | + * @param array<array-key, mixed> $headers | |
| 150 | + * | |
| 151 | + * @return array<array-key, string> | |
| 152 | + */ | |
| 153 | + private static function normalizeHeaderValues(array $headers) : array | |
| 154 | + { | |
| 155 | + $normalized = []; | |
| 156 | + foreach ($headers as $name => $value) { | |
| 157 | + if (\is_scalar($value) || \is_object($value) && \method_exists($value, '__toString')) { | |
| 158 | + $normalized[$name] = (string) $value; | |
| 159 | + } | |
| 151 | 160 | } |
| 152 | - $host = isset($parts['host']) ? $parts['host'] : null; | |
| 153 | - $port = isset($parts['port']) ? $parts['port'] : null; | |
| 154 | - return [$host, $port]; | |
| 161 | + return $normalized; | |
| 155 | 162 | } |
| 163 | + private static function getServerParam(string $key) : ?string | |
| 164 | + { | |
| 165 | + return isset($_SERVER[$key]) && \is_string($_SERVER[$key]) ? $_SERVER[$key] : null; | |
| 166 | + } | |
| 156 | 167 | /** |
| 168 | + * @param array<array-key, string> $headers | |
| 169 | + * | |
| 170 | + * @return array<array-key, string> | |
| 171 | + */ | |
| 172 | + private static function removeInvalidHostHeader(array $headers) : array | |
| 173 | + { | |
| 174 | + foreach ($headers as $name => $value) { | |
| 175 | + if (\YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower((string) $name) !== 'host') { | |
| 176 | + continue; | |
| 177 | + } | |
| 178 | + if (\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::parseHostHeader($value) === null) { | |
| 179 | + unset($headers[$name]); | |
| 180 | + } | |
| 181 | + } | |
| 182 | + return $headers; | |
| 183 | + } | |
| 184 | + /** | |
| 185 | + * @return array{0: string|null, 1: int|null} | |
| 186 | + */ | |
| 187 | + private static function extractHostAndPortFromAuthority(string $authority) : array | |
| 188 | + { | |
| 189 | + return \YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::parseHostHeader($authority) ?? [null, null]; | |
| 190 | + } | |
| 191 | + /** | |
| 157 | 192 | * Get a Uri populated with values from $_SERVER. |
| 158 | - * | |
| 159 | - * @return UriInterface | |
| 160 | 193 | */ |
| 161 | - public static function getUriFromGlobals() | |
| 194 | + public static function getUriFromGlobals() : \YoastSEO_Vendor\Psr\Http\Message\UriInterface | |
| 162 | 195 | { |
| 163 | 196 | $uri = new \YoastSEO_Vendor\GuzzleHttp\Psr7\Uri(''); |
| 164 | - $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http'); | |
| 197 | + $https = self::getServerParam('HTTPS'); | |
| 198 | + $uri = $uri->withScheme(!empty($https) && $https !== 'off' ? 'https' : 'http'); | |
| 165 | 199 | $hasPort = \false; |
| 166 | - if (isset($_SERVER['HTTP_HOST'])) { | |
| 167 | - list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']); | |
| 200 | + $authority = self::getServerParam('HTTP_HOST'); | |
| 201 | + if ($authority !== null) { | |
| 202 | + [$host, $port] = self::extractHostAndPortFromAuthority($authority); | |
| 168 | 203 | if ($host !== null) { |
| 169 | 204 | $uri = $uri->withHost($host); |
| 170 | 205 | } |
| 171 | 206 | if ($port !== null) { |
| @@ -171,19 +206,21 @@ | ||
| 171 | 206 | if ($port !== null) { |
| 172 | 207 | $hasPort = \true; |
| 173 | 208 | $uri = $uri->withPort($port); |
| 174 | 209 | } |
| 175 | - } elseif (isset($_SERVER['SERVER_NAME'])) { | |
| 176 | - $uri = $uri->withHost($_SERVER['SERVER_NAME']); | |
| 177 | - } elseif (isset($_SERVER['SERVER_ADDR'])) { | |
| 178 | - $uri = $uri->withHost($_SERVER['SERVER_ADDR']); | |
| 210 | + } elseif (($serverName = self::getServerParam('SERVER_NAME')) !== null) { | |
| 211 | + $uri = $uri->withHost($serverName); | |
| 212 | + } elseif (($serverAddr = self::getServerParam('SERVER_ADDR')) !== null) { | |
| 213 | + $uri = $uri->withHost($serverAddr); | |
| 179 | 214 | } |
| 180 | - if (!$hasPort && isset($_SERVER['SERVER_PORT'])) { | |
| 181 | - $uri = $uri->withPort($_SERVER['SERVER_PORT']); | |
| 215 | + $serverPort = self::getServerParam('SERVER_PORT'); | |
| 216 | + if (!$hasPort && $serverPort !== null && \preg_match('/^[+-]?\\d+$/D', $serverPort) === 1) { | |
| 217 | + $uri = $uri->withPort((int) $serverPort); | |
| 182 | 218 | } |
| 183 | 219 | $hasQuery = \false; |
| 184 | - if (isset($_SERVER['REQUEST_URI'])) { | |
| 185 | - $requestUriParts = \explode('?', $_SERVER['REQUEST_URI'], 2); | |
| 220 | + $requestUri = self::getServerParam('REQUEST_URI'); | |
| 221 | + if ($requestUri !== null) { | |
| 222 | + $requestUriParts = \explode('?', $requestUri, 2); | |
| 186 | 223 | $uri = $uri->withPath($requestUriParts[0]); |
| 187 | 224 | if (isset($requestUriParts[1])) { |
| 188 | 225 | $hasQuery = \true; |
| 189 | 226 | $uri = $uri->withQuery($requestUriParts[1]); |
| @@ -188,63 +225,63 @@ | ||
| 188 | 225 | $hasQuery = \true; |
| 189 | 226 | $uri = $uri->withQuery($requestUriParts[1]); |
| 190 | 227 | } |
| 191 | 228 | } |
| 192 | - if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) { | |
| 193 | - $uri = $uri->withQuery($_SERVER['QUERY_STRING']); | |
| 229 | + $queryString = self::getServerParam('QUERY_STRING'); | |
| 230 | + if (!$hasQuery && $queryString !== null) { | |
| 231 | + $uri = $uri->withQuery($queryString); | |
| 194 | 232 | } |
| 195 | 233 | return $uri; |
| 196 | 234 | } |
| 197 | - /** | |
| 198 | - * {@inheritdoc} | |
| 199 | - */ | |
| 200 | - public function getServerParams() | |
| 235 | + public function getServerParams() : array | |
| 201 | 236 | { |
| 202 | 237 | return $this->serverParams; |
| 203 | 238 | } |
| 204 | - /** | |
| 205 | - * {@inheritdoc} | |
| 206 | - */ | |
| 207 | - public function getUploadedFiles() | |
| 239 | + public function getUploadedFiles() : array | |
| 208 | 240 | { |
| 209 | 241 | return $this->uploadedFiles; |
| 210 | 242 | } |
| 211 | - /** | |
| 212 | - * {@inheritdoc} | |
| 213 | - */ | |
| 214 | - public function withUploadedFiles(array $uploadedFiles) | |
| 243 | + public function withUploadedFiles(array $uploadedFiles) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 215 | 244 | { |
| 245 | + $invalidUploadedFileFound = \false; | |
| 246 | + $invalidUploadedFile = null; | |
| 247 | + $stack = [$uploadedFiles]; | |
| 248 | + while ($stack !== []) { | |
| 249 | + foreach (\array_pop($stack) as $uploadedFile) { | |
| 250 | + if ($uploadedFile instanceof \YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface) { | |
| 251 | + continue; | |
| 252 | + } | |
| 253 | + if (\is_array($uploadedFile)) { | |
| 254 | + $stack[] = $uploadedFile; | |
| 255 | + continue; | |
| 256 | + } | |
| 257 | + $invalidUploadedFileFound = \true; | |
| 258 | + $invalidUploadedFile = $uploadedFile; | |
| 259 | + break 2; | |
| 260 | + } | |
| 261 | + } | |
| 262 | + if ($invalidUploadedFileFound) { | |
| 263 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s inside ServerRequestInterface::withUploadedFiles() is deprecated; guzzlehttp/psr7 3.0 requires an UploadedFileInterface[] tree.', \get_debug_type($invalidUploadedFile)); | |
| 264 | + } | |
| 216 | 265 | $new = clone $this; |
| 217 | 266 | $new->uploadedFiles = $uploadedFiles; |
| 218 | 267 | return $new; |
| 219 | 268 | } |
| 220 | - /** | |
| 221 | - * {@inheritdoc} | |
| 222 | - */ | |
| 223 | - public function getCookieParams() | |
| 269 | + public function getCookieParams() : array | |
| 224 | 270 | { |
| 225 | 271 | return $this->cookieParams; |
| 226 | 272 | } |
| 227 | - /** | |
| 228 | - * {@inheritdoc} | |
| 229 | - */ | |
| 230 | - public function withCookieParams(array $cookies) | |
| 273 | + public function withCookieParams(array $cookies) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 231 | 274 | { |
| 232 | 275 | $new = clone $this; |
| 233 | 276 | $new->cookieParams = $cookies; |
| 234 | 277 | return $new; |
| 235 | 278 | } |
| 236 | - /** | |
| 237 | - * {@inheritdoc} | |
| 238 | - */ | |
| 239 | - public function getQueryParams() | |
| 279 | + public function getQueryParams() : array | |
| 240 | 280 | { |
| 241 | 281 | return $this->queryParams; |
| 242 | 282 | } |
| 243 | - /** | |
| 244 | - * {@inheritdoc} | |
| 245 | - */ | |
| 246 | - public function withQueryParams(array $query) | |
| 283 | + public function withQueryParams(array $query) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 247 | 284 | { |
| 248 | 285 | $new = clone $this; |
| 249 | 286 | $new->queryParams = $query; |
| 250 | 287 | return $new; |
| @@ -249,54 +286,54 @@ | ||
| 249 | 286 | $new->queryParams = $query; |
| 250 | 287 | return $new; |
| 251 | 288 | } |
| 252 | 289 | /** |
| 253 | - * {@inheritdoc} | |
| 290 | + * @return array|object|null | |
| 254 | 291 | */ |
| 255 | 292 | public function getParsedBody() |
| 256 | 293 | { |
| 257 | 294 | return $this->parsedBody; |
| 258 | 295 | } |
| 259 | - /** | |
| 260 | - * {@inheritdoc} | |
| 261 | - */ | |
| 262 | - public function withParsedBody($data) | |
| 296 | + public function withParsedBody($data) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 263 | 297 | { |
| 298 | + if ($data !== null && !\is_array($data) && !\is_object($data)) { | |
| 299 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to ServerRequestInterface::withParsedBody() is deprecated; guzzlehttp/psr7 3.0 requires array|object|null.', \get_debug_type($data)); | |
| 300 | + } | |
| 264 | 301 | $new = clone $this; |
| 265 | 302 | $new->parsedBody = $data; |
| 266 | 303 | return $new; |
| 267 | 304 | } |
| 268 | - /** | |
| 269 | - * {@inheritdoc} | |
| 270 | - */ | |
| 271 | - public function getAttributes() | |
| 305 | + public function getAttributes() : array | |
| 272 | 306 | { |
| 273 | 307 | return $this->attributes; |
| 274 | 308 | } |
| 275 | 309 | /** |
| 276 | - * {@inheritdoc} | |
| 310 | + * @return mixed | |
| 277 | 311 | */ |
| 278 | 312 | public function getAttribute($attribute, $default = null) |
| 279 | 313 | { |
| 314 | + if (!\is_string($attribute)) { | |
| 315 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to ServerRequestInterface::getAttribute() is deprecated; guzzlehttp/psr7 3.0 requires string for $attribute.', \get_debug_type($attribute)); | |
| 316 | + } | |
| 280 | 317 | if (\false === \array_key_exists($attribute, $this->attributes)) { |
| 281 | 318 | return $default; |
| 282 | 319 | } |
| 283 | 320 | return $this->attributes[$attribute]; |
| 284 | 321 | } |
| 285 | - /** | |
| 286 | - * {@inheritdoc} | |
| 287 | - */ | |
| 288 | - public function withAttribute($attribute, $value) | |
| 322 | + public function withAttribute($attribute, $value) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 289 | 323 | { |
| 324 | + if (!\is_string($attribute)) { | |
| 325 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to ServerRequestInterface::withAttribute() is deprecated; guzzlehttp/psr7 3.0 requires string for $attribute.', \get_debug_type($attribute)); | |
| 326 | + } | |
| 290 | 327 | $new = clone $this; |
| 291 | 328 | $new->attributes[$attribute] = $value; |
| 292 | 329 | return $new; |
| 293 | 330 | } |
| 294 | - /** | |
| 295 | - * {@inheritdoc} | |
| 296 | - */ | |
| 297 | - public function withoutAttribute($attribute) | |
| 331 | + public function withoutAttribute($attribute) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface | |
| 298 | 332 | { |
| 333 | + if (!\is_string($attribute)) { | |
| 334 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to ServerRequestInterface::withoutAttribute() is deprecated; guzzlehttp/psr7 3.0 requires string for $attribute.', \get_debug_type($attribute)); | |
| 335 | + } | |
| 299 | 336 | if (\false === \array_key_exists($attribute, $this->attributes)) { |
| 300 | 337 | return $this; |
| 301 | 338 | } |
| 302 | 339 | $new = clone $this; |