| 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\ServerRequestInterface; |
| 8 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 9 |
use YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface; |
| 10 |
use YoastSEO_Vendor\Psr\Http\Message\UriInterface; |
| 11 |
/** |
| 12 |
* Server-side HTTP request |
| 13 |
* |
| 14 |
* Extends the Request definition to add methods for accessing incoming data, |
| 15 |
* specifically server parameters, cookies, matched path parameters, query |
| 16 |
* string arguments, body parameters, and upload file information. |
| 17 |
* |
| 18 |
* "Attributes" are discovered via decomposing the request (and usually |
| 19 |
* specifically the URI path), and typically will be injected by the application. |
| 20 |
* |
| 21 |
* Requests are considered immutable; all methods that might change state are |
| 22 |
* implemented such that they retain the internal state of the current |
| 23 |
* message and return a new instance that contains the changed state. |
| 24 |
*/ |
| 25 |
class ServerRequest extends \YoastSEO_Vendor\GuzzleHttp\Psr7\Request implements \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $attributes = []; |
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $cookieParams = []; |
| 35 |
/** |
| 36 |
* @var array|object|null |
| 37 |
*/ |
| 38 |
private $parsedBody; |
| 39 |
/** |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $queryParams = []; |
| 43 |
/** |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
private $serverParams; |
| 47 |
/** |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
private $uploadedFiles = []; |
| 51 |
/** |
| 52 |
* @param string $method HTTP method |
| 53 |
* @param string|UriInterface $uri URI |
| 54 |
* @param (string|string[])[] $headers Request headers |
| 55 |
* @param string|resource|StreamInterface|null $body Request body |
| 56 |
* @param string $version Protocol version |
| 57 |
* @param array $serverParams Typically the $_SERVER superglobal |
| 58 |
*/ |
| 59 |
public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = []) |
| 60 |
{ |
| 61 |
$this->serverParams = $serverParams; |
| 62 |
parent::__construct($method, $uri, $headers, $body, $version); |
| 63 |
} |
| 64 |
/** |
| 65 |
* Return an UploadedFile instance array. |
| 66 |
* |
| 67 |
* @param array $files An array which respect $_FILES structure |
| 68 |
* |
| 69 |
* @throws InvalidArgumentException for unrecognized values |
| 70 |
*/ |
| 71 |
public static function normalizeFiles(array $files) : array |
| 72 |
{ |
| 73 |
$normalized = []; |
| 74 |
foreach ($files as $key => $value) { |
| 75 |
if ($value instanceof \YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface) { |
| 76 |
$normalized[$key] = $value; |
| 77 |
} elseif (\is_array($value) && isset($value['tmp_name'])) { |
| 78 |
$normalized[$key] = self::createUploadedFileFromSpec($value); |
| 79 |
} elseif (\is_array($value)) { |
| 80 |
$normalized[$key] = self::normalizeFiles($value); |
| 81 |
continue; |
| 82 |
} else { |
| 83 |
throw new \InvalidArgumentException('Invalid value in files specification'); |
| 84 |
} |
| 85 |
} |
| 86 |
return $normalized; |
| 87 |
} |
| 88 |
/** |
| 89 |
* Create and return an UploadedFile instance from a $_FILES specification. |
| 90 |
* |
| 91 |
* If the specification represents an array of values, this method will |
| 92 |
* delegate to normalizeNestedFileSpec() and return that return value. |
| 93 |
* |
| 94 |
* @param array $value $_FILES struct |
| 95 |
* |
| 96 |
* @return UploadedFileInterface|UploadedFileInterface[] |
| 97 |
*/ |
| 98 |
private static function createUploadedFileFromSpec(array $value) |
| 99 |
{ |
| 100 |
if (\is_array($value['tmp_name'])) { |
| 101 |
return self::normalizeNestedFileSpec($value); |
| 102 |
} |
| 103 |
return new \YoastSEO_Vendor\GuzzleHttp\Psr7\UploadedFile($value['tmp_name'], (int) $value['size'], (int) $value['error'], $value['name'], $value['type']); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Normalize an array of file specifications. |
| 107 |
* |
| 108 |
* Loops through all nested files and returns a normalized array of |
| 109 |
* UploadedFileInterface instances. |
| 110 |
* |
| 111 |
* @return UploadedFileInterface[] |
| 112 |
*/ |
| 113 |
private static function normalizeNestedFileSpec(array $files = []) : array |
| 114 |
{ |
| 115 |
$normalizedFiles = []; |
| 116 |
foreach (\array_keys($files['tmp_name']) as $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]; |
| 118 |
$normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); |
| 119 |
} |
| 120 |
return $normalizedFiles; |
| 121 |
} |
| 122 |
/** |
| 123 |
* Return a ServerRequest populated with superglobals: |
| 124 |
* $_GET |
| 125 |
* $_POST |
| 126 |
* $_COOKIE |
| 127 |
* $_FILES |
| 128 |
* $_SERVER |
| 129 |
*/ |
| 130 |
public static function fromGlobals() : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 131 |
{ |
| 132 |
$method = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToUpper(self::getServerParam('REQUEST_METHOD') ?? 'GET'); |
| 133 |
$headers = self::removeInvalidHostHeader(self::getAllHeaders()); |
| 134 |
$uri = self::getUriFromGlobals(); |
| 135 |
$body = new \YoastSEO_Vendor\GuzzleHttp\Psr7\CachingStream(new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream('php://input', 'r+')); |
| 136 |
$serverProtocol = self::getServerParam('SERVER_PROTOCOL'); |
| 137 |
$protocol = $serverProtocol !== null ? \str_replace('HTTP/', '', $serverProtocol) : '1.1'; |
| 138 |
$serverRequest = new \YoastSEO_Vendor\GuzzleHttp\Psr7\ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER); |
| 139 |
return $serverRequest->withCookieParams($_COOKIE)->withQueryParams($_GET)->withParsedBody($_POST)->withUploadedFiles(self::normalizeFiles($_FILES)); |
| 140 |
} |
| 141 |
/** |
| 142 |
* @return array<array-key, string> |
| 143 |
*/ |
| 144 |
private static function getAllHeaders() : array |
| 145 |
{ |
| 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 |
} |
| 160 |
} |
| 161 |
return $normalized; |
| 162 |
} |
| 163 |
private static function getServerParam(string $key) : ?string |
| 164 |
{ |
| 165 |
return isset($_SERVER[$key]) && \is_string($_SERVER[$key]) ? $_SERVER[$key] : null; |
| 166 |
} |
| 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 |
/** |
| 192 |
* Get a Uri populated with values from $_SERVER. |
| 193 |
*/ |
| 194 |
public static function getUriFromGlobals() : \YoastSEO_Vendor\Psr\Http\Message\UriInterface |
| 195 |
{ |
| 196 |
$uri = new \YoastSEO_Vendor\GuzzleHttp\Psr7\Uri(''); |
| 197 |
$https = self::getServerParam('HTTPS'); |
| 198 |
$uri = $uri->withScheme(!empty($https) && $https !== 'off' ? 'https' : 'http'); |
| 199 |
$hasPort = \false; |
| 200 |
$authority = self::getServerParam('HTTP_HOST'); |
| 201 |
if ($authority !== null) { |
| 202 |
[$host, $port] = self::extractHostAndPortFromAuthority($authority); |
| 203 |
if ($host !== null) { |
| 204 |
$uri = $uri->withHost($host); |
| 205 |
} |
| 206 |
if ($port !== null) { |
| 207 |
$hasPort = \true; |
| 208 |
$uri = $uri->withPort($port); |
| 209 |
} |
| 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); |
| 214 |
} |
| 215 |
$serverPort = self::getServerParam('SERVER_PORT'); |
| 216 |
if (!$hasPort && $serverPort !== null && \preg_match('/^[+-]?\\d+$/D', $serverPort) === 1) { |
| 217 |
$uri = $uri->withPort((int) $serverPort); |
| 218 |
} |
| 219 |
$hasQuery = \false; |
| 220 |
$requestUri = self::getServerParam('REQUEST_URI'); |
| 221 |
if ($requestUri !== null) { |
| 222 |
$requestUriParts = \explode('?', $requestUri, 2); |
| 223 |
$uri = $uri->withPath($requestUriParts[0]); |
| 224 |
if (isset($requestUriParts[1])) { |
| 225 |
$hasQuery = \true; |
| 226 |
$uri = $uri->withQuery($requestUriParts[1]); |
| 227 |
} |
| 228 |
} |
| 229 |
$queryString = self::getServerParam('QUERY_STRING'); |
| 230 |
if (!$hasQuery && $queryString !== null) { |
| 231 |
$uri = $uri->withQuery($queryString); |
| 232 |
} |
| 233 |
return $uri; |
| 234 |
} |
| 235 |
public function getServerParams() : array |
| 236 |
{ |
| 237 |
return $this->serverParams; |
| 238 |
} |
| 239 |
public function getUploadedFiles() : array |
| 240 |
{ |
| 241 |
return $this->uploadedFiles; |
| 242 |
} |
| 243 |
public function withUploadedFiles(array $uploadedFiles) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 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 |
} |
| 265 |
$new = clone $this; |
| 266 |
$new->uploadedFiles = $uploadedFiles; |
| 267 |
return $new; |
| 268 |
} |
| 269 |
public function getCookieParams() : array |
| 270 |
{ |
| 271 |
return $this->cookieParams; |
| 272 |
} |
| 273 |
public function withCookieParams(array $cookies) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 274 |
{ |
| 275 |
$new = clone $this; |
| 276 |
$new->cookieParams = $cookies; |
| 277 |
return $new; |
| 278 |
} |
| 279 |
public function getQueryParams() : array |
| 280 |
{ |
| 281 |
return $this->queryParams; |
| 282 |
} |
| 283 |
public function withQueryParams(array $query) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 284 |
{ |
| 285 |
$new = clone $this; |
| 286 |
$new->queryParams = $query; |
| 287 |
return $new; |
| 288 |
} |
| 289 |
/** |
| 290 |
* @return array|object|null |
| 291 |
*/ |
| 292 |
public function getParsedBody() |
| 293 |
{ |
| 294 |
return $this->parsedBody; |
| 295 |
} |
| 296 |
public function withParsedBody($data) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 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 |
} |
| 301 |
$new = clone $this; |
| 302 |
$new->parsedBody = $data; |
| 303 |
return $new; |
| 304 |
} |
| 305 |
public function getAttributes() : array |
| 306 |
{ |
| 307 |
return $this->attributes; |
| 308 |
} |
| 309 |
/** |
| 310 |
* @return mixed |
| 311 |
*/ |
| 312 |
public function getAttribute($attribute, $default = null) |
| 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 |
} |
| 317 |
if (\false === \array_key_exists($attribute, $this->attributes)) { |
| 318 |
return $default; |
| 319 |
} |
| 320 |
return $this->attributes[$attribute]; |
| 321 |
} |
| 322 |
public function withAttribute($attribute, $value) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 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 |
} |
| 327 |
$new = clone $this; |
| 328 |
$new->attributes[$attribute] = $value; |
| 329 |
return $new; |
| 330 |
} |
| 331 |
public function withoutAttribute($attribute) : \YoastSEO_Vendor\Psr\Http\Message\ServerRequestInterface |
| 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 |
} |
| 336 |
if (\false === \array_key_exists($attribute, $this->attributes)) { |
| 337 |
return $this; |
| 338 |
} |
| 339 |
$new = clone $this; |
| 340 |
unset($new->attributes[$attribute]); |
| 341 |
return $new; |
| 342 |
} |
| 343 |
} |
| 344 |
|