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