← All changes
|
includes/sdk/google/guzzlehttp/guzzle/src/Handler/StreamHandler.php
+47
-42
1.2.2
→
1.4.2
View file →
| @@ -1,20 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Dudlewebs\WPMCS\GuzzleHttp\Handler; | |
| 3 | +namespace Dudlewebs\WPMCS\GCP\GuzzleHttp\Handler; | |
| 4 | 4 | |
| 5 | -use Dudlewebs\WPMCS\GuzzleHttp\Exception\ConnectException; | |
| 6 | -use Dudlewebs\WPMCS\GuzzleHttp\Exception\RequestException; | |
| 7 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise as P; | |
| 8 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise\FulfilledPromise; | |
| 9 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise\PromiseInterface; | |
| 10 | -use Dudlewebs\WPMCS\GuzzleHttp\Psr7; | |
| 11 | -use Dudlewebs\WPMCS\GuzzleHttp\TransferStats; | |
| 12 | -use Dudlewebs\WPMCS\GuzzleHttp\Utils; | |
| 13 | -use Dudlewebs\WPMCS\Psr\Http\Message\RequestInterface; | |
| 14 | -use Dudlewebs\WPMCS\Psr\Http\Message\ResponseInterface; | |
| 15 | -use Dudlewebs\WPMCS\Psr\Http\Message\StreamInterface; | |
| 16 | -use Dudlewebs\WPMCS\Psr\Http\Message\UriInterface; | |
| 5 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Exception\ConnectException; | |
| 6 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Exception\RequestException; | |
| 7 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise as P; | |
| 8 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise\FulfilledPromise; | |
| 9 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise\PromiseInterface; | |
| 10 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Psr7; | |
| 11 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\TransferStats; | |
| 12 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Utils; | |
| 13 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\RequestInterface; | |
| 14 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\ResponseInterface; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\StreamInterface; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\UriInterface; | |
| 17 | 17 | /** |
| 18 | 18 | * HTTP handler that uses PHP's HTTP stream wrapper. |
| 19 | 19 | * |
| 20 | 20 | * @final |
| @@ -30,9 +30,9 @@ | ||
| 30 | 30 | * |
| 31 | 31 | * @param RequestInterface $request Request to send. |
| 32 | 32 | * @param array $options Request transfer options. |
| 33 | 33 | */ |
| 34 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface | |
| 34 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface | |
| 35 | 35 | { |
| 36 | 36 | // Sleep if there is a delay specified. |
| 37 | 37 | if (isset($options['delay'])) { |
| 38 | 38 | \usleep($options['delay'] * 1000); |
| @@ -38,9 +38,9 @@ | ||
| 38 | 38 | \usleep($options['delay'] * 1000); |
| 39 | 39 | } |
| 40 | 40 | $protocolVersion = $request->getProtocolVersion(); |
| 41 | 41 | if ('1.0' !== $protocolVersion && '1.1' !== $protocolVersion) { |
| 42 | - throw new ConnectException(sprintf('HTTP/%s is not supported by the stream handler.', $protocolVersion), $request); | |
| 42 | + throw new ConnectException(\sprintf('HTTP/%s is not supported by the stream handler.', $protocolVersion), $request); | |
| 43 | 43 | } |
| 44 | 44 | $startTime = isset($options['on_stats']) ? Utils::currentTime() : null; |
| 45 | 45 | try { |
| 46 | 46 | // Does not support the expect header. |
| @@ -45,10 +45,10 @@ | ||
| 45 | 45 | try { |
| 46 | 46 | // Does not support the expect header. |
| 47 | 47 | $request = $request->withoutHeader('Expect'); |
| 48 | 48 | // Append a content-length header if body size is zero to match |
| 49 | - // cURL's behavior. | |
| 50 | - if (0 === $request->getBody()->getSize()) { | |
| 49 | + // the behavior of `CurlHandler` | |
| 50 | + if ((0 === \strcasecmp('PUT', $request->getMethod()) || 0 === \strcasecmp('POST', $request->getMethod())) && 0 === $request->getBody()->getSize()) { | |
| 51 | 51 | $request = $request->withHeader('Content-Length', '0'); |
| 52 | 52 | } |
| 53 | 53 | return $this->createResponse($request, $options, $this->createStream($request, $options), $startTime); |
| 54 | 54 | } catch (\InvalidArgumentException $e) { |
| @@ -65,9 +65,9 @@ | ||
| 65 | 65 | $this->invokeStats($options, $request, $startTime, null, $e); |
| 66 | 66 | return P\Create::rejectionFor($e); |
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | - private function invokeStats(array $options, RequestInterface $request, ?float $startTime, ?ResponseInterface $response = null, ?\Throwable $error = null): void | |
| 69 | + private function invokeStats(array $options, RequestInterface $request, ?float $startTime, ?ResponseInterface $response = null, ?\Throwable $error = null) : void | |
| 70 | 70 | { |
| 71 | 71 | if (isset($options['on_stats'])) { |
| 72 | 72 | $stats = new TransferStats($request, $response, Utils::currentTime() - $startTime, $error, []); |
| 73 | 73 | $options['on_stats']($stats); |
| @@ -75,9 +75,9 @@ | ||
| 75 | 75 | } |
| 76 | 76 | /** |
| 77 | 77 | * @param resource $stream |
| 78 | 78 | */ |
| 79 | - private function createResponse(RequestInterface $request, array $options, $stream, ?float $startTime): PromiseInterface | |
| 79 | + private function createResponse(RequestInterface $request, array $options, $stream, ?float $startTime) : PromiseInterface | |
| 80 | 80 | { |
| 81 | 81 | $hdrs = $this->lastHeaders; |
| 82 | 82 | $this->lastHeaders = []; |
| 83 | 83 | try { |
| @@ -110,9 +110,9 @@ | ||
| 110 | 110 | } |
| 111 | 111 | $this->invokeStats($options, $request, $startTime, $response, null); |
| 112 | 112 | return new FulfilledPromise($response); |
| 113 | 113 | } |
| 114 | - private function createSink(StreamInterface $stream, array $options): StreamInterface | |
| 114 | + private function createSink(StreamInterface $stream, array $options) : StreamInterface | |
| 115 | 115 | { |
| 116 | 116 | if (!empty($options['stream'])) { |
| 117 | 117 | return $stream; |
| 118 | 118 | } |
| @@ -121,9 +121,9 @@ | ||
| 121 | 121 | } |
| 122 | 122 | /** |
| 123 | 123 | * @param resource $stream |
| 124 | 124 | */ |
| 125 | - private function checkDecode(array $options, array $headers, $stream): array | |
| 125 | + private function checkDecode(array $options, array $headers, $stream) : array | |
| 126 | 126 | { |
| 127 | 127 | // Automatically decode responses when instructed. |
| 128 | 128 | if (!empty($options['decode_content'])) { |
| 129 | 129 | $normalizedKeys = Utils::normalizeHeaderKeys($headers); |
| @@ -156,9 +156,9 @@ | ||
| 156 | 156 | * data to read. |
| 157 | 157 | * |
| 158 | 158 | * @throws \RuntimeException when the sink option is invalid. |
| 159 | 159 | */ |
| 160 | - private function drain(StreamInterface $source, StreamInterface $sink, string $contentLength): StreamInterface | |
| 160 | + private function drain(StreamInterface $source, StreamInterface $sink, string $contentLength) : StreamInterface | |
| 161 | 161 | { |
| 162 | 162 | // If a content-length header is provided, then stop reading once |
| 163 | 163 | // that number of bytes has been read. This can prevent infinitely |
| 164 | 164 | // reading from a stream when dealing with servers that do not honor |
| @@ -179,9 +179,9 @@ | ||
| 179 | 179 | */ |
| 180 | 180 | private function createResource(callable $callback) |
| 181 | 181 | { |
| 182 | 182 | $errors = []; |
| 183 | - \set_error_handler(static function ($_, $msg, $file, $line) use (&$errors): bool { | |
| 183 | + \set_error_handler(static function ($_, $msg, $file, $line) use(&$errors) : bool { | |
| 184 | 184 | $errors[] = ['message' => $msg, 'file' => $file, 'line' => $line]; |
| 185 | 185 | return \true; |
| 186 | 186 | }); |
| 187 | 187 | try { |
| @@ -244,16 +244,21 @@ | ||
| 244 | 244 | if (isset($options['auth'][2]) && 'ntlm' === $options['auth'][2]) { |
| 245 | 245 | throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler'); |
| 246 | 246 | } |
| 247 | 247 | $uri = $this->resolveHost($request, $options); |
| 248 | - $contextResource = $this->createResource(static function () use ($context, $params) { | |
| 248 | + $contextResource = $this->createResource(static function () use($context, $params) { | |
| 249 | 249 | return \stream_context_create($context, $params); |
| 250 | 250 | }); |
| 251 | - return $this->createResource(function () use ($uri, &$http_response_header, $contextResource, $context, $options, $request) { | |
| 251 | + return $this->createResource(function () use($uri, $contextResource, $context, $options, $request) { | |
| 252 | 252 | $resource = @\fopen((string) $uri, 'r', \false, $contextResource); |
| 253 | + // See https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_http_response_header_predefined_variable | |
| 254 | + if (\function_exists('Dudlewebs\\WPMCS\\GCP\\http_get_last_response_headers')) { | |
| 255 | + /** @var array|null */ | |
| 256 | + $http_response_header = \Dudlewebs\WPMCS\GCP\http_get_last_response_headers(); | |
| 257 | + } | |
| 253 | 258 | $this->lastHeaders = $http_response_header ?? []; |
| 254 | 259 | if (\false === $resource) { |
| 255 | - throw new ConnectException(sprintf('Connection refused for URI %s', $uri), $request, null, $context); | |
| 260 | + throw new ConnectException(\sprintf('Connection refused for URI %s', $uri), $request, null, $context); | |
| 256 | 261 | } |
| 257 | 262 | if (isset($options['read_timeout'])) { |
| 258 | 263 | $readTimeout = $options['read_timeout']; |
| 259 | 264 | $sec = (int) $readTimeout; |
| @@ -262,9 +267,9 @@ | ||
| 262 | 267 | } |
| 263 | 268 | return $resource; |
| 264 | 269 | }); |
| 265 | 270 | } |
| 266 | - private function resolveHost(RequestInterface $request, array $options): UriInterface | |
| 271 | + private function resolveHost(RequestInterface $request, array $options) : UriInterface | |
| 267 | 272 | { |
| 268 | 273 | $uri = $request->getUri(); |
| 269 | 274 | if (isset($options['force_ip_resolve']) && !\filter_var($uri->getHost(), \FILTER_VALIDATE_IP)) { |
| 270 | 275 | if ('v4' === $options['force_ip_resolve']) { |
| @@ -283,9 +288,9 @@ | ||
| 283 | 288 | } |
| 284 | 289 | } |
| 285 | 290 | return $uri; |
| 286 | 291 | } |
| 287 | - private function getDefaultContext(RequestInterface $request): array | |
| 292 | + private function getDefaultContext(RequestInterface $request) : array | |
| 288 | 293 | { |
| 289 | 294 | $headers = ''; |
| 290 | 295 | foreach ($request->getHeaders() as $name => $value) { |
| 291 | 296 | foreach ($value as $val) { |
| @@ -306,9 +311,9 @@ | ||
| 306 | 311 | } |
| 307 | 312 | /** |
| 308 | 313 | * @param mixed $value as passed via Request transfer options. |
| 309 | 314 | */ |
| 310 | - private function add_proxy(RequestInterface $request, array &$options, $value, array &$params): void | |
| 315 | + private function add_proxy(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 311 | 316 | { |
| 312 | 317 | $uri = null; |
| 313 | 318 | if (!\is_array($value)) { |
| 314 | 319 | $uri = $value; |
| @@ -334,9 +339,9 @@ | ||
| 334 | 339 | } |
| 335 | 340 | /** |
| 336 | 341 | * Parses the given proxy URL to make it compatible with the format PHP's stream context expects. |
| 337 | 342 | */ |
| 338 | - private function parse_proxy(string $url): array | |
| 343 | + private function parse_proxy(string $url) : array | |
| 339 | 344 | { |
| 340 | 345 | $parsed = \parse_url($url); |
| 341 | 346 | if ($parsed !== \false && isset($parsed['scheme']) && $parsed['scheme'] === 'http') { |
| 342 | 347 | if (isset($parsed['host']) && isset($parsed['port'])) { |
| @@ -352,9 +357,9 @@ | ||
| 352 | 357 | } |
| 353 | 358 | /** |
| 354 | 359 | * @param mixed $value as passed via Request transfer options. |
| 355 | 360 | */ |
| 356 | - private function add_timeout(RequestInterface $request, array &$options, $value, array &$params): void | |
| 361 | + private function add_timeout(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 357 | 362 | { |
| 358 | 363 | if ($value > 0) { |
| 359 | 364 | $options['http']['timeout'] = $value; |
| 360 | 365 | } |
| @@ -361,11 +366,11 @@ | ||
| 361 | 366 | } |
| 362 | 367 | /** |
| 363 | 368 | * @param mixed $value as passed via Request transfer options. |
| 364 | 369 | */ |
| 365 | - private function add_crypto_method(RequestInterface $request, array &$options, $value, array &$params): void | |
| 370 | + private function add_crypto_method(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 366 | 371 | { |
| 367 | - if ($value === \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT || $value === \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT || $value === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT || defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && $value === \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT) { | |
| 372 | + if ($value === \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT || $value === \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT || $value === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT || \defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && $value === \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT) { | |
| 368 | 373 | $options['http']['crypto_method'] = $value; |
| 369 | 374 | return; |
| 370 | 375 | } |
| 371 | 376 | throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided'); |
| @@ -372,9 +377,9 @@ | ||
| 372 | 377 | } |
| 373 | 378 | /** |
| 374 | 379 | * @param mixed $value as passed via Request transfer options. |
| 375 | 380 | */ |
| 376 | - private function add_verify(RequestInterface $request, array &$options, $value, array &$params): void | |
| 381 | + private function add_verify(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 377 | 382 | { |
| 378 | 383 | if ($value === \false) { |
| 379 | 384 | $options['ssl']['verify_peer'] = \false; |
| 380 | 385 | $options['ssl']['verify_peer_name'] = \false; |
| @@ -394,9 +399,9 @@ | ||
| 394 | 399 | } |
| 395 | 400 | /** |
| 396 | 401 | * @param mixed $value as passed via Request transfer options. |
| 397 | 402 | */ |
| 398 | - private function add_cert(RequestInterface $request, array &$options, $value, array &$params): void | |
| 403 | + private function add_cert(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 399 | 404 | { |
| 400 | 405 | if (\is_array($value)) { |
| 401 | 406 | $options['ssl']['passphrase'] = $value[1]; |
| 402 | 407 | $value = $value[0]; |
| @@ -408,11 +413,11 @@ | ||
| 408 | 413 | } |
| 409 | 414 | /** |
| 410 | 415 | * @param mixed $value as passed via Request transfer options. |
| 411 | 416 | */ |
| 412 | - private function add_progress(RequestInterface $request, array &$options, $value, array &$params): void | |
| 417 | + private function add_progress(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 413 | 418 | { |
| 414 | - self::addNotification($params, static function ($code, $a, $b, $c, $transferred, $total) use ($value) { | |
| 419 | + self::addNotification($params, static function ($code, $a, $b, $c, $transferred, $total) use($value) { | |
| 415 | 420 | if ($code == \STREAM_NOTIFY_PROGRESS) { |
| 416 | 421 | // The upload progress cannot be determined. Use 0 for cURL compatibility: |
| 417 | 422 | // https://curl.se/libcurl/c/CURLOPT_PROGRESSFUNCTION.html |
| 418 | 423 | $value($total, $transferred, 0, 0); |
| @@ -421,9 +426,9 @@ | ||
| 421 | 426 | } |
| 422 | 427 | /** |
| 423 | 428 | * @param mixed $value as passed via Request transfer options. |
| 424 | 429 | */ |
| 425 | - private function add_debug(RequestInterface $request, array &$options, $value, array &$params): void | |
| 430 | + private function add_debug(RequestInterface $request, array &$options, $value, array &$params) : void | |
| 426 | 431 | { |
| 427 | 432 | if ($value === \false) { |
| 428 | 433 | return; |
| 429 | 434 | } |
| @@ -430,9 +435,9 @@ | ||
| 430 | 435 | static $map = [\STREAM_NOTIFY_CONNECT => 'CONNECT', \STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED', \STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT', \STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS', \STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS', \STREAM_NOTIFY_REDIRECTED => 'REDIRECTED', \STREAM_NOTIFY_PROGRESS => 'PROGRESS', \STREAM_NOTIFY_FAILURE => 'FAILURE', \STREAM_NOTIFY_COMPLETED => 'COMPLETED', \STREAM_NOTIFY_RESOLVE => 'RESOLVE']; |
| 431 | 436 | static $args = ['severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max']; |
| 432 | 437 | $value = Utils::debugResource($value); |
| 433 | 438 | $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment(''); |
| 434 | - self::addNotification($params, static function (int $code, ...$passed) use ($ident, $value, $map, $args): void { | |
| 439 | + self::addNotification($params, static function (int $code, ...$passed) use($ident, $value, $map, $args) : void { | |
| 435 | 440 | \fprintf($value, '<%s> [%s] ', $ident, $map[$code]); |
| 436 | 441 | foreach (\array_filter($passed) as $i => $v) { |
| 437 | 442 | \fwrite($value, $args[$i] . ': "' . $v . '" '); |
| 438 | 443 | } |
| @@ -438,9 +443,9 @@ | ||
| 438 | 443 | } |
| 439 | 444 | \fwrite($value, "\n"); |
| 440 | 445 | }); |
| 441 | 446 | } |
| 442 | - private static function addNotification(array &$params, callable $notify): void | |
| 447 | + private static function addNotification(array &$params, callable $notify) : void | |
| 443 | 448 | { |
| 444 | 449 | // Wrap the existing function if needed. |
| 445 | 450 | if (!isset($params['notification'])) { |
| 446 | 451 | $params['notification'] = $notify; |
| @@ -447,11 +452,11 @@ | ||
| 447 | 452 | } else { |
| 448 | 453 | $params['notification'] = self::callArray([$params['notification'], $notify]); |
| 449 | 454 | } |
| 450 | 455 | } |
| 451 | - private static function callArray(array $functions): callable | |
| 456 | + private static function callArray(array $functions) : callable | |
| 452 | 457 | { |
| 453 | - return static function (...$args) use ($functions) { | |
| 458 | + return static function (...$args) use($functions) { | |
| 454 | 459 | foreach ($functions as $fn) { |
| 455 | 460 | $fn(...$args); |
| 456 | 461 | } |
| 457 | 462 | }; |