@@ -4,5 +4,5 @@ |
||
| 4 | 4 | |
| 5 | 5 | // Don't redefine the functions if included multiple times. |
| 6 | 6 | if (!\function_exists('OCA\\FullTextSearch_Elasticsearch\\Vendor\\GuzzleHttp\\describe_type')) { |
| 7 | - require __DIR__ . '/functions.php'; |
|
| 7 | + require __DIR__ . '/functions.php'; |
|
| 8 | 8 | } |
@@ -4,5 +4,5 @@ |
||
| 4 | 4 | |
| 5 | 5 | // Don't redefine the functions if included multiple times. |
| 6 | 6 | if (!\function_exists('OCA\\FullTextSearch_Elasticsearch\\Vendor\\GuzzleHttp\\describe_type')) { |
| 7 | - require __DIR__ . '/functions.php'; |
|
| 7 | + require __DIR__.'/functions.php'; |
|
| 8 | 8 | } |
@@ -18,145 +18,145 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class RedirectMiddleware |
| 20 | 20 | { |
| 21 | - public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 22 | - public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - public static $defaultSettings = ['max' => 5, 'protocols' => ['http', 'https'], 'strict' => \false, 'referer' => \false, 'track_redirects' => \false]; |
|
| 27 | - /** |
|
| 28 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 29 | - */ |
|
| 30 | - private $nextHandler; |
|
| 31 | - /** |
|
| 32 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 33 | - */ |
|
| 34 | - public function __construct(callable $nextHandler) |
|
| 35 | - { |
|
| 36 | - $this->nextHandler = $nextHandler; |
|
| 37 | - } |
|
| 38 | - public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 39 | - { |
|
| 40 | - $fn = $this->nextHandler; |
|
| 41 | - if (empty($options['allow_redirects'])) { |
|
| 42 | - return $fn($request, $options); |
|
| 43 | - } |
|
| 44 | - if ($options['allow_redirects'] === \true) { |
|
| 45 | - $options['allow_redirects'] = self::$defaultSettings; |
|
| 46 | - } elseif (!\is_array($options['allow_redirects'])) { |
|
| 47 | - throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 48 | - } else { |
|
| 49 | - // Merge the default settings with the provided settings |
|
| 50 | - $options['allow_redirects'] += self::$defaultSettings; |
|
| 51 | - } |
|
| 52 | - if (empty($options['allow_redirects']['max'])) { |
|
| 53 | - return $fn($request, $options); |
|
| 54 | - } |
|
| 55 | - return $fn($request, $options)->then(function (ResponseInterface $response) use($request, $options) { |
|
| 56 | - return $this->checkRedirect($request, $options, $response); |
|
| 57 | - }); |
|
| 58 | - } |
|
| 59 | - /** |
|
| 60 | - * @return ResponseInterface|PromiseInterface |
|
| 61 | - */ |
|
| 62 | - public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 63 | - { |
|
| 64 | - if (\strpos((string) $response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 65 | - return $response; |
|
| 66 | - } |
|
| 67 | - $this->guardMax($request, $response, $options); |
|
| 68 | - $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 69 | - // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 70 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && \defined('\\CURLOPT_HTTPAUTH')) { |
|
| 71 | - unset($options['curl'][\CURLOPT_HTTPAUTH], $options['curl'][\CURLOPT_USERPWD]); |
|
| 72 | - } |
|
| 73 | - if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 74 | - $options['allow_redirects']['on_redirect']($request, $response, $nextRequest->getUri()); |
|
| 75 | - } |
|
| 76 | - $promise = $this($nextRequest, $options); |
|
| 77 | - // Add headers to be able to track history of redirects. |
|
| 78 | - if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 79 | - return $this->withTracking($promise, (string) $nextRequest->getUri(), $response->getStatusCode()); |
|
| 80 | - } |
|
| 81 | - return $promise; |
|
| 82 | - } |
|
| 83 | - /** |
|
| 84 | - * Enable tracking on promise. |
|
| 85 | - */ |
|
| 86 | - private function withTracking(PromiseInterface $promise, string $uri, int $statusCode) : PromiseInterface |
|
| 87 | - { |
|
| 88 | - return $promise->then(static function (ResponseInterface $response) use($uri, $statusCode) { |
|
| 89 | - // Note that we are pushing to the front of the list as this |
|
| 90 | - // would be an earlier response than what is currently present |
|
| 91 | - // in the history header. |
|
| 92 | - $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 93 | - $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 94 | - \array_unshift($historyHeader, $uri); |
|
| 95 | - \array_unshift($statusHeader, (string) $statusCode); |
|
| 96 | - return $response->withHeader(self::HISTORY_HEADER, $historyHeader)->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 97 | - }); |
|
| 98 | - } |
|
| 99 | - /** |
|
| 100 | - * Check for too many redirects. |
|
| 101 | - * |
|
| 102 | - * @throws TooManyRedirectsException Too many redirects. |
|
| 103 | - */ |
|
| 104 | - private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options) : void |
|
| 105 | - { |
|
| 106 | - $current = $options['__redirect_count'] ?? 0; |
|
| 107 | - $options['__redirect_count'] = $current + 1; |
|
| 108 | - $max = $options['allow_redirects']['max']; |
|
| 109 | - if ($options['__redirect_count'] > $max) { |
|
| 110 | - throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response) : RequestInterface |
|
| 114 | - { |
|
| 115 | - // Request modifications to apply. |
|
| 116 | - $modify = []; |
|
| 117 | - $protocols = $options['allow_redirects']['protocols']; |
|
| 118 | - // Use a GET request if this is an entity enclosing request and we are |
|
| 119 | - // not forcing RFC compliance, but rather emulating what all browsers |
|
| 120 | - // would do. |
|
| 121 | - $statusCode = $response->getStatusCode(); |
|
| 122 | - if ($statusCode == 303 || $statusCode <= 302 && !$options['allow_redirects']['strict']) { |
|
| 123 | - $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 124 | - $requestMethod = $request->getMethod(); |
|
| 125 | - $modify['method'] = \in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 126 | - $modify['body'] = ''; |
|
| 127 | - } |
|
| 128 | - $uri = self::redirectUri($request, $response, $protocols); |
|
| 129 | - if (isset($options['idn_conversion']) && $options['idn_conversion'] !== \false) { |
|
| 130 | - $idnOptions = $options['idn_conversion'] === \true ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 131 | - $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 132 | - } |
|
| 133 | - $modify['uri'] = $uri; |
|
| 134 | - Psr7\Message::rewindBody($request); |
|
| 135 | - // Add the Referer header if it is told to do so and only |
|
| 136 | - // add the header if we are not redirecting from https to http. |
|
| 137 | - if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { |
|
| 138 | - $uri = $request->getUri()->withUserInfo(''); |
|
| 139 | - $modify['set_headers']['Referer'] = (string) $uri; |
|
| 140 | - } else { |
|
| 141 | - $modify['remove_headers'][] = 'Referer'; |
|
| 142 | - } |
|
| 143 | - // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 144 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 145 | - $modify['remove_headers'][] = 'Authorization'; |
|
| 146 | - $modify['remove_headers'][] = 'Cookie'; |
|
| 147 | - } |
|
| 148 | - return Psr7\Utils::modifyRequest($request, $modify); |
|
| 149 | - } |
|
| 150 | - /** |
|
| 151 | - * Set the appropriate URL on the request based on the location header. |
|
| 152 | - */ |
|
| 153 | - private static function redirectUri(RequestInterface $request, ResponseInterface $response, array $protocols) : UriInterface |
|
| 154 | - { |
|
| 155 | - $location = Psr7\UriResolver::resolve($request->getUri(), new Psr7\Uri($response->getHeaderLine('Location'))); |
|
| 156 | - // Ensure that the redirect URI is allowed based on the protocols. |
|
| 157 | - if (!\in_array($location->getScheme(), $protocols)) { |
|
| 158 | - throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 159 | - } |
|
| 160 | - return $location; |
|
| 161 | - } |
|
| 21 | + public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 22 | + public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + public static $defaultSettings = ['max' => 5, 'protocols' => ['http', 'https'], 'strict' => \false, 'referer' => \false, 'track_redirects' => \false]; |
|
| 27 | + /** |
|
| 28 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 29 | + */ |
|
| 30 | + private $nextHandler; |
|
| 31 | + /** |
|
| 32 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 33 | + */ |
|
| 34 | + public function __construct(callable $nextHandler) |
|
| 35 | + { |
|
| 36 | + $this->nextHandler = $nextHandler; |
|
| 37 | + } |
|
| 38 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 39 | + { |
|
| 40 | + $fn = $this->nextHandler; |
|
| 41 | + if (empty($options['allow_redirects'])) { |
|
| 42 | + return $fn($request, $options); |
|
| 43 | + } |
|
| 44 | + if ($options['allow_redirects'] === \true) { |
|
| 45 | + $options['allow_redirects'] = self::$defaultSettings; |
|
| 46 | + } elseif (!\is_array($options['allow_redirects'])) { |
|
| 47 | + throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 48 | + } else { |
|
| 49 | + // Merge the default settings with the provided settings |
|
| 50 | + $options['allow_redirects'] += self::$defaultSettings; |
|
| 51 | + } |
|
| 52 | + if (empty($options['allow_redirects']['max'])) { |
|
| 53 | + return $fn($request, $options); |
|
| 54 | + } |
|
| 55 | + return $fn($request, $options)->then(function (ResponseInterface $response) use($request, $options) { |
|
| 56 | + return $this->checkRedirect($request, $options, $response); |
|
| 57 | + }); |
|
| 58 | + } |
|
| 59 | + /** |
|
| 60 | + * @return ResponseInterface|PromiseInterface |
|
| 61 | + */ |
|
| 62 | + public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 63 | + { |
|
| 64 | + if (\strpos((string) $response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 65 | + return $response; |
|
| 66 | + } |
|
| 67 | + $this->guardMax($request, $response, $options); |
|
| 68 | + $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 69 | + // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 70 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && \defined('\\CURLOPT_HTTPAUTH')) { |
|
| 71 | + unset($options['curl'][\CURLOPT_HTTPAUTH], $options['curl'][\CURLOPT_USERPWD]); |
|
| 72 | + } |
|
| 73 | + if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 74 | + $options['allow_redirects']['on_redirect']($request, $response, $nextRequest->getUri()); |
|
| 75 | + } |
|
| 76 | + $promise = $this($nextRequest, $options); |
|
| 77 | + // Add headers to be able to track history of redirects. |
|
| 78 | + if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 79 | + return $this->withTracking($promise, (string) $nextRequest->getUri(), $response->getStatusCode()); |
|
| 80 | + } |
|
| 81 | + return $promise; |
|
| 82 | + } |
|
| 83 | + /** |
|
| 84 | + * Enable tracking on promise. |
|
| 85 | + */ |
|
| 86 | + private function withTracking(PromiseInterface $promise, string $uri, int $statusCode) : PromiseInterface |
|
| 87 | + { |
|
| 88 | + return $promise->then(static function (ResponseInterface $response) use($uri, $statusCode) { |
|
| 89 | + // Note that we are pushing to the front of the list as this |
|
| 90 | + // would be an earlier response than what is currently present |
|
| 91 | + // in the history header. |
|
| 92 | + $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 93 | + $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 94 | + \array_unshift($historyHeader, $uri); |
|
| 95 | + \array_unshift($statusHeader, (string) $statusCode); |
|
| 96 | + return $response->withHeader(self::HISTORY_HEADER, $historyHeader)->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 97 | + }); |
|
| 98 | + } |
|
| 99 | + /** |
|
| 100 | + * Check for too many redirects. |
|
| 101 | + * |
|
| 102 | + * @throws TooManyRedirectsException Too many redirects. |
|
| 103 | + */ |
|
| 104 | + private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options) : void |
|
| 105 | + { |
|
| 106 | + $current = $options['__redirect_count'] ?? 0; |
|
| 107 | + $options['__redirect_count'] = $current + 1; |
|
| 108 | + $max = $options['allow_redirects']['max']; |
|
| 109 | + if ($options['__redirect_count'] > $max) { |
|
| 110 | + throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response) : RequestInterface |
|
| 114 | + { |
|
| 115 | + // Request modifications to apply. |
|
| 116 | + $modify = []; |
|
| 117 | + $protocols = $options['allow_redirects']['protocols']; |
|
| 118 | + // Use a GET request if this is an entity enclosing request and we are |
|
| 119 | + // not forcing RFC compliance, but rather emulating what all browsers |
|
| 120 | + // would do. |
|
| 121 | + $statusCode = $response->getStatusCode(); |
|
| 122 | + if ($statusCode == 303 || $statusCode <= 302 && !$options['allow_redirects']['strict']) { |
|
| 123 | + $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 124 | + $requestMethod = $request->getMethod(); |
|
| 125 | + $modify['method'] = \in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 126 | + $modify['body'] = ''; |
|
| 127 | + } |
|
| 128 | + $uri = self::redirectUri($request, $response, $protocols); |
|
| 129 | + if (isset($options['idn_conversion']) && $options['idn_conversion'] !== \false) { |
|
| 130 | + $idnOptions = $options['idn_conversion'] === \true ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 131 | + $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 132 | + } |
|
| 133 | + $modify['uri'] = $uri; |
|
| 134 | + Psr7\Message::rewindBody($request); |
|
| 135 | + // Add the Referer header if it is told to do so and only |
|
| 136 | + // add the header if we are not redirecting from https to http. |
|
| 137 | + if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { |
|
| 138 | + $uri = $request->getUri()->withUserInfo(''); |
|
| 139 | + $modify['set_headers']['Referer'] = (string) $uri; |
|
| 140 | + } else { |
|
| 141 | + $modify['remove_headers'][] = 'Referer'; |
|
| 142 | + } |
|
| 143 | + // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 144 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 145 | + $modify['remove_headers'][] = 'Authorization'; |
|
| 146 | + $modify['remove_headers'][] = 'Cookie'; |
|
| 147 | + } |
|
| 148 | + return Psr7\Utils::modifyRequest($request, $modify); |
|
| 149 | + } |
|
| 150 | + /** |
|
| 151 | + * Set the appropriate URL on the request based on the location header. |
|
| 152 | + */ |
|
| 153 | + private static function redirectUri(RequestInterface $request, ResponseInterface $response, array $protocols) : UriInterface |
|
| 154 | + { |
|
| 155 | + $location = Psr7\UriResolver::resolve($request->getUri(), new Psr7\Uri($response->getHeaderLine('Location'))); |
|
| 156 | + // Ensure that the redirect URI is allowed based on the protocols. |
|
| 157 | + if (!\in_array($location->getScheme(), $protocols)) { |
|
| 158 | + throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 159 | + } |
|
| 160 | + return $location; |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | if (empty($options['allow_redirects']['max'])) { |
| 53 | 53 | return $fn($request, $options); |
| 54 | 54 | } |
| 55 | - return $fn($request, $options)->then(function (ResponseInterface $response) use($request, $options) { |
|
| 55 | + return $fn($request, $options)->then(function(ResponseInterface $response) use($request, $options) { |
|
| 56 | 56 | return $this->checkRedirect($request, $options, $response); |
| 57 | 57 | }); |
| 58 | 58 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
| 63 | 63 | { |
| 64 | - if (\strpos((string) $response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 64 | + if (\strpos((string)$response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 65 | 65 | return $response; |
| 66 | 66 | } |
| 67 | 67 | $this->guardMax($request, $response, $options); |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $promise = $this($nextRequest, $options); |
| 77 | 77 | // Add headers to be able to track history of redirects. |
| 78 | 78 | if (!empty($options['allow_redirects']['track_redirects'])) { |
| 79 | - return $this->withTracking($promise, (string) $nextRequest->getUri(), $response->getStatusCode()); |
|
| 79 | + return $this->withTracking($promise, (string)$nextRequest->getUri(), $response->getStatusCode()); |
|
| 80 | 80 | } |
| 81 | 81 | return $promise; |
| 82 | 82 | } |
@@ -85,14 +85,14 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | private function withTracking(PromiseInterface $promise, string $uri, int $statusCode) : PromiseInterface |
| 87 | 87 | { |
| 88 | - return $promise->then(static function (ResponseInterface $response) use($uri, $statusCode) { |
|
| 88 | + return $promise->then(static function(ResponseInterface $response) use($uri, $statusCode) { |
|
| 89 | 89 | // Note that we are pushing to the front of the list as this |
| 90 | 90 | // would be an earlier response than what is currently present |
| 91 | 91 | // in the history header. |
| 92 | 92 | $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
| 93 | 93 | $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
| 94 | 94 | \array_unshift($historyHeader, $uri); |
| 95 | - \array_unshift($statusHeader, (string) $statusCode); |
|
| 95 | + \array_unshift($statusHeader, (string)$statusCode); |
|
| 96 | 96 | return $response->withHeader(self::HISTORY_HEADER, $historyHeader)->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
| 97 | 97 | }); |
| 98 | 98 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | // add the header if we are not redirecting from https to http. |
| 137 | 137 | if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { |
| 138 | 138 | $uri = $request->getUri()->withUserInfo(''); |
| 139 | - $modify['set_headers']['Referer'] = (string) $uri; |
|
| 139 | + $modify['set_headers']['Referer'] = (string)$uri; |
|
| 140 | 140 | } else { |
| 141 | 141 | $modify['remove_headers'][] = 'Referer'; |
| 142 | 142 | } |
@@ -16,8 +16,7 @@ |
||
| 16 | 16 | * |
| 17 | 17 | * @final |
| 18 | 18 | */ |
| 19 | -class RedirectMiddleware |
|
| 20 | -{ |
|
| 19 | +class RedirectMiddleware { |
|
| 21 | 20 | public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
| 22 | 21 | public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
| 23 | 22 | /** |
@@ -15,388 +15,388 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Client implements ClientInterface, \OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientInterface |
| 17 | 17 | { |
| 18 | - use ClientTrait; |
|
| 19 | - /** |
|
| 20 | - * @var array Default request options |
|
| 21 | - */ |
|
| 22 | - private $config; |
|
| 23 | - /** |
|
| 24 | - * Clients accept an array of constructor parameters. |
|
| 25 | - * |
|
| 26 | - * Here's an example of creating a client using a base_uri and an array of |
|
| 27 | - * default request options to apply to each request: |
|
| 28 | - * |
|
| 29 | - * $client = new Client([ |
|
| 30 | - * 'base_uri' => 'http://www.foo.com/1.0/', |
|
| 31 | - * 'timeout' => 0, |
|
| 32 | - * 'allow_redirects' => false, |
|
| 33 | - * 'proxy' => '192.168.16.1:10' |
|
| 34 | - * ]); |
|
| 35 | - * |
|
| 36 | - * Client configuration settings include the following options: |
|
| 37 | - * |
|
| 38 | - * - handler: (callable) Function that transfers HTTP requests over the |
|
| 39 | - * wire. The function is called with a Psr7\Http\Message\RequestInterface |
|
| 40 | - * and array of transfer options, and must return a |
|
| 41 | - * GuzzleHttp\Promise\PromiseInterface that is fulfilled with a |
|
| 42 | - * Psr7\Http\Message\ResponseInterface on success. |
|
| 43 | - * If no handler is provided, a default handler will be created |
|
| 44 | - * that enables all of the request options below by attaching all of the |
|
| 45 | - * default middleware to the handler. |
|
| 46 | - * - base_uri: (string|UriInterface) Base URI of the client that is merged |
|
| 47 | - * into relative URIs. Can be a string or instance of UriInterface. |
|
| 48 | - * - **: any request option |
|
| 49 | - * |
|
| 50 | - * @param array $config Client configuration settings. |
|
| 51 | - * |
|
| 52 | - * @see \GuzzleHttp\RequestOptions for a list of available request options. |
|
| 53 | - */ |
|
| 54 | - public function __construct(array $config = []) |
|
| 55 | - { |
|
| 56 | - if (!isset($config['handler'])) { |
|
| 57 | - $config['handler'] = HandlerStack::create(); |
|
| 58 | - } elseif (!\is_callable($config['handler'])) { |
|
| 59 | - throw new InvalidArgumentException('handler must be a callable'); |
|
| 60 | - } |
|
| 61 | - // Convert the base_uri to a UriInterface |
|
| 62 | - if (isset($config['base_uri'])) { |
|
| 63 | - $config['base_uri'] = Psr7\Utils::uriFor($config['base_uri']); |
|
| 64 | - } |
|
| 65 | - $this->configureDefaults($config); |
|
| 66 | - } |
|
| 67 | - /** |
|
| 68 | - * @param string $method |
|
| 69 | - * @param array $args |
|
| 70 | - * |
|
| 71 | - * @return PromiseInterface|ResponseInterface |
|
| 72 | - * |
|
| 73 | - * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0. |
|
| 74 | - */ |
|
| 75 | - public function __call($method, $args) |
|
| 76 | - { |
|
| 77 | - if (\count($args) < 1) { |
|
| 78 | - throw new InvalidArgumentException('Magic request methods require a URI and optional options array'); |
|
| 79 | - } |
|
| 80 | - $uri = $args[0]; |
|
| 81 | - $opts = $args[1] ?? []; |
|
| 82 | - return \substr($method, -5) === 'Async' ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) : $this->request($method, $uri, $opts); |
|
| 83 | - } |
|
| 84 | - /** |
|
| 85 | - * Asynchronously send an HTTP request. |
|
| 86 | - * |
|
| 87 | - * @param array $options Request options to apply to the given |
|
| 88 | - * request and to the transfer. See \GuzzleHttp\RequestOptions. |
|
| 89 | - */ |
|
| 90 | - public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface |
|
| 91 | - { |
|
| 92 | - // Merge the base URI into the request URI if needed. |
|
| 93 | - $options = $this->prepareDefaults($options); |
|
| 94 | - return $this->transfer($request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), $options); |
|
| 95 | - } |
|
| 96 | - /** |
|
| 97 | - * Send an HTTP request. |
|
| 98 | - * |
|
| 99 | - * @param array $options Request options to apply to the given |
|
| 100 | - * request and to the transfer. See \GuzzleHttp\RequestOptions. |
|
| 101 | - * |
|
| 102 | - * @throws GuzzleException |
|
| 103 | - */ |
|
| 104 | - public function send(RequestInterface $request, array $options = []) : ResponseInterface |
|
| 105 | - { |
|
| 106 | - $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 107 | - return $this->sendAsync($request, $options)->wait(); |
|
| 108 | - } |
|
| 109 | - /** |
|
| 110 | - * The HttpClient PSR (PSR-18) specify this method. |
|
| 111 | - * |
|
| 112 | - * {@inheritDoc} |
|
| 113 | - */ |
|
| 114 | - public function sendRequest(RequestInterface $request) : ResponseInterface |
|
| 115 | - { |
|
| 116 | - $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 117 | - $options[RequestOptions::ALLOW_REDIRECTS] = \false; |
|
| 118 | - $options[RequestOptions::HTTP_ERRORS] = \false; |
|
| 119 | - return $this->sendAsync($request, $options)->wait(); |
|
| 120 | - } |
|
| 121 | - /** |
|
| 122 | - * Create and send an asynchronous HTTP request. |
|
| 123 | - * |
|
| 124 | - * Use an absolute path to override the base path of the client, or a |
|
| 125 | - * relative path to append to the base path of the client. The URL can |
|
| 126 | - * contain the query string as well. Use an array to provide a URL |
|
| 127 | - * template and additional variables to use in the URL template expansion. |
|
| 128 | - * |
|
| 129 | - * @param string $method HTTP method |
|
| 130 | - * @param string|UriInterface $uri URI object or string. |
|
| 131 | - * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. |
|
| 132 | - */ |
|
| 133 | - public function requestAsync(string $method, $uri = '', array $options = []) : PromiseInterface |
|
| 134 | - { |
|
| 135 | - $options = $this->prepareDefaults($options); |
|
| 136 | - // Remove request modifying parameter because it can be done up-front. |
|
| 137 | - $headers = $options['headers'] ?? []; |
|
| 138 | - $body = $options['body'] ?? null; |
|
| 139 | - $version = $options['version'] ?? '1.1'; |
|
| 140 | - // Merge the URI into the base URI. |
|
| 141 | - $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options); |
|
| 142 | - if (\is_array($body)) { |
|
| 143 | - throw $this->invalidBody(); |
|
| 144 | - } |
|
| 145 | - $request = new Psr7\Request($method, $uri, $headers, $body, $version); |
|
| 146 | - // Remove the option so that they are not doubly-applied. |
|
| 147 | - unset($options['headers'], $options['body'], $options['version']); |
|
| 148 | - return $this->transfer($request, $options); |
|
| 149 | - } |
|
| 150 | - /** |
|
| 151 | - * Create and send an HTTP request. |
|
| 152 | - * |
|
| 153 | - * Use an absolute path to override the base path of the client, or a |
|
| 154 | - * relative path to append to the base path of the client. The URL can |
|
| 155 | - * contain the query string as well. |
|
| 156 | - * |
|
| 157 | - * @param string $method HTTP method. |
|
| 158 | - * @param string|UriInterface $uri URI object or string. |
|
| 159 | - * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. |
|
| 160 | - * |
|
| 161 | - * @throws GuzzleException |
|
| 162 | - */ |
|
| 163 | - public function request(string $method, $uri = '', array $options = []) : ResponseInterface |
|
| 164 | - { |
|
| 165 | - $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 166 | - return $this->requestAsync($method, $uri, $options)->wait(); |
|
| 167 | - } |
|
| 168 | - /** |
|
| 169 | - * Get a client configuration option. |
|
| 170 | - * |
|
| 171 | - * These options include default request options of the client, a "handler" |
|
| 172 | - * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 173 | - * the concrete client. |
|
| 174 | - * |
|
| 175 | - * @param string|null $option The config option to retrieve. |
|
| 176 | - * |
|
| 177 | - * @return mixed |
|
| 178 | - * |
|
| 179 | - * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 180 | - */ |
|
| 181 | - public function getConfig(string $option = null) |
|
| 182 | - { |
|
| 183 | - return $option === null ? $this->config : $this->config[$option] ?? null; |
|
| 184 | - } |
|
| 185 | - private function buildUri(UriInterface $uri, array $config) : UriInterface |
|
| 186 | - { |
|
| 187 | - if (isset($config['base_uri'])) { |
|
| 188 | - $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri); |
|
| 189 | - } |
|
| 190 | - if (isset($config['idn_conversion']) && $config['idn_conversion'] !== \false) { |
|
| 191 | - $idnOptions = $config['idn_conversion'] === \true ? \IDNA_DEFAULT : $config['idn_conversion']; |
|
| 192 | - $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 193 | - } |
|
| 194 | - return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; |
|
| 195 | - } |
|
| 196 | - /** |
|
| 197 | - * Configures the default options for a client. |
|
| 198 | - */ |
|
| 199 | - private function configureDefaults(array $config) : void |
|
| 200 | - { |
|
| 201 | - $defaults = ['allow_redirects' => RedirectMiddleware::$defaultSettings, 'http_errors' => \true, 'decode_content' => \true, 'verify' => \true, 'cookies' => \false, 'idn_conversion' => \false]; |
|
| 202 | - // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. |
|
| 203 | - // We can only trust the HTTP_PROXY environment variable in a CLI |
|
| 204 | - // process due to the fact that PHP has no reliable mechanism to |
|
| 205 | - // get environment variables that start with "HTTP_". |
|
| 206 | - if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) { |
|
| 207 | - $defaults['proxy']['http'] = $proxy; |
|
| 208 | - } |
|
| 209 | - if ($proxy = Utils::getenv('HTTPS_PROXY')) { |
|
| 210 | - $defaults['proxy']['https'] = $proxy; |
|
| 211 | - } |
|
| 212 | - if ($noProxy = Utils::getenv('NO_PROXY')) { |
|
| 213 | - $cleanedNoProxy = \str_replace(' ', '', $noProxy); |
|
| 214 | - $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy); |
|
| 215 | - } |
|
| 216 | - $this->config = $config + $defaults; |
|
| 217 | - if (!empty($config['cookies']) && $config['cookies'] === \true) { |
|
| 218 | - $this->config['cookies'] = new CookieJar(); |
|
| 219 | - } |
|
| 220 | - // Add the default user-agent header. |
|
| 221 | - if (!isset($this->config['headers'])) { |
|
| 222 | - $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()]; |
|
| 223 | - } else { |
|
| 224 | - // Add the User-Agent header if one was not already set. |
|
| 225 | - foreach (\array_keys($this->config['headers']) as $name) { |
|
| 226 | - if (\strtolower($name) === 'user-agent') { |
|
| 227 | - return; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - $this->config['headers']['User-Agent'] = Utils::defaultUserAgent(); |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - /** |
|
| 234 | - * Merges default options into the array. |
|
| 235 | - * |
|
| 236 | - * @param array $options Options to modify by reference |
|
| 237 | - */ |
|
| 238 | - private function prepareDefaults(array $options) : array |
|
| 239 | - { |
|
| 240 | - $defaults = $this->config; |
|
| 241 | - if (!empty($defaults['headers'])) { |
|
| 242 | - // Default headers are only added if they are not present. |
|
| 243 | - $defaults['_conditional'] = $defaults['headers']; |
|
| 244 | - unset($defaults['headers']); |
|
| 245 | - } |
|
| 246 | - // Special handling for headers is required as they are added as |
|
| 247 | - // conditional headers and as headers passed to a request ctor. |
|
| 248 | - if (\array_key_exists('headers', $options)) { |
|
| 249 | - // Allows default headers to be unset. |
|
| 250 | - if ($options['headers'] === null) { |
|
| 251 | - $defaults['_conditional'] = []; |
|
| 252 | - unset($options['headers']); |
|
| 253 | - } elseif (!\is_array($options['headers'])) { |
|
| 254 | - throw new InvalidArgumentException('headers must be an array'); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - // Shallow merge defaults underneath options. |
|
| 258 | - $result = $options + $defaults; |
|
| 259 | - // Remove null values. |
|
| 260 | - foreach ($result as $k => $v) { |
|
| 261 | - if ($v === null) { |
|
| 262 | - unset($result[$k]); |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - return $result; |
|
| 266 | - } |
|
| 267 | - /** |
|
| 268 | - * Transfers the given request and applies request options. |
|
| 269 | - * |
|
| 270 | - * The URI of the request is not modified and the request options are used |
|
| 271 | - * as-is without merging in default options. |
|
| 272 | - * |
|
| 273 | - * @param array $options See \GuzzleHttp\RequestOptions. |
|
| 274 | - */ |
|
| 275 | - private function transfer(RequestInterface $request, array $options) : PromiseInterface |
|
| 276 | - { |
|
| 277 | - $request = $this->applyOptions($request, $options); |
|
| 278 | - /** @var HandlerStack $handler */ |
|
| 279 | - $handler = $options['handler']; |
|
| 280 | - try { |
|
| 281 | - return P\Create::promiseFor($handler($request, $options)); |
|
| 282 | - } catch (\Exception $e) { |
|
| 283 | - return P\Create::rejectionFor($e); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - /** |
|
| 287 | - * Applies the array of request options to a request. |
|
| 288 | - */ |
|
| 289 | - private function applyOptions(RequestInterface $request, array &$options) : RequestInterface |
|
| 290 | - { |
|
| 291 | - $modify = ['set_headers' => []]; |
|
| 292 | - if (isset($options['headers'])) { |
|
| 293 | - if (\array_keys($options['headers']) === \range(0, \count($options['headers']) - 1)) { |
|
| 294 | - throw new InvalidArgumentException('The headers array must have header name as keys.'); |
|
| 295 | - } |
|
| 296 | - $modify['set_headers'] = $options['headers']; |
|
| 297 | - unset($options['headers']); |
|
| 298 | - } |
|
| 299 | - if (isset($options['form_params'])) { |
|
| 300 | - if (isset($options['multipart'])) { |
|
| 301 | - throw new InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.'); |
|
| 302 | - } |
|
| 303 | - $options['body'] = \http_build_query($options['form_params'], '', '&'); |
|
| 304 | - unset($options['form_params']); |
|
| 305 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 306 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 307 | - $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 308 | - } |
|
| 309 | - if (isset($options['multipart'])) { |
|
| 310 | - $options['body'] = new Psr7\MultipartStream($options['multipart']); |
|
| 311 | - unset($options['multipart']); |
|
| 312 | - } |
|
| 313 | - if (isset($options['json'])) { |
|
| 314 | - $options['body'] = Utils::jsonEncode($options['json']); |
|
| 315 | - unset($options['json']); |
|
| 316 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 317 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 318 | - $options['_conditional']['Content-Type'] = 'application/json'; |
|
| 319 | - } |
|
| 320 | - if (!empty($options['decode_content']) && $options['decode_content'] !== \true) { |
|
| 321 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 322 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']); |
|
| 323 | - $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; |
|
| 324 | - } |
|
| 325 | - if (isset($options['body'])) { |
|
| 326 | - if (\is_array($options['body'])) { |
|
| 327 | - throw $this->invalidBody(); |
|
| 328 | - } |
|
| 329 | - $modify['body'] = Psr7\Utils::streamFor($options['body']); |
|
| 330 | - unset($options['body']); |
|
| 331 | - } |
|
| 332 | - if (!empty($options['auth']) && \is_array($options['auth'])) { |
|
| 333 | - $value = $options['auth']; |
|
| 334 | - $type = isset($value[2]) ? \strtolower($value[2]) : 'basic'; |
|
| 335 | - switch ($type) { |
|
| 336 | - case 'basic': |
|
| 337 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 338 | - $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); |
|
| 339 | - $modify['set_headers']['Authorization'] = 'Basic ' . \base64_encode("{$value[0]}:{$value[1]}"); |
|
| 340 | - break; |
|
| 341 | - case 'digest': |
|
| 342 | - // @todo: Do not rely on curl |
|
| 343 | - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST; |
|
| 344 | - $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; |
|
| 345 | - break; |
|
| 346 | - case 'ntlm': |
|
| 347 | - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM; |
|
| 348 | - $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; |
|
| 349 | - break; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - if (isset($options['query'])) { |
|
| 353 | - $value = $options['query']; |
|
| 354 | - if (\is_array($value)) { |
|
| 355 | - $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986); |
|
| 356 | - } |
|
| 357 | - if (!\is_string($value)) { |
|
| 358 | - throw new InvalidArgumentException('query must be a string or array'); |
|
| 359 | - } |
|
| 360 | - $modify['query'] = $value; |
|
| 361 | - unset($options['query']); |
|
| 362 | - } |
|
| 363 | - // Ensure that sink is not an invalid value. |
|
| 364 | - if (isset($options['sink'])) { |
|
| 365 | - // TODO: Add more sink validation? |
|
| 366 | - if (\is_bool($options['sink'])) { |
|
| 367 | - throw new InvalidArgumentException('sink must not be a boolean'); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - if (isset($options['version'])) { |
|
| 371 | - $modify['version'] = $options['version']; |
|
| 372 | - } |
|
| 373 | - $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 374 | - if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
| 375 | - // Use a multipart/form-data POST if a Content-Type is not set. |
|
| 376 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 377 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 378 | - $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary(); |
|
| 379 | - } |
|
| 380 | - // Merge in conditional headers if they are not present. |
|
| 381 | - if (isset($options['_conditional'])) { |
|
| 382 | - // Build up the changes so it's in a single clone of the message. |
|
| 383 | - $modify = []; |
|
| 384 | - foreach ($options['_conditional'] as $k => $v) { |
|
| 385 | - if (!$request->hasHeader($k)) { |
|
| 386 | - $modify['set_headers'][$k] = $v; |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 390 | - // Don't pass this internal value along to middleware/handlers. |
|
| 391 | - unset($options['_conditional']); |
|
| 392 | - } |
|
| 393 | - return $request; |
|
| 394 | - } |
|
| 395 | - /** |
|
| 396 | - * Return an InvalidArgumentException with pre-set message. |
|
| 397 | - */ |
|
| 398 | - private function invalidBody() : InvalidArgumentException |
|
| 399 | - { |
|
| 400 | - return new InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a request is not supported. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.'); |
|
| 401 | - } |
|
| 18 | + use ClientTrait; |
|
| 19 | + /** |
|
| 20 | + * @var array Default request options |
|
| 21 | + */ |
|
| 22 | + private $config; |
|
| 23 | + /** |
|
| 24 | + * Clients accept an array of constructor parameters. |
|
| 25 | + * |
|
| 26 | + * Here's an example of creating a client using a base_uri and an array of |
|
| 27 | + * default request options to apply to each request: |
|
| 28 | + * |
|
| 29 | + * $client = new Client([ |
|
| 30 | + * 'base_uri' => 'http://www.foo.com/1.0/', |
|
| 31 | + * 'timeout' => 0, |
|
| 32 | + * 'allow_redirects' => false, |
|
| 33 | + * 'proxy' => '192.168.16.1:10' |
|
| 34 | + * ]); |
|
| 35 | + * |
|
| 36 | + * Client configuration settings include the following options: |
|
| 37 | + * |
|
| 38 | + * - handler: (callable) Function that transfers HTTP requests over the |
|
| 39 | + * wire. The function is called with a Psr7\Http\Message\RequestInterface |
|
| 40 | + * and array of transfer options, and must return a |
|
| 41 | + * GuzzleHttp\Promise\PromiseInterface that is fulfilled with a |
|
| 42 | + * Psr7\Http\Message\ResponseInterface on success. |
|
| 43 | + * If no handler is provided, a default handler will be created |
|
| 44 | + * that enables all of the request options below by attaching all of the |
|
| 45 | + * default middleware to the handler. |
|
| 46 | + * - base_uri: (string|UriInterface) Base URI of the client that is merged |
|
| 47 | + * into relative URIs. Can be a string or instance of UriInterface. |
|
| 48 | + * - **: any request option |
|
| 49 | + * |
|
| 50 | + * @param array $config Client configuration settings. |
|
| 51 | + * |
|
| 52 | + * @see \GuzzleHttp\RequestOptions for a list of available request options. |
|
| 53 | + */ |
|
| 54 | + public function __construct(array $config = []) |
|
| 55 | + { |
|
| 56 | + if (!isset($config['handler'])) { |
|
| 57 | + $config['handler'] = HandlerStack::create(); |
|
| 58 | + } elseif (!\is_callable($config['handler'])) { |
|
| 59 | + throw new InvalidArgumentException('handler must be a callable'); |
|
| 60 | + } |
|
| 61 | + // Convert the base_uri to a UriInterface |
|
| 62 | + if (isset($config['base_uri'])) { |
|
| 63 | + $config['base_uri'] = Psr7\Utils::uriFor($config['base_uri']); |
|
| 64 | + } |
|
| 65 | + $this->configureDefaults($config); |
|
| 66 | + } |
|
| 67 | + /** |
|
| 68 | + * @param string $method |
|
| 69 | + * @param array $args |
|
| 70 | + * |
|
| 71 | + * @return PromiseInterface|ResponseInterface |
|
| 72 | + * |
|
| 73 | + * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0. |
|
| 74 | + */ |
|
| 75 | + public function __call($method, $args) |
|
| 76 | + { |
|
| 77 | + if (\count($args) < 1) { |
|
| 78 | + throw new InvalidArgumentException('Magic request methods require a URI and optional options array'); |
|
| 79 | + } |
|
| 80 | + $uri = $args[0]; |
|
| 81 | + $opts = $args[1] ?? []; |
|
| 82 | + return \substr($method, -5) === 'Async' ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) : $this->request($method, $uri, $opts); |
|
| 83 | + } |
|
| 84 | + /** |
|
| 85 | + * Asynchronously send an HTTP request. |
|
| 86 | + * |
|
| 87 | + * @param array $options Request options to apply to the given |
|
| 88 | + * request and to the transfer. See \GuzzleHttp\RequestOptions. |
|
| 89 | + */ |
|
| 90 | + public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface |
|
| 91 | + { |
|
| 92 | + // Merge the base URI into the request URI if needed. |
|
| 93 | + $options = $this->prepareDefaults($options); |
|
| 94 | + return $this->transfer($request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), $options); |
|
| 95 | + } |
|
| 96 | + /** |
|
| 97 | + * Send an HTTP request. |
|
| 98 | + * |
|
| 99 | + * @param array $options Request options to apply to the given |
|
| 100 | + * request and to the transfer. See \GuzzleHttp\RequestOptions. |
|
| 101 | + * |
|
| 102 | + * @throws GuzzleException |
|
| 103 | + */ |
|
| 104 | + public function send(RequestInterface $request, array $options = []) : ResponseInterface |
|
| 105 | + { |
|
| 106 | + $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 107 | + return $this->sendAsync($request, $options)->wait(); |
|
| 108 | + } |
|
| 109 | + /** |
|
| 110 | + * The HttpClient PSR (PSR-18) specify this method. |
|
| 111 | + * |
|
| 112 | + * {@inheritDoc} |
|
| 113 | + */ |
|
| 114 | + public function sendRequest(RequestInterface $request) : ResponseInterface |
|
| 115 | + { |
|
| 116 | + $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 117 | + $options[RequestOptions::ALLOW_REDIRECTS] = \false; |
|
| 118 | + $options[RequestOptions::HTTP_ERRORS] = \false; |
|
| 119 | + return $this->sendAsync($request, $options)->wait(); |
|
| 120 | + } |
|
| 121 | + /** |
|
| 122 | + * Create and send an asynchronous HTTP request. |
|
| 123 | + * |
|
| 124 | + * Use an absolute path to override the base path of the client, or a |
|
| 125 | + * relative path to append to the base path of the client. The URL can |
|
| 126 | + * contain the query string as well. Use an array to provide a URL |
|
| 127 | + * template and additional variables to use in the URL template expansion. |
|
| 128 | + * |
|
| 129 | + * @param string $method HTTP method |
|
| 130 | + * @param string|UriInterface $uri URI object or string. |
|
| 131 | + * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. |
|
| 132 | + */ |
|
| 133 | + public function requestAsync(string $method, $uri = '', array $options = []) : PromiseInterface |
|
| 134 | + { |
|
| 135 | + $options = $this->prepareDefaults($options); |
|
| 136 | + // Remove request modifying parameter because it can be done up-front. |
|
| 137 | + $headers = $options['headers'] ?? []; |
|
| 138 | + $body = $options['body'] ?? null; |
|
| 139 | + $version = $options['version'] ?? '1.1'; |
|
| 140 | + // Merge the URI into the base URI. |
|
| 141 | + $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options); |
|
| 142 | + if (\is_array($body)) { |
|
| 143 | + throw $this->invalidBody(); |
|
| 144 | + } |
|
| 145 | + $request = new Psr7\Request($method, $uri, $headers, $body, $version); |
|
| 146 | + // Remove the option so that they are not doubly-applied. |
|
| 147 | + unset($options['headers'], $options['body'], $options['version']); |
|
| 148 | + return $this->transfer($request, $options); |
|
| 149 | + } |
|
| 150 | + /** |
|
| 151 | + * Create and send an HTTP request. |
|
| 152 | + * |
|
| 153 | + * Use an absolute path to override the base path of the client, or a |
|
| 154 | + * relative path to append to the base path of the client. The URL can |
|
| 155 | + * contain the query string as well. |
|
| 156 | + * |
|
| 157 | + * @param string $method HTTP method. |
|
| 158 | + * @param string|UriInterface $uri URI object or string. |
|
| 159 | + * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. |
|
| 160 | + * |
|
| 161 | + * @throws GuzzleException |
|
| 162 | + */ |
|
| 163 | + public function request(string $method, $uri = '', array $options = []) : ResponseInterface |
|
| 164 | + { |
|
| 165 | + $options[RequestOptions::SYNCHRONOUS] = \true; |
|
| 166 | + return $this->requestAsync($method, $uri, $options)->wait(); |
|
| 167 | + } |
|
| 168 | + /** |
|
| 169 | + * Get a client configuration option. |
|
| 170 | + * |
|
| 171 | + * These options include default request options of the client, a "handler" |
|
| 172 | + * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 173 | + * the concrete client. |
|
| 174 | + * |
|
| 175 | + * @param string|null $option The config option to retrieve. |
|
| 176 | + * |
|
| 177 | + * @return mixed |
|
| 178 | + * |
|
| 179 | + * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 180 | + */ |
|
| 181 | + public function getConfig(string $option = null) |
|
| 182 | + { |
|
| 183 | + return $option === null ? $this->config : $this->config[$option] ?? null; |
|
| 184 | + } |
|
| 185 | + private function buildUri(UriInterface $uri, array $config) : UriInterface |
|
| 186 | + { |
|
| 187 | + if (isset($config['base_uri'])) { |
|
| 188 | + $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri); |
|
| 189 | + } |
|
| 190 | + if (isset($config['idn_conversion']) && $config['idn_conversion'] !== \false) { |
|
| 191 | + $idnOptions = $config['idn_conversion'] === \true ? \IDNA_DEFAULT : $config['idn_conversion']; |
|
| 192 | + $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 193 | + } |
|
| 194 | + return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; |
|
| 195 | + } |
|
| 196 | + /** |
|
| 197 | + * Configures the default options for a client. |
|
| 198 | + */ |
|
| 199 | + private function configureDefaults(array $config) : void |
|
| 200 | + { |
|
| 201 | + $defaults = ['allow_redirects' => RedirectMiddleware::$defaultSettings, 'http_errors' => \true, 'decode_content' => \true, 'verify' => \true, 'cookies' => \false, 'idn_conversion' => \false]; |
|
| 202 | + // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. |
|
| 203 | + // We can only trust the HTTP_PROXY environment variable in a CLI |
|
| 204 | + // process due to the fact that PHP has no reliable mechanism to |
|
| 205 | + // get environment variables that start with "HTTP_". |
|
| 206 | + if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) { |
|
| 207 | + $defaults['proxy']['http'] = $proxy; |
|
| 208 | + } |
|
| 209 | + if ($proxy = Utils::getenv('HTTPS_PROXY')) { |
|
| 210 | + $defaults['proxy']['https'] = $proxy; |
|
| 211 | + } |
|
| 212 | + if ($noProxy = Utils::getenv('NO_PROXY')) { |
|
| 213 | + $cleanedNoProxy = \str_replace(' ', '', $noProxy); |
|
| 214 | + $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy); |
|
| 215 | + } |
|
| 216 | + $this->config = $config + $defaults; |
|
| 217 | + if (!empty($config['cookies']) && $config['cookies'] === \true) { |
|
| 218 | + $this->config['cookies'] = new CookieJar(); |
|
| 219 | + } |
|
| 220 | + // Add the default user-agent header. |
|
| 221 | + if (!isset($this->config['headers'])) { |
|
| 222 | + $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()]; |
|
| 223 | + } else { |
|
| 224 | + // Add the User-Agent header if one was not already set. |
|
| 225 | + foreach (\array_keys($this->config['headers']) as $name) { |
|
| 226 | + if (\strtolower($name) === 'user-agent') { |
|
| 227 | + return; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + $this->config['headers']['User-Agent'] = Utils::defaultUserAgent(); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + /** |
|
| 234 | + * Merges default options into the array. |
|
| 235 | + * |
|
| 236 | + * @param array $options Options to modify by reference |
|
| 237 | + */ |
|
| 238 | + private function prepareDefaults(array $options) : array |
|
| 239 | + { |
|
| 240 | + $defaults = $this->config; |
|
| 241 | + if (!empty($defaults['headers'])) { |
|
| 242 | + // Default headers are only added if they are not present. |
|
| 243 | + $defaults['_conditional'] = $defaults['headers']; |
|
| 244 | + unset($defaults['headers']); |
|
| 245 | + } |
|
| 246 | + // Special handling for headers is required as they are added as |
|
| 247 | + // conditional headers and as headers passed to a request ctor. |
|
| 248 | + if (\array_key_exists('headers', $options)) { |
|
| 249 | + // Allows default headers to be unset. |
|
| 250 | + if ($options['headers'] === null) { |
|
| 251 | + $defaults['_conditional'] = []; |
|
| 252 | + unset($options['headers']); |
|
| 253 | + } elseif (!\is_array($options['headers'])) { |
|
| 254 | + throw new InvalidArgumentException('headers must be an array'); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + // Shallow merge defaults underneath options. |
|
| 258 | + $result = $options + $defaults; |
|
| 259 | + // Remove null values. |
|
| 260 | + foreach ($result as $k => $v) { |
|
| 261 | + if ($v === null) { |
|
| 262 | + unset($result[$k]); |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + return $result; |
|
| 266 | + } |
|
| 267 | + /** |
|
| 268 | + * Transfers the given request and applies request options. |
|
| 269 | + * |
|
| 270 | + * The URI of the request is not modified and the request options are used |
|
| 271 | + * as-is without merging in default options. |
|
| 272 | + * |
|
| 273 | + * @param array $options See \GuzzleHttp\RequestOptions. |
|
| 274 | + */ |
|
| 275 | + private function transfer(RequestInterface $request, array $options) : PromiseInterface |
|
| 276 | + { |
|
| 277 | + $request = $this->applyOptions($request, $options); |
|
| 278 | + /** @var HandlerStack $handler */ |
|
| 279 | + $handler = $options['handler']; |
|
| 280 | + try { |
|
| 281 | + return P\Create::promiseFor($handler($request, $options)); |
|
| 282 | + } catch (\Exception $e) { |
|
| 283 | + return P\Create::rejectionFor($e); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + /** |
|
| 287 | + * Applies the array of request options to a request. |
|
| 288 | + */ |
|
| 289 | + private function applyOptions(RequestInterface $request, array &$options) : RequestInterface |
|
| 290 | + { |
|
| 291 | + $modify = ['set_headers' => []]; |
|
| 292 | + if (isset($options['headers'])) { |
|
| 293 | + if (\array_keys($options['headers']) === \range(0, \count($options['headers']) - 1)) { |
|
| 294 | + throw new InvalidArgumentException('The headers array must have header name as keys.'); |
|
| 295 | + } |
|
| 296 | + $modify['set_headers'] = $options['headers']; |
|
| 297 | + unset($options['headers']); |
|
| 298 | + } |
|
| 299 | + if (isset($options['form_params'])) { |
|
| 300 | + if (isset($options['multipart'])) { |
|
| 301 | + throw new InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.'); |
|
| 302 | + } |
|
| 303 | + $options['body'] = \http_build_query($options['form_params'], '', '&'); |
|
| 304 | + unset($options['form_params']); |
|
| 305 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 306 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 307 | + $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 308 | + } |
|
| 309 | + if (isset($options['multipart'])) { |
|
| 310 | + $options['body'] = new Psr7\MultipartStream($options['multipart']); |
|
| 311 | + unset($options['multipart']); |
|
| 312 | + } |
|
| 313 | + if (isset($options['json'])) { |
|
| 314 | + $options['body'] = Utils::jsonEncode($options['json']); |
|
| 315 | + unset($options['json']); |
|
| 316 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 317 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 318 | + $options['_conditional']['Content-Type'] = 'application/json'; |
|
| 319 | + } |
|
| 320 | + if (!empty($options['decode_content']) && $options['decode_content'] !== \true) { |
|
| 321 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 322 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']); |
|
| 323 | + $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; |
|
| 324 | + } |
|
| 325 | + if (isset($options['body'])) { |
|
| 326 | + if (\is_array($options['body'])) { |
|
| 327 | + throw $this->invalidBody(); |
|
| 328 | + } |
|
| 329 | + $modify['body'] = Psr7\Utils::streamFor($options['body']); |
|
| 330 | + unset($options['body']); |
|
| 331 | + } |
|
| 332 | + if (!empty($options['auth']) && \is_array($options['auth'])) { |
|
| 333 | + $value = $options['auth']; |
|
| 334 | + $type = isset($value[2]) ? \strtolower($value[2]) : 'basic'; |
|
| 335 | + switch ($type) { |
|
| 336 | + case 'basic': |
|
| 337 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 338 | + $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); |
|
| 339 | + $modify['set_headers']['Authorization'] = 'Basic ' . \base64_encode("{$value[0]}:{$value[1]}"); |
|
| 340 | + break; |
|
| 341 | + case 'digest': |
|
| 342 | + // @todo: Do not rely on curl |
|
| 343 | + $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST; |
|
| 344 | + $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; |
|
| 345 | + break; |
|
| 346 | + case 'ntlm': |
|
| 347 | + $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM; |
|
| 348 | + $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; |
|
| 349 | + break; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + if (isset($options['query'])) { |
|
| 353 | + $value = $options['query']; |
|
| 354 | + if (\is_array($value)) { |
|
| 355 | + $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986); |
|
| 356 | + } |
|
| 357 | + if (!\is_string($value)) { |
|
| 358 | + throw new InvalidArgumentException('query must be a string or array'); |
|
| 359 | + } |
|
| 360 | + $modify['query'] = $value; |
|
| 361 | + unset($options['query']); |
|
| 362 | + } |
|
| 363 | + // Ensure that sink is not an invalid value. |
|
| 364 | + if (isset($options['sink'])) { |
|
| 365 | + // TODO: Add more sink validation? |
|
| 366 | + if (\is_bool($options['sink'])) { |
|
| 367 | + throw new InvalidArgumentException('sink must not be a boolean'); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + if (isset($options['version'])) { |
|
| 371 | + $modify['version'] = $options['version']; |
|
| 372 | + } |
|
| 373 | + $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 374 | + if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
| 375 | + // Use a multipart/form-data POST if a Content-Type is not set. |
|
| 376 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 377 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 378 | + $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary(); |
|
| 379 | + } |
|
| 380 | + // Merge in conditional headers if they are not present. |
|
| 381 | + if (isset($options['_conditional'])) { |
|
| 382 | + // Build up the changes so it's in a single clone of the message. |
|
| 383 | + $modify = []; |
|
| 384 | + foreach ($options['_conditional'] as $k => $v) { |
|
| 385 | + if (!$request->hasHeader($k)) { |
|
| 386 | + $modify['set_headers'][$k] = $v; |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 390 | + // Don't pass this internal value along to middleware/handlers. |
|
| 391 | + unset($options['_conditional']); |
|
| 392 | + } |
|
| 393 | + return $request; |
|
| 394 | + } |
|
| 395 | + /** |
|
| 396 | + * Return an InvalidArgumentException with pre-set message. |
|
| 397 | + */ |
|
| 398 | + private function invalidBody() : InvalidArgumentException |
|
| 399 | + { |
|
| 400 | + return new InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a request is not supported. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.'); |
|
| 401 | + } |
|
| 402 | 402 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | } |
| 299 | 299 | if (isset($options['form_params'])) { |
| 300 | 300 | if (isset($options['multipart'])) { |
| 301 | - throw new InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.'); |
|
| 301 | + throw new InvalidArgumentException('You cannot use '.'form_params and multipart at the same time. Use the '.'form_params option if you want to send application/'.'x-www-form-urlencoded requests, and the multipart '.'option to send multipart/form-data requests.'); |
|
| 302 | 302 | } |
| 303 | 303 | $options['body'] = \http_build_query($options['form_params'], '', '&'); |
| 304 | 304 | unset($options['form_params']); |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | case 'basic': |
| 337 | 337 | // Ensure that we don't have the header in different case and set the new value. |
| 338 | 338 | $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); |
| 339 | - $modify['set_headers']['Authorization'] = 'Basic ' . \base64_encode("{$value[0]}:{$value[1]}"); |
|
| 339 | + $modify['set_headers']['Authorization'] = 'Basic '.\base64_encode("{$value[0]}:{$value[1]}"); |
|
| 340 | 340 | break; |
| 341 | 341 | case 'digest': |
| 342 | 342 | // @todo: Do not rely on curl |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | // Use a multipart/form-data POST if a Content-Type is not set. |
| 376 | 376 | // Ensure that we don't have the header in different case and set the new value. |
| 377 | 377 | $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
| 378 | - $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary(); |
|
| 378 | + $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary='.$request->getBody()->getBoundary(); |
|
| 379 | 379 | } |
| 380 | 380 | // Merge in conditional headers if they are not present. |
| 381 | 381 | if (isset($options['_conditional'])) { |
@@ -397,6 +397,6 @@ discard block |
||
| 397 | 397 | */ |
| 398 | 398 | private function invalidBody() : InvalidArgumentException |
| 399 | 399 | { |
| 400 | - return new InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a request is not supported. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.'); |
|
| 400 | + return new InvalidArgumentException('Passing in the "body" request '.'option as an array to send a request is not supported. '.'Please use the "form_params" request option to send a '.'application/x-www-form-urlencoded request, or the "multipart" '.'request option to send a multipart/form-data request.'); |
|
| 401 | 401 | } |
| 402 | 402 | } |
@@ -5,8 +5,8 @@ |
||
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
| 6 | 6 | interface BodySummarizerInterface |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Returns a summarized message body. |
|
| 10 | - */ |
|
| 11 | - public function summarize(MessageInterface $message) : ?string; |
|
| 8 | + /** |
|
| 9 | + * Returns a summarized message body. |
|
| 10 | + */ |
|
| 11 | + public function summarize(MessageInterface $message) : ?string; |
|
| 12 | 12 | } |
@@ -3,8 +3,7 @@ |
||
| 3 | 3 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp; |
| 4 | 4 | |
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
| 6 | -interface BodySummarizerInterface |
|
| 7 | -{ |
|
| 6 | +interface BodySummarizerInterface { |
|
| 8 | 7 | /** |
| 9 | 8 | * Returns a summarized message body. |
| 10 | 9 | */ |
@@ -5,113 +5,113 @@ |
||
| 5 | 5 | |
| 6 | 6 | final class Header |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Parse an array of header values containing ";" separated data into an |
|
| 10 | - * array of associative arrays representing the header key value pair data |
|
| 11 | - * of the header. When a parameter does not contain a value, but just |
|
| 12 | - * contains a key, this function will inject a key with a '' string value. |
|
| 13 | - * |
|
| 14 | - * @param string|array $header Header to parse into components. |
|
| 15 | - */ |
|
| 16 | - public static function parse($header) : array |
|
| 17 | - { |
|
| 18 | - static $trimmed = "\"' \n\t\r"; |
|
| 19 | - $params = $matches = []; |
|
| 20 | - foreach ((array) $header as $value) { |
|
| 21 | - foreach (self::splitList($value) as $val) { |
|
| 22 | - $part = []; |
|
| 23 | - foreach (\preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) { |
|
| 24 | - if (\preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { |
|
| 25 | - $m = $matches[0]; |
|
| 26 | - if (isset($m[1])) { |
|
| 27 | - $part[\trim($m[0], $trimmed)] = \trim($m[1], $trimmed); |
|
| 28 | - } else { |
|
| 29 | - $part[] = \trim($m[0], $trimmed); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - } |
|
| 33 | - if ($part) { |
|
| 34 | - $params[] = $part; |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - return $params; |
|
| 39 | - } |
|
| 40 | - /** |
|
| 41 | - * Converts an array of header values that may contain comma separated |
|
| 42 | - * headers into an array of headers with no comma separated values. |
|
| 43 | - * |
|
| 44 | - * @param string|array $header Header to normalize. |
|
| 45 | - * |
|
| 46 | - * @deprecated Use self::splitList() instead. |
|
| 47 | - */ |
|
| 48 | - public static function normalize($header) : array |
|
| 49 | - { |
|
| 50 | - $result = []; |
|
| 51 | - foreach ((array) $header as $value) { |
|
| 52 | - foreach (self::splitList($value) as $parsed) { |
|
| 53 | - $result[] = $parsed; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - return $result; |
|
| 57 | - } |
|
| 58 | - /** |
|
| 59 | - * Splits a HTTP header defined to contain a comma-separated list into |
|
| 60 | - * each individual value. Empty values will be removed. |
|
| 61 | - * |
|
| 62 | - * Example headers include 'accept', 'cache-control' and 'if-none-match'. |
|
| 63 | - * |
|
| 64 | - * This method must not be used to parse headers that are not defined as |
|
| 65 | - * a list, such as 'user-agent' or 'set-cookie'. |
|
| 66 | - * |
|
| 67 | - * @param string|string[] $values Header value as returned by MessageInterface::getHeader() |
|
| 68 | - * |
|
| 69 | - * @return string[] |
|
| 70 | - */ |
|
| 71 | - public static function splitList($values) : array |
|
| 72 | - { |
|
| 73 | - if (!\is_array($values)) { |
|
| 74 | - $values = [$values]; |
|
| 75 | - } |
|
| 76 | - $result = []; |
|
| 77 | - foreach ($values as $value) { |
|
| 78 | - if (!\is_string($value)) { |
|
| 79 | - throw new \TypeError('$header must either be a string or an array containing strings.'); |
|
| 80 | - } |
|
| 81 | - $v = ''; |
|
| 82 | - $isQuoted = \false; |
|
| 83 | - $isEscaped = \false; |
|
| 84 | - for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
|
| 85 | - if ($isEscaped) { |
|
| 86 | - $v .= $value[$i]; |
|
| 87 | - $isEscaped = \false; |
|
| 88 | - continue; |
|
| 89 | - } |
|
| 90 | - if (!$isQuoted && $value[$i] === ',') { |
|
| 91 | - $v = \trim($v); |
|
| 92 | - if ($v !== '') { |
|
| 93 | - $result[] = $v; |
|
| 94 | - } |
|
| 95 | - $v = ''; |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - if ($isQuoted && $value[$i] === '\\') { |
|
| 99 | - $isEscaped = \true; |
|
| 100 | - $v .= $value[$i]; |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - if ($value[$i] === '"') { |
|
| 104 | - $isQuoted = !$isQuoted; |
|
| 105 | - $v .= $value[$i]; |
|
| 106 | - continue; |
|
| 107 | - } |
|
| 108 | - $v .= $value[$i]; |
|
| 109 | - } |
|
| 110 | - $v = \trim($v); |
|
| 111 | - if ($v !== '') { |
|
| 112 | - $result[] = $v; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - return $result; |
|
| 116 | - } |
|
| 8 | + /** |
|
| 9 | + * Parse an array of header values containing ";" separated data into an |
|
| 10 | + * array of associative arrays representing the header key value pair data |
|
| 11 | + * of the header. When a parameter does not contain a value, but just |
|
| 12 | + * contains a key, this function will inject a key with a '' string value. |
|
| 13 | + * |
|
| 14 | + * @param string|array $header Header to parse into components. |
|
| 15 | + */ |
|
| 16 | + public static function parse($header) : array |
|
| 17 | + { |
|
| 18 | + static $trimmed = "\"' \n\t\r"; |
|
| 19 | + $params = $matches = []; |
|
| 20 | + foreach ((array) $header as $value) { |
|
| 21 | + foreach (self::splitList($value) as $val) { |
|
| 22 | + $part = []; |
|
| 23 | + foreach (\preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) { |
|
| 24 | + if (\preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { |
|
| 25 | + $m = $matches[0]; |
|
| 26 | + if (isset($m[1])) { |
|
| 27 | + $part[\trim($m[0], $trimmed)] = \trim($m[1], $trimmed); |
|
| 28 | + } else { |
|
| 29 | + $part[] = \trim($m[0], $trimmed); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | + if ($part) { |
|
| 34 | + $params[] = $part; |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + return $params; |
|
| 39 | + } |
|
| 40 | + /** |
|
| 41 | + * Converts an array of header values that may contain comma separated |
|
| 42 | + * headers into an array of headers with no comma separated values. |
|
| 43 | + * |
|
| 44 | + * @param string|array $header Header to normalize. |
|
| 45 | + * |
|
| 46 | + * @deprecated Use self::splitList() instead. |
|
| 47 | + */ |
|
| 48 | + public static function normalize($header) : array |
|
| 49 | + { |
|
| 50 | + $result = []; |
|
| 51 | + foreach ((array) $header as $value) { |
|
| 52 | + foreach (self::splitList($value) as $parsed) { |
|
| 53 | + $result[] = $parsed; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + return $result; |
|
| 57 | + } |
|
| 58 | + /** |
|
| 59 | + * Splits a HTTP header defined to contain a comma-separated list into |
|
| 60 | + * each individual value. Empty values will be removed. |
|
| 61 | + * |
|
| 62 | + * Example headers include 'accept', 'cache-control' and 'if-none-match'. |
|
| 63 | + * |
|
| 64 | + * This method must not be used to parse headers that are not defined as |
|
| 65 | + * a list, such as 'user-agent' or 'set-cookie'. |
|
| 66 | + * |
|
| 67 | + * @param string|string[] $values Header value as returned by MessageInterface::getHeader() |
|
| 68 | + * |
|
| 69 | + * @return string[] |
|
| 70 | + */ |
|
| 71 | + public static function splitList($values) : array |
|
| 72 | + { |
|
| 73 | + if (!\is_array($values)) { |
|
| 74 | + $values = [$values]; |
|
| 75 | + } |
|
| 76 | + $result = []; |
|
| 77 | + foreach ($values as $value) { |
|
| 78 | + if (!\is_string($value)) { |
|
| 79 | + throw new \TypeError('$header must either be a string or an array containing strings.'); |
|
| 80 | + } |
|
| 81 | + $v = ''; |
|
| 82 | + $isQuoted = \false; |
|
| 83 | + $isEscaped = \false; |
|
| 84 | + for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
|
| 85 | + if ($isEscaped) { |
|
| 86 | + $v .= $value[$i]; |
|
| 87 | + $isEscaped = \false; |
|
| 88 | + continue; |
|
| 89 | + } |
|
| 90 | + if (!$isQuoted && $value[$i] === ',') { |
|
| 91 | + $v = \trim($v); |
|
| 92 | + if ($v !== '') { |
|
| 93 | + $result[] = $v; |
|
| 94 | + } |
|
| 95 | + $v = ''; |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + if ($isQuoted && $value[$i] === '\\') { |
|
| 99 | + $isEscaped = \true; |
|
| 100 | + $v .= $value[$i]; |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + if ($value[$i] === '"') { |
|
| 104 | + $isQuoted = !$isQuoted; |
|
| 105 | + $v .= $value[$i]; |
|
| 106 | + continue; |
|
| 107 | + } |
|
| 108 | + $v .= $value[$i]; |
|
| 109 | + } |
|
| 110 | + $v = \trim($v); |
|
| 111 | + if ($v !== '') { |
|
| 112 | + $result[] = $v; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + return $result; |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | 6 | final class Header |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | { |
| 18 | 18 | static $trimmed = "\"' \n\t\r"; |
| 19 | 19 | $params = $matches = []; |
| 20 | - foreach ((array) $header as $value) { |
|
| 20 | + foreach ((array)$header as $value) { |
|
| 21 | 21 | foreach (self::splitList($value) as $val) { |
| 22 | 22 | $part = []; |
| 23 | 23 | foreach (\preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) { |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | public static function normalize($header) : array |
| 49 | 49 | { |
| 50 | 50 | $result = []; |
| 51 | - foreach ((array) $header as $value) { |
|
| 51 | + foreach ((array)$header as $value) { |
|
| 52 | 52 | foreach (self::splitList($value) as $parsed) { |
| 53 | 53 | $result[] = $parsed; |
| 54 | 54 | } |
@@ -3,8 +3,7 @@ |
||
| 3 | 3 | declare (strict_types=1); |
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | -final class Header |
|
| 7 | -{ |
|
| 6 | +final class Header { |
|
| 8 | 7 | /** |
| 9 | 8 | * Parse an array of header values containing ";" separated data into an |
| 10 | 9 | * array of associative arrays representing the header key value pair data |
@@ -23,54 +23,54 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | final class HttpFactory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
| 25 | 25 | { |
| 26 | - public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null) : UploadedFileInterface |
|
| 27 | - { |
|
| 28 | - if ($size === null) { |
|
| 29 | - $size = $stream->getSize(); |
|
| 30 | - } |
|
| 31 | - return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
| 32 | - } |
|
| 33 | - public function createStream(string $content = '') : StreamInterface |
|
| 34 | - { |
|
| 35 | - return Utils::streamFor($content); |
|
| 36 | - } |
|
| 37 | - public function createStreamFromFile(string $file, string $mode = 'r') : StreamInterface |
|
| 38 | - { |
|
| 39 | - try { |
|
| 40 | - $resource = Utils::tryFopen($file, $mode); |
|
| 41 | - } catch (\RuntimeException $e) { |
|
| 42 | - if ('' === $mode || \false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], \true)) { |
|
| 43 | - throw new \InvalidArgumentException(\sprintf('Invalid file opening mode "%s"', $mode), 0, $e); |
|
| 44 | - } |
|
| 45 | - throw $e; |
|
| 46 | - } |
|
| 47 | - return Utils::streamFor($resource); |
|
| 48 | - } |
|
| 49 | - public function createStreamFromResource($resource) : StreamInterface |
|
| 50 | - { |
|
| 51 | - return Utils::streamFor($resource); |
|
| 52 | - } |
|
| 53 | - public function createServerRequest(string $method, $uri, array $serverParams = []) : ServerRequestInterface |
|
| 54 | - { |
|
| 55 | - if (empty($method)) { |
|
| 56 | - if (!empty($serverParams['REQUEST_METHOD'])) { |
|
| 57 | - $method = $serverParams['REQUEST_METHOD']; |
|
| 58 | - } else { |
|
| 59 | - throw new \InvalidArgumentException('Cannot determine HTTP method'); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
| 63 | - } |
|
| 64 | - public function createResponse(int $code = 200, string $reasonPhrase = '') : ResponseInterface |
|
| 65 | - { |
|
| 66 | - return new Response($code, [], null, '1.1', $reasonPhrase); |
|
| 67 | - } |
|
| 68 | - public function createRequest(string $method, $uri) : RequestInterface |
|
| 69 | - { |
|
| 70 | - return new Request($method, $uri); |
|
| 71 | - } |
|
| 72 | - public function createUri(string $uri = '') : UriInterface |
|
| 73 | - { |
|
| 74 | - return new Uri($uri); |
|
| 75 | - } |
|
| 26 | + public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null) : UploadedFileInterface |
|
| 27 | + { |
|
| 28 | + if ($size === null) { |
|
| 29 | + $size = $stream->getSize(); |
|
| 30 | + } |
|
| 31 | + return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
| 32 | + } |
|
| 33 | + public function createStream(string $content = '') : StreamInterface |
|
| 34 | + { |
|
| 35 | + return Utils::streamFor($content); |
|
| 36 | + } |
|
| 37 | + public function createStreamFromFile(string $file, string $mode = 'r') : StreamInterface |
|
| 38 | + { |
|
| 39 | + try { |
|
| 40 | + $resource = Utils::tryFopen($file, $mode); |
|
| 41 | + } catch (\RuntimeException $e) { |
|
| 42 | + if ('' === $mode || \false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], \true)) { |
|
| 43 | + throw new \InvalidArgumentException(\sprintf('Invalid file opening mode "%s"', $mode), 0, $e); |
|
| 44 | + } |
|
| 45 | + throw $e; |
|
| 46 | + } |
|
| 47 | + return Utils::streamFor($resource); |
|
| 48 | + } |
|
| 49 | + public function createStreamFromResource($resource) : StreamInterface |
|
| 50 | + { |
|
| 51 | + return Utils::streamFor($resource); |
|
| 52 | + } |
|
| 53 | + public function createServerRequest(string $method, $uri, array $serverParams = []) : ServerRequestInterface |
|
| 54 | + { |
|
| 55 | + if (empty($method)) { |
|
| 56 | + if (!empty($serverParams['REQUEST_METHOD'])) { |
|
| 57 | + $method = $serverParams['REQUEST_METHOD']; |
|
| 58 | + } else { |
|
| 59 | + throw new \InvalidArgumentException('Cannot determine HTTP method'); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
| 63 | + } |
|
| 64 | + public function createResponse(int $code = 200, string $reasonPhrase = '') : ResponseInterface |
|
| 65 | + { |
|
| 66 | + return new Response($code, [], null, '1.1', $reasonPhrase); |
|
| 67 | + } |
|
| 68 | + public function createRequest(string $method, $uri) : RequestInterface |
|
| 69 | + { |
|
| 70 | + return new Request($method, $uri); |
|
| 71 | + } |
|
| 72 | + public function createUri(string $uri = '') : UriInterface |
|
| 73 | + { |
|
| 74 | + return new Uri($uri); |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestFactoryInterface; |
@@ -16,134 +16,134 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class PumpStream implements StreamInterface |
| 18 | 18 | { |
| 19 | - /** @var callable(int): (string|false|null)|null */ |
|
| 20 | - private $source; |
|
| 21 | - /** @var int|null */ |
|
| 22 | - private $size; |
|
| 23 | - /** @var int */ |
|
| 24 | - private $tellPos = 0; |
|
| 25 | - /** @var array */ |
|
| 26 | - private $metadata; |
|
| 27 | - /** @var BufferStream */ |
|
| 28 | - private $buffer; |
|
| 29 | - /** |
|
| 30 | - * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY |
|
| 31 | - * accept an integer argument used to control the |
|
| 32 | - * amount of data to return. The callable MUST |
|
| 33 | - * return a string when called, or false|null on error |
|
| 34 | - * or EOF. |
|
| 35 | - * @param array{size?: int, metadata?: array} $options Stream options: |
|
| 36 | - * - metadata: Hash of metadata to use with stream. |
|
| 37 | - * - size: Size of the stream, if known. |
|
| 38 | - */ |
|
| 39 | - public function __construct(callable $source, array $options = []) |
|
| 40 | - { |
|
| 41 | - $this->source = $source; |
|
| 42 | - $this->size = $options['size'] ?? null; |
|
| 43 | - $this->metadata = $options['metadata'] ?? []; |
|
| 44 | - $this->buffer = new BufferStream(); |
|
| 45 | - } |
|
| 46 | - public function __toString() : string |
|
| 47 | - { |
|
| 48 | - try { |
|
| 49 | - return Utils::copyToString($this); |
|
| 50 | - } catch (\Throwable $e) { |
|
| 51 | - if (\PHP_VERSION_ID >= 70400) { |
|
| 52 | - throw $e; |
|
| 53 | - } |
|
| 54 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 55 | - return ''; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - public function close() : void |
|
| 59 | - { |
|
| 60 | - $this->detach(); |
|
| 61 | - } |
|
| 62 | - public function detach() |
|
| 63 | - { |
|
| 64 | - $this->tellPos = 0; |
|
| 65 | - $this->source = null; |
|
| 66 | - return null; |
|
| 67 | - } |
|
| 68 | - public function getSize() : ?int |
|
| 69 | - { |
|
| 70 | - return $this->size; |
|
| 71 | - } |
|
| 72 | - public function tell() : int |
|
| 73 | - { |
|
| 74 | - return $this->tellPos; |
|
| 75 | - } |
|
| 76 | - public function eof() : bool |
|
| 77 | - { |
|
| 78 | - return $this->source === null; |
|
| 79 | - } |
|
| 80 | - public function isSeekable() : bool |
|
| 81 | - { |
|
| 82 | - return \false; |
|
| 83 | - } |
|
| 84 | - public function rewind() : void |
|
| 85 | - { |
|
| 86 | - $this->seek(0); |
|
| 87 | - } |
|
| 88 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
| 89 | - { |
|
| 90 | - throw new \RuntimeException('Cannot seek a PumpStream'); |
|
| 91 | - } |
|
| 92 | - public function isWritable() : bool |
|
| 93 | - { |
|
| 94 | - return \false; |
|
| 95 | - } |
|
| 96 | - public function write($string) : int |
|
| 97 | - { |
|
| 98 | - throw new \RuntimeException('Cannot write to a PumpStream'); |
|
| 99 | - } |
|
| 100 | - public function isReadable() : bool |
|
| 101 | - { |
|
| 102 | - return \true; |
|
| 103 | - } |
|
| 104 | - public function read($length) : string |
|
| 105 | - { |
|
| 106 | - $data = $this->buffer->read($length); |
|
| 107 | - $readLen = \strlen($data); |
|
| 108 | - $this->tellPos += $readLen; |
|
| 109 | - $remaining = $length - $readLen; |
|
| 110 | - if ($remaining) { |
|
| 111 | - $this->pump($remaining); |
|
| 112 | - $data .= $this->buffer->read($remaining); |
|
| 113 | - $this->tellPos += \strlen($data) - $readLen; |
|
| 114 | - } |
|
| 115 | - return $data; |
|
| 116 | - } |
|
| 117 | - public function getContents() : string |
|
| 118 | - { |
|
| 119 | - $result = ''; |
|
| 120 | - while (!$this->eof()) { |
|
| 121 | - $result .= $this->read(1000000); |
|
| 122 | - } |
|
| 123 | - return $result; |
|
| 124 | - } |
|
| 125 | - /** |
|
| 126 | - * @return mixed |
|
| 127 | - */ |
|
| 128 | - public function getMetadata($key = null) |
|
| 129 | - { |
|
| 130 | - if (!$key) { |
|
| 131 | - return $this->metadata; |
|
| 132 | - } |
|
| 133 | - return $this->metadata[$key] ?? null; |
|
| 134 | - } |
|
| 135 | - private function pump(int $length) : void |
|
| 136 | - { |
|
| 137 | - if ($this->source !== null) { |
|
| 138 | - do { |
|
| 139 | - $data = ($this->source)($length); |
|
| 140 | - if ($data === \false || $data === null) { |
|
| 141 | - $this->source = null; |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - $this->buffer->write($data); |
|
| 145 | - $length -= \strlen($data); |
|
| 146 | - } while ($length > 0); |
|
| 147 | - } |
|
| 148 | - } |
|
| 19 | + /** @var callable(int): (string|false|null)|null */ |
|
| 20 | + private $source; |
|
| 21 | + /** @var int|null */ |
|
| 22 | + private $size; |
|
| 23 | + /** @var int */ |
|
| 24 | + private $tellPos = 0; |
|
| 25 | + /** @var array */ |
|
| 26 | + private $metadata; |
|
| 27 | + /** @var BufferStream */ |
|
| 28 | + private $buffer; |
|
| 29 | + /** |
|
| 30 | + * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY |
|
| 31 | + * accept an integer argument used to control the |
|
| 32 | + * amount of data to return. The callable MUST |
|
| 33 | + * return a string when called, or false|null on error |
|
| 34 | + * or EOF. |
|
| 35 | + * @param array{size?: int, metadata?: array} $options Stream options: |
|
| 36 | + * - metadata: Hash of metadata to use with stream. |
|
| 37 | + * - size: Size of the stream, if known. |
|
| 38 | + */ |
|
| 39 | + public function __construct(callable $source, array $options = []) |
|
| 40 | + { |
|
| 41 | + $this->source = $source; |
|
| 42 | + $this->size = $options['size'] ?? null; |
|
| 43 | + $this->metadata = $options['metadata'] ?? []; |
|
| 44 | + $this->buffer = new BufferStream(); |
|
| 45 | + } |
|
| 46 | + public function __toString() : string |
|
| 47 | + { |
|
| 48 | + try { |
|
| 49 | + return Utils::copyToString($this); |
|
| 50 | + } catch (\Throwable $e) { |
|
| 51 | + if (\PHP_VERSION_ID >= 70400) { |
|
| 52 | + throw $e; |
|
| 53 | + } |
|
| 54 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 55 | + return ''; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + public function close() : void |
|
| 59 | + { |
|
| 60 | + $this->detach(); |
|
| 61 | + } |
|
| 62 | + public function detach() |
|
| 63 | + { |
|
| 64 | + $this->tellPos = 0; |
|
| 65 | + $this->source = null; |
|
| 66 | + return null; |
|
| 67 | + } |
|
| 68 | + public function getSize() : ?int |
|
| 69 | + { |
|
| 70 | + return $this->size; |
|
| 71 | + } |
|
| 72 | + public function tell() : int |
|
| 73 | + { |
|
| 74 | + return $this->tellPos; |
|
| 75 | + } |
|
| 76 | + public function eof() : bool |
|
| 77 | + { |
|
| 78 | + return $this->source === null; |
|
| 79 | + } |
|
| 80 | + public function isSeekable() : bool |
|
| 81 | + { |
|
| 82 | + return \false; |
|
| 83 | + } |
|
| 84 | + public function rewind() : void |
|
| 85 | + { |
|
| 86 | + $this->seek(0); |
|
| 87 | + } |
|
| 88 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
| 89 | + { |
|
| 90 | + throw new \RuntimeException('Cannot seek a PumpStream'); |
|
| 91 | + } |
|
| 92 | + public function isWritable() : bool |
|
| 93 | + { |
|
| 94 | + return \false; |
|
| 95 | + } |
|
| 96 | + public function write($string) : int |
|
| 97 | + { |
|
| 98 | + throw new \RuntimeException('Cannot write to a PumpStream'); |
|
| 99 | + } |
|
| 100 | + public function isReadable() : bool |
|
| 101 | + { |
|
| 102 | + return \true; |
|
| 103 | + } |
|
| 104 | + public function read($length) : string |
|
| 105 | + { |
|
| 106 | + $data = $this->buffer->read($length); |
|
| 107 | + $readLen = \strlen($data); |
|
| 108 | + $this->tellPos += $readLen; |
|
| 109 | + $remaining = $length - $readLen; |
|
| 110 | + if ($remaining) { |
|
| 111 | + $this->pump($remaining); |
|
| 112 | + $data .= $this->buffer->read($remaining); |
|
| 113 | + $this->tellPos += \strlen($data) - $readLen; |
|
| 114 | + } |
|
| 115 | + return $data; |
|
| 116 | + } |
|
| 117 | + public function getContents() : string |
|
| 118 | + { |
|
| 119 | + $result = ''; |
|
| 120 | + while (!$this->eof()) { |
|
| 121 | + $result .= $this->read(1000000); |
|
| 122 | + } |
|
| 123 | + return $result; |
|
| 124 | + } |
|
| 125 | + /** |
|
| 126 | + * @return mixed |
|
| 127 | + */ |
|
| 128 | + public function getMetadata($key = null) |
|
| 129 | + { |
|
| 130 | + if (!$key) { |
|
| 131 | + return $this->metadata; |
|
| 132 | + } |
|
| 133 | + return $this->metadata[$key] ?? null; |
|
| 134 | + } |
|
| 135 | + private function pump(int $length) : void |
|
| 136 | + { |
|
| 137 | + if ($this->source !== null) { |
|
| 138 | + do { |
|
| 139 | + $data = ($this->source)($length); |
|
| 140 | + if ($data === \false || $data === null) { |
|
| 141 | + $this->source = null; |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + $this->buffer->write($data); |
|
| 145 | + $length -= \strlen($data); |
|
| 146 | + } while ($length > 0); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | if (\PHP_VERSION_ID >= 70400) { |
| 52 | 52 | throw $e; |
| 53 | 53 | } |
| 54 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 54 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string)$e), \E_USER_ERROR); |
|
| 55 | 55 | return ''; |
| 56 | 56 | } |
| 57 | 57 | } |
@@ -14,8 +14,7 @@ |
||
| 14 | 14 | * the read() function of the PumpStream. The provided callable MUST return |
| 15 | 15 | * false when there is no more data to read. |
| 16 | 16 | */ |
| 17 | -final class PumpStream implements StreamInterface |
|
| 18 | -{ |
|
| 17 | +final class PumpStream implements StreamInterface { |
|
| 19 | 18 | /** @var callable(int): (string|false|null)|null */ |
| 20 | 19 | private $source; |
| 21 | 20 | /** @var int|null */ |
@@ -10,121 +10,121 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | final class MultipartStream implements StreamInterface |
| 12 | 12 | { |
| 13 | - use StreamDecoratorTrait; |
|
| 14 | - /** @var string */ |
|
| 15 | - private $boundary; |
|
| 16 | - /** @var StreamInterface */ |
|
| 17 | - private $stream; |
|
| 18 | - /** |
|
| 19 | - * @param array $elements Array of associative arrays, each containing a |
|
| 20 | - * required "name" key mapping to the form field, |
|
| 21 | - * name, a required "contents" key mapping to a |
|
| 22 | - * StreamInterface/resource/string, an optional |
|
| 23 | - * "headers" associative array of custom headers, |
|
| 24 | - * and an optional "filename" key mapping to a |
|
| 25 | - * string to send as the filename in the part. |
|
| 26 | - * @param string $boundary You can optionally provide a specific boundary |
|
| 27 | - * |
|
| 28 | - * @throws \InvalidArgumentException |
|
| 29 | - */ |
|
| 30 | - public function __construct(array $elements = [], string $boundary = null) |
|
| 31 | - { |
|
| 32 | - $this->boundary = $boundary ?: \bin2hex(\random_bytes(20)); |
|
| 33 | - $this->stream = $this->createStream($elements); |
|
| 34 | - } |
|
| 35 | - public function getBoundary() : string |
|
| 36 | - { |
|
| 37 | - return $this->boundary; |
|
| 38 | - } |
|
| 39 | - public function isWritable() : bool |
|
| 40 | - { |
|
| 41 | - return \false; |
|
| 42 | - } |
|
| 43 | - /** |
|
| 44 | - * Get the headers needed before transferring the content of a POST file |
|
| 45 | - * |
|
| 46 | - * @param string[] $headers |
|
| 47 | - */ |
|
| 48 | - private function getHeaders(array $headers) : string |
|
| 49 | - { |
|
| 50 | - $str = ''; |
|
| 51 | - foreach ($headers as $key => $value) { |
|
| 52 | - $str .= "{$key}: {$value}\r\n"; |
|
| 53 | - } |
|
| 54 | - return "--{$this->boundary}\r\n" . \trim($str) . "\r\n\r\n"; |
|
| 55 | - } |
|
| 56 | - /** |
|
| 57 | - * Create the aggregate stream that will be used to upload the POST data |
|
| 58 | - */ |
|
| 59 | - protected function createStream(array $elements = []) : StreamInterface |
|
| 60 | - { |
|
| 61 | - $stream = new AppendStream(); |
|
| 62 | - foreach ($elements as $element) { |
|
| 63 | - if (!\is_array($element)) { |
|
| 64 | - throw new \UnexpectedValueException('An array is expected'); |
|
| 65 | - } |
|
| 66 | - $this->addElement($stream, $element); |
|
| 67 | - } |
|
| 68 | - // Add the trailing boundary with CRLF |
|
| 69 | - $stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n")); |
|
| 70 | - return $stream; |
|
| 71 | - } |
|
| 72 | - private function addElement(AppendStream $stream, array $element) : void |
|
| 73 | - { |
|
| 74 | - foreach (['contents', 'name'] as $key) { |
|
| 75 | - if (!\array_key_exists($key, $element)) { |
|
| 76 | - throw new \InvalidArgumentException("A '{$key}' key is required"); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - $element['contents'] = Utils::streamFor($element['contents']); |
|
| 80 | - if (empty($element['filename'])) { |
|
| 81 | - $uri = $element['contents']->getMetadata('uri'); |
|
| 82 | - if ($uri && \is_string($uri) && \substr($uri, 0, 6) !== 'php://' && \substr($uri, 0, 7) !== 'data://') { |
|
| 83 | - $element['filename'] = $uri; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - [$body, $headers] = $this->createElement($element['name'], $element['contents'], $element['filename'] ?? null, $element['headers'] ?? []); |
|
| 87 | - $stream->addStream(Utils::streamFor($this->getHeaders($headers))); |
|
| 88 | - $stream->addStream($body); |
|
| 89 | - $stream->addStream(Utils::streamFor("\r\n")); |
|
| 90 | - } |
|
| 91 | - /** |
|
| 92 | - * @param string[] $headers |
|
| 93 | - * |
|
| 94 | - * @return array{0: StreamInterface, 1: string[]} |
|
| 95 | - */ |
|
| 96 | - private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers) : array |
|
| 97 | - { |
|
| 98 | - // Set a default content-disposition header if one was no provided |
|
| 99 | - $disposition = self::getHeader($headers, 'content-disposition'); |
|
| 100 | - if (!$disposition) { |
|
| 101 | - $headers['Content-Disposition'] = $filename === '0' || $filename ? \sprintf('form-data; name="%s"; filename="%s"', $name, \basename($filename)) : "form-data; name=\"{$name}\""; |
|
| 102 | - } |
|
| 103 | - // Set a default content-length header if one was no provided |
|
| 104 | - $length = self::getHeader($headers, 'content-length'); |
|
| 105 | - if (!$length) { |
|
| 106 | - if ($length = $stream->getSize()) { |
|
| 107 | - $headers['Content-Length'] = (string) $length; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - // Set a default Content-Type if one was not supplied |
|
| 111 | - $type = self::getHeader($headers, 'content-type'); |
|
| 112 | - if (!$type && ($filename === '0' || $filename)) { |
|
| 113 | - $headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream'; |
|
| 114 | - } |
|
| 115 | - return [$stream, $headers]; |
|
| 116 | - } |
|
| 117 | - /** |
|
| 118 | - * @param string[] $headers |
|
| 119 | - */ |
|
| 120 | - private static function getHeader(array $headers, string $key) : ?string |
|
| 121 | - { |
|
| 122 | - $lowercaseHeader = \strtolower($key); |
|
| 123 | - foreach ($headers as $k => $v) { |
|
| 124 | - if (\strtolower((string) $k) === $lowercaseHeader) { |
|
| 125 | - return $v; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - return null; |
|
| 129 | - } |
|
| 13 | + use StreamDecoratorTrait; |
|
| 14 | + /** @var string */ |
|
| 15 | + private $boundary; |
|
| 16 | + /** @var StreamInterface */ |
|
| 17 | + private $stream; |
|
| 18 | + /** |
|
| 19 | + * @param array $elements Array of associative arrays, each containing a |
|
| 20 | + * required "name" key mapping to the form field, |
|
| 21 | + * name, a required "contents" key mapping to a |
|
| 22 | + * StreamInterface/resource/string, an optional |
|
| 23 | + * "headers" associative array of custom headers, |
|
| 24 | + * and an optional "filename" key mapping to a |
|
| 25 | + * string to send as the filename in the part. |
|
| 26 | + * @param string $boundary You can optionally provide a specific boundary |
|
| 27 | + * |
|
| 28 | + * @throws \InvalidArgumentException |
|
| 29 | + */ |
|
| 30 | + public function __construct(array $elements = [], string $boundary = null) |
|
| 31 | + { |
|
| 32 | + $this->boundary = $boundary ?: \bin2hex(\random_bytes(20)); |
|
| 33 | + $this->stream = $this->createStream($elements); |
|
| 34 | + } |
|
| 35 | + public function getBoundary() : string |
|
| 36 | + { |
|
| 37 | + return $this->boundary; |
|
| 38 | + } |
|
| 39 | + public function isWritable() : bool |
|
| 40 | + { |
|
| 41 | + return \false; |
|
| 42 | + } |
|
| 43 | + /** |
|
| 44 | + * Get the headers needed before transferring the content of a POST file |
|
| 45 | + * |
|
| 46 | + * @param string[] $headers |
|
| 47 | + */ |
|
| 48 | + private function getHeaders(array $headers) : string |
|
| 49 | + { |
|
| 50 | + $str = ''; |
|
| 51 | + foreach ($headers as $key => $value) { |
|
| 52 | + $str .= "{$key}: {$value}\r\n"; |
|
| 53 | + } |
|
| 54 | + return "--{$this->boundary}\r\n" . \trim($str) . "\r\n\r\n"; |
|
| 55 | + } |
|
| 56 | + /** |
|
| 57 | + * Create the aggregate stream that will be used to upload the POST data |
|
| 58 | + */ |
|
| 59 | + protected function createStream(array $elements = []) : StreamInterface |
|
| 60 | + { |
|
| 61 | + $stream = new AppendStream(); |
|
| 62 | + foreach ($elements as $element) { |
|
| 63 | + if (!\is_array($element)) { |
|
| 64 | + throw new \UnexpectedValueException('An array is expected'); |
|
| 65 | + } |
|
| 66 | + $this->addElement($stream, $element); |
|
| 67 | + } |
|
| 68 | + // Add the trailing boundary with CRLF |
|
| 69 | + $stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n")); |
|
| 70 | + return $stream; |
|
| 71 | + } |
|
| 72 | + private function addElement(AppendStream $stream, array $element) : void |
|
| 73 | + { |
|
| 74 | + foreach (['contents', 'name'] as $key) { |
|
| 75 | + if (!\array_key_exists($key, $element)) { |
|
| 76 | + throw new \InvalidArgumentException("A '{$key}' key is required"); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + $element['contents'] = Utils::streamFor($element['contents']); |
|
| 80 | + if (empty($element['filename'])) { |
|
| 81 | + $uri = $element['contents']->getMetadata('uri'); |
|
| 82 | + if ($uri && \is_string($uri) && \substr($uri, 0, 6) !== 'php://' && \substr($uri, 0, 7) !== 'data://') { |
|
| 83 | + $element['filename'] = $uri; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + [$body, $headers] = $this->createElement($element['name'], $element['contents'], $element['filename'] ?? null, $element['headers'] ?? []); |
|
| 87 | + $stream->addStream(Utils::streamFor($this->getHeaders($headers))); |
|
| 88 | + $stream->addStream($body); |
|
| 89 | + $stream->addStream(Utils::streamFor("\r\n")); |
|
| 90 | + } |
|
| 91 | + /** |
|
| 92 | + * @param string[] $headers |
|
| 93 | + * |
|
| 94 | + * @return array{0: StreamInterface, 1: string[]} |
|
| 95 | + */ |
|
| 96 | + private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers) : array |
|
| 97 | + { |
|
| 98 | + // Set a default content-disposition header if one was no provided |
|
| 99 | + $disposition = self::getHeader($headers, 'content-disposition'); |
|
| 100 | + if (!$disposition) { |
|
| 101 | + $headers['Content-Disposition'] = $filename === '0' || $filename ? \sprintf('form-data; name="%s"; filename="%s"', $name, \basename($filename)) : "form-data; name=\"{$name}\""; |
|
| 102 | + } |
|
| 103 | + // Set a default content-length header if one was no provided |
|
| 104 | + $length = self::getHeader($headers, 'content-length'); |
|
| 105 | + if (!$length) { |
|
| 106 | + if ($length = $stream->getSize()) { |
|
| 107 | + $headers['Content-Length'] = (string) $length; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + // Set a default Content-Type if one was not supplied |
|
| 111 | + $type = self::getHeader($headers, 'content-type'); |
|
| 112 | + if (!$type && ($filename === '0' || $filename)) { |
|
| 113 | + $headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream'; |
|
| 114 | + } |
|
| 115 | + return [$stream, $headers]; |
|
| 116 | + } |
|
| 117 | + /** |
|
| 118 | + * @param string[] $headers |
|
| 119 | + */ |
|
| 120 | + private static function getHeader(array $headers, string $key) : ?string |
|
| 121 | + { |
|
| 122 | + $lowercaseHeader = \strtolower($key); |
|
| 123 | + foreach ($headers as $k => $v) { |
|
| 124 | + if (\strtolower((string) $k) === $lowercaseHeader) { |
|
| 125 | + return $v; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | foreach ($headers as $key => $value) { |
| 52 | 52 | $str .= "{$key}: {$value}\r\n"; |
| 53 | 53 | } |
| 54 | - return "--{$this->boundary}\r\n" . \trim($str) . "\r\n\r\n"; |
|
| 54 | + return "--{$this->boundary}\r\n".\trim($str)."\r\n\r\n"; |
|
| 55 | 55 | } |
| 56 | 56 | /** |
| 57 | 57 | * Create the aggregate stream that will be used to upload the POST data |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $length = self::getHeader($headers, 'content-length'); |
| 105 | 105 | if (!$length) { |
| 106 | 106 | if ($length = $stream->getSize()) { |
| 107 | - $headers['Content-Length'] = (string) $length; |
|
| 107 | + $headers['Content-Length'] = (string)$length; |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | // Set a default Content-Type if one was not supplied |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | { |
| 122 | 122 | $lowercaseHeader = \strtolower($key); |
| 123 | 123 | foreach ($headers as $k => $v) { |
| 124 | - if (\strtolower((string) $k) === $lowercaseHeader) { |
|
| 124 | + if (\strtolower((string)$k) === $lowercaseHeader) { |
|
| 125 | 125 | return $v; |
| 126 | 126 | } |
| 127 | 127 | } |
@@ -8,8 +8,7 @@ |
||
| 8 | 8 | * Stream that when read returns bytes for a streaming multipart or |
| 9 | 9 | * multipart/form-data stream. |
| 10 | 10 | */ |
| 11 | -final class MultipartStream implements StreamInterface |
|
| 12 | -{ |
|
| 11 | +final class MultipartStream implements StreamInterface { |
|
| 13 | 12 | use StreamDecoratorTrait; |
| 14 | 13 | /** @var string */ |
| 15 | 14 | private $boundary; |
@@ -13,137 +13,137 @@ |
||
| 13 | 13 | #[\AllowDynamicProperties] |
| 14 | 14 | final class FnStream implements StreamInterface |
| 15 | 15 | { |
| 16 | - private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
|
| 17 | - /** @var array<string, callable> */ |
|
| 18 | - private $methods; |
|
| 19 | - /** |
|
| 20 | - * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 21 | - */ |
|
| 22 | - public function __construct(array $methods) |
|
| 23 | - { |
|
| 24 | - $this->methods = $methods; |
|
| 25 | - // Create the functions on the class |
|
| 26 | - foreach ($methods as $name => $fn) { |
|
| 27 | - $this->{'_fn_' . $name} = $fn; |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - /** |
|
| 31 | - * Lazily determine which methods are not implemented. |
|
| 32 | - * |
|
| 33 | - * @throws \BadMethodCallException |
|
| 34 | - */ |
|
| 35 | - public function __get(string $name) : void |
|
| 36 | - { |
|
| 37 | - throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
|
| 38 | - } |
|
| 39 | - /** |
|
| 40 | - * The close method is called on the underlying stream only if possible. |
|
| 41 | - */ |
|
| 42 | - public function __destruct() |
|
| 43 | - { |
|
| 44 | - if (isset($this->_fn_close)) { |
|
| 45 | - ($this->_fn_close)(); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - /** |
|
| 49 | - * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 50 | - * |
|
| 51 | - * @throws \LogicException |
|
| 52 | - */ |
|
| 53 | - public function __wakeup() : void |
|
| 54 | - { |
|
| 55 | - throw new \LogicException('FnStream should never be unserialized'); |
|
| 56 | - } |
|
| 57 | - /** |
|
| 58 | - * Adds custom functionality to an underlying stream by intercepting |
|
| 59 | - * specific method calls. |
|
| 60 | - * |
|
| 61 | - * @param StreamInterface $stream Stream to decorate |
|
| 62 | - * @param array<string, callable> $methods Hash of method name to a closure |
|
| 63 | - * |
|
| 64 | - * @return FnStream |
|
| 65 | - */ |
|
| 66 | - public static function decorate(StreamInterface $stream, array $methods) |
|
| 67 | - { |
|
| 68 | - // If any of the required methods were not provided, then simply |
|
| 69 | - // proxy to the decorated stream. |
|
| 70 | - foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { |
|
| 71 | - /** @var callable $callable */ |
|
| 72 | - $callable = [$stream, $diff]; |
|
| 73 | - $methods[$diff] = $callable; |
|
| 74 | - } |
|
| 75 | - return new self($methods); |
|
| 76 | - } |
|
| 77 | - public function __toString() : string |
|
| 78 | - { |
|
| 79 | - try { |
|
| 80 | - /** @var string */ |
|
| 81 | - return ($this->_fn___toString)(); |
|
| 82 | - } catch (\Throwable $e) { |
|
| 83 | - if (\PHP_VERSION_ID >= 70400) { |
|
| 84 | - throw $e; |
|
| 85 | - } |
|
| 86 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 87 | - return ''; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - public function close() : void |
|
| 91 | - { |
|
| 92 | - ($this->_fn_close)(); |
|
| 93 | - } |
|
| 94 | - public function detach() |
|
| 95 | - { |
|
| 96 | - return ($this->_fn_detach)(); |
|
| 97 | - } |
|
| 98 | - public function getSize() : ?int |
|
| 99 | - { |
|
| 100 | - return ($this->_fn_getSize)(); |
|
| 101 | - } |
|
| 102 | - public function tell() : int |
|
| 103 | - { |
|
| 104 | - return ($this->_fn_tell)(); |
|
| 105 | - } |
|
| 106 | - public function eof() : bool |
|
| 107 | - { |
|
| 108 | - return ($this->_fn_eof)(); |
|
| 109 | - } |
|
| 110 | - public function isSeekable() : bool |
|
| 111 | - { |
|
| 112 | - return ($this->_fn_isSeekable)(); |
|
| 113 | - } |
|
| 114 | - public function rewind() : void |
|
| 115 | - { |
|
| 116 | - ($this->_fn_rewind)(); |
|
| 117 | - } |
|
| 118 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
| 119 | - { |
|
| 120 | - ($this->_fn_seek)($offset, $whence); |
|
| 121 | - } |
|
| 122 | - public function isWritable() : bool |
|
| 123 | - { |
|
| 124 | - return ($this->_fn_isWritable)(); |
|
| 125 | - } |
|
| 126 | - public function write($string) : int |
|
| 127 | - { |
|
| 128 | - return ($this->_fn_write)($string); |
|
| 129 | - } |
|
| 130 | - public function isReadable() : bool |
|
| 131 | - { |
|
| 132 | - return ($this->_fn_isReadable)(); |
|
| 133 | - } |
|
| 134 | - public function read($length) : string |
|
| 135 | - { |
|
| 136 | - return ($this->_fn_read)($length); |
|
| 137 | - } |
|
| 138 | - public function getContents() : string |
|
| 139 | - { |
|
| 140 | - return ($this->_fn_getContents)(); |
|
| 141 | - } |
|
| 142 | - /** |
|
| 143 | - * @return mixed |
|
| 144 | - */ |
|
| 145 | - public function getMetadata($key = null) |
|
| 146 | - { |
|
| 147 | - return ($this->_fn_getMetadata)($key); |
|
| 148 | - } |
|
| 16 | + private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
|
| 17 | + /** @var array<string, callable> */ |
|
| 18 | + private $methods; |
|
| 19 | + /** |
|
| 20 | + * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 21 | + */ |
|
| 22 | + public function __construct(array $methods) |
|
| 23 | + { |
|
| 24 | + $this->methods = $methods; |
|
| 25 | + // Create the functions on the class |
|
| 26 | + foreach ($methods as $name => $fn) { |
|
| 27 | + $this->{'_fn_' . $name} = $fn; |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + /** |
|
| 31 | + * Lazily determine which methods are not implemented. |
|
| 32 | + * |
|
| 33 | + * @throws \BadMethodCallException |
|
| 34 | + */ |
|
| 35 | + public function __get(string $name) : void |
|
| 36 | + { |
|
| 37 | + throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
|
| 38 | + } |
|
| 39 | + /** |
|
| 40 | + * The close method is called on the underlying stream only if possible. |
|
| 41 | + */ |
|
| 42 | + public function __destruct() |
|
| 43 | + { |
|
| 44 | + if (isset($this->_fn_close)) { |
|
| 45 | + ($this->_fn_close)(); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + /** |
|
| 49 | + * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 50 | + * |
|
| 51 | + * @throws \LogicException |
|
| 52 | + */ |
|
| 53 | + public function __wakeup() : void |
|
| 54 | + { |
|
| 55 | + throw new \LogicException('FnStream should never be unserialized'); |
|
| 56 | + } |
|
| 57 | + /** |
|
| 58 | + * Adds custom functionality to an underlying stream by intercepting |
|
| 59 | + * specific method calls. |
|
| 60 | + * |
|
| 61 | + * @param StreamInterface $stream Stream to decorate |
|
| 62 | + * @param array<string, callable> $methods Hash of method name to a closure |
|
| 63 | + * |
|
| 64 | + * @return FnStream |
|
| 65 | + */ |
|
| 66 | + public static function decorate(StreamInterface $stream, array $methods) |
|
| 67 | + { |
|
| 68 | + // If any of the required methods were not provided, then simply |
|
| 69 | + // proxy to the decorated stream. |
|
| 70 | + foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { |
|
| 71 | + /** @var callable $callable */ |
|
| 72 | + $callable = [$stream, $diff]; |
|
| 73 | + $methods[$diff] = $callable; |
|
| 74 | + } |
|
| 75 | + return new self($methods); |
|
| 76 | + } |
|
| 77 | + public function __toString() : string |
|
| 78 | + { |
|
| 79 | + try { |
|
| 80 | + /** @var string */ |
|
| 81 | + return ($this->_fn___toString)(); |
|
| 82 | + } catch (\Throwable $e) { |
|
| 83 | + if (\PHP_VERSION_ID >= 70400) { |
|
| 84 | + throw $e; |
|
| 85 | + } |
|
| 86 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 87 | + return ''; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + public function close() : void |
|
| 91 | + { |
|
| 92 | + ($this->_fn_close)(); |
|
| 93 | + } |
|
| 94 | + public function detach() |
|
| 95 | + { |
|
| 96 | + return ($this->_fn_detach)(); |
|
| 97 | + } |
|
| 98 | + public function getSize() : ?int |
|
| 99 | + { |
|
| 100 | + return ($this->_fn_getSize)(); |
|
| 101 | + } |
|
| 102 | + public function tell() : int |
|
| 103 | + { |
|
| 104 | + return ($this->_fn_tell)(); |
|
| 105 | + } |
|
| 106 | + public function eof() : bool |
|
| 107 | + { |
|
| 108 | + return ($this->_fn_eof)(); |
|
| 109 | + } |
|
| 110 | + public function isSeekable() : bool |
|
| 111 | + { |
|
| 112 | + return ($this->_fn_isSeekable)(); |
|
| 113 | + } |
|
| 114 | + public function rewind() : void |
|
| 115 | + { |
|
| 116 | + ($this->_fn_rewind)(); |
|
| 117 | + } |
|
| 118 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
| 119 | + { |
|
| 120 | + ($this->_fn_seek)($offset, $whence); |
|
| 121 | + } |
|
| 122 | + public function isWritable() : bool |
|
| 123 | + { |
|
| 124 | + return ($this->_fn_isWritable)(); |
|
| 125 | + } |
|
| 126 | + public function write($string) : int |
|
| 127 | + { |
|
| 128 | + return ($this->_fn_write)($string); |
|
| 129 | + } |
|
| 130 | + public function isReadable() : bool |
|
| 131 | + { |
|
| 132 | + return ($this->_fn_isReadable)(); |
|
| 133 | + } |
|
| 134 | + public function read($length) : string |
|
| 135 | + { |
|
| 136 | + return ($this->_fn_read)($length); |
|
| 137 | + } |
|
| 138 | + public function getContents() : string |
|
| 139 | + { |
|
| 140 | + return ($this->_fn_getContents)(); |
|
| 141 | + } |
|
| 142 | + /** |
|
| 143 | + * @return mixed |
|
| 144 | + */ |
|
| 145 | + public function getMetadata($key = null) |
|
| 146 | + { |
|
| 147 | + return ($this->_fn_getMetadata)($key); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7; |
| 5 | 5 | |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | $this->methods = $methods; |
| 25 | 25 | // Create the functions on the class |
| 26 | 26 | foreach ($methods as $name => $fn) { |
| 27 | - $this->{'_fn_' . $name} = $fn; |
|
| 27 | + $this->{'_fn_'.$name} = $fn; |
|
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | /** |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function __get(string $name) : void |
| 36 | 36 | { |
| 37 | - throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
|
| 37 | + throw new \BadMethodCallException(\str_replace('_fn_', '', $name).'() is not implemented in the FnStream'); |
|
| 38 | 38 | } |
| 39 | 39 | /** |
| 40 | 40 | * The close method is called on the underlying stream only if possible. |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | if (\PHP_VERSION_ID >= 70400) { |
| 84 | 84 | throw $e; |
| 85 | 85 | } |
| 86 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 86 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string)$e), \E_USER_ERROR); |
|
| 87 | 87 | return ''; |
| 88 | 88 | } |
| 89 | 89 | } |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * to create a concrete class for a simple extension point. |
| 12 | 12 | */ |
| 13 | 13 | #[\AllowDynamicProperties] |
| 14 | -final class FnStream implements StreamInterface |
|
| 15 | -{ |
|
| 14 | +final class FnStream implements StreamInterface { |
|
| 16 | 15 | private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
| 17 | 16 | /** @var array<string, callable> */ |
| 18 | 17 | private $methods; |