@@ -12,67 +12,67 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface ClientInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The Guzzle major version. |
|
| 17 | - */ |
|
| 18 | - public const MAJOR_VERSION = 7; |
|
| 19 | - /** |
|
| 20 | - * Send an HTTP request. |
|
| 21 | - * |
|
| 22 | - * @param RequestInterface $request Request to send |
|
| 23 | - * @param array $options Request options to apply to the given |
|
| 24 | - * request and to the transfer. |
|
| 25 | - * |
|
| 26 | - * @throws GuzzleException |
|
| 27 | - */ |
|
| 28 | - public function send(RequestInterface $request, array $options = []) : ResponseInterface; |
|
| 29 | - /** |
|
| 30 | - * Asynchronously send an HTTP request. |
|
| 31 | - * |
|
| 32 | - * @param RequestInterface $request Request to send |
|
| 33 | - * @param array $options Request options to apply to the given |
|
| 34 | - * request and to the transfer. |
|
| 35 | - */ |
|
| 36 | - public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface; |
|
| 37 | - /** |
|
| 38 | - * Create and send an HTTP request. |
|
| 39 | - * |
|
| 40 | - * Use an absolute path to override the base path of the client, or a |
|
| 41 | - * relative path to append to the base path of the client. The URL can |
|
| 42 | - * contain the query string as well. |
|
| 43 | - * |
|
| 44 | - * @param string $method HTTP method. |
|
| 45 | - * @param string|UriInterface $uri URI object or string. |
|
| 46 | - * @param array $options Request options to apply. |
|
| 47 | - * |
|
| 48 | - * @throws GuzzleException |
|
| 49 | - */ |
|
| 50 | - public function request(string $method, $uri, array $options = []) : ResponseInterface; |
|
| 51 | - /** |
|
| 52 | - * Create and send an asynchronous HTTP request. |
|
| 53 | - * |
|
| 54 | - * Use an absolute path to override the base path of the client, or a |
|
| 55 | - * relative path to append to the base path of the client. The URL can |
|
| 56 | - * contain the query string as well. Use an array to provide a URL |
|
| 57 | - * template and additional variables to use in the URL template expansion. |
|
| 58 | - * |
|
| 59 | - * @param string $method HTTP method |
|
| 60 | - * @param string|UriInterface $uri URI object or string. |
|
| 61 | - * @param array $options Request options to apply. |
|
| 62 | - */ |
|
| 63 | - public function requestAsync(string $method, $uri, array $options = []) : PromiseInterface; |
|
| 64 | - /** |
|
| 65 | - * Get a client configuration option. |
|
| 66 | - * |
|
| 67 | - * These options include default request options of the client, a "handler" |
|
| 68 | - * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 69 | - * the concrete client. |
|
| 70 | - * |
|
| 71 | - * @param string|null $option The config option to retrieve. |
|
| 72 | - * |
|
| 73 | - * @return mixed |
|
| 74 | - * |
|
| 75 | - * @deprecated ClientInterface::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 76 | - */ |
|
| 77 | - public function getConfig(?string $option = null); |
|
| 15 | + /** |
|
| 16 | + * The Guzzle major version. |
|
| 17 | + */ |
|
| 18 | + public const MAJOR_VERSION = 7; |
|
| 19 | + /** |
|
| 20 | + * Send an HTTP request. |
|
| 21 | + * |
|
| 22 | + * @param RequestInterface $request Request to send |
|
| 23 | + * @param array $options Request options to apply to the given |
|
| 24 | + * request and to the transfer. |
|
| 25 | + * |
|
| 26 | + * @throws GuzzleException |
|
| 27 | + */ |
|
| 28 | + public function send(RequestInterface $request, array $options = []) : ResponseInterface; |
|
| 29 | + /** |
|
| 30 | + * Asynchronously send an HTTP request. |
|
| 31 | + * |
|
| 32 | + * @param RequestInterface $request Request to send |
|
| 33 | + * @param array $options Request options to apply to the given |
|
| 34 | + * request and to the transfer. |
|
| 35 | + */ |
|
| 36 | + public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface; |
|
| 37 | + /** |
|
| 38 | + * Create and send an HTTP request. |
|
| 39 | + * |
|
| 40 | + * Use an absolute path to override the base path of the client, or a |
|
| 41 | + * relative path to append to the base path of the client. The URL can |
|
| 42 | + * contain the query string as well. |
|
| 43 | + * |
|
| 44 | + * @param string $method HTTP method. |
|
| 45 | + * @param string|UriInterface $uri URI object or string. |
|
| 46 | + * @param array $options Request options to apply. |
|
| 47 | + * |
|
| 48 | + * @throws GuzzleException |
|
| 49 | + */ |
|
| 50 | + public function request(string $method, $uri, array $options = []) : ResponseInterface; |
|
| 51 | + /** |
|
| 52 | + * Create and send an asynchronous HTTP request. |
|
| 53 | + * |
|
| 54 | + * Use an absolute path to override the base path of the client, or a |
|
| 55 | + * relative path to append to the base path of the client. The URL can |
|
| 56 | + * contain the query string as well. Use an array to provide a URL |
|
| 57 | + * template and additional variables to use in the URL template expansion. |
|
| 58 | + * |
|
| 59 | + * @param string $method HTTP method |
|
| 60 | + * @param string|UriInterface $uri URI object or string. |
|
| 61 | + * @param array $options Request options to apply. |
|
| 62 | + */ |
|
| 63 | + public function requestAsync(string $method, $uri, array $options = []) : PromiseInterface; |
|
| 64 | + /** |
|
| 65 | + * Get a client configuration option. |
|
| 66 | + * |
|
| 67 | + * These options include default request options of the client, a "handler" |
|
| 68 | + * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 69 | + * the concrete client. |
|
| 70 | + * |
|
| 71 | + * @param string|null $option The config option to retrieve. |
|
| 72 | + * |
|
| 73 | + * @return mixed |
|
| 74 | + * |
|
| 75 | + * @deprecated ClientInterface::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 76 | + */ |
|
| 77 | + public function getConfig(?string $option = null); |
|
| 78 | 78 | } |
@@ -12,100 +12,100 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class RequestException extends TransferException implements RequestExceptionInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var RequestInterface |
|
| 17 | - */ |
|
| 18 | - private $request; |
|
| 19 | - /** |
|
| 20 | - * @var ResponseInterface|null |
|
| 21 | - */ |
|
| 22 | - private $response; |
|
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - private $handlerContext; |
|
| 27 | - public function __construct(string $message, RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 28 | - { |
|
| 29 | - // Set the code of the exception if the response is set and not future. |
|
| 30 | - $code = $response ? $response->getStatusCode() : 0; |
|
| 31 | - parent::__construct($message, $code, $previous); |
|
| 32 | - $this->request = $request; |
|
| 33 | - $this->response = $response; |
|
| 34 | - $this->handlerContext = $handlerContext; |
|
| 35 | - } |
|
| 36 | - /** |
|
| 37 | - * Wrap non-RequestExceptions with a RequestException |
|
| 38 | - */ |
|
| 39 | - public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
| 40 | - { |
|
| 41 | - return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
| 42 | - } |
|
| 43 | - /** |
|
| 44 | - * Factory method to create a new exception with a normalized error message |
|
| 45 | - * |
|
| 46 | - * @param RequestInterface $request Request sent |
|
| 47 | - * @param ResponseInterface $response Response received |
|
| 48 | - * @param \Throwable|null $previous Previous exception |
|
| 49 | - * @param array $handlerContext Optional handler context |
|
| 50 | - * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
| 51 | - */ |
|
| 52 | - public static function create(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = [], ?BodySummarizerInterface $bodySummarizer = null) : self |
|
| 53 | - { |
|
| 54 | - if (!$response) { |
|
| 55 | - return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
| 56 | - } |
|
| 57 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
| 58 | - if ($level === 4) { |
|
| 59 | - $label = 'Client error'; |
|
| 60 | - $className = ClientException::class; |
|
| 61 | - } elseif ($level === 5) { |
|
| 62 | - $label = 'Server error'; |
|
| 63 | - $className = ServerException::class; |
|
| 64 | - } else { |
|
| 65 | - $label = 'Unsuccessful request'; |
|
| 66 | - $className = __CLASS__; |
|
| 67 | - } |
|
| 68 | - $uri = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri()); |
|
| 69 | - // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
| 70 | - // <html> ... (truncated) |
|
| 71 | - $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
| 72 | - $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
| 73 | - if ($summary !== null) { |
|
| 74 | - $message .= ":\n{$summary}\n"; |
|
| 75 | - } |
|
| 76 | - return new $className($message, $request, $response, $previous, $handlerContext); |
|
| 77 | - } |
|
| 78 | - /** |
|
| 79 | - * Get the request that caused the exception |
|
| 80 | - */ |
|
| 81 | - public function getRequest() : RequestInterface |
|
| 82 | - { |
|
| 83 | - return $this->request; |
|
| 84 | - } |
|
| 85 | - /** |
|
| 86 | - * Get the associated response |
|
| 87 | - */ |
|
| 88 | - public function getResponse() : ?ResponseInterface |
|
| 89 | - { |
|
| 90 | - return $this->response; |
|
| 91 | - } |
|
| 92 | - /** |
|
| 93 | - * Check if a response was received |
|
| 94 | - */ |
|
| 95 | - public function hasResponse() : bool |
|
| 96 | - { |
|
| 97 | - return $this->response !== null; |
|
| 98 | - } |
|
| 99 | - /** |
|
| 100 | - * Get contextual information about the error from the underlying handler. |
|
| 101 | - * |
|
| 102 | - * The contents of this array will vary depending on which handler you are |
|
| 103 | - * using. It may also be just an empty array. Relying on this data will |
|
| 104 | - * couple you to a specific handler, but can give more debug information |
|
| 105 | - * when needed. |
|
| 106 | - */ |
|
| 107 | - public function getHandlerContext() : array |
|
| 108 | - { |
|
| 109 | - return $this->handlerContext; |
|
| 110 | - } |
|
| 15 | + /** |
|
| 16 | + * @var RequestInterface |
|
| 17 | + */ |
|
| 18 | + private $request; |
|
| 19 | + /** |
|
| 20 | + * @var ResponseInterface|null |
|
| 21 | + */ |
|
| 22 | + private $response; |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + private $handlerContext; |
|
| 27 | + public function __construct(string $message, RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 28 | + { |
|
| 29 | + // Set the code of the exception if the response is set and not future. |
|
| 30 | + $code = $response ? $response->getStatusCode() : 0; |
|
| 31 | + parent::__construct($message, $code, $previous); |
|
| 32 | + $this->request = $request; |
|
| 33 | + $this->response = $response; |
|
| 34 | + $this->handlerContext = $handlerContext; |
|
| 35 | + } |
|
| 36 | + /** |
|
| 37 | + * Wrap non-RequestExceptions with a RequestException |
|
| 38 | + */ |
|
| 39 | + public static function wrapException(RequestInterface $request, \Throwable $e) : RequestException |
|
| 40 | + { |
|
| 41 | + return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
| 42 | + } |
|
| 43 | + /** |
|
| 44 | + * Factory method to create a new exception with a normalized error message |
|
| 45 | + * |
|
| 46 | + * @param RequestInterface $request Request sent |
|
| 47 | + * @param ResponseInterface $response Response received |
|
| 48 | + * @param \Throwable|null $previous Previous exception |
|
| 49 | + * @param array $handlerContext Optional handler context |
|
| 50 | + * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
| 51 | + */ |
|
| 52 | + public static function create(RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $previous = null, array $handlerContext = [], ?BodySummarizerInterface $bodySummarizer = null) : self |
|
| 53 | + { |
|
| 54 | + if (!$response) { |
|
| 55 | + return new self('Error completing request', $request, null, $previous, $handlerContext); |
|
| 56 | + } |
|
| 57 | + $level = (int) \floor($response->getStatusCode() / 100); |
|
| 58 | + if ($level === 4) { |
|
| 59 | + $label = 'Client error'; |
|
| 60 | + $className = ClientException::class; |
|
| 61 | + } elseif ($level === 5) { |
|
| 62 | + $label = 'Server error'; |
|
| 63 | + $className = ServerException::class; |
|
| 64 | + } else { |
|
| 65 | + $label = 'Unsuccessful request'; |
|
| 66 | + $className = __CLASS__; |
|
| 67 | + } |
|
| 68 | + $uri = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri()); |
|
| 69 | + // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
| 70 | + // <html> ... (truncated) |
|
| 71 | + $message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri->__toString(), $response->getStatusCode(), $response->getReasonPhrase()); |
|
| 72 | + $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
| 73 | + if ($summary !== null) { |
|
| 74 | + $message .= ":\n{$summary}\n"; |
|
| 75 | + } |
|
| 76 | + return new $className($message, $request, $response, $previous, $handlerContext); |
|
| 77 | + } |
|
| 78 | + /** |
|
| 79 | + * Get the request that caused the exception |
|
| 80 | + */ |
|
| 81 | + public function getRequest() : RequestInterface |
|
| 82 | + { |
|
| 83 | + return $this->request; |
|
| 84 | + } |
|
| 85 | + /** |
|
| 86 | + * Get the associated response |
|
| 87 | + */ |
|
| 88 | + public function getResponse() : ?ResponseInterface |
|
| 89 | + { |
|
| 90 | + return $this->response; |
|
| 91 | + } |
|
| 92 | + /** |
|
| 93 | + * Check if a response was received |
|
| 94 | + */ |
|
| 95 | + public function hasResponse() : bool |
|
| 96 | + { |
|
| 97 | + return $this->response !== null; |
|
| 98 | + } |
|
| 99 | + /** |
|
| 100 | + * Get contextual information about the error from the underlying handler. |
|
| 101 | + * |
|
| 102 | + * The contents of this array will vary depending on which handler you are |
|
| 103 | + * using. It may also be just an empty array. Relying on this data will |
|
| 104 | + * couple you to a specific handler, but can give more debug information |
|
| 105 | + * when needed. |
|
| 106 | + */ |
|
| 107 | + public function getHandlerContext() : array |
|
| 108 | + { |
|
| 109 | + return $this->handlerContext; |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -9,23 +9,23 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class BadResponseException extends RequestException |
| 11 | 11 | { |
| 12 | - public function __construct(string $message, RequestInterface $request, ResponseInterface $response, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 13 | - { |
|
| 14 | - parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
| 15 | - } |
|
| 16 | - /** |
|
| 17 | - * Current exception and the ones that extend it will always have a response. |
|
| 18 | - */ |
|
| 19 | - public function hasResponse() : bool |
|
| 20 | - { |
|
| 21 | - return \true; |
|
| 22 | - } |
|
| 23 | - /** |
|
| 24 | - * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
| 25 | - */ |
|
| 26 | - public function getResponse() : ResponseInterface |
|
| 27 | - { |
|
| 28 | - /** @var ResponseInterface */ |
|
| 29 | - return parent::getResponse(); |
|
| 30 | - } |
|
| 12 | + public function __construct(string $message, RequestInterface $request, ResponseInterface $response, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 13 | + { |
|
| 14 | + parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
| 15 | + } |
|
| 16 | + /** |
|
| 17 | + * Current exception and the ones that extend it will always have a response. |
|
| 18 | + */ |
|
| 19 | + public function hasResponse() : bool |
|
| 20 | + { |
|
| 21 | + return \true; |
|
| 22 | + } |
|
| 23 | + /** |
|
| 24 | + * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
| 25 | + */ |
|
| 26 | + public function getResponse() : ResponseInterface |
|
| 27 | + { |
|
| 28 | + /** @var ResponseInterface */ |
|
| 29 | + return parent::getResponse(); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Exception when an HTTP error occurs (4xx or 5xx error) |
| 9 | 9 | */ |
| 10 | -class BadResponseException extends RequestException |
|
| 11 | -{ |
|
| 10 | +class BadResponseException extends RequestException { |
|
| 12 | 11 | public function __construct(string $message, RequestInterface $request, ResponseInterface $response, ?\Throwable $previous = null, array $handlerContext = []) |
| 13 | 12 | { |
| 14 | 13 | parent::__construct($message, $request, $response, $previous, $handlerContext); |
@@ -11,37 +11,37 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ConnectException extends TransferException implements NetworkExceptionInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var RequestInterface |
|
| 16 | - */ |
|
| 17 | - private $request; |
|
| 18 | - /** |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - private $handlerContext; |
|
| 22 | - public function __construct(string $message, RequestInterface $request, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 23 | - { |
|
| 24 | - parent::__construct($message, 0, $previous); |
|
| 25 | - $this->request = $request; |
|
| 26 | - $this->handlerContext = $handlerContext; |
|
| 27 | - } |
|
| 28 | - /** |
|
| 29 | - * Get the request that caused the exception |
|
| 30 | - */ |
|
| 31 | - public function getRequest() : RequestInterface |
|
| 32 | - { |
|
| 33 | - return $this->request; |
|
| 34 | - } |
|
| 35 | - /** |
|
| 36 | - * Get contextual information about the error from the underlying handler. |
|
| 37 | - * |
|
| 38 | - * The contents of this array will vary depending on which handler you are |
|
| 39 | - * using. It may also be just an empty array. Relying on this data will |
|
| 40 | - * couple you to a specific handler, but can give more debug information |
|
| 41 | - * when needed. |
|
| 42 | - */ |
|
| 43 | - public function getHandlerContext() : array |
|
| 44 | - { |
|
| 45 | - return $this->handlerContext; |
|
| 46 | - } |
|
| 14 | + /** |
|
| 15 | + * @var RequestInterface |
|
| 16 | + */ |
|
| 17 | + private $request; |
|
| 18 | + /** |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + private $handlerContext; |
|
| 22 | + public function __construct(string $message, RequestInterface $request, ?\Throwable $previous = null, array $handlerContext = []) |
|
| 23 | + { |
|
| 24 | + parent::__construct($message, 0, $previous); |
|
| 25 | + $this->request = $request; |
|
| 26 | + $this->handlerContext = $handlerContext; |
|
| 27 | + } |
|
| 28 | + /** |
|
| 29 | + * Get the request that caused the exception |
|
| 30 | + */ |
|
| 31 | + public function getRequest() : RequestInterface |
|
| 32 | + { |
|
| 33 | + return $this->request; |
|
| 34 | + } |
|
| 35 | + /** |
|
| 36 | + * Get contextual information about the error from the underlying handler. |
|
| 37 | + * |
|
| 38 | + * The contents of this array will vary depending on which handler you are |
|
| 39 | + * using. It may also be just an empty array. Relying on this data will |
|
| 40 | + * couple you to a specific handler, but can give more debug information |
|
| 41 | + * when needed. |
|
| 42 | + */ |
|
| 43 | + public function getHandlerContext() : array |
|
| 44 | + { |
|
| 45 | + return $this->handlerContext; |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -18,57 +18,57 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface CookieJarInterface extends \Countable, \IteratorAggregate |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Create a request with added cookie headers. |
|
| 23 | - * |
|
| 24 | - * If no matching cookies are found in the cookie jar, then no Cookie |
|
| 25 | - * header is added to the request and the same request is returned. |
|
| 26 | - * |
|
| 27 | - * @param RequestInterface $request Request object to modify. |
|
| 28 | - * |
|
| 29 | - * @return RequestInterface returns the modified request. |
|
| 30 | - */ |
|
| 31 | - public function withCookieHeader(RequestInterface $request) : RequestInterface; |
|
| 32 | - /** |
|
| 33 | - * Extract cookies from an HTTP response and store them in the CookieJar. |
|
| 34 | - * |
|
| 35 | - * @param RequestInterface $request Request that was sent |
|
| 36 | - * @param ResponseInterface $response Response that was received |
|
| 37 | - */ |
|
| 38 | - public function extractCookies(RequestInterface $request, ResponseInterface $response) : void; |
|
| 39 | - /** |
|
| 40 | - * Sets a cookie in the cookie jar. |
|
| 41 | - * |
|
| 42 | - * @param SetCookie $cookie Cookie to set. |
|
| 43 | - * |
|
| 44 | - * @return bool Returns true on success or false on failure |
|
| 45 | - */ |
|
| 46 | - public function setCookie(SetCookie $cookie) : bool; |
|
| 47 | - /** |
|
| 48 | - * Remove cookies currently held in the cookie jar. |
|
| 49 | - * |
|
| 50 | - * Invoking this method without arguments will empty the whole cookie jar. |
|
| 51 | - * If given a $domain argument only cookies belonging to that domain will |
|
| 52 | - * be removed. If given a $domain and $path argument, cookies belonging to |
|
| 53 | - * the specified path within that domain are removed. If given all three |
|
| 54 | - * arguments, then the cookie with the specified name, path and domain is |
|
| 55 | - * removed. |
|
| 56 | - * |
|
| 57 | - * @param string|null $domain Clears cookies matching a domain |
|
| 58 | - * @param string|null $path Clears cookies matching a domain and path |
|
| 59 | - * @param string|null $name Clears cookies matching a domain, path, and name |
|
| 60 | - */ |
|
| 61 | - public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void; |
|
| 62 | - /** |
|
| 63 | - * Discard all sessions cookies. |
|
| 64 | - * |
|
| 65 | - * Removes cookies that don't have an expire field or a have a discard |
|
| 66 | - * field set to true. To be called when the user agent shuts down according |
|
| 67 | - * to RFC 2965. |
|
| 68 | - */ |
|
| 69 | - public function clearSessionCookies() : void; |
|
| 70 | - /** |
|
| 71 | - * Converts the cookie jar to an array. |
|
| 72 | - */ |
|
| 73 | - public function toArray() : array; |
|
| 21 | + /** |
|
| 22 | + * Create a request with added cookie headers. |
|
| 23 | + * |
|
| 24 | + * If no matching cookies are found in the cookie jar, then no Cookie |
|
| 25 | + * header is added to the request and the same request is returned. |
|
| 26 | + * |
|
| 27 | + * @param RequestInterface $request Request object to modify. |
|
| 28 | + * |
|
| 29 | + * @return RequestInterface returns the modified request. |
|
| 30 | + */ |
|
| 31 | + public function withCookieHeader(RequestInterface $request) : RequestInterface; |
|
| 32 | + /** |
|
| 33 | + * Extract cookies from an HTTP response and store them in the CookieJar. |
|
| 34 | + * |
|
| 35 | + * @param RequestInterface $request Request that was sent |
|
| 36 | + * @param ResponseInterface $response Response that was received |
|
| 37 | + */ |
|
| 38 | + public function extractCookies(RequestInterface $request, ResponseInterface $response) : void; |
|
| 39 | + /** |
|
| 40 | + * Sets a cookie in the cookie jar. |
|
| 41 | + * |
|
| 42 | + * @param SetCookie $cookie Cookie to set. |
|
| 43 | + * |
|
| 44 | + * @return bool Returns true on success or false on failure |
|
| 45 | + */ |
|
| 46 | + public function setCookie(SetCookie $cookie) : bool; |
|
| 47 | + /** |
|
| 48 | + * Remove cookies currently held in the cookie jar. |
|
| 49 | + * |
|
| 50 | + * Invoking this method without arguments will empty the whole cookie jar. |
|
| 51 | + * If given a $domain argument only cookies belonging to that domain will |
|
| 52 | + * be removed. If given a $domain and $path argument, cookies belonging to |
|
| 53 | + * the specified path within that domain are removed. If given all three |
|
| 54 | + * arguments, then the cookie with the specified name, path and domain is |
|
| 55 | + * removed. |
|
| 56 | + * |
|
| 57 | + * @param string|null $domain Clears cookies matching a domain |
|
| 58 | + * @param string|null $path Clears cookies matching a domain and path |
|
| 59 | + * @param string|null $name Clears cookies matching a domain, path, and name |
|
| 60 | + */ |
|
| 61 | + public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void; |
|
| 62 | + /** |
|
| 63 | + * Discard all sessions cookies. |
|
| 64 | + * |
|
| 65 | + * Removes cookies that don't have an expire field or a have a discard |
|
| 66 | + * field set to true. To be called when the user agent shuts down according |
|
| 67 | + * to RFC 2965. |
|
| 68 | + */ |
|
| 69 | + public function clearSessionCookies() : void; |
|
| 70 | + /** |
|
| 71 | + * Converts the cookie jar to an array. |
|
| 72 | + */ |
|
| 73 | + public function toArray() : array; |
|
| 74 | 74 | } |
@@ -9,232 +9,232 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class CookieJar implements CookieJarInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var SetCookie[] Loaded cookie data |
|
| 14 | - */ |
|
| 15 | - private $cookies = []; |
|
| 16 | - /** |
|
| 17 | - * @var bool |
|
| 18 | - */ |
|
| 19 | - private $strictMode; |
|
| 20 | - /** |
|
| 21 | - * @param bool $strictMode Set to true to throw exceptions when invalid |
|
| 22 | - * cookies are added to the cookie jar. |
|
| 23 | - * @param array $cookieArray Array of SetCookie objects or a hash of |
|
| 24 | - * arrays that can be used with the SetCookie |
|
| 25 | - * constructor |
|
| 26 | - */ |
|
| 27 | - public function __construct(bool $strictMode = \false, array $cookieArray = []) |
|
| 28 | - { |
|
| 29 | - $this->strictMode = $strictMode; |
|
| 30 | - foreach ($cookieArray as $cookie) { |
|
| 31 | - if (!$cookie instanceof SetCookie) { |
|
| 32 | - $cookie = new SetCookie($cookie); |
|
| 33 | - } |
|
| 34 | - $this->setCookie($cookie); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - /** |
|
| 38 | - * Create a new Cookie jar from an associative array and domain. |
|
| 39 | - * |
|
| 40 | - * @param array $cookies Cookies to create the jar from |
|
| 41 | - * @param string $domain Domain to set the cookies to |
|
| 42 | - */ |
|
| 43 | - public static function fromArray(array $cookies, string $domain) : self |
|
| 44 | - { |
|
| 45 | - $cookieJar = new self(); |
|
| 46 | - foreach ($cookies as $name => $value) { |
|
| 47 | - $cookieJar->setCookie(new SetCookie(['Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => \true])); |
|
| 48 | - } |
|
| 49 | - return $cookieJar; |
|
| 50 | - } |
|
| 51 | - /** |
|
| 52 | - * Evaluate if this cookie should be persisted to storage |
|
| 53 | - * that survives between requests. |
|
| 54 | - * |
|
| 55 | - * @param SetCookie $cookie Being evaluated. |
|
| 56 | - * @param bool $allowSessionCookies If we should persist session cookies |
|
| 57 | - */ |
|
| 58 | - public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = \false) : bool |
|
| 59 | - { |
|
| 60 | - if ($cookie->getExpires() || $allowSessionCookies) { |
|
| 61 | - if (!$cookie->getDiscard()) { |
|
| 62 | - return \true; |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - return \false; |
|
| 66 | - } |
|
| 67 | - /** |
|
| 68 | - * Finds and returns the cookie based on the name |
|
| 69 | - * |
|
| 70 | - * @param string $name cookie name to search for |
|
| 71 | - * |
|
| 72 | - * @return SetCookie|null cookie that was found or null if not found |
|
| 73 | - */ |
|
| 74 | - public function getCookieByName(string $name) : ?SetCookie |
|
| 75 | - { |
|
| 76 | - foreach ($this->cookies as $cookie) { |
|
| 77 | - if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
| 78 | - return $cookie; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - return null; |
|
| 82 | - } |
|
| 83 | - public function toArray() : array |
|
| 84 | - { |
|
| 85 | - return \array_map(static function (SetCookie $cookie) : array { |
|
| 86 | - return $cookie->toArray(); |
|
| 87 | - }, $this->getIterator()->getArrayCopy()); |
|
| 88 | - } |
|
| 89 | - public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void |
|
| 90 | - { |
|
| 91 | - if (!$domain) { |
|
| 92 | - $this->cookies = []; |
|
| 93 | - return; |
|
| 94 | - } elseif (!$path) { |
|
| 95 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($domain) : bool { |
|
| 96 | - return !$cookie->matchesDomain($domain); |
|
| 97 | - }); |
|
| 98 | - } elseif (!$name) { |
|
| 99 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain) : bool { |
|
| 100 | - return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 101 | - }); |
|
| 102 | - } else { |
|
| 103 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain, $name) { |
|
| 104 | - return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 105 | - }); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - public function clearSessionCookies() : void |
|
| 109 | - { |
|
| 110 | - $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) : bool { |
|
| 111 | - return !$cookie->getDiscard() && $cookie->getExpires(); |
|
| 112 | - }); |
|
| 113 | - } |
|
| 114 | - public function setCookie(SetCookie $cookie) : bool |
|
| 115 | - { |
|
| 116 | - // If the name string is empty (but not 0), ignore the set-cookie |
|
| 117 | - // string entirely. |
|
| 118 | - $name = $cookie->getName(); |
|
| 119 | - if (!$name && $name !== '0') { |
|
| 120 | - return \false; |
|
| 121 | - } |
|
| 122 | - // Only allow cookies with set and valid domain, name, value |
|
| 123 | - $result = $cookie->validate(); |
|
| 124 | - if ($result !== \true) { |
|
| 125 | - if ($this->strictMode) { |
|
| 126 | - throw new \RuntimeException('Invalid cookie: ' . $result); |
|
| 127 | - } |
|
| 128 | - $this->removeCookieIfEmpty($cookie); |
|
| 129 | - return \false; |
|
| 130 | - } |
|
| 131 | - // Resolve conflicts with previously set cookies |
|
| 132 | - foreach ($this->cookies as $i => $c) { |
|
| 133 | - // Two cookies are identical, when their path, and domain are |
|
| 134 | - // identical. |
|
| 135 | - if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName()) { |
|
| 136 | - continue; |
|
| 137 | - } |
|
| 138 | - // The previously set cookie is a discard cookie and this one is |
|
| 139 | - // not so allow the new cookie to be set |
|
| 140 | - if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
| 141 | - unset($this->cookies[$i]); |
|
| 142 | - continue; |
|
| 143 | - } |
|
| 144 | - // If the new cookie's expiration is further into the future, then |
|
| 145 | - // replace the old cookie |
|
| 146 | - if ($cookie->getExpires() > $c->getExpires()) { |
|
| 147 | - unset($this->cookies[$i]); |
|
| 148 | - continue; |
|
| 149 | - } |
|
| 150 | - // If the value has changed, we better change it |
|
| 151 | - if ($cookie->getValue() !== $c->getValue()) { |
|
| 152 | - unset($this->cookies[$i]); |
|
| 153 | - continue; |
|
| 154 | - } |
|
| 155 | - // The cookie exists, so no need to continue |
|
| 156 | - return \false; |
|
| 157 | - } |
|
| 158 | - $this->cookies[] = $cookie; |
|
| 159 | - return \true; |
|
| 160 | - } |
|
| 161 | - public function count() : int |
|
| 162 | - { |
|
| 163 | - return \count($this->cookies); |
|
| 164 | - } |
|
| 165 | - /** |
|
| 166 | - * @return \ArrayIterator<int, SetCookie> |
|
| 167 | - */ |
|
| 168 | - public function getIterator() : \ArrayIterator |
|
| 169 | - { |
|
| 170 | - return new \ArrayIterator(\array_values($this->cookies)); |
|
| 171 | - } |
|
| 172 | - public function extractCookies(RequestInterface $request, ResponseInterface $response) : void |
|
| 173 | - { |
|
| 174 | - if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
| 175 | - foreach ($cookieHeader as $cookie) { |
|
| 176 | - $sc = SetCookie::fromString($cookie); |
|
| 177 | - if (!$sc->getDomain()) { |
|
| 178 | - $sc->setDomain($request->getUri()->getHost()); |
|
| 179 | - } |
|
| 180 | - if (0 !== \strpos($sc->getPath(), '/')) { |
|
| 181 | - $sc->setPath($this->getCookiePathFromRequest($request)); |
|
| 182 | - } |
|
| 183 | - if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
| 184 | - continue; |
|
| 185 | - } |
|
| 186 | - // Note: At this point `$sc->getDomain()` being a public suffix should |
|
| 187 | - // be rejected, but we don't want to pull in the full PSL dependency. |
|
| 188 | - $this->setCookie($sc); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - /** |
|
| 193 | - * Computes cookie path following RFC 6265 section 5.1.4 |
|
| 194 | - * |
|
| 195 | - * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
| 196 | - */ |
|
| 197 | - private function getCookiePathFromRequest(RequestInterface $request) : string |
|
| 198 | - { |
|
| 199 | - $uriPath = $request->getUri()->getPath(); |
|
| 200 | - if ('' === $uriPath) { |
|
| 201 | - return '/'; |
|
| 202 | - } |
|
| 203 | - if (0 !== \strpos($uriPath, '/')) { |
|
| 204 | - return '/'; |
|
| 205 | - } |
|
| 206 | - if ('/' === $uriPath) { |
|
| 207 | - return '/'; |
|
| 208 | - } |
|
| 209 | - $lastSlashPos = \strrpos($uriPath, '/'); |
|
| 210 | - if (0 === $lastSlashPos || \false === $lastSlashPos) { |
|
| 211 | - return '/'; |
|
| 212 | - } |
|
| 213 | - return \substr($uriPath, 0, $lastSlashPos); |
|
| 214 | - } |
|
| 215 | - public function withCookieHeader(RequestInterface $request) : RequestInterface |
|
| 216 | - { |
|
| 217 | - $values = []; |
|
| 218 | - $uri = $request->getUri(); |
|
| 219 | - $scheme = $uri->getScheme(); |
|
| 220 | - $host = $uri->getHost(); |
|
| 221 | - $path = $uri->getPath() ?: '/'; |
|
| 222 | - foreach ($this->cookies as $cookie) { |
|
| 223 | - if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) { |
|
| 224 | - $values[] = $cookie->getName() . '=' . $cookie->getValue(); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request; |
|
| 228 | - } |
|
| 229 | - /** |
|
| 230 | - * If a cookie already exists and the server asks to set it again with a |
|
| 231 | - * null value, the cookie must be deleted. |
|
| 232 | - */ |
|
| 233 | - private function removeCookieIfEmpty(SetCookie $cookie) : void |
|
| 234 | - { |
|
| 235 | - $cookieValue = $cookie->getValue(); |
|
| 236 | - if ($cookieValue === null || $cookieValue === '') { |
|
| 237 | - $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName()); |
|
| 238 | - } |
|
| 239 | - } |
|
| 12 | + /** |
|
| 13 | + * @var SetCookie[] Loaded cookie data |
|
| 14 | + */ |
|
| 15 | + private $cookies = []; |
|
| 16 | + /** |
|
| 17 | + * @var bool |
|
| 18 | + */ |
|
| 19 | + private $strictMode; |
|
| 20 | + /** |
|
| 21 | + * @param bool $strictMode Set to true to throw exceptions when invalid |
|
| 22 | + * cookies are added to the cookie jar. |
|
| 23 | + * @param array $cookieArray Array of SetCookie objects or a hash of |
|
| 24 | + * arrays that can be used with the SetCookie |
|
| 25 | + * constructor |
|
| 26 | + */ |
|
| 27 | + public function __construct(bool $strictMode = \false, array $cookieArray = []) |
|
| 28 | + { |
|
| 29 | + $this->strictMode = $strictMode; |
|
| 30 | + foreach ($cookieArray as $cookie) { |
|
| 31 | + if (!$cookie instanceof SetCookie) { |
|
| 32 | + $cookie = new SetCookie($cookie); |
|
| 33 | + } |
|
| 34 | + $this->setCookie($cookie); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + /** |
|
| 38 | + * Create a new Cookie jar from an associative array and domain. |
|
| 39 | + * |
|
| 40 | + * @param array $cookies Cookies to create the jar from |
|
| 41 | + * @param string $domain Domain to set the cookies to |
|
| 42 | + */ |
|
| 43 | + public static function fromArray(array $cookies, string $domain) : self |
|
| 44 | + { |
|
| 45 | + $cookieJar = new self(); |
|
| 46 | + foreach ($cookies as $name => $value) { |
|
| 47 | + $cookieJar->setCookie(new SetCookie(['Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => \true])); |
|
| 48 | + } |
|
| 49 | + return $cookieJar; |
|
| 50 | + } |
|
| 51 | + /** |
|
| 52 | + * Evaluate if this cookie should be persisted to storage |
|
| 53 | + * that survives between requests. |
|
| 54 | + * |
|
| 55 | + * @param SetCookie $cookie Being evaluated. |
|
| 56 | + * @param bool $allowSessionCookies If we should persist session cookies |
|
| 57 | + */ |
|
| 58 | + public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = \false) : bool |
|
| 59 | + { |
|
| 60 | + if ($cookie->getExpires() || $allowSessionCookies) { |
|
| 61 | + if (!$cookie->getDiscard()) { |
|
| 62 | + return \true; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + return \false; |
|
| 66 | + } |
|
| 67 | + /** |
|
| 68 | + * Finds and returns the cookie based on the name |
|
| 69 | + * |
|
| 70 | + * @param string $name cookie name to search for |
|
| 71 | + * |
|
| 72 | + * @return SetCookie|null cookie that was found or null if not found |
|
| 73 | + */ |
|
| 74 | + public function getCookieByName(string $name) : ?SetCookie |
|
| 75 | + { |
|
| 76 | + foreach ($this->cookies as $cookie) { |
|
| 77 | + if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
| 78 | + return $cookie; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + return null; |
|
| 82 | + } |
|
| 83 | + public function toArray() : array |
|
| 84 | + { |
|
| 85 | + return \array_map(static function (SetCookie $cookie) : array { |
|
| 86 | + return $cookie->toArray(); |
|
| 87 | + }, $this->getIterator()->getArrayCopy()); |
|
| 88 | + } |
|
| 89 | + public function clear(?string $domain = null, ?string $path = null, ?string $name = null) : void |
|
| 90 | + { |
|
| 91 | + if (!$domain) { |
|
| 92 | + $this->cookies = []; |
|
| 93 | + return; |
|
| 94 | + } elseif (!$path) { |
|
| 95 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($domain) : bool { |
|
| 96 | + return !$cookie->matchesDomain($domain); |
|
| 97 | + }); |
|
| 98 | + } elseif (!$name) { |
|
| 99 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain) : bool { |
|
| 100 | + return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 101 | + }); |
|
| 102 | + } else { |
|
| 103 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) use($path, $domain, $name) { |
|
| 104 | + return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); |
|
| 105 | + }); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + public function clearSessionCookies() : void |
|
| 109 | + { |
|
| 110 | + $this->cookies = \array_filter($this->cookies, static function (SetCookie $cookie) : bool { |
|
| 111 | + return !$cookie->getDiscard() && $cookie->getExpires(); |
|
| 112 | + }); |
|
| 113 | + } |
|
| 114 | + public function setCookie(SetCookie $cookie) : bool |
|
| 115 | + { |
|
| 116 | + // If the name string is empty (but not 0), ignore the set-cookie |
|
| 117 | + // string entirely. |
|
| 118 | + $name = $cookie->getName(); |
|
| 119 | + if (!$name && $name !== '0') { |
|
| 120 | + return \false; |
|
| 121 | + } |
|
| 122 | + // Only allow cookies with set and valid domain, name, value |
|
| 123 | + $result = $cookie->validate(); |
|
| 124 | + if ($result !== \true) { |
|
| 125 | + if ($this->strictMode) { |
|
| 126 | + throw new \RuntimeException('Invalid cookie: ' . $result); |
|
| 127 | + } |
|
| 128 | + $this->removeCookieIfEmpty($cookie); |
|
| 129 | + return \false; |
|
| 130 | + } |
|
| 131 | + // Resolve conflicts with previously set cookies |
|
| 132 | + foreach ($this->cookies as $i => $c) { |
|
| 133 | + // Two cookies are identical, when their path, and domain are |
|
| 134 | + // identical. |
|
| 135 | + if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName()) { |
|
| 136 | + continue; |
|
| 137 | + } |
|
| 138 | + // The previously set cookie is a discard cookie and this one is |
|
| 139 | + // not so allow the new cookie to be set |
|
| 140 | + if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
| 141 | + unset($this->cookies[$i]); |
|
| 142 | + continue; |
|
| 143 | + } |
|
| 144 | + // If the new cookie's expiration is further into the future, then |
|
| 145 | + // replace the old cookie |
|
| 146 | + if ($cookie->getExpires() > $c->getExpires()) { |
|
| 147 | + unset($this->cookies[$i]); |
|
| 148 | + continue; |
|
| 149 | + } |
|
| 150 | + // If the value has changed, we better change it |
|
| 151 | + if ($cookie->getValue() !== $c->getValue()) { |
|
| 152 | + unset($this->cookies[$i]); |
|
| 153 | + continue; |
|
| 154 | + } |
|
| 155 | + // The cookie exists, so no need to continue |
|
| 156 | + return \false; |
|
| 157 | + } |
|
| 158 | + $this->cookies[] = $cookie; |
|
| 159 | + return \true; |
|
| 160 | + } |
|
| 161 | + public function count() : int |
|
| 162 | + { |
|
| 163 | + return \count($this->cookies); |
|
| 164 | + } |
|
| 165 | + /** |
|
| 166 | + * @return \ArrayIterator<int, SetCookie> |
|
| 167 | + */ |
|
| 168 | + public function getIterator() : \ArrayIterator |
|
| 169 | + { |
|
| 170 | + return new \ArrayIterator(\array_values($this->cookies)); |
|
| 171 | + } |
|
| 172 | + public function extractCookies(RequestInterface $request, ResponseInterface $response) : void |
|
| 173 | + { |
|
| 174 | + if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
| 175 | + foreach ($cookieHeader as $cookie) { |
|
| 176 | + $sc = SetCookie::fromString($cookie); |
|
| 177 | + if (!$sc->getDomain()) { |
|
| 178 | + $sc->setDomain($request->getUri()->getHost()); |
|
| 179 | + } |
|
| 180 | + if (0 !== \strpos($sc->getPath(), '/')) { |
|
| 181 | + $sc->setPath($this->getCookiePathFromRequest($request)); |
|
| 182 | + } |
|
| 183 | + if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
| 184 | + continue; |
|
| 185 | + } |
|
| 186 | + // Note: At this point `$sc->getDomain()` being a public suffix should |
|
| 187 | + // be rejected, but we don't want to pull in the full PSL dependency. |
|
| 188 | + $this->setCookie($sc); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + /** |
|
| 193 | + * Computes cookie path following RFC 6265 section 5.1.4 |
|
| 194 | + * |
|
| 195 | + * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
| 196 | + */ |
|
| 197 | + private function getCookiePathFromRequest(RequestInterface $request) : string |
|
| 198 | + { |
|
| 199 | + $uriPath = $request->getUri()->getPath(); |
|
| 200 | + if ('' === $uriPath) { |
|
| 201 | + return '/'; |
|
| 202 | + } |
|
| 203 | + if (0 !== \strpos($uriPath, '/')) { |
|
| 204 | + return '/'; |
|
| 205 | + } |
|
| 206 | + if ('/' === $uriPath) { |
|
| 207 | + return '/'; |
|
| 208 | + } |
|
| 209 | + $lastSlashPos = \strrpos($uriPath, '/'); |
|
| 210 | + if (0 === $lastSlashPos || \false === $lastSlashPos) { |
|
| 211 | + return '/'; |
|
| 212 | + } |
|
| 213 | + return \substr($uriPath, 0, $lastSlashPos); |
|
| 214 | + } |
|
| 215 | + public function withCookieHeader(RequestInterface $request) : RequestInterface |
|
| 216 | + { |
|
| 217 | + $values = []; |
|
| 218 | + $uri = $request->getUri(); |
|
| 219 | + $scheme = $uri->getScheme(); |
|
| 220 | + $host = $uri->getHost(); |
|
| 221 | + $path = $uri->getPath() ?: '/'; |
|
| 222 | + foreach ($this->cookies as $cookie) { |
|
| 223 | + if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) { |
|
| 224 | + $values[] = $cookie->getName() . '=' . $cookie->getValue(); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request; |
|
| 228 | + } |
|
| 229 | + /** |
|
| 230 | + * If a cookie already exists and the server asks to set it again with a |
|
| 231 | + * null value, the cookie must be deleted. |
|
| 232 | + */ |
|
| 233 | + private function removeCookieIfEmpty(SetCookie $cookie) : void |
|
| 234 | + { |
|
| 235 | + $cookieValue = $cookie->getValue(); |
|
| 236 | + if ($cookieValue === null || $cookieValue === '') { |
|
| 237 | + $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName()); |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | 240 | } |
@@ -10,147 +10,147 @@ discard block |
||
| 10 | 10 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\UriInterface; |
| 11 | 11 | final class Utils |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Debug function used to describe the provided value type and class. |
|
| 15 | - * |
|
| 16 | - * @param mixed $input |
|
| 17 | - * |
|
| 18 | - * @return string Returns a string containing the type of the variable and |
|
| 19 | - * if a class is provided, the class name. |
|
| 20 | - */ |
|
| 21 | - public static function describeType($input) : string |
|
| 22 | - { |
|
| 23 | - switch (\gettype($input)) { |
|
| 24 | - case 'object': |
|
| 25 | - return 'object(' . \get_class($input) . ')'; |
|
| 26 | - case 'array': |
|
| 27 | - return 'array(' . \count($input) . ')'; |
|
| 28 | - default: |
|
| 29 | - \ob_start(); |
|
| 30 | - \var_dump($input); |
|
| 31 | - // normalize float vs double |
|
| 32 | - /** @var string $varDumpContent */ |
|
| 33 | - $varDumpContent = \ob_get_clean(); |
|
| 34 | - return \str_replace('double(', 'float(', \rtrim($varDumpContent)); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - /** |
|
| 38 | - * Parses an array of header lines into an associative array of headers. |
|
| 39 | - * |
|
| 40 | - * @param iterable $lines Header lines array of strings in the following |
|
| 41 | - * format: "Name: Value" |
|
| 42 | - */ |
|
| 43 | - public static function headersFromLines(iterable $lines) : array |
|
| 44 | - { |
|
| 45 | - $headers = []; |
|
| 46 | - foreach ($lines as $line) { |
|
| 47 | - $parts = \explode(':', $line, 2); |
|
| 48 | - $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; |
|
| 49 | - } |
|
| 50 | - return $headers; |
|
| 51 | - } |
|
| 52 | - /** |
|
| 53 | - * Returns a debug stream based on the provided variable. |
|
| 54 | - * |
|
| 55 | - * @param mixed $value Optional value |
|
| 56 | - * |
|
| 57 | - * @return resource |
|
| 58 | - */ |
|
| 59 | - public static function debugResource($value = null) |
|
| 60 | - { |
|
| 61 | - if (\is_resource($value)) { |
|
| 62 | - return $value; |
|
| 63 | - } |
|
| 64 | - if (\defined('STDOUT')) { |
|
| 65 | - return \STDOUT; |
|
| 66 | - } |
|
| 67 | - return Psr7\Utils::tryFopen('php://output', 'w'); |
|
| 68 | - } |
|
| 69 | - /** |
|
| 70 | - * Chooses and creates a default handler to use based on the environment. |
|
| 71 | - * |
|
| 72 | - * The returned handler is not wrapped by any default middlewares. |
|
| 73 | - * |
|
| 74 | - * @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the best handler for the given system. |
|
| 75 | - * |
|
| 76 | - * @throws \RuntimeException if no viable Handler is available. |
|
| 77 | - */ |
|
| 78 | - public static function chooseHandler() : callable |
|
| 79 | - { |
|
| 80 | - $handler = null; |
|
| 81 | - if (\defined('CURLOPT_CUSTOMREQUEST') && \function_exists('curl_version') && \version_compare(\curl_version()['version'], '7.21.2') >= 0) { |
|
| 82 | - if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { |
|
| 83 | - $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); |
|
| 84 | - } elseif (\function_exists('curl_exec')) { |
|
| 85 | - $handler = new CurlHandler(); |
|
| 86 | - } elseif (\function_exists('curl_multi_exec')) { |
|
| 87 | - $handler = new CurlMultiHandler(); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - if (\ini_get('allow_url_fopen')) { |
|
| 91 | - $handler = $handler ? Proxy::wrapStreaming($handler, new StreamHandler()) : new StreamHandler(); |
|
| 92 | - } elseif (!$handler) { |
|
| 93 | - throw new \RuntimeException('GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); |
|
| 94 | - } |
|
| 95 | - return $handler; |
|
| 96 | - } |
|
| 97 | - /** |
|
| 98 | - * Get the default User-Agent string to use with Guzzle. |
|
| 99 | - */ |
|
| 100 | - public static function defaultUserAgent() : string |
|
| 101 | - { |
|
| 102 | - return \sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION); |
|
| 103 | - } |
|
| 104 | - /** |
|
| 105 | - * Returns the default cacert bundle for the current system. |
|
| 106 | - * |
|
| 107 | - * First, the openssl.cafile and curl.cainfo php.ini settings are checked. |
|
| 108 | - * If those settings are not configured, then the common locations for |
|
| 109 | - * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X |
|
| 110 | - * and Windows are checked. If any of these file locations are found on |
|
| 111 | - * disk, they will be utilized. |
|
| 112 | - * |
|
| 113 | - * Note: the result of this function is cached for subsequent calls. |
|
| 114 | - * |
|
| 115 | - * @throws \RuntimeException if no bundle can be found. |
|
| 116 | - * |
|
| 117 | - * @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+. |
|
| 118 | - */ |
|
| 119 | - public static function defaultCaBundle() : string |
|
| 120 | - { |
|
| 121 | - static $cached = null; |
|
| 122 | - static $cafiles = [ |
|
| 123 | - // Red Hat, CentOS, Fedora (provided by the ca-certificates package) |
|
| 124 | - '/etc/pki/tls/certs/ca-bundle.crt', |
|
| 125 | - // Ubuntu, Debian (provided by the ca-certificates package) |
|
| 126 | - '/etc/ssl/certs/ca-certificates.crt', |
|
| 127 | - // FreeBSD (provided by the ca_root_nss package) |
|
| 128 | - '/usr/local/share/certs/ca-root-nss.crt', |
|
| 129 | - // SLES 12 (provided by the ca-certificates package) |
|
| 130 | - '/var/lib/ca-certificates/ca-bundle.pem', |
|
| 131 | - // OS X provided by homebrew (using the default path) |
|
| 132 | - '/usr/local/etc/openssl/cert.pem', |
|
| 133 | - // Google app engine |
|
| 134 | - '/etc/ca-certificates.crt', |
|
| 135 | - // Windows? |
|
| 136 | - 'C:\\windows\\system32\\curl-ca-bundle.crt', |
|
| 137 | - 'C:\\windows\\curl-ca-bundle.crt', |
|
| 138 | - ]; |
|
| 139 | - if ($cached) { |
|
| 140 | - return $cached; |
|
| 141 | - } |
|
| 142 | - if ($ca = \ini_get('openssl.cafile')) { |
|
| 143 | - return $cached = $ca; |
|
| 144 | - } |
|
| 145 | - if ($ca = \ini_get('curl.cainfo')) { |
|
| 146 | - return $cached = $ca; |
|
| 147 | - } |
|
| 148 | - foreach ($cafiles as $filename) { |
|
| 149 | - if (\file_exists($filename)) { |
|
| 150 | - return $cached = $filename; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - throw new \RuntimeException(<<<EOT |
|
| 13 | + /** |
|
| 14 | + * Debug function used to describe the provided value type and class. |
|
| 15 | + * |
|
| 16 | + * @param mixed $input |
|
| 17 | + * |
|
| 18 | + * @return string Returns a string containing the type of the variable and |
|
| 19 | + * if a class is provided, the class name. |
|
| 20 | + */ |
|
| 21 | + public static function describeType($input) : string |
|
| 22 | + { |
|
| 23 | + switch (\gettype($input)) { |
|
| 24 | + case 'object': |
|
| 25 | + return 'object(' . \get_class($input) . ')'; |
|
| 26 | + case 'array': |
|
| 27 | + return 'array(' . \count($input) . ')'; |
|
| 28 | + default: |
|
| 29 | + \ob_start(); |
|
| 30 | + \var_dump($input); |
|
| 31 | + // normalize float vs double |
|
| 32 | + /** @var string $varDumpContent */ |
|
| 33 | + $varDumpContent = \ob_get_clean(); |
|
| 34 | + return \str_replace('double(', 'float(', \rtrim($varDumpContent)); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + /** |
|
| 38 | + * Parses an array of header lines into an associative array of headers. |
|
| 39 | + * |
|
| 40 | + * @param iterable $lines Header lines array of strings in the following |
|
| 41 | + * format: "Name: Value" |
|
| 42 | + */ |
|
| 43 | + public static function headersFromLines(iterable $lines) : array |
|
| 44 | + { |
|
| 45 | + $headers = []; |
|
| 46 | + foreach ($lines as $line) { |
|
| 47 | + $parts = \explode(':', $line, 2); |
|
| 48 | + $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; |
|
| 49 | + } |
|
| 50 | + return $headers; |
|
| 51 | + } |
|
| 52 | + /** |
|
| 53 | + * Returns a debug stream based on the provided variable. |
|
| 54 | + * |
|
| 55 | + * @param mixed $value Optional value |
|
| 56 | + * |
|
| 57 | + * @return resource |
|
| 58 | + */ |
|
| 59 | + public static function debugResource($value = null) |
|
| 60 | + { |
|
| 61 | + if (\is_resource($value)) { |
|
| 62 | + return $value; |
|
| 63 | + } |
|
| 64 | + if (\defined('STDOUT')) { |
|
| 65 | + return \STDOUT; |
|
| 66 | + } |
|
| 67 | + return Psr7\Utils::tryFopen('php://output', 'w'); |
|
| 68 | + } |
|
| 69 | + /** |
|
| 70 | + * Chooses and creates a default handler to use based on the environment. |
|
| 71 | + * |
|
| 72 | + * The returned handler is not wrapped by any default middlewares. |
|
| 73 | + * |
|
| 74 | + * @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the best handler for the given system. |
|
| 75 | + * |
|
| 76 | + * @throws \RuntimeException if no viable Handler is available. |
|
| 77 | + */ |
|
| 78 | + public static function chooseHandler() : callable |
|
| 79 | + { |
|
| 80 | + $handler = null; |
|
| 81 | + if (\defined('CURLOPT_CUSTOMREQUEST') && \function_exists('curl_version') && \version_compare(\curl_version()['version'], '7.21.2') >= 0) { |
|
| 82 | + if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { |
|
| 83 | + $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); |
|
| 84 | + } elseif (\function_exists('curl_exec')) { |
|
| 85 | + $handler = new CurlHandler(); |
|
| 86 | + } elseif (\function_exists('curl_multi_exec')) { |
|
| 87 | + $handler = new CurlMultiHandler(); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + if (\ini_get('allow_url_fopen')) { |
|
| 91 | + $handler = $handler ? Proxy::wrapStreaming($handler, new StreamHandler()) : new StreamHandler(); |
|
| 92 | + } elseif (!$handler) { |
|
| 93 | + throw new \RuntimeException('GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); |
|
| 94 | + } |
|
| 95 | + return $handler; |
|
| 96 | + } |
|
| 97 | + /** |
|
| 98 | + * Get the default User-Agent string to use with Guzzle. |
|
| 99 | + */ |
|
| 100 | + public static function defaultUserAgent() : string |
|
| 101 | + { |
|
| 102 | + return \sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION); |
|
| 103 | + } |
|
| 104 | + /** |
|
| 105 | + * Returns the default cacert bundle for the current system. |
|
| 106 | + * |
|
| 107 | + * First, the openssl.cafile and curl.cainfo php.ini settings are checked. |
|
| 108 | + * If those settings are not configured, then the common locations for |
|
| 109 | + * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X |
|
| 110 | + * and Windows are checked. If any of these file locations are found on |
|
| 111 | + * disk, they will be utilized. |
|
| 112 | + * |
|
| 113 | + * Note: the result of this function is cached for subsequent calls. |
|
| 114 | + * |
|
| 115 | + * @throws \RuntimeException if no bundle can be found. |
|
| 116 | + * |
|
| 117 | + * @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+. |
|
| 118 | + */ |
|
| 119 | + public static function defaultCaBundle() : string |
|
| 120 | + { |
|
| 121 | + static $cached = null; |
|
| 122 | + static $cafiles = [ |
|
| 123 | + // Red Hat, CentOS, Fedora (provided by the ca-certificates package) |
|
| 124 | + '/etc/pki/tls/certs/ca-bundle.crt', |
|
| 125 | + // Ubuntu, Debian (provided by the ca-certificates package) |
|
| 126 | + '/etc/ssl/certs/ca-certificates.crt', |
|
| 127 | + // FreeBSD (provided by the ca_root_nss package) |
|
| 128 | + '/usr/local/share/certs/ca-root-nss.crt', |
|
| 129 | + // SLES 12 (provided by the ca-certificates package) |
|
| 130 | + '/var/lib/ca-certificates/ca-bundle.pem', |
|
| 131 | + // OS X provided by homebrew (using the default path) |
|
| 132 | + '/usr/local/etc/openssl/cert.pem', |
|
| 133 | + // Google app engine |
|
| 134 | + '/etc/ca-certificates.crt', |
|
| 135 | + // Windows? |
|
| 136 | + 'C:\\windows\\system32\\curl-ca-bundle.crt', |
|
| 137 | + 'C:\\windows\\curl-ca-bundle.crt', |
|
| 138 | + ]; |
|
| 139 | + if ($cached) { |
|
| 140 | + return $cached; |
|
| 141 | + } |
|
| 142 | + if ($ca = \ini_get('openssl.cafile')) { |
|
| 143 | + return $cached = $ca; |
|
| 144 | + } |
|
| 145 | + if ($ca = \ini_get('curl.cainfo')) { |
|
| 146 | + return $cached = $ca; |
|
| 147 | + } |
|
| 148 | + foreach ($cafiles as $filename) { |
|
| 149 | + if (\file_exists($filename)) { |
|
| 150 | + return $cached = $filename; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + throw new \RuntimeException(<<<EOT |
|
| 154 | 154 | No system CA bundle could be found in any of the the common system locations. |
| 155 | 155 | PHP versions earlier than 5.6 are not properly configured to use the system's |
| 156 | 156 | CA bundle by default. In order to verify peer certificates, you will need to |
@@ -164,176 +164,176 @@ discard block |
||
| 164 | 164 | https://curl.haxx.se/docs/sslcerts.html for more information. |
| 165 | 165 | EOT |
| 166 | 166 | ); |
| 167 | - } |
|
| 168 | - /** |
|
| 169 | - * Creates an associative array of lowercase header names to the actual |
|
| 170 | - * header casing. |
|
| 171 | - */ |
|
| 172 | - public static function normalizeHeaderKeys(array $headers) : array |
|
| 173 | - { |
|
| 174 | - $result = []; |
|
| 175 | - foreach (\array_keys($headers) as $key) { |
|
| 176 | - $result[\strtolower($key)] = $key; |
|
| 177 | - } |
|
| 178 | - return $result; |
|
| 179 | - } |
|
| 180 | - /** |
|
| 181 | - * Returns true if the provided host matches any of the no proxy areas. |
|
| 182 | - * |
|
| 183 | - * This method will strip a port from the host if it is present. Each pattern |
|
| 184 | - * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a |
|
| 185 | - * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == |
|
| 186 | - * "baz.foo.com", but ".foo.com" != "foo.com"). |
|
| 187 | - * |
|
| 188 | - * Areas are matched in the following cases: |
|
| 189 | - * 1. "*" (without quotes) always matches any hosts. |
|
| 190 | - * 2. An exact match. |
|
| 191 | - * 3. The area starts with "." and the area is the last part of the host. e.g. |
|
| 192 | - * '.mit.edu' will match any host that ends with '.mit.edu'. |
|
| 193 | - * |
|
| 194 | - * @param string $host Host to check against the patterns. |
|
| 195 | - * @param string[] $noProxyArray An array of host patterns. |
|
| 196 | - * |
|
| 197 | - * @throws InvalidArgumentException |
|
| 198 | - */ |
|
| 199 | - public static function isHostInNoProxy(string $host, array $noProxyArray) : bool |
|
| 200 | - { |
|
| 201 | - if (\strlen($host) === 0) { |
|
| 202 | - throw new InvalidArgumentException('Empty host provided'); |
|
| 203 | - } |
|
| 204 | - // Strip port if present. |
|
| 205 | - [$host] = \explode(':', $host, 2); |
|
| 206 | - foreach ($noProxyArray as $area) { |
|
| 207 | - // Always match on wildcards. |
|
| 208 | - if ($area === '*') { |
|
| 209 | - return \true; |
|
| 210 | - } |
|
| 211 | - if (empty($area)) { |
|
| 212 | - // Don't match on empty values. |
|
| 213 | - continue; |
|
| 214 | - } |
|
| 215 | - if ($area === $host) { |
|
| 216 | - // Exact matches. |
|
| 217 | - return \true; |
|
| 218 | - } |
|
| 219 | - // Special match if the area when prefixed with ".". Remove any |
|
| 220 | - // existing leading "." and add a new leading ".". |
|
| 221 | - $area = '.' . \ltrim($area, '.'); |
|
| 222 | - if (\substr($host, -\strlen($area)) === $area) { |
|
| 223 | - return \true; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - return \false; |
|
| 227 | - } |
|
| 228 | - /** |
|
| 229 | - * Wrapper for json_decode that throws when an error occurs. |
|
| 230 | - * |
|
| 231 | - * @param string $json JSON data to parse |
|
| 232 | - * @param bool $assoc When true, returned objects will be converted |
|
| 233 | - * into associative arrays. |
|
| 234 | - * @param int $depth User specified recursion depth. |
|
| 235 | - * @param int $options Bitmask of JSON decode options. |
|
| 236 | - * |
|
| 237 | - * @return object|array|string|int|float|bool|null |
|
| 238 | - * |
|
| 239 | - * @throws InvalidArgumentException if the JSON cannot be decoded. |
|
| 240 | - * |
|
| 241 | - * @see https://www.php.net/manual/en/function.json-decode.php |
|
| 242 | - */ |
|
| 243 | - public static function jsonDecode(string $json, bool $assoc = \false, int $depth = 512, int $options = 0) |
|
| 244 | - { |
|
| 245 | - $data = \json_decode($json, $assoc, $depth, $options); |
|
| 246 | - if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 247 | - throw new InvalidArgumentException('json_decode error: ' . \json_last_error_msg()); |
|
| 248 | - } |
|
| 249 | - return $data; |
|
| 250 | - } |
|
| 251 | - /** |
|
| 252 | - * Wrapper for JSON encoding that throws when an error occurs. |
|
| 253 | - * |
|
| 254 | - * @param mixed $value The value being encoded |
|
| 255 | - * @param int $options JSON encode option bitmask |
|
| 256 | - * @param int $depth Set the maximum depth. Must be greater than zero. |
|
| 257 | - * |
|
| 258 | - * @throws InvalidArgumentException if the JSON cannot be encoded. |
|
| 259 | - * |
|
| 260 | - * @see https://www.php.net/manual/en/function.json-encode.php |
|
| 261 | - */ |
|
| 262 | - public static function jsonEncode($value, int $options = 0, int $depth = 512) : string |
|
| 263 | - { |
|
| 264 | - $json = \json_encode($value, $options, $depth); |
|
| 265 | - if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 266 | - throw new InvalidArgumentException('json_encode error: ' . \json_last_error_msg()); |
|
| 267 | - } |
|
| 268 | - /** @var string */ |
|
| 269 | - return $json; |
|
| 270 | - } |
|
| 271 | - /** |
|
| 272 | - * Wrapper for the hrtime() or microtime() functions |
|
| 273 | - * (depending on the PHP version, one of the two is used) |
|
| 274 | - * |
|
| 275 | - * @return float UNIX timestamp |
|
| 276 | - * |
|
| 277 | - * @internal |
|
| 278 | - */ |
|
| 279 | - public static function currentTime() : float |
|
| 280 | - { |
|
| 281 | - return (float) \function_exists('hrtime') ? \hrtime(\true) / 1000000000.0 : \microtime(\true); |
|
| 282 | - } |
|
| 283 | - /** |
|
| 284 | - * @throws InvalidArgumentException |
|
| 285 | - * |
|
| 286 | - * @internal |
|
| 287 | - */ |
|
| 288 | - public static function idnUriConvert(UriInterface $uri, int $options = 0) : UriInterface |
|
| 289 | - { |
|
| 290 | - if ($uri->getHost()) { |
|
| 291 | - $asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
|
| 292 | - if ($asciiHost === \false) { |
|
| 293 | - $errorBitSet = $info['errors'] ?? 0; |
|
| 294 | - $errorConstants = \array_filter(\array_keys(\get_defined_constants()), static function (string $name) : bool { |
|
| 295 | - return \substr($name, 0, 11) === 'IDNA_ERROR_'; |
|
| 296 | - }); |
|
| 297 | - $errors = []; |
|
| 298 | - foreach ($errorConstants as $errorConstant) { |
|
| 299 | - if ($errorBitSet & \constant($errorConstant)) { |
|
| 300 | - $errors[] = $errorConstant; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - $errorMessage = 'IDN conversion failed'; |
|
| 304 | - if ($errors) { |
|
| 305 | - $errorMessage .= ' (errors: ' . \implode(', ', $errors) . ')'; |
|
| 306 | - } |
|
| 307 | - throw new InvalidArgumentException($errorMessage); |
|
| 308 | - } |
|
| 309 | - if ($uri->getHost() !== $asciiHost) { |
|
| 310 | - // Replace URI only if the ASCII version is different |
|
| 311 | - $uri = $uri->withHost($asciiHost); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - return $uri; |
|
| 315 | - } |
|
| 316 | - /** |
|
| 317 | - * @internal |
|
| 318 | - */ |
|
| 319 | - public static function getenv(string $name) : ?string |
|
| 320 | - { |
|
| 321 | - if (isset($_SERVER[$name])) { |
|
| 322 | - return (string) $_SERVER[$name]; |
|
| 323 | - } |
|
| 324 | - if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== \false && $value !== null) { |
|
| 325 | - return (string) $value; |
|
| 326 | - } |
|
| 327 | - return null; |
|
| 328 | - } |
|
| 329 | - /** |
|
| 330 | - * @return string|false |
|
| 331 | - */ |
|
| 332 | - private static function idnToAsci(string $domain, int $options, ?array &$info = []) |
|
| 333 | - { |
|
| 334 | - if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 335 | - return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
|
| 336 | - } |
|
| 337 | - throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old'); |
|
| 338 | - } |
|
| 167 | + } |
|
| 168 | + /** |
|
| 169 | + * Creates an associative array of lowercase header names to the actual |
|
| 170 | + * header casing. |
|
| 171 | + */ |
|
| 172 | + public static function normalizeHeaderKeys(array $headers) : array |
|
| 173 | + { |
|
| 174 | + $result = []; |
|
| 175 | + foreach (\array_keys($headers) as $key) { |
|
| 176 | + $result[\strtolower($key)] = $key; |
|
| 177 | + } |
|
| 178 | + return $result; |
|
| 179 | + } |
|
| 180 | + /** |
|
| 181 | + * Returns true if the provided host matches any of the no proxy areas. |
|
| 182 | + * |
|
| 183 | + * This method will strip a port from the host if it is present. Each pattern |
|
| 184 | + * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a |
|
| 185 | + * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == |
|
| 186 | + * "baz.foo.com", but ".foo.com" != "foo.com"). |
|
| 187 | + * |
|
| 188 | + * Areas are matched in the following cases: |
|
| 189 | + * 1. "*" (without quotes) always matches any hosts. |
|
| 190 | + * 2. An exact match. |
|
| 191 | + * 3. The area starts with "." and the area is the last part of the host. e.g. |
|
| 192 | + * '.mit.edu' will match any host that ends with '.mit.edu'. |
|
| 193 | + * |
|
| 194 | + * @param string $host Host to check against the patterns. |
|
| 195 | + * @param string[] $noProxyArray An array of host patterns. |
|
| 196 | + * |
|
| 197 | + * @throws InvalidArgumentException |
|
| 198 | + */ |
|
| 199 | + public static function isHostInNoProxy(string $host, array $noProxyArray) : bool |
|
| 200 | + { |
|
| 201 | + if (\strlen($host) === 0) { |
|
| 202 | + throw new InvalidArgumentException('Empty host provided'); |
|
| 203 | + } |
|
| 204 | + // Strip port if present. |
|
| 205 | + [$host] = \explode(':', $host, 2); |
|
| 206 | + foreach ($noProxyArray as $area) { |
|
| 207 | + // Always match on wildcards. |
|
| 208 | + if ($area === '*') { |
|
| 209 | + return \true; |
|
| 210 | + } |
|
| 211 | + if (empty($area)) { |
|
| 212 | + // Don't match on empty values. |
|
| 213 | + continue; |
|
| 214 | + } |
|
| 215 | + if ($area === $host) { |
|
| 216 | + // Exact matches. |
|
| 217 | + return \true; |
|
| 218 | + } |
|
| 219 | + // Special match if the area when prefixed with ".". Remove any |
|
| 220 | + // existing leading "." and add a new leading ".". |
|
| 221 | + $area = '.' . \ltrim($area, '.'); |
|
| 222 | + if (\substr($host, -\strlen($area)) === $area) { |
|
| 223 | + return \true; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + return \false; |
|
| 227 | + } |
|
| 228 | + /** |
|
| 229 | + * Wrapper for json_decode that throws when an error occurs. |
|
| 230 | + * |
|
| 231 | + * @param string $json JSON data to parse |
|
| 232 | + * @param bool $assoc When true, returned objects will be converted |
|
| 233 | + * into associative arrays. |
|
| 234 | + * @param int $depth User specified recursion depth. |
|
| 235 | + * @param int $options Bitmask of JSON decode options. |
|
| 236 | + * |
|
| 237 | + * @return object|array|string|int|float|bool|null |
|
| 238 | + * |
|
| 239 | + * @throws InvalidArgumentException if the JSON cannot be decoded. |
|
| 240 | + * |
|
| 241 | + * @see https://www.php.net/manual/en/function.json-decode.php |
|
| 242 | + */ |
|
| 243 | + public static function jsonDecode(string $json, bool $assoc = \false, int $depth = 512, int $options = 0) |
|
| 244 | + { |
|
| 245 | + $data = \json_decode($json, $assoc, $depth, $options); |
|
| 246 | + if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 247 | + throw new InvalidArgumentException('json_decode error: ' . \json_last_error_msg()); |
|
| 248 | + } |
|
| 249 | + return $data; |
|
| 250 | + } |
|
| 251 | + /** |
|
| 252 | + * Wrapper for JSON encoding that throws when an error occurs. |
|
| 253 | + * |
|
| 254 | + * @param mixed $value The value being encoded |
|
| 255 | + * @param int $options JSON encode option bitmask |
|
| 256 | + * @param int $depth Set the maximum depth. Must be greater than zero. |
|
| 257 | + * |
|
| 258 | + * @throws InvalidArgumentException if the JSON cannot be encoded. |
|
| 259 | + * |
|
| 260 | + * @see https://www.php.net/manual/en/function.json-encode.php |
|
| 261 | + */ |
|
| 262 | + public static function jsonEncode($value, int $options = 0, int $depth = 512) : string |
|
| 263 | + { |
|
| 264 | + $json = \json_encode($value, $options, $depth); |
|
| 265 | + if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 266 | + throw new InvalidArgumentException('json_encode error: ' . \json_last_error_msg()); |
|
| 267 | + } |
|
| 268 | + /** @var string */ |
|
| 269 | + return $json; |
|
| 270 | + } |
|
| 271 | + /** |
|
| 272 | + * Wrapper for the hrtime() or microtime() functions |
|
| 273 | + * (depending on the PHP version, one of the two is used) |
|
| 274 | + * |
|
| 275 | + * @return float UNIX timestamp |
|
| 276 | + * |
|
| 277 | + * @internal |
|
| 278 | + */ |
|
| 279 | + public static function currentTime() : float |
|
| 280 | + { |
|
| 281 | + return (float) \function_exists('hrtime') ? \hrtime(\true) / 1000000000.0 : \microtime(\true); |
|
| 282 | + } |
|
| 283 | + /** |
|
| 284 | + * @throws InvalidArgumentException |
|
| 285 | + * |
|
| 286 | + * @internal |
|
| 287 | + */ |
|
| 288 | + public static function idnUriConvert(UriInterface $uri, int $options = 0) : UriInterface |
|
| 289 | + { |
|
| 290 | + if ($uri->getHost()) { |
|
| 291 | + $asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
|
| 292 | + if ($asciiHost === \false) { |
|
| 293 | + $errorBitSet = $info['errors'] ?? 0; |
|
| 294 | + $errorConstants = \array_filter(\array_keys(\get_defined_constants()), static function (string $name) : bool { |
|
| 295 | + return \substr($name, 0, 11) === 'IDNA_ERROR_'; |
|
| 296 | + }); |
|
| 297 | + $errors = []; |
|
| 298 | + foreach ($errorConstants as $errorConstant) { |
|
| 299 | + if ($errorBitSet & \constant($errorConstant)) { |
|
| 300 | + $errors[] = $errorConstant; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + $errorMessage = 'IDN conversion failed'; |
|
| 304 | + if ($errors) { |
|
| 305 | + $errorMessage .= ' (errors: ' . \implode(', ', $errors) . ')'; |
|
| 306 | + } |
|
| 307 | + throw new InvalidArgumentException($errorMessage); |
|
| 308 | + } |
|
| 309 | + if ($uri->getHost() !== $asciiHost) { |
|
| 310 | + // Replace URI only if the ASCII version is different |
|
| 311 | + $uri = $uri->withHost($asciiHost); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + return $uri; |
|
| 315 | + } |
|
| 316 | + /** |
|
| 317 | + * @internal |
|
| 318 | + */ |
|
| 319 | + public static function getenv(string $name) : ?string |
|
| 320 | + { |
|
| 321 | + if (isset($_SERVER[$name])) { |
|
| 322 | + return (string) $_SERVER[$name]; |
|
| 323 | + } |
|
| 324 | + if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== \false && $value !== null) { |
|
| 325 | + return (string) $value; |
|
| 326 | + } |
|
| 327 | + return null; |
|
| 328 | + } |
|
| 329 | + /** |
|
| 330 | + * @return string|false |
|
| 331 | + */ |
|
| 332 | + private static function idnToAsci(string $domain, int $options, ?array &$info = []) |
|
| 333 | + { |
|
| 334 | + if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 335 | + return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
|
| 336 | + } |
|
| 337 | + throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old'); |
|
| 338 | + } |
|
| 339 | 339 | } |
@@ -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 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 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 | } |
@@ -12,75 +12,75 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class PrepareBodyMiddleware |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 17 | - */ |
|
| 18 | - private $nextHandler; |
|
| 19 | - /** |
|
| 20 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 21 | - */ |
|
| 22 | - public function __construct(callable $nextHandler) |
|
| 23 | - { |
|
| 24 | - $this->nextHandler = $nextHandler; |
|
| 25 | - } |
|
| 26 | - public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 27 | - { |
|
| 28 | - $fn = $this->nextHandler; |
|
| 29 | - // Don't do anything if the request has no body. |
|
| 30 | - if ($request->getBody()->getSize() === 0) { |
|
| 31 | - return $fn($request, $options); |
|
| 32 | - } |
|
| 33 | - $modify = []; |
|
| 34 | - // Add a default content-type if possible. |
|
| 35 | - if (!$request->hasHeader('Content-Type')) { |
|
| 36 | - if ($uri = $request->getBody()->getMetadata('uri')) { |
|
| 37 | - if (\is_string($uri) && ($type = Psr7\MimeType::fromFilename($uri))) { |
|
| 38 | - $modify['set_headers']['Content-Type'] = $type; |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - // Add a default content-length or transfer-encoding header. |
|
| 43 | - if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) { |
|
| 44 | - $size = $request->getBody()->getSize(); |
|
| 45 | - if ($size !== null) { |
|
| 46 | - $modify['set_headers']['Content-Length'] = $size; |
|
| 47 | - } else { |
|
| 48 | - $modify['set_headers']['Transfer-Encoding'] = 'chunked'; |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - // Add the expect header if needed. |
|
| 52 | - $this->addExpectHeader($request, $options, $modify); |
|
| 53 | - return $fn(Psr7\Utils::modifyRequest($request, $modify), $options); |
|
| 54 | - } |
|
| 55 | - /** |
|
| 56 | - * Add expect header |
|
| 57 | - */ |
|
| 58 | - private function addExpectHeader(RequestInterface $request, array $options, array &$modify) : void |
|
| 59 | - { |
|
| 60 | - // Determine if the Expect header should be used |
|
| 61 | - if ($request->hasHeader('Expect')) { |
|
| 62 | - return; |
|
| 63 | - } |
|
| 64 | - $expect = $options['expect'] ?? null; |
|
| 65 | - // Return if disabled or using HTTP/1.0 |
|
| 66 | - if ($expect === \false || $request->getProtocolVersion() === '1.0') { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - // The expect header is unconditionally enabled |
|
| 70 | - if ($expect === \true) { |
|
| 71 | - $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - // By default, send the expect header when the payload is > 1mb |
|
| 75 | - if ($expect === null) { |
|
| 76 | - $expect = 1048576; |
|
| 77 | - } |
|
| 78 | - // Always add if the body cannot be rewound, the size cannot be |
|
| 79 | - // determined, or the size is greater than the cutoff threshold |
|
| 80 | - $body = $request->getBody(); |
|
| 81 | - $size = $body->getSize(); |
|
| 82 | - if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { |
|
| 83 | - $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 84 | - } |
|
| 85 | - } |
|
| 15 | + /** |
|
| 16 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 17 | + */ |
|
| 18 | + private $nextHandler; |
|
| 19 | + /** |
|
| 20 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 21 | + */ |
|
| 22 | + public function __construct(callable $nextHandler) |
|
| 23 | + { |
|
| 24 | + $this->nextHandler = $nextHandler; |
|
| 25 | + } |
|
| 26 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 27 | + { |
|
| 28 | + $fn = $this->nextHandler; |
|
| 29 | + // Don't do anything if the request has no body. |
|
| 30 | + if ($request->getBody()->getSize() === 0) { |
|
| 31 | + return $fn($request, $options); |
|
| 32 | + } |
|
| 33 | + $modify = []; |
|
| 34 | + // Add a default content-type if possible. |
|
| 35 | + if (!$request->hasHeader('Content-Type')) { |
|
| 36 | + if ($uri = $request->getBody()->getMetadata('uri')) { |
|
| 37 | + if (\is_string($uri) && ($type = Psr7\MimeType::fromFilename($uri))) { |
|
| 38 | + $modify['set_headers']['Content-Type'] = $type; |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + // Add a default content-length or transfer-encoding header. |
|
| 43 | + if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) { |
|
| 44 | + $size = $request->getBody()->getSize(); |
|
| 45 | + if ($size !== null) { |
|
| 46 | + $modify['set_headers']['Content-Length'] = $size; |
|
| 47 | + } else { |
|
| 48 | + $modify['set_headers']['Transfer-Encoding'] = 'chunked'; |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + // Add the expect header if needed. |
|
| 52 | + $this->addExpectHeader($request, $options, $modify); |
|
| 53 | + return $fn(Psr7\Utils::modifyRequest($request, $modify), $options); |
|
| 54 | + } |
|
| 55 | + /** |
|
| 56 | + * Add expect header |
|
| 57 | + */ |
|
| 58 | + private function addExpectHeader(RequestInterface $request, array $options, array &$modify) : void |
|
| 59 | + { |
|
| 60 | + // Determine if the Expect header should be used |
|
| 61 | + if ($request->hasHeader('Expect')) { |
|
| 62 | + return; |
|
| 63 | + } |
|
| 64 | + $expect = $options['expect'] ?? null; |
|
| 65 | + // Return if disabled or using HTTP/1.0 |
|
| 66 | + if ($expect === \false || $request->getProtocolVersion() === '1.0') { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + // The expect header is unconditionally enabled |
|
| 70 | + if ($expect === \true) { |
|
| 71 | + $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + // By default, send the expect header when the payload is > 1mb |
|
| 75 | + if ($expect === null) { |
|
| 76 | + $expect = 1048576; |
|
| 77 | + } |
|
| 78 | + // Always add if the body cannot be rewound, the size cannot be |
|
| 79 | + // determined, or the size is greater than the cutoff threshold |
|
| 80 | + $body = $request->getBody(); |
|
| 81 | + $size = $body->getSize(); |
|
| 82 | + if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { |
|
| 83 | + $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | } |