@@ -13,39 +13,39 @@ |
||
13 | 13 | */ |
14 | 14 | class Proxy |
15 | 15 | { |
16 | - /** |
|
17 | - * Sends synchronous requests to a specific handler while sending all other |
|
18 | - * requests to another handler. |
|
19 | - * |
|
20 | - * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $default Handler used for normal responses |
|
21 | - * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $sync Handler used for synchronous responses. |
|
22 | - * |
|
23 | - * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the composed handler. |
|
24 | - */ |
|
25 | - public static function wrapSync(callable $default, callable $sync): callable |
|
26 | - { |
|
27 | - return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface { |
|
28 | - return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options); |
|
29 | - }; |
|
30 | - } |
|
16 | + /** |
|
17 | + * Sends synchronous requests to a specific handler while sending all other |
|
18 | + * requests to another handler. |
|
19 | + * |
|
20 | + * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $default Handler used for normal responses |
|
21 | + * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $sync Handler used for synchronous responses. |
|
22 | + * |
|
23 | + * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the composed handler. |
|
24 | + */ |
|
25 | + public static function wrapSync(callable $default, callable $sync): callable |
|
26 | + { |
|
27 | + return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface { |
|
28 | + return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options); |
|
29 | + }; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Sends streaming requests to a streaming compatible handler while sending |
|
34 | - * all other requests to a default handler. |
|
35 | - * |
|
36 | - * This, for example, could be useful for taking advantage of the |
|
37 | - * performance benefits of curl while still supporting true streaming |
|
38 | - * through the StreamHandler. |
|
39 | - * |
|
40 | - * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $default Handler used for non-streaming responses |
|
41 | - * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $streaming Handler used for streaming responses |
|
42 | - * |
|
43 | - * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the composed handler. |
|
44 | - */ |
|
45 | - public static function wrapStreaming(callable $default, callable $streaming): callable |
|
46 | - { |
|
47 | - return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface { |
|
48 | - return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options); |
|
49 | - }; |
|
50 | - } |
|
32 | + /** |
|
33 | + * Sends streaming requests to a streaming compatible handler while sending |
|
34 | + * all other requests to a default handler. |
|
35 | + * |
|
36 | + * This, for example, could be useful for taking advantage of the |
|
37 | + * performance benefits of curl while still supporting true streaming |
|
38 | + * through the StreamHandler. |
|
39 | + * |
|
40 | + * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $default Handler used for non-streaming responses |
|
41 | + * @param callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface $streaming Handler used for streaming responses |
|
42 | + * |
|
43 | + * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the composed handler. |
|
44 | + */ |
|
45 | + public static function wrapStreaming(callable $default, callable $streaming): callable |
|
46 | + { |
|
47 | + return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface { |
|
48 | + return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options); |
|
49 | + }; |
|
50 | + } |
|
51 | 51 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | */ |
25 | 25 | public static function wrapSync(callable $default, callable $sync): callable |
26 | 26 | { |
27 | - return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface { |
|
27 | + return static function(RequestInterface $request, array $options) use ($default, $sync): PromiseInterface { |
|
28 | 28 | return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options); |
29 | 29 | }; |
30 | 30 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public static function wrapStreaming(callable $default, callable $streaming): callable |
46 | 46 | { |
47 | - return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface { |
|
47 | + return static function(RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface { |
|
48 | 48 | return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options); |
49 | 49 | }; |
50 | 50 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @final |
13 | 13 | */ |
14 | -class Proxy |
|
15 | -{ |
|
14 | +class Proxy { |
|
16 | 15 | /** |
17 | 16 | * Sends synchronous requests to a specific handler while sending all other |
18 | 17 | * requests to another handler. |
@@ -6,20 +6,20 @@ |
||
6 | 6 | |
7 | 7 | interface CurlFactoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * Creates a cURL handle resource. |
|
11 | - * |
|
12 | - * @param RequestInterface $request Request |
|
13 | - * @param array $options Transfer options |
|
14 | - * |
|
15 | - * @throws \RuntimeException when an option cannot be applied |
|
16 | - */ |
|
17 | - public function create(RequestInterface $request, array $options): EasyHandle; |
|
9 | + /** |
|
10 | + * Creates a cURL handle resource. |
|
11 | + * |
|
12 | + * @param RequestInterface $request Request |
|
13 | + * @param array $options Transfer options |
|
14 | + * |
|
15 | + * @throws \RuntimeException when an option cannot be applied |
|
16 | + */ |
|
17 | + public function create(RequestInterface $request, array $options): EasyHandle; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Release an easy handle, allowing it to be reused or closed. |
|
21 | - * |
|
22 | - * This function must call unset on the easy handle's "handle" property. |
|
23 | - */ |
|
24 | - public function release(EasyHandle $easy): void; |
|
19 | + /** |
|
20 | + * Release an easy handle, allowing it to be reused or closed. |
|
21 | + * |
|
22 | + * This function must call unset on the easy handle's "handle" property. |
|
23 | + */ |
|
24 | + public function release(EasyHandle $easy): void; |
|
25 | 25 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
6 | 6 | |
7 | -interface CurlFactoryInterface |
|
8 | -{ |
|
7 | +interface CurlFactoryInterface { |
|
9 | 8 | /** |
10 | 9 | * Creates a cURL handle resource. |
11 | 10 | * |
@@ -19,620 +19,620 @@ |
||
19 | 19 | */ |
20 | 20 | class CurlFactory implements CurlFactoryInterface |
21 | 21 | { |
22 | - public const CURL_VERSION_STR = 'curl_version'; |
|
23 | - |
|
24 | - /** |
|
25 | - * @deprecated |
|
26 | - */ |
|
27 | - public const LOW_CURL_VERSION_NUMBER = '7.21.2'; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var resource[]|\CurlHandle[] |
|
31 | - */ |
|
32 | - private $handles = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var int Total number of idle handles to keep in cache |
|
36 | - */ |
|
37 | - private $maxHandles; |
|
38 | - |
|
39 | - /** |
|
40 | - * @param int $maxHandles Maximum number of idle handles. |
|
41 | - */ |
|
42 | - public function __construct(int $maxHandles) |
|
43 | - { |
|
44 | - $this->maxHandles = $maxHandles; |
|
45 | - } |
|
46 | - |
|
47 | - public function create(RequestInterface $request, array $options): EasyHandle |
|
48 | - { |
|
49 | - if (isset($options['curl']['body_as_string'])) { |
|
50 | - $options['_body_as_string'] = $options['curl']['body_as_string']; |
|
51 | - unset($options['curl']['body_as_string']); |
|
52 | - } |
|
53 | - |
|
54 | - $easy = new EasyHandle(); |
|
55 | - $easy->request = $request; |
|
56 | - $easy->options = $options; |
|
57 | - $conf = $this->getDefaultConf($easy); |
|
58 | - $this->applyMethod($easy, $conf); |
|
59 | - $this->applyHandlerOptions($easy, $conf); |
|
60 | - $this->applyHeaders($easy, $conf); |
|
61 | - unset($conf['_headers']); |
|
62 | - |
|
63 | - // Add handler options from the request configuration options |
|
64 | - if (isset($options['curl'])) { |
|
65 | - $conf = \array_replace($conf, $options['curl']); |
|
66 | - } |
|
67 | - |
|
68 | - $conf[\CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy); |
|
69 | - $easy->handle = $this->handles ? \array_pop($this->handles) : \curl_init(); |
|
70 | - curl_setopt_array($easy->handle, $conf); |
|
71 | - |
|
72 | - return $easy; |
|
73 | - } |
|
74 | - |
|
75 | - public function release(EasyHandle $easy): void |
|
76 | - { |
|
77 | - $resource = $easy->handle; |
|
78 | - unset($easy->handle); |
|
79 | - |
|
80 | - if (\count($this->handles) >= $this->maxHandles) { |
|
81 | - \curl_close($resource); |
|
82 | - } else { |
|
83 | - // Remove all callback functions as they can hold onto references |
|
84 | - // and are not cleaned up by curl_reset. Using curl_setopt_array |
|
85 | - // does not work for some reason, so removing each one |
|
86 | - // individually. |
|
87 | - \curl_setopt($resource, \CURLOPT_HEADERFUNCTION, null); |
|
88 | - \curl_setopt($resource, \CURLOPT_READFUNCTION, null); |
|
89 | - \curl_setopt($resource, \CURLOPT_WRITEFUNCTION, null); |
|
90 | - \curl_setopt($resource, \CURLOPT_PROGRESSFUNCTION, null); |
|
91 | - \curl_reset($resource); |
|
92 | - $this->handles[] = $resource; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Completes a cURL transaction, either returning a response promise or a |
|
98 | - * rejected promise. |
|
99 | - * |
|
100 | - * @param callable(RequestInterface, array): PromiseInterface $handler |
|
101 | - * @param CurlFactoryInterface $factory Dictates how the handle is released |
|
102 | - */ |
|
103 | - public static function finish(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
|
104 | - { |
|
105 | - if (isset($easy->options['on_stats'])) { |
|
106 | - self::invokeStats($easy); |
|
107 | - } |
|
108 | - |
|
109 | - if (!$easy->response || $easy->errno) { |
|
110 | - return self::finishError($handler, $easy, $factory); |
|
111 | - } |
|
112 | - |
|
113 | - // Return the response if it is present and there is no error. |
|
114 | - $factory->release($easy); |
|
115 | - |
|
116 | - // Rewind the body of the response if possible. |
|
117 | - $body = $easy->response->getBody(); |
|
118 | - if ($body->isSeekable()) { |
|
119 | - $body->rewind(); |
|
120 | - } |
|
121 | - |
|
122 | - return new FulfilledPromise($easy->response); |
|
123 | - } |
|
124 | - |
|
125 | - private static function invokeStats(EasyHandle $easy): void |
|
126 | - { |
|
127 | - $curlStats = \curl_getinfo($easy->handle); |
|
128 | - $curlStats['appconnect_time'] = \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME); |
|
129 | - $stats = new TransferStats( |
|
130 | - $easy->request, |
|
131 | - $easy->response, |
|
132 | - $curlStats['total_time'], |
|
133 | - $easy->errno, |
|
134 | - $curlStats |
|
135 | - ); |
|
136 | - ($easy->options['on_stats'])($stats); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @param callable(RequestInterface, array): PromiseInterface $handler |
|
141 | - */ |
|
142 | - private static function finishError(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
|
143 | - { |
|
144 | - // Get error information and release the handle to the factory. |
|
145 | - $ctx = [ |
|
146 | - 'errno' => $easy->errno, |
|
147 | - 'error' => \curl_error($easy->handle), |
|
148 | - 'appconnect_time' => \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME), |
|
149 | - ] + \curl_getinfo($easy->handle); |
|
150 | - $ctx[self::CURL_VERSION_STR] = \curl_version()['version']; |
|
151 | - $factory->release($easy); |
|
152 | - |
|
153 | - // Retry when nothing is present or when curl failed to rewind. |
|
154 | - if (empty($easy->options['_err_message']) && (!$easy->errno || $easy->errno == 65)) { |
|
155 | - return self::retryFailedRewind($handler, $easy, $ctx); |
|
156 | - } |
|
157 | - |
|
158 | - return self::createRejection($easy, $ctx); |
|
159 | - } |
|
160 | - |
|
161 | - private static function createRejection(EasyHandle $easy, array $ctx): PromiseInterface |
|
162 | - { |
|
163 | - static $connectionErrors = [ |
|
164 | - \CURLE_OPERATION_TIMEOUTED => true, |
|
165 | - \CURLE_COULDNT_RESOLVE_HOST => true, |
|
166 | - \CURLE_COULDNT_CONNECT => true, |
|
167 | - \CURLE_SSL_CONNECT_ERROR => true, |
|
168 | - \CURLE_GOT_NOTHING => true, |
|
169 | - ]; |
|
170 | - |
|
171 | - if ($easy->createResponseException) { |
|
172 | - return P\Create::rejectionFor( |
|
173 | - new RequestException( |
|
174 | - 'An error was encountered while creating the response', |
|
175 | - $easy->request, |
|
176 | - $easy->response, |
|
177 | - $easy->createResponseException, |
|
178 | - $ctx |
|
179 | - ) |
|
180 | - ); |
|
181 | - } |
|
182 | - |
|
183 | - // If an exception was encountered during the onHeaders event, then |
|
184 | - // return a rejected promise that wraps that exception. |
|
185 | - if ($easy->onHeadersException) { |
|
186 | - return P\Create::rejectionFor( |
|
187 | - new RequestException( |
|
188 | - 'An error was encountered during the on_headers event', |
|
189 | - $easy->request, |
|
190 | - $easy->response, |
|
191 | - $easy->onHeadersException, |
|
192 | - $ctx |
|
193 | - ) |
|
194 | - ); |
|
195 | - } |
|
196 | - |
|
197 | - $message = \sprintf( |
|
198 | - 'cURL error %s: %s (%s)', |
|
199 | - $ctx['errno'], |
|
200 | - $ctx['error'], |
|
201 | - 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html' |
|
202 | - ); |
|
203 | - $uriString = (string) $easy->request->getUri(); |
|
204 | - if ($uriString !== '' && false === \strpos($ctx['error'], $uriString)) { |
|
205 | - $message .= \sprintf(' for %s', $uriString); |
|
206 | - } |
|
207 | - |
|
208 | - // Create a connection exception if it was a specific error code. |
|
209 | - $error = isset($connectionErrors[$easy->errno]) |
|
210 | - ? new ConnectException($message, $easy->request, null, $ctx) |
|
211 | - : new RequestException($message, $easy->request, $easy->response, null, $ctx); |
|
212 | - |
|
213 | - return P\Create::rejectionFor($error); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @return array<int|string, mixed> |
|
218 | - */ |
|
219 | - private function getDefaultConf(EasyHandle $easy): array |
|
220 | - { |
|
221 | - $conf = [ |
|
222 | - '_headers' => $easy->request->getHeaders(), |
|
223 | - \CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(), |
|
224 | - \CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''), |
|
225 | - \CURLOPT_RETURNTRANSFER => false, |
|
226 | - \CURLOPT_HEADER => false, |
|
227 | - \CURLOPT_CONNECTTIMEOUT => 300, |
|
228 | - ]; |
|
229 | - |
|
230 | - if (\defined('CURLOPT_PROTOCOLS')) { |
|
231 | - $conf[\CURLOPT_PROTOCOLS] = \CURLPROTO_HTTP | \CURLPROTO_HTTPS; |
|
232 | - } |
|
233 | - |
|
234 | - $version = $easy->request->getProtocolVersion(); |
|
235 | - if ($version == 1.1) { |
|
236 | - $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1; |
|
237 | - } elseif ($version == 2.0) { |
|
238 | - $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2_0; |
|
239 | - } else { |
|
240 | - $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_0; |
|
241 | - } |
|
242 | - |
|
243 | - return $conf; |
|
244 | - } |
|
245 | - |
|
246 | - private function applyMethod(EasyHandle $easy, array &$conf): void |
|
247 | - { |
|
248 | - $body = $easy->request->getBody(); |
|
249 | - $size = $body->getSize(); |
|
250 | - |
|
251 | - if ($size === null || $size > 0) { |
|
252 | - $this->applyBody($easy->request, $easy->options, $conf); |
|
253 | - |
|
254 | - return; |
|
255 | - } |
|
256 | - |
|
257 | - $method = $easy->request->getMethod(); |
|
258 | - if ($method === 'PUT' || $method === 'POST') { |
|
259 | - // See https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 |
|
260 | - if (!$easy->request->hasHeader('Content-Length')) { |
|
261 | - $conf[\CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; |
|
262 | - } |
|
263 | - } elseif ($method === 'HEAD') { |
|
264 | - $conf[\CURLOPT_NOBODY] = true; |
|
265 | - unset( |
|
266 | - $conf[\CURLOPT_WRITEFUNCTION], |
|
267 | - $conf[\CURLOPT_READFUNCTION], |
|
268 | - $conf[\CURLOPT_FILE], |
|
269 | - $conf[\CURLOPT_INFILE] |
|
270 | - ); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - private function applyBody(RequestInterface $request, array $options, array &$conf): void |
|
275 | - { |
|
276 | - $size = $request->hasHeader('Content-Length') |
|
277 | - ? (int) $request->getHeaderLine('Content-Length') |
|
278 | - : null; |
|
279 | - |
|
280 | - // Send the body as a string if the size is less than 1MB OR if the |
|
281 | - // [curl][body_as_string] request value is set. |
|
282 | - if (($size !== null && $size < 1000000) || !empty($options['_body_as_string'])) { |
|
283 | - $conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody(); |
|
284 | - // Don't duplicate the Content-Length header |
|
285 | - $this->removeHeader('Content-Length', $conf); |
|
286 | - $this->removeHeader('Transfer-Encoding', $conf); |
|
287 | - } else { |
|
288 | - $conf[\CURLOPT_UPLOAD] = true; |
|
289 | - if ($size !== null) { |
|
290 | - $conf[\CURLOPT_INFILESIZE] = $size; |
|
291 | - $this->removeHeader('Content-Length', $conf); |
|
292 | - } |
|
293 | - $body = $request->getBody(); |
|
294 | - if ($body->isSeekable()) { |
|
295 | - $body->rewind(); |
|
296 | - } |
|
297 | - $conf[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) { |
|
298 | - return $body->read($length); |
|
299 | - }; |
|
300 | - } |
|
301 | - |
|
302 | - // If the Expect header is not present, prevent curl from adding it |
|
303 | - if (!$request->hasHeader('Expect')) { |
|
304 | - $conf[\CURLOPT_HTTPHEADER][] = 'Expect:'; |
|
305 | - } |
|
306 | - |
|
307 | - // cURL sometimes adds a content-type by default. Prevent this. |
|
308 | - if (!$request->hasHeader('Content-Type')) { |
|
309 | - $conf[\CURLOPT_HTTPHEADER][] = 'Content-Type:'; |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - private function applyHeaders(EasyHandle $easy, array &$conf): void |
|
314 | - { |
|
315 | - foreach ($conf['_headers'] as $name => $values) { |
|
316 | - foreach ($values as $value) { |
|
317 | - $value = (string) $value; |
|
318 | - if ($value === '') { |
|
319 | - // cURL requires a special format for empty headers. |
|
320 | - // See https://github.com/guzzle/guzzle/issues/1882 for more details. |
|
321 | - $conf[\CURLOPT_HTTPHEADER][] = "$name;"; |
|
322 | - } else { |
|
323 | - $conf[\CURLOPT_HTTPHEADER][] = "$name: $value"; |
|
324 | - } |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - // Remove the Accept header if one was not set |
|
329 | - if (!$easy->request->hasHeader('Accept')) { |
|
330 | - $conf[\CURLOPT_HTTPHEADER][] = 'Accept:'; |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Remove a header from the options array. |
|
336 | - * |
|
337 | - * @param string $name Case-insensitive header to remove |
|
338 | - * @param array $options Array of options to modify |
|
339 | - */ |
|
340 | - private function removeHeader(string $name, array &$options): void |
|
341 | - { |
|
342 | - foreach (\array_keys($options['_headers']) as $key) { |
|
343 | - if (!\strcasecmp($key, $name)) { |
|
344 | - unset($options['_headers'][$key]); |
|
345 | - |
|
346 | - return; |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - private function applyHandlerOptions(EasyHandle $easy, array &$conf): void |
|
352 | - { |
|
353 | - $options = $easy->options; |
|
354 | - if (isset($options['verify'])) { |
|
355 | - if ($options['verify'] === false) { |
|
356 | - unset($conf[\CURLOPT_CAINFO]); |
|
357 | - $conf[\CURLOPT_SSL_VERIFYHOST] = 0; |
|
358 | - $conf[\CURLOPT_SSL_VERIFYPEER] = false; |
|
359 | - } else { |
|
360 | - $conf[\CURLOPT_SSL_VERIFYHOST] = 2; |
|
361 | - $conf[\CURLOPT_SSL_VERIFYPEER] = true; |
|
362 | - if (\is_string($options['verify'])) { |
|
363 | - // Throw an error if the file/folder/link path is not valid or doesn't exist. |
|
364 | - if (!\file_exists($options['verify'])) { |
|
365 | - throw new \InvalidArgumentException("SSL CA bundle not found: {$options['verify']}"); |
|
366 | - } |
|
367 | - // If it's a directory or a link to a directory use CURLOPT_CAPATH. |
|
368 | - // If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO. |
|
369 | - if ( |
|
370 | - \is_dir($options['verify']) |
|
371 | - || ( |
|
372 | - \is_link($options['verify']) === true |
|
373 | - && ($verifyLink = \readlink($options['verify'])) !== false |
|
374 | - && \is_dir($verifyLink) |
|
375 | - ) |
|
376 | - ) { |
|
377 | - $conf[\CURLOPT_CAPATH] = $options['verify']; |
|
378 | - } else { |
|
379 | - $conf[\CURLOPT_CAINFO] = $options['verify']; |
|
380 | - } |
|
381 | - } |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - if (!isset($options['curl'][\CURLOPT_ENCODING]) && !empty($options['decode_content'])) { |
|
386 | - $accept = $easy->request->getHeaderLine('Accept-Encoding'); |
|
387 | - if ($accept) { |
|
388 | - $conf[\CURLOPT_ENCODING] = $accept; |
|
389 | - } else { |
|
390 | - // The empty string enables all available decoders and implicitly |
|
391 | - // sets a matching 'Accept-Encoding' header. |
|
392 | - $conf[\CURLOPT_ENCODING] = ''; |
|
393 | - // But as the user did not specify any acceptable encodings we need |
|
394 | - // to overwrite this implicit header with an empty one. |
|
395 | - $conf[\CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - if (!isset($options['sink'])) { |
|
400 | - // Use a default temp stream if no sink was set. |
|
401 | - $options['sink'] = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::tryFopen('php://temp', 'w+'); |
|
402 | - } |
|
403 | - $sink = $options['sink']; |
|
404 | - if (!\is_string($sink)) { |
|
405 | - $sink = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::streamFor($sink); |
|
406 | - } elseif (!\is_dir(\dirname($sink))) { |
|
407 | - // Ensure that the directory exists before failing in curl. |
|
408 | - throw new \RuntimeException(\sprintf('Directory %s does not exist for sink value of %s', \dirname($sink), $sink)); |
|
409 | - } else { |
|
410 | - $sink = new LazyOpenStream($sink, 'w+'); |
|
411 | - } |
|
412 | - $easy->sink = $sink; |
|
413 | - $conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int { |
|
414 | - return $sink->write($write); |
|
415 | - }; |
|
416 | - |
|
417 | - $timeoutRequiresNoSignal = false; |
|
418 | - if (isset($options['timeout'])) { |
|
419 | - $timeoutRequiresNoSignal |= $options['timeout'] < 1; |
|
420 | - $conf[\CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000; |
|
421 | - } |
|
422 | - |
|
423 | - // CURL default value is CURL_IPRESOLVE_WHATEVER |
|
424 | - if (isset($options['force_ip_resolve'])) { |
|
425 | - if ('v4' === $options['force_ip_resolve']) { |
|
426 | - $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V4; |
|
427 | - } elseif ('v6' === $options['force_ip_resolve']) { |
|
428 | - $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V6; |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - if (isset($options['connect_timeout'])) { |
|
433 | - $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1; |
|
434 | - $conf[\CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000; |
|
435 | - } |
|
436 | - |
|
437 | - if ($timeoutRequiresNoSignal && \strtoupper(\substr(\PHP_OS, 0, 3)) !== 'WIN') { |
|
438 | - $conf[\CURLOPT_NOSIGNAL] = true; |
|
439 | - } |
|
440 | - |
|
441 | - if (isset($options['proxy'])) { |
|
442 | - if (!\is_array($options['proxy'])) { |
|
443 | - $conf[\CURLOPT_PROXY] = $options['proxy']; |
|
444 | - } else { |
|
445 | - $scheme = $easy->request->getUri()->getScheme(); |
|
446 | - if (isset($options['proxy'][$scheme])) { |
|
447 | - $host = $easy->request->getUri()->getHost(); |
|
448 | - if (isset($options['proxy']['no']) && Utils::isHostInNoProxy($host, $options['proxy']['no'])) { |
|
449 | - unset($conf[\CURLOPT_PROXY]); |
|
450 | - } else { |
|
451 | - $conf[\CURLOPT_PROXY] = $options['proxy'][$scheme]; |
|
452 | - } |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
456 | - |
|
457 | - if (isset($options['crypto_method'])) { |
|
458 | - if (\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT === $options['crypto_method']) { |
|
459 | - if (!defined('CURL_SSLVERSION_TLSv1_0')) { |
|
460 | - throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.0 not supported by your version of cURL'); |
|
461 | - } |
|
462 | - $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_0; |
|
463 | - } elseif (\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT === $options['crypto_method']) { |
|
464 | - if (!defined('CURL_SSLVERSION_TLSv1_1')) { |
|
465 | - throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.1 not supported by your version of cURL'); |
|
466 | - } |
|
467 | - $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_1; |
|
468 | - } elseif (\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT === $options['crypto_method']) { |
|
469 | - if (!defined('CURL_SSLVERSION_TLSv1_2')) { |
|
470 | - throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.2 not supported by your version of cURL'); |
|
471 | - } |
|
472 | - $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2; |
|
473 | - } elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) { |
|
474 | - if (!defined('CURL_SSLVERSION_TLSv1_3')) { |
|
475 | - throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL'); |
|
476 | - } |
|
477 | - $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3; |
|
478 | - } else { |
|
479 | - throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided'); |
|
480 | - } |
|
481 | - } |
|
482 | - |
|
483 | - if (isset($options['cert'])) { |
|
484 | - $cert = $options['cert']; |
|
485 | - if (\is_array($cert)) { |
|
486 | - $conf[\CURLOPT_SSLCERTPASSWD] = $cert[1]; |
|
487 | - $cert = $cert[0]; |
|
488 | - } |
|
489 | - if (!\file_exists($cert)) { |
|
490 | - throw new \InvalidArgumentException("SSL certificate not found: {$cert}"); |
|
491 | - } |
|
492 | - // OpenSSL (versions 0.9.3 and later) also support "P12" for PKCS#12-encoded files. |
|
493 | - // see https://curl.se/libcurl/c/CURLOPT_SSLCERTTYPE.html |
|
494 | - $ext = pathinfo($cert, \PATHINFO_EXTENSION); |
|
495 | - if (preg_match('#^(der|p12)$#i', $ext)) { |
|
496 | - $conf[\CURLOPT_SSLCERTTYPE] = strtoupper($ext); |
|
497 | - } |
|
498 | - $conf[\CURLOPT_SSLCERT] = $cert; |
|
499 | - } |
|
500 | - |
|
501 | - if (isset($options['ssl_key'])) { |
|
502 | - if (\is_array($options['ssl_key'])) { |
|
503 | - if (\count($options['ssl_key']) === 2) { |
|
504 | - [$sslKey, $conf[\CURLOPT_SSLKEYPASSWD]] = $options['ssl_key']; |
|
505 | - } else { |
|
506 | - [$sslKey] = $options['ssl_key']; |
|
507 | - } |
|
508 | - } |
|
509 | - |
|
510 | - $sslKey = $sslKey ?? $options['ssl_key']; |
|
511 | - |
|
512 | - if (!\file_exists($sslKey)) { |
|
513 | - throw new \InvalidArgumentException("SSL private key not found: {$sslKey}"); |
|
514 | - } |
|
515 | - $conf[\CURLOPT_SSLKEY] = $sslKey; |
|
516 | - } |
|
517 | - |
|
518 | - if (isset($options['progress'])) { |
|
519 | - $progress = $options['progress']; |
|
520 | - if (!\is_callable($progress)) { |
|
521 | - throw new \InvalidArgumentException('progress client option must be callable'); |
|
522 | - } |
|
523 | - $conf[\CURLOPT_NOPROGRESS] = false; |
|
524 | - $conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) { |
|
525 | - $progress($downloadSize, $downloaded, $uploadSize, $uploaded); |
|
526 | - }; |
|
527 | - } |
|
528 | - |
|
529 | - if (!empty($options['debug'])) { |
|
530 | - $conf[\CURLOPT_STDERR] = Utils::debugResource($options['debug']); |
|
531 | - $conf[\CURLOPT_VERBOSE] = true; |
|
532 | - } |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * This function ensures that a response was set on a transaction. If one |
|
537 | - * was not set, then the request is retried if possible. This error |
|
538 | - * typically means you are sending a payload, curl encountered a |
|
539 | - * "Connection died, retrying a fresh connect" error, tried to rewind the |
|
540 | - * stream, and then encountered a "necessary data rewind wasn't possible" |
|
541 | - * error, causing the request to be sent through curl_multi_info_read() |
|
542 | - * without an error status. |
|
543 | - * |
|
544 | - * @param callable(RequestInterface, array): PromiseInterface $handler |
|
545 | - */ |
|
546 | - private static function retryFailedRewind(callable $handler, EasyHandle $easy, array $ctx): PromiseInterface |
|
547 | - { |
|
548 | - try { |
|
549 | - // Only rewind if the body has been read from. |
|
550 | - $body = $easy->request->getBody(); |
|
551 | - if ($body->tell() > 0) { |
|
552 | - $body->rewind(); |
|
553 | - } |
|
554 | - } catch (\RuntimeException $e) { |
|
555 | - $ctx['error'] = 'The connection unexpectedly failed without ' |
|
556 | - .'providing an error. The request would have been retried, ' |
|
557 | - .'but attempting to rewind the request body failed. ' |
|
558 | - .'Exception: '.$e; |
|
559 | - |
|
560 | - return self::createRejection($easy, $ctx); |
|
561 | - } |
|
562 | - |
|
563 | - // Retry no more than 3 times before giving up. |
|
564 | - if (!isset($easy->options['_curl_retries'])) { |
|
565 | - $easy->options['_curl_retries'] = 1; |
|
566 | - } elseif ($easy->options['_curl_retries'] == 2) { |
|
567 | - $ctx['error'] = 'The cURL request was retried 3 times ' |
|
568 | - .'and did not succeed. The most likely reason for the failure ' |
|
569 | - .'is that cURL was unable to rewind the body of the request ' |
|
570 | - .'and subsequent retries resulted in the same error. Turn on ' |
|
571 | - .'the debug option to see what went wrong. See ' |
|
572 | - .'https://bugs.php.net/bug.php?id=47204 for more information.'; |
|
573 | - |
|
574 | - return self::createRejection($easy, $ctx); |
|
575 | - } else { |
|
576 | - ++$easy->options['_curl_retries']; |
|
577 | - } |
|
578 | - |
|
579 | - return $handler($easy->request, $easy->options); |
|
580 | - } |
|
581 | - |
|
582 | - private function createHeaderFn(EasyHandle $easy): callable |
|
583 | - { |
|
584 | - if (isset($easy->options['on_headers'])) { |
|
585 | - $onHeaders = $easy->options['on_headers']; |
|
586 | - |
|
587 | - if (!\is_callable($onHeaders)) { |
|
588 | - throw new \InvalidArgumentException('on_headers must be callable'); |
|
589 | - } |
|
590 | - } else { |
|
591 | - $onHeaders = null; |
|
592 | - } |
|
593 | - |
|
594 | - return static function ($ch, $h) use ( |
|
595 | - $onHeaders, |
|
596 | - $easy, |
|
597 | - &$startingResponse |
|
598 | - ) { |
|
599 | - $value = \trim($h); |
|
600 | - if ($value === '') { |
|
601 | - $startingResponse = true; |
|
602 | - try { |
|
603 | - $easy->createResponse(); |
|
604 | - } catch (\Exception $e) { |
|
605 | - $easy->createResponseException = $e; |
|
606 | - |
|
607 | - return -1; |
|
608 | - } |
|
609 | - if ($onHeaders !== null) { |
|
610 | - try { |
|
611 | - $onHeaders($easy->response); |
|
612 | - } catch (\Exception $e) { |
|
613 | - // Associate the exception with the handle and trigger |
|
614 | - // a curl header write error by returning 0. |
|
615 | - $easy->onHeadersException = $e; |
|
616 | - |
|
617 | - return -1; |
|
618 | - } |
|
619 | - } |
|
620 | - } elseif ($startingResponse) { |
|
621 | - $startingResponse = false; |
|
622 | - $easy->headers = [$value]; |
|
623 | - } else { |
|
624 | - $easy->headers[] = $value; |
|
625 | - } |
|
626 | - |
|
627 | - return \strlen($h); |
|
628 | - }; |
|
629 | - } |
|
630 | - |
|
631 | - public function __destruct() |
|
632 | - { |
|
633 | - foreach ($this->handles as $id => $handle) { |
|
634 | - \curl_close($handle); |
|
635 | - unset($this->handles[$id]); |
|
636 | - } |
|
637 | - } |
|
22 | + public const CURL_VERSION_STR = 'curl_version'; |
|
23 | + |
|
24 | + /** |
|
25 | + * @deprecated |
|
26 | + */ |
|
27 | + public const LOW_CURL_VERSION_NUMBER = '7.21.2'; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var resource[]|\CurlHandle[] |
|
31 | + */ |
|
32 | + private $handles = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var int Total number of idle handles to keep in cache |
|
36 | + */ |
|
37 | + private $maxHandles; |
|
38 | + |
|
39 | + /** |
|
40 | + * @param int $maxHandles Maximum number of idle handles. |
|
41 | + */ |
|
42 | + public function __construct(int $maxHandles) |
|
43 | + { |
|
44 | + $this->maxHandles = $maxHandles; |
|
45 | + } |
|
46 | + |
|
47 | + public function create(RequestInterface $request, array $options): EasyHandle |
|
48 | + { |
|
49 | + if (isset($options['curl']['body_as_string'])) { |
|
50 | + $options['_body_as_string'] = $options['curl']['body_as_string']; |
|
51 | + unset($options['curl']['body_as_string']); |
|
52 | + } |
|
53 | + |
|
54 | + $easy = new EasyHandle(); |
|
55 | + $easy->request = $request; |
|
56 | + $easy->options = $options; |
|
57 | + $conf = $this->getDefaultConf($easy); |
|
58 | + $this->applyMethod($easy, $conf); |
|
59 | + $this->applyHandlerOptions($easy, $conf); |
|
60 | + $this->applyHeaders($easy, $conf); |
|
61 | + unset($conf['_headers']); |
|
62 | + |
|
63 | + // Add handler options from the request configuration options |
|
64 | + if (isset($options['curl'])) { |
|
65 | + $conf = \array_replace($conf, $options['curl']); |
|
66 | + } |
|
67 | + |
|
68 | + $conf[\CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy); |
|
69 | + $easy->handle = $this->handles ? \array_pop($this->handles) : \curl_init(); |
|
70 | + curl_setopt_array($easy->handle, $conf); |
|
71 | + |
|
72 | + return $easy; |
|
73 | + } |
|
74 | + |
|
75 | + public function release(EasyHandle $easy): void |
|
76 | + { |
|
77 | + $resource = $easy->handle; |
|
78 | + unset($easy->handle); |
|
79 | + |
|
80 | + if (\count($this->handles) >= $this->maxHandles) { |
|
81 | + \curl_close($resource); |
|
82 | + } else { |
|
83 | + // Remove all callback functions as they can hold onto references |
|
84 | + // and are not cleaned up by curl_reset. Using curl_setopt_array |
|
85 | + // does not work for some reason, so removing each one |
|
86 | + // individually. |
|
87 | + \curl_setopt($resource, \CURLOPT_HEADERFUNCTION, null); |
|
88 | + \curl_setopt($resource, \CURLOPT_READFUNCTION, null); |
|
89 | + \curl_setopt($resource, \CURLOPT_WRITEFUNCTION, null); |
|
90 | + \curl_setopt($resource, \CURLOPT_PROGRESSFUNCTION, null); |
|
91 | + \curl_reset($resource); |
|
92 | + $this->handles[] = $resource; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Completes a cURL transaction, either returning a response promise or a |
|
98 | + * rejected promise. |
|
99 | + * |
|
100 | + * @param callable(RequestInterface, array): PromiseInterface $handler |
|
101 | + * @param CurlFactoryInterface $factory Dictates how the handle is released |
|
102 | + */ |
|
103 | + public static function finish(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
|
104 | + { |
|
105 | + if (isset($easy->options['on_stats'])) { |
|
106 | + self::invokeStats($easy); |
|
107 | + } |
|
108 | + |
|
109 | + if (!$easy->response || $easy->errno) { |
|
110 | + return self::finishError($handler, $easy, $factory); |
|
111 | + } |
|
112 | + |
|
113 | + // Return the response if it is present and there is no error. |
|
114 | + $factory->release($easy); |
|
115 | + |
|
116 | + // Rewind the body of the response if possible. |
|
117 | + $body = $easy->response->getBody(); |
|
118 | + if ($body->isSeekable()) { |
|
119 | + $body->rewind(); |
|
120 | + } |
|
121 | + |
|
122 | + return new FulfilledPromise($easy->response); |
|
123 | + } |
|
124 | + |
|
125 | + private static function invokeStats(EasyHandle $easy): void |
|
126 | + { |
|
127 | + $curlStats = \curl_getinfo($easy->handle); |
|
128 | + $curlStats['appconnect_time'] = \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME); |
|
129 | + $stats = new TransferStats( |
|
130 | + $easy->request, |
|
131 | + $easy->response, |
|
132 | + $curlStats['total_time'], |
|
133 | + $easy->errno, |
|
134 | + $curlStats |
|
135 | + ); |
|
136 | + ($easy->options['on_stats'])($stats); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @param callable(RequestInterface, array): PromiseInterface $handler |
|
141 | + */ |
|
142 | + private static function finishError(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
|
143 | + { |
|
144 | + // Get error information and release the handle to the factory. |
|
145 | + $ctx = [ |
|
146 | + 'errno' => $easy->errno, |
|
147 | + 'error' => \curl_error($easy->handle), |
|
148 | + 'appconnect_time' => \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME), |
|
149 | + ] + \curl_getinfo($easy->handle); |
|
150 | + $ctx[self::CURL_VERSION_STR] = \curl_version()['version']; |
|
151 | + $factory->release($easy); |
|
152 | + |
|
153 | + // Retry when nothing is present or when curl failed to rewind. |
|
154 | + if (empty($easy->options['_err_message']) && (!$easy->errno || $easy->errno == 65)) { |
|
155 | + return self::retryFailedRewind($handler, $easy, $ctx); |
|
156 | + } |
|
157 | + |
|
158 | + return self::createRejection($easy, $ctx); |
|
159 | + } |
|
160 | + |
|
161 | + private static function createRejection(EasyHandle $easy, array $ctx): PromiseInterface |
|
162 | + { |
|
163 | + static $connectionErrors = [ |
|
164 | + \CURLE_OPERATION_TIMEOUTED => true, |
|
165 | + \CURLE_COULDNT_RESOLVE_HOST => true, |
|
166 | + \CURLE_COULDNT_CONNECT => true, |
|
167 | + \CURLE_SSL_CONNECT_ERROR => true, |
|
168 | + \CURLE_GOT_NOTHING => true, |
|
169 | + ]; |
|
170 | + |
|
171 | + if ($easy->createResponseException) { |
|
172 | + return P\Create::rejectionFor( |
|
173 | + new RequestException( |
|
174 | + 'An error was encountered while creating the response', |
|
175 | + $easy->request, |
|
176 | + $easy->response, |
|
177 | + $easy->createResponseException, |
|
178 | + $ctx |
|
179 | + ) |
|
180 | + ); |
|
181 | + } |
|
182 | + |
|
183 | + // If an exception was encountered during the onHeaders event, then |
|
184 | + // return a rejected promise that wraps that exception. |
|
185 | + if ($easy->onHeadersException) { |
|
186 | + return P\Create::rejectionFor( |
|
187 | + new RequestException( |
|
188 | + 'An error was encountered during the on_headers event', |
|
189 | + $easy->request, |
|
190 | + $easy->response, |
|
191 | + $easy->onHeadersException, |
|
192 | + $ctx |
|
193 | + ) |
|
194 | + ); |
|
195 | + } |
|
196 | + |
|
197 | + $message = \sprintf( |
|
198 | + 'cURL error %s: %s (%s)', |
|
199 | + $ctx['errno'], |
|
200 | + $ctx['error'], |
|
201 | + 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html' |
|
202 | + ); |
|
203 | + $uriString = (string) $easy->request->getUri(); |
|
204 | + if ($uriString !== '' && false === \strpos($ctx['error'], $uriString)) { |
|
205 | + $message .= \sprintf(' for %s', $uriString); |
|
206 | + } |
|
207 | + |
|
208 | + // Create a connection exception if it was a specific error code. |
|
209 | + $error = isset($connectionErrors[$easy->errno]) |
|
210 | + ? new ConnectException($message, $easy->request, null, $ctx) |
|
211 | + : new RequestException($message, $easy->request, $easy->response, null, $ctx); |
|
212 | + |
|
213 | + return P\Create::rejectionFor($error); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @return array<int|string, mixed> |
|
218 | + */ |
|
219 | + private function getDefaultConf(EasyHandle $easy): array |
|
220 | + { |
|
221 | + $conf = [ |
|
222 | + '_headers' => $easy->request->getHeaders(), |
|
223 | + \CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(), |
|
224 | + \CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''), |
|
225 | + \CURLOPT_RETURNTRANSFER => false, |
|
226 | + \CURLOPT_HEADER => false, |
|
227 | + \CURLOPT_CONNECTTIMEOUT => 300, |
|
228 | + ]; |
|
229 | + |
|
230 | + if (\defined('CURLOPT_PROTOCOLS')) { |
|
231 | + $conf[\CURLOPT_PROTOCOLS] = \CURLPROTO_HTTP | \CURLPROTO_HTTPS; |
|
232 | + } |
|
233 | + |
|
234 | + $version = $easy->request->getProtocolVersion(); |
|
235 | + if ($version == 1.1) { |
|
236 | + $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1; |
|
237 | + } elseif ($version == 2.0) { |
|
238 | + $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2_0; |
|
239 | + } else { |
|
240 | + $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_0; |
|
241 | + } |
|
242 | + |
|
243 | + return $conf; |
|
244 | + } |
|
245 | + |
|
246 | + private function applyMethod(EasyHandle $easy, array &$conf): void |
|
247 | + { |
|
248 | + $body = $easy->request->getBody(); |
|
249 | + $size = $body->getSize(); |
|
250 | + |
|
251 | + if ($size === null || $size > 0) { |
|
252 | + $this->applyBody($easy->request, $easy->options, $conf); |
|
253 | + |
|
254 | + return; |
|
255 | + } |
|
256 | + |
|
257 | + $method = $easy->request->getMethod(); |
|
258 | + if ($method === 'PUT' || $method === 'POST') { |
|
259 | + // See https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 |
|
260 | + if (!$easy->request->hasHeader('Content-Length')) { |
|
261 | + $conf[\CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; |
|
262 | + } |
|
263 | + } elseif ($method === 'HEAD') { |
|
264 | + $conf[\CURLOPT_NOBODY] = true; |
|
265 | + unset( |
|
266 | + $conf[\CURLOPT_WRITEFUNCTION], |
|
267 | + $conf[\CURLOPT_READFUNCTION], |
|
268 | + $conf[\CURLOPT_FILE], |
|
269 | + $conf[\CURLOPT_INFILE] |
|
270 | + ); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + private function applyBody(RequestInterface $request, array $options, array &$conf): void |
|
275 | + { |
|
276 | + $size = $request->hasHeader('Content-Length') |
|
277 | + ? (int) $request->getHeaderLine('Content-Length') |
|
278 | + : null; |
|
279 | + |
|
280 | + // Send the body as a string if the size is less than 1MB OR if the |
|
281 | + // [curl][body_as_string] request value is set. |
|
282 | + if (($size !== null && $size < 1000000) || !empty($options['_body_as_string'])) { |
|
283 | + $conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody(); |
|
284 | + // Don't duplicate the Content-Length header |
|
285 | + $this->removeHeader('Content-Length', $conf); |
|
286 | + $this->removeHeader('Transfer-Encoding', $conf); |
|
287 | + } else { |
|
288 | + $conf[\CURLOPT_UPLOAD] = true; |
|
289 | + if ($size !== null) { |
|
290 | + $conf[\CURLOPT_INFILESIZE] = $size; |
|
291 | + $this->removeHeader('Content-Length', $conf); |
|
292 | + } |
|
293 | + $body = $request->getBody(); |
|
294 | + if ($body->isSeekable()) { |
|
295 | + $body->rewind(); |
|
296 | + } |
|
297 | + $conf[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) { |
|
298 | + return $body->read($length); |
|
299 | + }; |
|
300 | + } |
|
301 | + |
|
302 | + // If the Expect header is not present, prevent curl from adding it |
|
303 | + if (!$request->hasHeader('Expect')) { |
|
304 | + $conf[\CURLOPT_HTTPHEADER][] = 'Expect:'; |
|
305 | + } |
|
306 | + |
|
307 | + // cURL sometimes adds a content-type by default. Prevent this. |
|
308 | + if (!$request->hasHeader('Content-Type')) { |
|
309 | + $conf[\CURLOPT_HTTPHEADER][] = 'Content-Type:'; |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + private function applyHeaders(EasyHandle $easy, array &$conf): void |
|
314 | + { |
|
315 | + foreach ($conf['_headers'] as $name => $values) { |
|
316 | + foreach ($values as $value) { |
|
317 | + $value = (string) $value; |
|
318 | + if ($value === '') { |
|
319 | + // cURL requires a special format for empty headers. |
|
320 | + // See https://github.com/guzzle/guzzle/issues/1882 for more details. |
|
321 | + $conf[\CURLOPT_HTTPHEADER][] = "$name;"; |
|
322 | + } else { |
|
323 | + $conf[\CURLOPT_HTTPHEADER][] = "$name: $value"; |
|
324 | + } |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + // Remove the Accept header if one was not set |
|
329 | + if (!$easy->request->hasHeader('Accept')) { |
|
330 | + $conf[\CURLOPT_HTTPHEADER][] = 'Accept:'; |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Remove a header from the options array. |
|
336 | + * |
|
337 | + * @param string $name Case-insensitive header to remove |
|
338 | + * @param array $options Array of options to modify |
|
339 | + */ |
|
340 | + private function removeHeader(string $name, array &$options): void |
|
341 | + { |
|
342 | + foreach (\array_keys($options['_headers']) as $key) { |
|
343 | + if (!\strcasecmp($key, $name)) { |
|
344 | + unset($options['_headers'][$key]); |
|
345 | + |
|
346 | + return; |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + private function applyHandlerOptions(EasyHandle $easy, array &$conf): void |
|
352 | + { |
|
353 | + $options = $easy->options; |
|
354 | + if (isset($options['verify'])) { |
|
355 | + if ($options['verify'] === false) { |
|
356 | + unset($conf[\CURLOPT_CAINFO]); |
|
357 | + $conf[\CURLOPT_SSL_VERIFYHOST] = 0; |
|
358 | + $conf[\CURLOPT_SSL_VERIFYPEER] = false; |
|
359 | + } else { |
|
360 | + $conf[\CURLOPT_SSL_VERIFYHOST] = 2; |
|
361 | + $conf[\CURLOPT_SSL_VERIFYPEER] = true; |
|
362 | + if (\is_string($options['verify'])) { |
|
363 | + // Throw an error if the file/folder/link path is not valid or doesn't exist. |
|
364 | + if (!\file_exists($options['verify'])) { |
|
365 | + throw new \InvalidArgumentException("SSL CA bundle not found: {$options['verify']}"); |
|
366 | + } |
|
367 | + // If it's a directory or a link to a directory use CURLOPT_CAPATH. |
|
368 | + // If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO. |
|
369 | + if ( |
|
370 | + \is_dir($options['verify']) |
|
371 | + || ( |
|
372 | + \is_link($options['verify']) === true |
|
373 | + && ($verifyLink = \readlink($options['verify'])) !== false |
|
374 | + && \is_dir($verifyLink) |
|
375 | + ) |
|
376 | + ) { |
|
377 | + $conf[\CURLOPT_CAPATH] = $options['verify']; |
|
378 | + } else { |
|
379 | + $conf[\CURLOPT_CAINFO] = $options['verify']; |
|
380 | + } |
|
381 | + } |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + if (!isset($options['curl'][\CURLOPT_ENCODING]) && !empty($options['decode_content'])) { |
|
386 | + $accept = $easy->request->getHeaderLine('Accept-Encoding'); |
|
387 | + if ($accept) { |
|
388 | + $conf[\CURLOPT_ENCODING] = $accept; |
|
389 | + } else { |
|
390 | + // The empty string enables all available decoders and implicitly |
|
391 | + // sets a matching 'Accept-Encoding' header. |
|
392 | + $conf[\CURLOPT_ENCODING] = ''; |
|
393 | + // But as the user did not specify any acceptable encodings we need |
|
394 | + // to overwrite this implicit header with an empty one. |
|
395 | + $conf[\CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + if (!isset($options['sink'])) { |
|
400 | + // Use a default temp stream if no sink was set. |
|
401 | + $options['sink'] = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::tryFopen('php://temp', 'w+'); |
|
402 | + } |
|
403 | + $sink = $options['sink']; |
|
404 | + if (!\is_string($sink)) { |
|
405 | + $sink = \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::streamFor($sink); |
|
406 | + } elseif (!\is_dir(\dirname($sink))) { |
|
407 | + // Ensure that the directory exists before failing in curl. |
|
408 | + throw new \RuntimeException(\sprintf('Directory %s does not exist for sink value of %s', \dirname($sink), $sink)); |
|
409 | + } else { |
|
410 | + $sink = new LazyOpenStream($sink, 'w+'); |
|
411 | + } |
|
412 | + $easy->sink = $sink; |
|
413 | + $conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int { |
|
414 | + return $sink->write($write); |
|
415 | + }; |
|
416 | + |
|
417 | + $timeoutRequiresNoSignal = false; |
|
418 | + if (isset($options['timeout'])) { |
|
419 | + $timeoutRequiresNoSignal |= $options['timeout'] < 1; |
|
420 | + $conf[\CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000; |
|
421 | + } |
|
422 | + |
|
423 | + // CURL default value is CURL_IPRESOLVE_WHATEVER |
|
424 | + if (isset($options['force_ip_resolve'])) { |
|
425 | + if ('v4' === $options['force_ip_resolve']) { |
|
426 | + $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V4; |
|
427 | + } elseif ('v6' === $options['force_ip_resolve']) { |
|
428 | + $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V6; |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + if (isset($options['connect_timeout'])) { |
|
433 | + $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1; |
|
434 | + $conf[\CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000; |
|
435 | + } |
|
436 | + |
|
437 | + if ($timeoutRequiresNoSignal && \strtoupper(\substr(\PHP_OS, 0, 3)) !== 'WIN') { |
|
438 | + $conf[\CURLOPT_NOSIGNAL] = true; |
|
439 | + } |
|
440 | + |
|
441 | + if (isset($options['proxy'])) { |
|
442 | + if (!\is_array($options['proxy'])) { |
|
443 | + $conf[\CURLOPT_PROXY] = $options['proxy']; |
|
444 | + } else { |
|
445 | + $scheme = $easy->request->getUri()->getScheme(); |
|
446 | + if (isset($options['proxy'][$scheme])) { |
|
447 | + $host = $easy->request->getUri()->getHost(); |
|
448 | + if (isset($options['proxy']['no']) && Utils::isHostInNoProxy($host, $options['proxy']['no'])) { |
|
449 | + unset($conf[\CURLOPT_PROXY]); |
|
450 | + } else { |
|
451 | + $conf[\CURLOPT_PROXY] = $options['proxy'][$scheme]; |
|
452 | + } |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | + |
|
457 | + if (isset($options['crypto_method'])) { |
|
458 | + if (\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT === $options['crypto_method']) { |
|
459 | + if (!defined('CURL_SSLVERSION_TLSv1_0')) { |
|
460 | + throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.0 not supported by your version of cURL'); |
|
461 | + } |
|
462 | + $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_0; |
|
463 | + } elseif (\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT === $options['crypto_method']) { |
|
464 | + if (!defined('CURL_SSLVERSION_TLSv1_1')) { |
|
465 | + throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.1 not supported by your version of cURL'); |
|
466 | + } |
|
467 | + $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_1; |
|
468 | + } elseif (\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT === $options['crypto_method']) { |
|
469 | + if (!defined('CURL_SSLVERSION_TLSv1_2')) { |
|
470 | + throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.2 not supported by your version of cURL'); |
|
471 | + } |
|
472 | + $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2; |
|
473 | + } elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) { |
|
474 | + if (!defined('CURL_SSLVERSION_TLSv1_3')) { |
|
475 | + throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL'); |
|
476 | + } |
|
477 | + $conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3; |
|
478 | + } else { |
|
479 | + throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided'); |
|
480 | + } |
|
481 | + } |
|
482 | + |
|
483 | + if (isset($options['cert'])) { |
|
484 | + $cert = $options['cert']; |
|
485 | + if (\is_array($cert)) { |
|
486 | + $conf[\CURLOPT_SSLCERTPASSWD] = $cert[1]; |
|
487 | + $cert = $cert[0]; |
|
488 | + } |
|
489 | + if (!\file_exists($cert)) { |
|
490 | + throw new \InvalidArgumentException("SSL certificate not found: {$cert}"); |
|
491 | + } |
|
492 | + // OpenSSL (versions 0.9.3 and later) also support "P12" for PKCS#12-encoded files. |
|
493 | + // see https://curl.se/libcurl/c/CURLOPT_SSLCERTTYPE.html |
|
494 | + $ext = pathinfo($cert, \PATHINFO_EXTENSION); |
|
495 | + if (preg_match('#^(der|p12)$#i', $ext)) { |
|
496 | + $conf[\CURLOPT_SSLCERTTYPE] = strtoupper($ext); |
|
497 | + } |
|
498 | + $conf[\CURLOPT_SSLCERT] = $cert; |
|
499 | + } |
|
500 | + |
|
501 | + if (isset($options['ssl_key'])) { |
|
502 | + if (\is_array($options['ssl_key'])) { |
|
503 | + if (\count($options['ssl_key']) === 2) { |
|
504 | + [$sslKey, $conf[\CURLOPT_SSLKEYPASSWD]] = $options['ssl_key']; |
|
505 | + } else { |
|
506 | + [$sslKey] = $options['ssl_key']; |
|
507 | + } |
|
508 | + } |
|
509 | + |
|
510 | + $sslKey = $sslKey ?? $options['ssl_key']; |
|
511 | + |
|
512 | + if (!\file_exists($sslKey)) { |
|
513 | + throw new \InvalidArgumentException("SSL private key not found: {$sslKey}"); |
|
514 | + } |
|
515 | + $conf[\CURLOPT_SSLKEY] = $sslKey; |
|
516 | + } |
|
517 | + |
|
518 | + if (isset($options['progress'])) { |
|
519 | + $progress = $options['progress']; |
|
520 | + if (!\is_callable($progress)) { |
|
521 | + throw new \InvalidArgumentException('progress client option must be callable'); |
|
522 | + } |
|
523 | + $conf[\CURLOPT_NOPROGRESS] = false; |
|
524 | + $conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) { |
|
525 | + $progress($downloadSize, $downloaded, $uploadSize, $uploaded); |
|
526 | + }; |
|
527 | + } |
|
528 | + |
|
529 | + if (!empty($options['debug'])) { |
|
530 | + $conf[\CURLOPT_STDERR] = Utils::debugResource($options['debug']); |
|
531 | + $conf[\CURLOPT_VERBOSE] = true; |
|
532 | + } |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * This function ensures that a response was set on a transaction. If one |
|
537 | + * was not set, then the request is retried if possible. This error |
|
538 | + * typically means you are sending a payload, curl encountered a |
|
539 | + * "Connection died, retrying a fresh connect" error, tried to rewind the |
|
540 | + * stream, and then encountered a "necessary data rewind wasn't possible" |
|
541 | + * error, causing the request to be sent through curl_multi_info_read() |
|
542 | + * without an error status. |
|
543 | + * |
|
544 | + * @param callable(RequestInterface, array): PromiseInterface $handler |
|
545 | + */ |
|
546 | + private static function retryFailedRewind(callable $handler, EasyHandle $easy, array $ctx): PromiseInterface |
|
547 | + { |
|
548 | + try { |
|
549 | + // Only rewind if the body has been read from. |
|
550 | + $body = $easy->request->getBody(); |
|
551 | + if ($body->tell() > 0) { |
|
552 | + $body->rewind(); |
|
553 | + } |
|
554 | + } catch (\RuntimeException $e) { |
|
555 | + $ctx['error'] = 'The connection unexpectedly failed without ' |
|
556 | + .'providing an error. The request would have been retried, ' |
|
557 | + .'but attempting to rewind the request body failed. ' |
|
558 | + .'Exception: '.$e; |
|
559 | + |
|
560 | + return self::createRejection($easy, $ctx); |
|
561 | + } |
|
562 | + |
|
563 | + // Retry no more than 3 times before giving up. |
|
564 | + if (!isset($easy->options['_curl_retries'])) { |
|
565 | + $easy->options['_curl_retries'] = 1; |
|
566 | + } elseif ($easy->options['_curl_retries'] == 2) { |
|
567 | + $ctx['error'] = 'The cURL request was retried 3 times ' |
|
568 | + .'and did not succeed. The most likely reason for the failure ' |
|
569 | + .'is that cURL was unable to rewind the body of the request ' |
|
570 | + .'and subsequent retries resulted in the same error. Turn on ' |
|
571 | + .'the debug option to see what went wrong. See ' |
|
572 | + .'https://bugs.php.net/bug.php?id=47204 for more information.'; |
|
573 | + |
|
574 | + return self::createRejection($easy, $ctx); |
|
575 | + } else { |
|
576 | + ++$easy->options['_curl_retries']; |
|
577 | + } |
|
578 | + |
|
579 | + return $handler($easy->request, $easy->options); |
|
580 | + } |
|
581 | + |
|
582 | + private function createHeaderFn(EasyHandle $easy): callable |
|
583 | + { |
|
584 | + if (isset($easy->options['on_headers'])) { |
|
585 | + $onHeaders = $easy->options['on_headers']; |
|
586 | + |
|
587 | + if (!\is_callable($onHeaders)) { |
|
588 | + throw new \InvalidArgumentException('on_headers must be callable'); |
|
589 | + } |
|
590 | + } else { |
|
591 | + $onHeaders = null; |
|
592 | + } |
|
593 | + |
|
594 | + return static function ($ch, $h) use ( |
|
595 | + $onHeaders, |
|
596 | + $easy, |
|
597 | + &$startingResponse |
|
598 | + ) { |
|
599 | + $value = \trim($h); |
|
600 | + if ($value === '') { |
|
601 | + $startingResponse = true; |
|
602 | + try { |
|
603 | + $easy->createResponse(); |
|
604 | + } catch (\Exception $e) { |
|
605 | + $easy->createResponseException = $e; |
|
606 | + |
|
607 | + return -1; |
|
608 | + } |
|
609 | + if ($onHeaders !== null) { |
|
610 | + try { |
|
611 | + $onHeaders($easy->response); |
|
612 | + } catch (\Exception $e) { |
|
613 | + // Associate the exception with the handle and trigger |
|
614 | + // a curl header write error by returning 0. |
|
615 | + $easy->onHeadersException = $e; |
|
616 | + |
|
617 | + return -1; |
|
618 | + } |
|
619 | + } |
|
620 | + } elseif ($startingResponse) { |
|
621 | + $startingResponse = false; |
|
622 | + $easy->headers = [$value]; |
|
623 | + } else { |
|
624 | + $easy->headers[] = $value; |
|
625 | + } |
|
626 | + |
|
627 | + return \strlen($h); |
|
628 | + }; |
|
629 | + } |
|
630 | + |
|
631 | + public function __destruct() |
|
632 | + { |
|
633 | + foreach ($this->handles as $id => $handle) { |
|
634 | + \curl_close($handle); |
|
635 | + unset($this->handles[$id]); |
|
636 | + } |
|
637 | + } |
|
638 | 638 | } |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | $ctx['error'], |
201 | 201 | 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html' |
202 | 202 | ); |
203 | - $uriString = (string) $easy->request->getUri(); |
|
203 | + $uriString = (string)$easy->request->getUri(); |
|
204 | 204 | if ($uriString !== '' && false === \strpos($ctx['error'], $uriString)) { |
205 | 205 | $message .= \sprintf(' for %s', $uriString); |
206 | 206 | } |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | $conf = [ |
222 | 222 | '_headers' => $easy->request->getHeaders(), |
223 | 223 | \CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(), |
224 | - \CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''), |
|
224 | + \CURLOPT_URL => (string)$easy->request->getUri()->withFragment(''), |
|
225 | 225 | \CURLOPT_RETURNTRANSFER => false, |
226 | 226 | \CURLOPT_HEADER => false, |
227 | 227 | \CURLOPT_CONNECTTIMEOUT => 300, |
@@ -274,13 +274,13 @@ discard block |
||
274 | 274 | private function applyBody(RequestInterface $request, array $options, array &$conf): void |
275 | 275 | { |
276 | 276 | $size = $request->hasHeader('Content-Length') |
277 | - ? (int) $request->getHeaderLine('Content-Length') |
|
277 | + ? (int)$request->getHeaderLine('Content-Length') |
|
278 | 278 | : null; |
279 | 279 | |
280 | 280 | // Send the body as a string if the size is less than 1MB OR if the |
281 | 281 | // [curl][body_as_string] request value is set. |
282 | 282 | if (($size !== null && $size < 1000000) || !empty($options['_body_as_string'])) { |
283 | - $conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody(); |
|
283 | + $conf[\CURLOPT_POSTFIELDS] = (string)$request->getBody(); |
|
284 | 284 | // Don't duplicate the Content-Length header |
285 | 285 | $this->removeHeader('Content-Length', $conf); |
286 | 286 | $this->removeHeader('Transfer-Encoding', $conf); |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | if ($body->isSeekable()) { |
295 | 295 | $body->rewind(); |
296 | 296 | } |
297 | - $conf[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) { |
|
297 | + $conf[\CURLOPT_READFUNCTION] = static function($ch, $fd, $length) use ($body) { |
|
298 | 298 | return $body->read($length); |
299 | 299 | }; |
300 | 300 | } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | { |
315 | 315 | foreach ($conf['_headers'] as $name => $values) { |
316 | 316 | foreach ($values as $value) { |
317 | - $value = (string) $value; |
|
317 | + $value = (string)$value; |
|
318 | 318 | if ($value === '') { |
319 | 319 | // cURL requires a special format for empty headers. |
320 | 320 | // See https://github.com/guzzle/guzzle/issues/1882 for more details. |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | $sink = new LazyOpenStream($sink, 'w+'); |
411 | 411 | } |
412 | 412 | $easy->sink = $sink; |
413 | - $conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int { |
|
413 | + $conf[\CURLOPT_WRITEFUNCTION] = static function($ch, $write) use ($sink): int { |
|
414 | 414 | return $sink->write($write); |
415 | 415 | }; |
416 | 416 | |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | throw new \InvalidArgumentException('progress client option must be callable'); |
522 | 522 | } |
523 | 523 | $conf[\CURLOPT_NOPROGRESS] = false; |
524 | - $conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) { |
|
524 | + $conf[\CURLOPT_PROGRESSFUNCTION] = static function($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) { |
|
525 | 525 | $progress($downloadSize, $downloaded, $uploadSize, $uploaded); |
526 | 526 | }; |
527 | 527 | } |
@@ -591,7 +591,7 @@ discard block |
||
591 | 591 | $onHeaders = null; |
592 | 592 | } |
593 | 593 | |
594 | - return static function ($ch, $h) use ( |
|
594 | + return static function($ch, $h) use ( |
|
595 | 595 | $onHeaders, |
596 | 596 | $easy, |
597 | 597 | &$startingResponse |
@@ -17,8 +17,7 @@ |
||
17 | 17 | * |
18 | 18 | * @final |
19 | 19 | */ |
20 | -class CurlFactory implements CurlFactoryInterface |
|
21 | -{ |
|
20 | +class CurlFactory implements CurlFactoryInterface { |
|
22 | 21 | public const CURL_VERSION_STR = 'curl_version'; |
23 | 22 | |
24 | 23 | /** |
@@ -16,34 +16,34 @@ |
||
16 | 16 | */ |
17 | 17 | class CurlHandler |
18 | 18 | { |
19 | - /** |
|
20 | - * @var CurlFactoryInterface |
|
21 | - */ |
|
22 | - private $factory; |
|
19 | + /** |
|
20 | + * @var CurlFactoryInterface |
|
21 | + */ |
|
22 | + private $factory; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Accepts an associative array of options: |
|
26 | - * |
|
27 | - * - handle_factory: Optional curl factory used to create cURL handles. |
|
28 | - * |
|
29 | - * @param array{handle_factory?: ?CurlFactoryInterface} $options Array of options to use with the handler |
|
30 | - */ |
|
31 | - public function __construct(array $options = []) |
|
32 | - { |
|
33 | - $this->factory = $options['handle_factory'] |
|
34 | - ?? new CurlFactory(3); |
|
35 | - } |
|
24 | + /** |
|
25 | + * Accepts an associative array of options: |
|
26 | + * |
|
27 | + * - handle_factory: Optional curl factory used to create cURL handles. |
|
28 | + * |
|
29 | + * @param array{handle_factory?: ?CurlFactoryInterface} $options Array of options to use with the handler |
|
30 | + */ |
|
31 | + public function __construct(array $options = []) |
|
32 | + { |
|
33 | + $this->factory = $options['handle_factory'] |
|
34 | + ?? new CurlFactory(3); |
|
35 | + } |
|
36 | 36 | |
37 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
38 | - { |
|
39 | - if (isset($options['delay'])) { |
|
40 | - \usleep($options['delay'] * 1000); |
|
41 | - } |
|
37 | + public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
38 | + { |
|
39 | + if (isset($options['delay'])) { |
|
40 | + \usleep($options['delay'] * 1000); |
|
41 | + } |
|
42 | 42 | |
43 | - $easy = $this->factory->create($request, $options); |
|
44 | - \curl_exec($easy->handle); |
|
45 | - $easy->errno = \curl_errno($easy->handle); |
|
43 | + $easy = $this->factory->create($request, $options); |
|
44 | + \curl_exec($easy->handle); |
|
45 | + $easy->errno = \curl_errno($easy->handle); |
|
46 | 46 | |
47 | - return CurlFactory::finish($this, $easy, $this->factory); |
|
48 | - } |
|
47 | + return CurlFactory::finish($this, $easy, $this->factory); |
|
48 | + } |
|
49 | 49 | } |
@@ -14,8 +14,7 @@ |
||
14 | 14 | * |
15 | 15 | * @final |
16 | 16 | */ |
17 | -class CurlHandler |
|
18 | -{ |
|
17 | +class CurlHandler { |
|
19 | 18 | /** |
20 | 19 | * @var CurlFactoryInterface |
21 | 20 | */ |
@@ -9,266 +9,266 @@ |
||
9 | 9 | */ |
10 | 10 | final class RequestOptions |
11 | 11 | { |
12 | - /** |
|
13 | - * allow_redirects: (bool|array) Controls redirect behavior. Pass false |
|
14 | - * to disable redirects, pass true to enable redirects, pass an |
|
15 | - * associative to provide custom redirect settings. Defaults to "false". |
|
16 | - * This option only works if your handler has the RedirectMiddleware. When |
|
17 | - * passing an associative array, you can provide the following key value |
|
18 | - * pairs: |
|
19 | - * |
|
20 | - * - max: (int, default=5) maximum number of allowed redirects. |
|
21 | - * - strict: (bool, default=false) Set to true to use strict redirects |
|
22 | - * meaning redirect POST requests with POST requests vs. doing what most |
|
23 | - * browsers do which is redirect POST requests with GET requests |
|
24 | - * - referer: (bool, default=false) Set to true to enable the Referer |
|
25 | - * header. |
|
26 | - * - protocols: (array, default=['http', 'https']) Allowed redirect |
|
27 | - * protocols. |
|
28 | - * - on_redirect: (callable) PHP callable that is invoked when a redirect |
|
29 | - * is encountered. The callable is invoked with the request, the redirect |
|
30 | - * response that was received, and the effective URI. Any return value |
|
31 | - * from the on_redirect function is ignored. |
|
32 | - */ |
|
33 | - public const ALLOW_REDIRECTS = 'allow_redirects'; |
|
34 | - |
|
35 | - /** |
|
36 | - * auth: (array) Pass an array of HTTP authentication parameters to use |
|
37 | - * with the request. The array must contain the username in index [0], |
|
38 | - * the password in index [1], and you can optionally provide a built-in |
|
39 | - * authentication type in index [2]. Pass null to disable authentication |
|
40 | - * for a request. |
|
41 | - */ |
|
42 | - public const AUTH = 'auth'; |
|
43 | - |
|
44 | - /** |
|
45 | - * body: (resource|string|null|int|float|StreamInterface|callable|\Iterator) |
|
46 | - * Body to send in the request. |
|
47 | - */ |
|
48 | - public const BODY = 'body'; |
|
49 | - |
|
50 | - /** |
|
51 | - * cert: (string|array) Set to a string to specify the path to a file |
|
52 | - * containing a PEM formatted SSL client side certificate. If a password |
|
53 | - * is required, then set cert to an array containing the path to the PEM |
|
54 | - * file in the first array element followed by the certificate password |
|
55 | - * in the second array element. |
|
56 | - */ |
|
57 | - public const CERT = 'cert'; |
|
58 | - |
|
59 | - /** |
|
60 | - * cookies: (bool|OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface, default=false) |
|
61 | - * Specifies whether or not cookies are used in a request or what cookie |
|
62 | - * jar to use or what cookies to send. This option only works if your |
|
63 | - * handler has the `cookie` middleware. Valid values are `false` and |
|
64 | - * an instance of {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface}. |
|
65 | - */ |
|
66 | - public const COOKIES = 'cookies'; |
|
67 | - |
|
68 | - /** |
|
69 | - * connect_timeout: (float, default=0) Float describing the number of |
|
70 | - * seconds to wait while trying to connect to a server. Use 0 to wait |
|
71 | - * 300 seconds (the default behavior). |
|
72 | - */ |
|
73 | - public const CONNECT_TIMEOUT = 'connect_timeout'; |
|
74 | - |
|
75 | - /** |
|
76 | - * crypto_method: (int) A value describing the minimum TLS protocol |
|
77 | - * version to use. |
|
78 | - * |
|
79 | - * This setting must be set to one of the |
|
80 | - * ``STREAM_CRYPTO_METHOD_TLS*_CLIENT`` constants. PHP 7.4 or higher is |
|
81 | - * required in order to use TLS 1.3, and cURL 7.34.0 or higher is required |
|
82 | - * in order to specify a crypto method, with cURL 7.52.0 or higher being |
|
83 | - * required to use TLS 1.3. |
|
84 | - */ |
|
85 | - public const CRYPTO_METHOD = 'crypto_method'; |
|
86 | - |
|
87 | - /** |
|
88 | - * debug: (bool|resource) Set to true or set to a PHP stream returned by |
|
89 | - * fopen() enable debug output with the HTTP handler used to send a |
|
90 | - * request. |
|
91 | - */ |
|
92 | - public const DEBUG = 'debug'; |
|
93 | - |
|
94 | - /** |
|
95 | - * decode_content: (bool, default=true) Specify whether or not |
|
96 | - * Content-Encoding responses (gzip, deflate, etc.) are automatically |
|
97 | - * decoded. |
|
98 | - */ |
|
99 | - public const DECODE_CONTENT = 'decode_content'; |
|
100 | - |
|
101 | - /** |
|
102 | - * delay: (int) The amount of time to delay before sending in milliseconds. |
|
103 | - */ |
|
104 | - public const DELAY = 'delay'; |
|
105 | - |
|
106 | - /** |
|
107 | - * expect: (bool|integer) Controls the behavior of the |
|
108 | - * "Expect: 100-Continue" header. |
|
109 | - * |
|
110 | - * Set to `true` to enable the "Expect: 100-Continue" header for all |
|
111 | - * requests that sends a body. Set to `false` to disable the |
|
112 | - * "Expect: 100-Continue" header for all requests. Set to a number so that |
|
113 | - * the size of the payload must be greater than the number in order to send |
|
114 | - * the Expect header. Setting to a number will send the Expect header for |
|
115 | - * all requests in which the size of the payload cannot be determined or |
|
116 | - * where the body is not rewindable. |
|
117 | - * |
|
118 | - * By default, Guzzle will add the "Expect: 100-Continue" header when the |
|
119 | - * size of the body of a request is greater than 1 MB and a request is |
|
120 | - * using HTTP/1.1. |
|
121 | - */ |
|
122 | - public const EXPECT = 'expect'; |
|
123 | - |
|
124 | - /** |
|
125 | - * form_params: (array) Associative array of form field names to values |
|
126 | - * where each value is a string or array of strings. Sets the Content-Type |
|
127 | - * header to application/x-www-form-urlencoded when no Content-Type header |
|
128 | - * is already present. |
|
129 | - */ |
|
130 | - public const FORM_PARAMS = 'form_params'; |
|
131 | - |
|
132 | - /** |
|
133 | - * headers: (array) Associative array of HTTP headers. Each value MUST be |
|
134 | - * a string or array of strings. |
|
135 | - */ |
|
136 | - public const HEADERS = 'headers'; |
|
137 | - |
|
138 | - /** |
|
139 | - * http_errors: (bool, default=true) Set to false to disable exceptions |
|
140 | - * when a non- successful HTTP response is received. By default, |
|
141 | - * exceptions will be thrown for 4xx and 5xx responses. This option only |
|
142 | - * works if your handler has the `httpErrors` middleware. |
|
143 | - */ |
|
144 | - public const HTTP_ERRORS = 'http_errors'; |
|
145 | - |
|
146 | - /** |
|
147 | - * idn: (bool|int, default=true) A combination of IDNA_* constants for |
|
148 | - * idn_to_ascii() PHP's function (see "options" parameter). Set to false to |
|
149 | - * disable IDN support completely, or to true to use the default |
|
150 | - * configuration (IDNA_DEFAULT constant). |
|
151 | - */ |
|
152 | - public const IDN_CONVERSION = 'idn_conversion'; |
|
153 | - |
|
154 | - /** |
|
155 | - * json: (mixed) Adds JSON data to a request. The provided value is JSON |
|
156 | - * encoded and a Content-Type header of application/json will be added to |
|
157 | - * the request if no Content-Type header is already present. |
|
158 | - */ |
|
159 | - public const JSON = 'json'; |
|
160 | - |
|
161 | - /** |
|
162 | - * multipart: (array) Array of associative arrays, each containing a |
|
163 | - * required "name" key mapping to the form field, name, a required |
|
164 | - * "contents" key mapping to a StreamInterface|resource|string, an |
|
165 | - * optional "headers" associative array of custom headers, and an |
|
166 | - * optional "filename" key mapping to a string to send as the filename in |
|
167 | - * the part. If no "filename" key is present, then no "filename" attribute |
|
168 | - * will be added to the part. |
|
169 | - */ |
|
170 | - public const MULTIPART = 'multipart'; |
|
171 | - |
|
172 | - /** |
|
173 | - * on_headers: (callable) A callable that is invoked when the HTTP headers |
|
174 | - * of the response have been received but the body has not yet begun to |
|
175 | - * download. |
|
176 | - */ |
|
177 | - public const ON_HEADERS = 'on_headers'; |
|
178 | - |
|
179 | - /** |
|
180 | - * on_stats: (callable) allows you to get access to transfer statistics of |
|
181 | - * a request and access the lower level transfer details of the handler |
|
182 | - * associated with your client. ``on_stats`` is a callable that is invoked |
|
183 | - * when a handler has finished sending a request. The callback is invoked |
|
184 | - * with transfer statistics about the request, the response received, or |
|
185 | - * the error encountered. Included in the data is the total amount of time |
|
186 | - * taken to send the request. |
|
187 | - */ |
|
188 | - public const ON_STATS = 'on_stats'; |
|
189 | - |
|
190 | - /** |
|
191 | - * progress: (callable) Defines a function to invoke when transfer |
|
192 | - * progress is made. The function accepts the following positional |
|
193 | - * arguments: the total number of bytes expected to be downloaded, the |
|
194 | - * number of bytes downloaded so far, the number of bytes expected to be |
|
195 | - * uploaded, the number of bytes uploaded so far. |
|
196 | - */ |
|
197 | - public const PROGRESS = 'progress'; |
|
198 | - |
|
199 | - /** |
|
200 | - * proxy: (string|array) Pass a string to specify an HTTP proxy, or an |
|
201 | - * array to specify different proxies for different protocols (where the |
|
202 | - * key is the protocol and the value is a proxy string). |
|
203 | - */ |
|
204 | - public const PROXY = 'proxy'; |
|
205 | - |
|
206 | - /** |
|
207 | - * query: (array|string) Associative array of query string values to add |
|
208 | - * to the request. This option uses PHP's http_build_query() to create |
|
209 | - * the string representation. Pass a string value if you need more |
|
210 | - * control than what this method provides |
|
211 | - */ |
|
212 | - public const QUERY = 'query'; |
|
213 | - |
|
214 | - /** |
|
215 | - * sink: (resource|string|StreamInterface) Where the data of the |
|
216 | - * response is written to. Defaults to a PHP temp stream. Providing a |
|
217 | - * string will write data to a file by the given name. |
|
218 | - */ |
|
219 | - public const SINK = 'sink'; |
|
220 | - |
|
221 | - /** |
|
222 | - * synchronous: (bool) Set to true to inform HTTP handlers that you intend |
|
223 | - * on waiting on the response. This can be useful for optimizations. Note |
|
224 | - * that a promise is still returned if you are using one of the async |
|
225 | - * client methods. |
|
226 | - */ |
|
227 | - public const SYNCHRONOUS = 'synchronous'; |
|
228 | - |
|
229 | - /** |
|
230 | - * ssl_key: (array|string) Specify the path to a file containing a private |
|
231 | - * SSL key in PEM format. If a password is required, then set to an array |
|
232 | - * containing the path to the SSL key in the first array element followed |
|
233 | - * by the password required for the certificate in the second element. |
|
234 | - */ |
|
235 | - public const SSL_KEY = 'ssl_key'; |
|
236 | - |
|
237 | - /** |
|
238 | - * stream: Set to true to attempt to stream a response rather than |
|
239 | - * download it all up-front. |
|
240 | - */ |
|
241 | - public const STREAM = 'stream'; |
|
242 | - |
|
243 | - /** |
|
244 | - * verify: (bool|string, default=true) Describes the SSL certificate |
|
245 | - * verification behavior of a request. Set to true to enable SSL |
|
246 | - * certificate verification using the system CA bundle when available |
|
247 | - * (the default). Set to false to disable certificate verification (this |
|
248 | - * is insecure!). Set to a string to provide the path to a CA bundle on |
|
249 | - * disk to enable verification using a custom certificate. |
|
250 | - */ |
|
251 | - public const VERIFY = 'verify'; |
|
252 | - |
|
253 | - /** |
|
254 | - * timeout: (float, default=0) Float describing the timeout of the |
|
255 | - * request in seconds. Use 0 to wait indefinitely (the default behavior). |
|
256 | - */ |
|
257 | - public const TIMEOUT = 'timeout'; |
|
258 | - |
|
259 | - /** |
|
260 | - * read_timeout: (float, default=default_socket_timeout ini setting) Float describing |
|
261 | - * the body read timeout, for stream requests. |
|
262 | - */ |
|
263 | - public const READ_TIMEOUT = 'read_timeout'; |
|
264 | - |
|
265 | - /** |
|
266 | - * version: (float) Specifies the HTTP protocol version to attempt to use. |
|
267 | - */ |
|
268 | - public const VERSION = 'version'; |
|
269 | - |
|
270 | - /** |
|
271 | - * force_ip_resolve: (bool) Force client to use only ipv4 or ipv6 protocol |
|
272 | - */ |
|
273 | - public const FORCE_IP_RESOLVE = 'force_ip_resolve'; |
|
12 | + /** |
|
13 | + * allow_redirects: (bool|array) Controls redirect behavior. Pass false |
|
14 | + * to disable redirects, pass true to enable redirects, pass an |
|
15 | + * associative to provide custom redirect settings. Defaults to "false". |
|
16 | + * This option only works if your handler has the RedirectMiddleware. When |
|
17 | + * passing an associative array, you can provide the following key value |
|
18 | + * pairs: |
|
19 | + * |
|
20 | + * - max: (int, default=5) maximum number of allowed redirects. |
|
21 | + * - strict: (bool, default=false) Set to true to use strict redirects |
|
22 | + * meaning redirect POST requests with POST requests vs. doing what most |
|
23 | + * browsers do which is redirect POST requests with GET requests |
|
24 | + * - referer: (bool, default=false) Set to true to enable the Referer |
|
25 | + * header. |
|
26 | + * - protocols: (array, default=['http', 'https']) Allowed redirect |
|
27 | + * protocols. |
|
28 | + * - on_redirect: (callable) PHP callable that is invoked when a redirect |
|
29 | + * is encountered. The callable is invoked with the request, the redirect |
|
30 | + * response that was received, and the effective URI. Any return value |
|
31 | + * from the on_redirect function is ignored. |
|
32 | + */ |
|
33 | + public const ALLOW_REDIRECTS = 'allow_redirects'; |
|
34 | + |
|
35 | + /** |
|
36 | + * auth: (array) Pass an array of HTTP authentication parameters to use |
|
37 | + * with the request. The array must contain the username in index [0], |
|
38 | + * the password in index [1], and you can optionally provide a built-in |
|
39 | + * authentication type in index [2]. Pass null to disable authentication |
|
40 | + * for a request. |
|
41 | + */ |
|
42 | + public const AUTH = 'auth'; |
|
43 | + |
|
44 | + /** |
|
45 | + * body: (resource|string|null|int|float|StreamInterface|callable|\Iterator) |
|
46 | + * Body to send in the request. |
|
47 | + */ |
|
48 | + public const BODY = 'body'; |
|
49 | + |
|
50 | + /** |
|
51 | + * cert: (string|array) Set to a string to specify the path to a file |
|
52 | + * containing a PEM formatted SSL client side certificate. If a password |
|
53 | + * is required, then set cert to an array containing the path to the PEM |
|
54 | + * file in the first array element followed by the certificate password |
|
55 | + * in the second array element. |
|
56 | + */ |
|
57 | + public const CERT = 'cert'; |
|
58 | + |
|
59 | + /** |
|
60 | + * cookies: (bool|OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface, default=false) |
|
61 | + * Specifies whether or not cookies are used in a request or what cookie |
|
62 | + * jar to use or what cookies to send. This option only works if your |
|
63 | + * handler has the `cookie` middleware. Valid values are `false` and |
|
64 | + * an instance of {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface}. |
|
65 | + */ |
|
66 | + public const COOKIES = 'cookies'; |
|
67 | + |
|
68 | + /** |
|
69 | + * connect_timeout: (float, default=0) Float describing the number of |
|
70 | + * seconds to wait while trying to connect to a server. Use 0 to wait |
|
71 | + * 300 seconds (the default behavior). |
|
72 | + */ |
|
73 | + public const CONNECT_TIMEOUT = 'connect_timeout'; |
|
74 | + |
|
75 | + /** |
|
76 | + * crypto_method: (int) A value describing the minimum TLS protocol |
|
77 | + * version to use. |
|
78 | + * |
|
79 | + * This setting must be set to one of the |
|
80 | + * ``STREAM_CRYPTO_METHOD_TLS*_CLIENT`` constants. PHP 7.4 or higher is |
|
81 | + * required in order to use TLS 1.3, and cURL 7.34.0 or higher is required |
|
82 | + * in order to specify a crypto method, with cURL 7.52.0 or higher being |
|
83 | + * required to use TLS 1.3. |
|
84 | + */ |
|
85 | + public const CRYPTO_METHOD = 'crypto_method'; |
|
86 | + |
|
87 | + /** |
|
88 | + * debug: (bool|resource) Set to true or set to a PHP stream returned by |
|
89 | + * fopen() enable debug output with the HTTP handler used to send a |
|
90 | + * request. |
|
91 | + */ |
|
92 | + public const DEBUG = 'debug'; |
|
93 | + |
|
94 | + /** |
|
95 | + * decode_content: (bool, default=true) Specify whether or not |
|
96 | + * Content-Encoding responses (gzip, deflate, etc.) are automatically |
|
97 | + * decoded. |
|
98 | + */ |
|
99 | + public const DECODE_CONTENT = 'decode_content'; |
|
100 | + |
|
101 | + /** |
|
102 | + * delay: (int) The amount of time to delay before sending in milliseconds. |
|
103 | + */ |
|
104 | + public const DELAY = 'delay'; |
|
105 | + |
|
106 | + /** |
|
107 | + * expect: (bool|integer) Controls the behavior of the |
|
108 | + * "Expect: 100-Continue" header. |
|
109 | + * |
|
110 | + * Set to `true` to enable the "Expect: 100-Continue" header for all |
|
111 | + * requests that sends a body. Set to `false` to disable the |
|
112 | + * "Expect: 100-Continue" header for all requests. Set to a number so that |
|
113 | + * the size of the payload must be greater than the number in order to send |
|
114 | + * the Expect header. Setting to a number will send the Expect header for |
|
115 | + * all requests in which the size of the payload cannot be determined or |
|
116 | + * where the body is not rewindable. |
|
117 | + * |
|
118 | + * By default, Guzzle will add the "Expect: 100-Continue" header when the |
|
119 | + * size of the body of a request is greater than 1 MB and a request is |
|
120 | + * using HTTP/1.1. |
|
121 | + */ |
|
122 | + public const EXPECT = 'expect'; |
|
123 | + |
|
124 | + /** |
|
125 | + * form_params: (array) Associative array of form field names to values |
|
126 | + * where each value is a string or array of strings. Sets the Content-Type |
|
127 | + * header to application/x-www-form-urlencoded when no Content-Type header |
|
128 | + * is already present. |
|
129 | + */ |
|
130 | + public const FORM_PARAMS = 'form_params'; |
|
131 | + |
|
132 | + /** |
|
133 | + * headers: (array) Associative array of HTTP headers. Each value MUST be |
|
134 | + * a string or array of strings. |
|
135 | + */ |
|
136 | + public const HEADERS = 'headers'; |
|
137 | + |
|
138 | + /** |
|
139 | + * http_errors: (bool, default=true) Set to false to disable exceptions |
|
140 | + * when a non- successful HTTP response is received. By default, |
|
141 | + * exceptions will be thrown for 4xx and 5xx responses. This option only |
|
142 | + * works if your handler has the `httpErrors` middleware. |
|
143 | + */ |
|
144 | + public const HTTP_ERRORS = 'http_errors'; |
|
145 | + |
|
146 | + /** |
|
147 | + * idn: (bool|int, default=true) A combination of IDNA_* constants for |
|
148 | + * idn_to_ascii() PHP's function (see "options" parameter). Set to false to |
|
149 | + * disable IDN support completely, or to true to use the default |
|
150 | + * configuration (IDNA_DEFAULT constant). |
|
151 | + */ |
|
152 | + public const IDN_CONVERSION = 'idn_conversion'; |
|
153 | + |
|
154 | + /** |
|
155 | + * json: (mixed) Adds JSON data to a request. The provided value is JSON |
|
156 | + * encoded and a Content-Type header of application/json will be added to |
|
157 | + * the request if no Content-Type header is already present. |
|
158 | + */ |
|
159 | + public const JSON = 'json'; |
|
160 | + |
|
161 | + /** |
|
162 | + * multipart: (array) Array of associative arrays, each containing a |
|
163 | + * required "name" key mapping to the form field, name, a required |
|
164 | + * "contents" key mapping to a StreamInterface|resource|string, an |
|
165 | + * optional "headers" associative array of custom headers, and an |
|
166 | + * optional "filename" key mapping to a string to send as the filename in |
|
167 | + * the part. If no "filename" key is present, then no "filename" attribute |
|
168 | + * will be added to the part. |
|
169 | + */ |
|
170 | + public const MULTIPART = 'multipart'; |
|
171 | + |
|
172 | + /** |
|
173 | + * on_headers: (callable) A callable that is invoked when the HTTP headers |
|
174 | + * of the response have been received but the body has not yet begun to |
|
175 | + * download. |
|
176 | + */ |
|
177 | + public const ON_HEADERS = 'on_headers'; |
|
178 | + |
|
179 | + /** |
|
180 | + * on_stats: (callable) allows you to get access to transfer statistics of |
|
181 | + * a request and access the lower level transfer details of the handler |
|
182 | + * associated with your client. ``on_stats`` is a callable that is invoked |
|
183 | + * when a handler has finished sending a request. The callback is invoked |
|
184 | + * with transfer statistics about the request, the response received, or |
|
185 | + * the error encountered. Included in the data is the total amount of time |
|
186 | + * taken to send the request. |
|
187 | + */ |
|
188 | + public const ON_STATS = 'on_stats'; |
|
189 | + |
|
190 | + /** |
|
191 | + * progress: (callable) Defines a function to invoke when transfer |
|
192 | + * progress is made. The function accepts the following positional |
|
193 | + * arguments: the total number of bytes expected to be downloaded, the |
|
194 | + * number of bytes downloaded so far, the number of bytes expected to be |
|
195 | + * uploaded, the number of bytes uploaded so far. |
|
196 | + */ |
|
197 | + public const PROGRESS = 'progress'; |
|
198 | + |
|
199 | + /** |
|
200 | + * proxy: (string|array) Pass a string to specify an HTTP proxy, or an |
|
201 | + * array to specify different proxies for different protocols (where the |
|
202 | + * key is the protocol and the value is a proxy string). |
|
203 | + */ |
|
204 | + public const PROXY = 'proxy'; |
|
205 | + |
|
206 | + /** |
|
207 | + * query: (array|string) Associative array of query string values to add |
|
208 | + * to the request. This option uses PHP's http_build_query() to create |
|
209 | + * the string representation. Pass a string value if you need more |
|
210 | + * control than what this method provides |
|
211 | + */ |
|
212 | + public const QUERY = 'query'; |
|
213 | + |
|
214 | + /** |
|
215 | + * sink: (resource|string|StreamInterface) Where the data of the |
|
216 | + * response is written to. Defaults to a PHP temp stream. Providing a |
|
217 | + * string will write data to a file by the given name. |
|
218 | + */ |
|
219 | + public const SINK = 'sink'; |
|
220 | + |
|
221 | + /** |
|
222 | + * synchronous: (bool) Set to true to inform HTTP handlers that you intend |
|
223 | + * on waiting on the response. This can be useful for optimizations. Note |
|
224 | + * that a promise is still returned if you are using one of the async |
|
225 | + * client methods. |
|
226 | + */ |
|
227 | + public const SYNCHRONOUS = 'synchronous'; |
|
228 | + |
|
229 | + /** |
|
230 | + * ssl_key: (array|string) Specify the path to a file containing a private |
|
231 | + * SSL key in PEM format. If a password is required, then set to an array |
|
232 | + * containing the path to the SSL key in the first array element followed |
|
233 | + * by the password required for the certificate in the second element. |
|
234 | + */ |
|
235 | + public const SSL_KEY = 'ssl_key'; |
|
236 | + |
|
237 | + /** |
|
238 | + * stream: Set to true to attempt to stream a response rather than |
|
239 | + * download it all up-front. |
|
240 | + */ |
|
241 | + public const STREAM = 'stream'; |
|
242 | + |
|
243 | + /** |
|
244 | + * verify: (bool|string, default=true) Describes the SSL certificate |
|
245 | + * verification behavior of a request. Set to true to enable SSL |
|
246 | + * certificate verification using the system CA bundle when available |
|
247 | + * (the default). Set to false to disable certificate verification (this |
|
248 | + * is insecure!). Set to a string to provide the path to a CA bundle on |
|
249 | + * disk to enable verification using a custom certificate. |
|
250 | + */ |
|
251 | + public const VERIFY = 'verify'; |
|
252 | + |
|
253 | + /** |
|
254 | + * timeout: (float, default=0) Float describing the timeout of the |
|
255 | + * request in seconds. Use 0 to wait indefinitely (the default behavior). |
|
256 | + */ |
|
257 | + public const TIMEOUT = 'timeout'; |
|
258 | + |
|
259 | + /** |
|
260 | + * read_timeout: (float, default=default_socket_timeout ini setting) Float describing |
|
261 | + * the body read timeout, for stream requests. |
|
262 | + */ |
|
263 | + public const READ_TIMEOUT = 'read_timeout'; |
|
264 | + |
|
265 | + /** |
|
266 | + * version: (float) Specifies the HTTP protocol version to attempt to use. |
|
267 | + */ |
|
268 | + public const VERSION = 'version'; |
|
269 | + |
|
270 | + /** |
|
271 | + * force_ip_resolve: (bool) Force client to use only ipv4 or ipv6 protocol |
|
272 | + */ |
|
273 | + public const FORCE_IP_RESOLVE = 'force_ip_resolve'; |
|
274 | 274 | } |
@@ -7,8 +7,7 @@ |
||
7 | 7 | * |
8 | 8 | * @see https://docs.guzzlephp.org/en/latest/request-options.html |
9 | 9 | */ |
10 | -final class RequestOptions |
|
11 | -{ |
|
10 | +final class RequestOptions { |
|
12 | 11 | /** |
13 | 12 | * allow_redirects: (bool|array) Controls redirect behavior. Pass false |
14 | 13 | * to disable redirects, pass true to enable redirects, pass an |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | */ |
16 | 16 | function describe_type($input): string |
17 | 17 | { |
18 | - return Utils::describeType($input); |
|
18 | + return Utils::describeType($input); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | function headers_from_lines(iterable $lines): array |
30 | 30 | { |
31 | - return Utils::headersFromLines($lines); |
|
31 | + return Utils::headersFromLines($lines); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | function debug_resource($value = null) |
44 | 44 | { |
45 | - return Utils::debugResource($value); |
|
45 | + return Utils::debugResource($value); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | */ |
59 | 59 | function choose_handler(): callable |
60 | 60 | { |
61 | - return Utils::chooseHandler(); |
|
61 | + return Utils::chooseHandler(); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | function default_user_agent(): string |
70 | 70 | { |
71 | - return Utils::defaultUserAgent(); |
|
71 | + return Utils::defaultUserAgent(); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | */ |
89 | 89 | function default_ca_bundle(): string |
90 | 90 | { |
91 | - return Utils::defaultCaBundle(); |
|
91 | + return Utils::defaultCaBundle(); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | function normalize_header_keys(array $headers): array |
101 | 101 | { |
102 | - return Utils::normalizeHeaderKeys($headers); |
|
102 | + return Utils::normalizeHeaderKeys($headers); |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | /** |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | function is_host_in_noproxy(string $host, array $noProxyArray): bool |
127 | 127 | { |
128 | - return Utils::isHostInNoProxy($host, $noProxyArray); |
|
128 | + return Utils::isHostInNoProxy($host, $noProxyArray); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | */ |
147 | 147 | function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
148 | 148 | { |
149 | - return Utils::jsonDecode($json, $assoc, $depth, $options); |
|
149 | + return Utils::jsonDecode($json, $assoc, $depth, $options); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -163,5 +163,5 @@ discard block |
||
163 | 163 | */ |
164 | 164 | function json_encode($value, int $options = 0, int $depth = 512): string |
165 | 165 | { |
166 | - return Utils::jsonEncode($value, $options, $depth); |
|
166 | + return Utils::jsonEncode($value, $options, $depth); |
|
167 | 167 | } |
@@ -7,482 +7,482 @@ |
||
7 | 7 | */ |
8 | 8 | class SetCookie |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - private static $defaults = [ |
|
14 | - 'Name' => null, |
|
15 | - 'Value' => null, |
|
16 | - 'Domain' => null, |
|
17 | - 'Path' => '/', |
|
18 | - 'Max-Age' => null, |
|
19 | - 'Expires' => null, |
|
20 | - 'Secure' => false, |
|
21 | - 'Discard' => false, |
|
22 | - 'HttpOnly' => false, |
|
23 | - ]; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array Cookie data |
|
27 | - */ |
|
28 | - private $data; |
|
29 | - |
|
30 | - /** |
|
31 | - * Create a new SetCookie object from a string. |
|
32 | - * |
|
33 | - * @param string $cookie Set-Cookie header string |
|
34 | - */ |
|
35 | - public static function fromString(string $cookie): self |
|
36 | - { |
|
37 | - // Create the default return array |
|
38 | - $data = self::$defaults; |
|
39 | - // Explode the cookie string using a series of semicolons |
|
40 | - $pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
|
41 | - // The name of the cookie (first kvp) must exist and include an equal sign. |
|
42 | - if (!isset($pieces[0]) || \strpos($pieces[0], '=') === false) { |
|
43 | - return new self($data); |
|
44 | - } |
|
45 | - |
|
46 | - // Add the cookie pieces into the parsed data array |
|
47 | - foreach ($pieces as $part) { |
|
48 | - $cookieParts = \explode('=', $part, 2); |
|
49 | - $key = \trim($cookieParts[0]); |
|
50 | - $value = isset($cookieParts[1]) |
|
51 | - ? \trim($cookieParts[1], " \n\r\t\0\x0B") |
|
52 | - : true; |
|
53 | - |
|
54 | - // Only check for non-cookies when cookies have been found |
|
55 | - if (!isset($data['Name'])) { |
|
56 | - $data['Name'] = $key; |
|
57 | - $data['Value'] = $value; |
|
58 | - } else { |
|
59 | - foreach (\array_keys(self::$defaults) as $search) { |
|
60 | - if (!\strcasecmp($search, $key)) { |
|
61 | - if ($search === 'Max-Age') { |
|
62 | - if (is_numeric($value)) { |
|
63 | - $data[$search] = (int) $value; |
|
64 | - } |
|
65 | - } else { |
|
66 | - $data[$search] = $value; |
|
67 | - } |
|
68 | - continue 2; |
|
69 | - } |
|
70 | - } |
|
71 | - $data[$key] = $value; |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - return new self($data); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param array $data Array of cookie data provided by a Cookie parser |
|
80 | - */ |
|
81 | - public function __construct(array $data = []) |
|
82 | - { |
|
83 | - $this->data = self::$defaults; |
|
84 | - |
|
85 | - if (isset($data['Name'])) { |
|
86 | - $this->setName($data['Name']); |
|
87 | - } |
|
88 | - |
|
89 | - if (isset($data['Value'])) { |
|
90 | - $this->setValue($data['Value']); |
|
91 | - } |
|
92 | - |
|
93 | - if (isset($data['Domain'])) { |
|
94 | - $this->setDomain($data['Domain']); |
|
95 | - } |
|
96 | - |
|
97 | - if (isset($data['Path'])) { |
|
98 | - $this->setPath($data['Path']); |
|
99 | - } |
|
100 | - |
|
101 | - if (isset($data['Max-Age'])) { |
|
102 | - $this->setMaxAge($data['Max-Age']); |
|
103 | - } |
|
104 | - |
|
105 | - if (isset($data['Expires'])) { |
|
106 | - $this->setExpires($data['Expires']); |
|
107 | - } |
|
108 | - |
|
109 | - if (isset($data['Secure'])) { |
|
110 | - $this->setSecure($data['Secure']); |
|
111 | - } |
|
112 | - |
|
113 | - if (isset($data['Discard'])) { |
|
114 | - $this->setDiscard($data['Discard']); |
|
115 | - } |
|
116 | - |
|
117 | - if (isset($data['HttpOnly'])) { |
|
118 | - $this->setHttpOnly($data['HttpOnly']); |
|
119 | - } |
|
120 | - |
|
121 | - // Set the remaining values that don't have extra validation logic |
|
122 | - foreach (array_diff(array_keys($data), array_keys(self::$defaults)) as $key) { |
|
123 | - $this->data[$key] = $data[$key]; |
|
124 | - } |
|
125 | - |
|
126 | - // Extract the Expires value and turn it into a UNIX timestamp if needed |
|
127 | - if (!$this->getExpires() && $this->getMaxAge()) { |
|
128 | - // Calculate the Expires date |
|
129 | - $this->setExpires(\time() + $this->getMaxAge()); |
|
130 | - } elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
|
131 | - $this->setExpires($expires); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - public function __toString() |
|
136 | - { |
|
137 | - $str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; '; |
|
138 | - foreach ($this->data as $k => $v) { |
|
139 | - if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { |
|
140 | - if ($k === 'Expires') { |
|
141 | - $str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; '; |
|
142 | - } else { |
|
143 | - $str .= ($v === true ? $k : "{$k}={$v}").'; '; |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - return \rtrim($str, '; '); |
|
149 | - } |
|
150 | - |
|
151 | - public function toArray(): array |
|
152 | - { |
|
153 | - return $this->data; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the cookie name. |
|
158 | - * |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function getName() |
|
162 | - { |
|
163 | - return $this->data['Name']; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Set the cookie name. |
|
168 | - * |
|
169 | - * @param string $name Cookie name |
|
170 | - */ |
|
171 | - public function setName($name): void |
|
172 | - { |
|
173 | - if (!is_string($name)) { |
|
174 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
175 | - } |
|
176 | - |
|
177 | - $this->data['Name'] = (string) $name; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Get the cookie value. |
|
182 | - * |
|
183 | - * @return string|null |
|
184 | - */ |
|
185 | - public function getValue() |
|
186 | - { |
|
187 | - return $this->data['Value']; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Set the cookie value. |
|
192 | - * |
|
193 | - * @param string $value Cookie value |
|
194 | - */ |
|
195 | - public function setValue($value): void |
|
196 | - { |
|
197 | - if (!is_string($value)) { |
|
198 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
199 | - } |
|
200 | - |
|
201 | - $this->data['Value'] = (string) $value; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Get the domain. |
|
206 | - * |
|
207 | - * @return string|null |
|
208 | - */ |
|
209 | - public function getDomain() |
|
210 | - { |
|
211 | - return $this->data['Domain']; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Set the domain of the cookie. |
|
216 | - * |
|
217 | - * @param string|null $domain |
|
218 | - */ |
|
219 | - public function setDomain($domain): void |
|
220 | - { |
|
221 | - if (!is_string($domain) && null !== $domain) { |
|
222 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
223 | - } |
|
224 | - |
|
225 | - $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Get the path. |
|
230 | - * |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - public function getPath() |
|
234 | - { |
|
235 | - return $this->data['Path']; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Set the path of the cookie. |
|
240 | - * |
|
241 | - * @param string $path Path of the cookie |
|
242 | - */ |
|
243 | - public function setPath($path): void |
|
244 | - { |
|
245 | - if (!is_string($path)) { |
|
246 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
247 | - } |
|
248 | - |
|
249 | - $this->data['Path'] = (string) $path; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Maximum lifetime of the cookie in seconds. |
|
254 | - * |
|
255 | - * @return int|null |
|
256 | - */ |
|
257 | - public function getMaxAge() |
|
258 | - { |
|
259 | - return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Set the max-age of the cookie. |
|
264 | - * |
|
265 | - * @param int|null $maxAge Max age of the cookie in seconds |
|
266 | - */ |
|
267 | - public function setMaxAge($maxAge): void |
|
268 | - { |
|
269 | - if (!is_int($maxAge) && null !== $maxAge) { |
|
270 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
271 | - } |
|
272 | - |
|
273 | - $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * The UNIX timestamp when the cookie Expires. |
|
278 | - * |
|
279 | - * @return string|int|null |
|
280 | - */ |
|
281 | - public function getExpires() |
|
282 | - { |
|
283 | - return $this->data['Expires']; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Set the unix timestamp for which the cookie will expire. |
|
288 | - * |
|
289 | - * @param int|string|null $timestamp Unix timestamp or any English textual datetime description. |
|
290 | - */ |
|
291 | - public function setExpires($timestamp): void |
|
292 | - { |
|
293 | - if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) { |
|
294 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
295 | - } |
|
296 | - |
|
297 | - $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Get whether or not this is a secure cookie. |
|
302 | - * |
|
303 | - * @return bool |
|
304 | - */ |
|
305 | - public function getSecure() |
|
306 | - { |
|
307 | - return $this->data['Secure']; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Set whether or not the cookie is secure. |
|
312 | - * |
|
313 | - * @param bool $secure Set to true or false if secure |
|
314 | - */ |
|
315 | - public function setSecure($secure): void |
|
316 | - { |
|
317 | - if (!is_bool($secure)) { |
|
318 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
319 | - } |
|
320 | - |
|
321 | - $this->data['Secure'] = (bool) $secure; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Get whether or not this is a session cookie. |
|
326 | - * |
|
327 | - * @return bool|null |
|
328 | - */ |
|
329 | - public function getDiscard() |
|
330 | - { |
|
331 | - return $this->data['Discard']; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Set whether or not this is a session cookie. |
|
336 | - * |
|
337 | - * @param bool $discard Set to true or false if this is a session cookie |
|
338 | - */ |
|
339 | - public function setDiscard($discard): void |
|
340 | - { |
|
341 | - if (!is_bool($discard)) { |
|
342 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
343 | - } |
|
344 | - |
|
345 | - $this->data['Discard'] = (bool) $discard; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Get whether or not this is an HTTP only cookie. |
|
350 | - * |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - public function getHttpOnly() |
|
354 | - { |
|
355 | - return $this->data['HttpOnly']; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Set whether or not this is an HTTP only cookie. |
|
360 | - * |
|
361 | - * @param bool $httpOnly Set to true or false if this is HTTP only |
|
362 | - */ |
|
363 | - public function setHttpOnly($httpOnly): void |
|
364 | - { |
|
365 | - if (!is_bool($httpOnly)) { |
|
366 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
367 | - } |
|
368 | - |
|
369 | - $this->data['HttpOnly'] = (bool) $httpOnly; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Check if the cookie matches a path value. |
|
374 | - * |
|
375 | - * A request-path path-matches a given cookie-path if at least one of |
|
376 | - * the following conditions holds: |
|
377 | - * |
|
378 | - * - The cookie-path and the request-path are identical. |
|
379 | - * - The cookie-path is a prefix of the request-path, and the last |
|
380 | - * character of the cookie-path is %x2F ("/"). |
|
381 | - * - The cookie-path is a prefix of the request-path, and the first |
|
382 | - * character of the request-path that is not included in the cookie- |
|
383 | - * path is a %x2F ("/") character. |
|
384 | - * |
|
385 | - * @param string $requestPath Path to check against |
|
386 | - */ |
|
387 | - public function matchesPath(string $requestPath): bool |
|
388 | - { |
|
389 | - $cookiePath = $this->getPath(); |
|
390 | - |
|
391 | - // Match on exact matches or when path is the default empty "/" |
|
392 | - if ($cookiePath === '/' || $cookiePath == $requestPath) { |
|
393 | - return true; |
|
394 | - } |
|
395 | - |
|
396 | - // Ensure that the cookie-path is a prefix of the request path. |
|
397 | - if (0 !== \strpos($requestPath, $cookiePath)) { |
|
398 | - return false; |
|
399 | - } |
|
400 | - |
|
401 | - // Match if the last character of the cookie-path is "/" |
|
402 | - if (\substr($cookiePath, -1, 1) === '/') { |
|
403 | - return true; |
|
404 | - } |
|
405 | - |
|
406 | - // Match if the first character not included in cookie path is "/" |
|
407 | - return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Check if the cookie matches a domain value. |
|
412 | - * |
|
413 | - * @param string $domain Domain to check against |
|
414 | - */ |
|
415 | - public function matchesDomain(string $domain): bool |
|
416 | - { |
|
417 | - $cookieDomain = $this->getDomain(); |
|
418 | - if (null === $cookieDomain) { |
|
419 | - return true; |
|
420 | - } |
|
421 | - |
|
422 | - // Remove the leading '.' as per spec in RFC 6265. |
|
423 | - // https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3 |
|
424 | - $cookieDomain = \ltrim(\strtolower($cookieDomain), '.'); |
|
425 | - |
|
426 | - $domain = \strtolower($domain); |
|
427 | - |
|
428 | - // Domain not set or exact match. |
|
429 | - if ('' === $cookieDomain || $domain === $cookieDomain) { |
|
430 | - return true; |
|
431 | - } |
|
432 | - |
|
433 | - // Matching the subdomain according to RFC 6265. |
|
434 | - // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 |
|
435 | - if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
|
436 | - return false; |
|
437 | - } |
|
438 | - |
|
439 | - return (bool) \preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Check if the cookie is expired. |
|
444 | - */ |
|
445 | - public function isExpired(): bool |
|
446 | - { |
|
447 | - return $this->getExpires() !== null && \time() > $this->getExpires(); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Check if the cookie is valid according to RFC 6265. |
|
452 | - * |
|
453 | - * @return bool|string Returns true if valid or an error message if invalid |
|
454 | - */ |
|
455 | - public function validate() |
|
456 | - { |
|
457 | - $name = $this->getName(); |
|
458 | - if ($name === '') { |
|
459 | - return 'The cookie name must not be empty'; |
|
460 | - } |
|
461 | - |
|
462 | - // Check if any of the invalid characters are present in the cookie name |
|
463 | - if (\preg_match( |
|
464 | - '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/', |
|
465 | - $name |
|
466 | - )) { |
|
467 | - return 'Cookie name must not contain invalid characters: ASCII ' |
|
468 | - .'Control characters (0-31;127), space, tab and the ' |
|
469 | - .'following characters: ()<>@,;:\"/?={}'; |
|
470 | - } |
|
471 | - |
|
472 | - // Value must not be null. 0 and empty string are valid. Empty strings |
|
473 | - // are technically against RFC 6265, but known to happen in the wild. |
|
474 | - $value = $this->getValue(); |
|
475 | - if ($value === null) { |
|
476 | - return 'The cookie value must not be empty'; |
|
477 | - } |
|
478 | - |
|
479 | - // Domains must not be empty, but can be 0. "0" is not a valid internet |
|
480 | - // domain, but may be used as server name in a private network. |
|
481 | - $domain = $this->getDomain(); |
|
482 | - if ($domain === null || $domain === '') { |
|
483 | - return 'The cookie domain must not be empty'; |
|
484 | - } |
|
485 | - |
|
486 | - return true; |
|
487 | - } |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + private static $defaults = [ |
|
14 | + 'Name' => null, |
|
15 | + 'Value' => null, |
|
16 | + 'Domain' => null, |
|
17 | + 'Path' => '/', |
|
18 | + 'Max-Age' => null, |
|
19 | + 'Expires' => null, |
|
20 | + 'Secure' => false, |
|
21 | + 'Discard' => false, |
|
22 | + 'HttpOnly' => false, |
|
23 | + ]; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array Cookie data |
|
27 | + */ |
|
28 | + private $data; |
|
29 | + |
|
30 | + /** |
|
31 | + * Create a new SetCookie object from a string. |
|
32 | + * |
|
33 | + * @param string $cookie Set-Cookie header string |
|
34 | + */ |
|
35 | + public static function fromString(string $cookie): self |
|
36 | + { |
|
37 | + // Create the default return array |
|
38 | + $data = self::$defaults; |
|
39 | + // Explode the cookie string using a series of semicolons |
|
40 | + $pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
|
41 | + // The name of the cookie (first kvp) must exist and include an equal sign. |
|
42 | + if (!isset($pieces[0]) || \strpos($pieces[0], '=') === false) { |
|
43 | + return new self($data); |
|
44 | + } |
|
45 | + |
|
46 | + // Add the cookie pieces into the parsed data array |
|
47 | + foreach ($pieces as $part) { |
|
48 | + $cookieParts = \explode('=', $part, 2); |
|
49 | + $key = \trim($cookieParts[0]); |
|
50 | + $value = isset($cookieParts[1]) |
|
51 | + ? \trim($cookieParts[1], " \n\r\t\0\x0B") |
|
52 | + : true; |
|
53 | + |
|
54 | + // Only check for non-cookies when cookies have been found |
|
55 | + if (!isset($data['Name'])) { |
|
56 | + $data['Name'] = $key; |
|
57 | + $data['Value'] = $value; |
|
58 | + } else { |
|
59 | + foreach (\array_keys(self::$defaults) as $search) { |
|
60 | + if (!\strcasecmp($search, $key)) { |
|
61 | + if ($search === 'Max-Age') { |
|
62 | + if (is_numeric($value)) { |
|
63 | + $data[$search] = (int) $value; |
|
64 | + } |
|
65 | + } else { |
|
66 | + $data[$search] = $value; |
|
67 | + } |
|
68 | + continue 2; |
|
69 | + } |
|
70 | + } |
|
71 | + $data[$key] = $value; |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + return new self($data); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param array $data Array of cookie data provided by a Cookie parser |
|
80 | + */ |
|
81 | + public function __construct(array $data = []) |
|
82 | + { |
|
83 | + $this->data = self::$defaults; |
|
84 | + |
|
85 | + if (isset($data['Name'])) { |
|
86 | + $this->setName($data['Name']); |
|
87 | + } |
|
88 | + |
|
89 | + if (isset($data['Value'])) { |
|
90 | + $this->setValue($data['Value']); |
|
91 | + } |
|
92 | + |
|
93 | + if (isset($data['Domain'])) { |
|
94 | + $this->setDomain($data['Domain']); |
|
95 | + } |
|
96 | + |
|
97 | + if (isset($data['Path'])) { |
|
98 | + $this->setPath($data['Path']); |
|
99 | + } |
|
100 | + |
|
101 | + if (isset($data['Max-Age'])) { |
|
102 | + $this->setMaxAge($data['Max-Age']); |
|
103 | + } |
|
104 | + |
|
105 | + if (isset($data['Expires'])) { |
|
106 | + $this->setExpires($data['Expires']); |
|
107 | + } |
|
108 | + |
|
109 | + if (isset($data['Secure'])) { |
|
110 | + $this->setSecure($data['Secure']); |
|
111 | + } |
|
112 | + |
|
113 | + if (isset($data['Discard'])) { |
|
114 | + $this->setDiscard($data['Discard']); |
|
115 | + } |
|
116 | + |
|
117 | + if (isset($data['HttpOnly'])) { |
|
118 | + $this->setHttpOnly($data['HttpOnly']); |
|
119 | + } |
|
120 | + |
|
121 | + // Set the remaining values that don't have extra validation logic |
|
122 | + foreach (array_diff(array_keys($data), array_keys(self::$defaults)) as $key) { |
|
123 | + $this->data[$key] = $data[$key]; |
|
124 | + } |
|
125 | + |
|
126 | + // Extract the Expires value and turn it into a UNIX timestamp if needed |
|
127 | + if (!$this->getExpires() && $this->getMaxAge()) { |
|
128 | + // Calculate the Expires date |
|
129 | + $this->setExpires(\time() + $this->getMaxAge()); |
|
130 | + } elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
|
131 | + $this->setExpires($expires); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + public function __toString() |
|
136 | + { |
|
137 | + $str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; '; |
|
138 | + foreach ($this->data as $k => $v) { |
|
139 | + if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { |
|
140 | + if ($k === 'Expires') { |
|
141 | + $str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; '; |
|
142 | + } else { |
|
143 | + $str .= ($v === true ? $k : "{$k}={$v}").'; '; |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + return \rtrim($str, '; '); |
|
149 | + } |
|
150 | + |
|
151 | + public function toArray(): array |
|
152 | + { |
|
153 | + return $this->data; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the cookie name. |
|
158 | + * |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function getName() |
|
162 | + { |
|
163 | + return $this->data['Name']; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Set the cookie name. |
|
168 | + * |
|
169 | + * @param string $name Cookie name |
|
170 | + */ |
|
171 | + public function setName($name): void |
|
172 | + { |
|
173 | + if (!is_string($name)) { |
|
174 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
175 | + } |
|
176 | + |
|
177 | + $this->data['Name'] = (string) $name; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Get the cookie value. |
|
182 | + * |
|
183 | + * @return string|null |
|
184 | + */ |
|
185 | + public function getValue() |
|
186 | + { |
|
187 | + return $this->data['Value']; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Set the cookie value. |
|
192 | + * |
|
193 | + * @param string $value Cookie value |
|
194 | + */ |
|
195 | + public function setValue($value): void |
|
196 | + { |
|
197 | + if (!is_string($value)) { |
|
198 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
199 | + } |
|
200 | + |
|
201 | + $this->data['Value'] = (string) $value; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Get the domain. |
|
206 | + * |
|
207 | + * @return string|null |
|
208 | + */ |
|
209 | + public function getDomain() |
|
210 | + { |
|
211 | + return $this->data['Domain']; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Set the domain of the cookie. |
|
216 | + * |
|
217 | + * @param string|null $domain |
|
218 | + */ |
|
219 | + public function setDomain($domain): void |
|
220 | + { |
|
221 | + if (!is_string($domain) && null !== $domain) { |
|
222 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
223 | + } |
|
224 | + |
|
225 | + $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Get the path. |
|
230 | + * |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + public function getPath() |
|
234 | + { |
|
235 | + return $this->data['Path']; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Set the path of the cookie. |
|
240 | + * |
|
241 | + * @param string $path Path of the cookie |
|
242 | + */ |
|
243 | + public function setPath($path): void |
|
244 | + { |
|
245 | + if (!is_string($path)) { |
|
246 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
247 | + } |
|
248 | + |
|
249 | + $this->data['Path'] = (string) $path; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Maximum lifetime of the cookie in seconds. |
|
254 | + * |
|
255 | + * @return int|null |
|
256 | + */ |
|
257 | + public function getMaxAge() |
|
258 | + { |
|
259 | + return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Set the max-age of the cookie. |
|
264 | + * |
|
265 | + * @param int|null $maxAge Max age of the cookie in seconds |
|
266 | + */ |
|
267 | + public function setMaxAge($maxAge): void |
|
268 | + { |
|
269 | + if (!is_int($maxAge) && null !== $maxAge) { |
|
270 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
271 | + } |
|
272 | + |
|
273 | + $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * The UNIX timestamp when the cookie Expires. |
|
278 | + * |
|
279 | + * @return string|int|null |
|
280 | + */ |
|
281 | + public function getExpires() |
|
282 | + { |
|
283 | + return $this->data['Expires']; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Set the unix timestamp for which the cookie will expire. |
|
288 | + * |
|
289 | + * @param int|string|null $timestamp Unix timestamp or any English textual datetime description. |
|
290 | + */ |
|
291 | + public function setExpires($timestamp): void |
|
292 | + { |
|
293 | + if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) { |
|
294 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
295 | + } |
|
296 | + |
|
297 | + $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Get whether or not this is a secure cookie. |
|
302 | + * |
|
303 | + * @return bool |
|
304 | + */ |
|
305 | + public function getSecure() |
|
306 | + { |
|
307 | + return $this->data['Secure']; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Set whether or not the cookie is secure. |
|
312 | + * |
|
313 | + * @param bool $secure Set to true or false if secure |
|
314 | + */ |
|
315 | + public function setSecure($secure): void |
|
316 | + { |
|
317 | + if (!is_bool($secure)) { |
|
318 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
319 | + } |
|
320 | + |
|
321 | + $this->data['Secure'] = (bool) $secure; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Get whether or not this is a session cookie. |
|
326 | + * |
|
327 | + * @return bool|null |
|
328 | + */ |
|
329 | + public function getDiscard() |
|
330 | + { |
|
331 | + return $this->data['Discard']; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Set whether or not this is a session cookie. |
|
336 | + * |
|
337 | + * @param bool $discard Set to true or false if this is a session cookie |
|
338 | + */ |
|
339 | + public function setDiscard($discard): void |
|
340 | + { |
|
341 | + if (!is_bool($discard)) { |
|
342 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
343 | + } |
|
344 | + |
|
345 | + $this->data['Discard'] = (bool) $discard; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Get whether or not this is an HTTP only cookie. |
|
350 | + * |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + public function getHttpOnly() |
|
354 | + { |
|
355 | + return $this->data['HttpOnly']; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Set whether or not this is an HTTP only cookie. |
|
360 | + * |
|
361 | + * @param bool $httpOnly Set to true or false if this is HTTP only |
|
362 | + */ |
|
363 | + public function setHttpOnly($httpOnly): void |
|
364 | + { |
|
365 | + if (!is_bool($httpOnly)) { |
|
366 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
367 | + } |
|
368 | + |
|
369 | + $this->data['HttpOnly'] = (bool) $httpOnly; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Check if the cookie matches a path value. |
|
374 | + * |
|
375 | + * A request-path path-matches a given cookie-path if at least one of |
|
376 | + * the following conditions holds: |
|
377 | + * |
|
378 | + * - The cookie-path and the request-path are identical. |
|
379 | + * - The cookie-path is a prefix of the request-path, and the last |
|
380 | + * character of the cookie-path is %x2F ("/"). |
|
381 | + * - The cookie-path is a prefix of the request-path, and the first |
|
382 | + * character of the request-path that is not included in the cookie- |
|
383 | + * path is a %x2F ("/") character. |
|
384 | + * |
|
385 | + * @param string $requestPath Path to check against |
|
386 | + */ |
|
387 | + public function matchesPath(string $requestPath): bool |
|
388 | + { |
|
389 | + $cookiePath = $this->getPath(); |
|
390 | + |
|
391 | + // Match on exact matches or when path is the default empty "/" |
|
392 | + if ($cookiePath === '/' || $cookiePath == $requestPath) { |
|
393 | + return true; |
|
394 | + } |
|
395 | + |
|
396 | + // Ensure that the cookie-path is a prefix of the request path. |
|
397 | + if (0 !== \strpos($requestPath, $cookiePath)) { |
|
398 | + return false; |
|
399 | + } |
|
400 | + |
|
401 | + // Match if the last character of the cookie-path is "/" |
|
402 | + if (\substr($cookiePath, -1, 1) === '/') { |
|
403 | + return true; |
|
404 | + } |
|
405 | + |
|
406 | + // Match if the first character not included in cookie path is "/" |
|
407 | + return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Check if the cookie matches a domain value. |
|
412 | + * |
|
413 | + * @param string $domain Domain to check against |
|
414 | + */ |
|
415 | + public function matchesDomain(string $domain): bool |
|
416 | + { |
|
417 | + $cookieDomain = $this->getDomain(); |
|
418 | + if (null === $cookieDomain) { |
|
419 | + return true; |
|
420 | + } |
|
421 | + |
|
422 | + // Remove the leading '.' as per spec in RFC 6265. |
|
423 | + // https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3 |
|
424 | + $cookieDomain = \ltrim(\strtolower($cookieDomain), '.'); |
|
425 | + |
|
426 | + $domain = \strtolower($domain); |
|
427 | + |
|
428 | + // Domain not set or exact match. |
|
429 | + if ('' === $cookieDomain || $domain === $cookieDomain) { |
|
430 | + return true; |
|
431 | + } |
|
432 | + |
|
433 | + // Matching the subdomain according to RFC 6265. |
|
434 | + // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 |
|
435 | + if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
|
436 | + return false; |
|
437 | + } |
|
438 | + |
|
439 | + return (bool) \preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Check if the cookie is expired. |
|
444 | + */ |
|
445 | + public function isExpired(): bool |
|
446 | + { |
|
447 | + return $this->getExpires() !== null && \time() > $this->getExpires(); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Check if the cookie is valid according to RFC 6265. |
|
452 | + * |
|
453 | + * @return bool|string Returns true if valid or an error message if invalid |
|
454 | + */ |
|
455 | + public function validate() |
|
456 | + { |
|
457 | + $name = $this->getName(); |
|
458 | + if ($name === '') { |
|
459 | + return 'The cookie name must not be empty'; |
|
460 | + } |
|
461 | + |
|
462 | + // Check if any of the invalid characters are present in the cookie name |
|
463 | + if (\preg_match( |
|
464 | + '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/', |
|
465 | + $name |
|
466 | + )) { |
|
467 | + return 'Cookie name must not contain invalid characters: ASCII ' |
|
468 | + .'Control characters (0-31;127), space, tab and the ' |
|
469 | + .'following characters: ()<>@,;:\"/?={}'; |
|
470 | + } |
|
471 | + |
|
472 | + // Value must not be null. 0 and empty string are valid. Empty strings |
|
473 | + // are technically against RFC 6265, but known to happen in the wild. |
|
474 | + $value = $this->getValue(); |
|
475 | + if ($value === null) { |
|
476 | + return 'The cookie value must not be empty'; |
|
477 | + } |
|
478 | + |
|
479 | + // Domains must not be empty, but can be 0. "0" is not a valid internet |
|
480 | + // domain, but may be used as server name in a private network. |
|
481 | + $domain = $this->getDomain(); |
|
482 | + if ($domain === null || $domain === '') { |
|
483 | + return 'The cookie domain must not be empty'; |
|
484 | + } |
|
485 | + |
|
486 | + return true; |
|
487 | + } |
|
488 | 488 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | if (!\strcasecmp($search, $key)) { |
61 | 61 | if ($search === 'Max-Age') { |
62 | 62 | if (is_numeric($value)) { |
63 | - $data[$search] = (int) $value; |
|
63 | + $data[$search] = (int)$value; |
|
64 | 64 | } |
65 | 65 | } else { |
66 | 66 | $data[$search] = $value; |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
175 | 175 | } |
176 | 176 | |
177 | - $this->data['Name'] = (string) $name; |
|
177 | + $this->data['Name'] = (string)$name; |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | /** |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
199 | 199 | } |
200 | 200 | |
201 | - $this->data['Value'] = (string) $value; |
|
201 | + $this->data['Value'] = (string)$value; |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
223 | 223 | } |
224 | 224 | |
225 | - $this->data['Domain'] = null === $domain ? null : (string) $domain; |
|
225 | + $this->data['Domain'] = null === $domain ? null : (string)$domain; |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | /** |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
247 | 247 | } |
248 | 248 | |
249 | - $this->data['Path'] = (string) $path; |
|
249 | + $this->data['Path'] = (string)$path; |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | /** |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | */ |
257 | 257 | public function getMaxAge() |
258 | 258 | { |
259 | - return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
|
259 | + return null === $this->data['Max-Age'] ? null : (int)$this->data['Max-Age']; |
|
260 | 260 | } |
261 | 261 | |
262 | 262 | /** |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
271 | 271 | } |
272 | 272 | |
273 | - $this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
|
273 | + $this->data['Max-Age'] = $maxAge === null ? null : (int)$maxAge; |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | /** |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
295 | 295 | } |
296 | 296 | |
297 | - $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int) $timestamp : \strtotime((string) $timestamp)); |
|
297 | + $this->data['Expires'] = null === $timestamp ? null : (\is_numeric($timestamp) ? (int)$timestamp : \strtotime((string)$timestamp)); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
319 | 319 | } |
320 | 320 | |
321 | - $this->data['Secure'] = (bool) $secure; |
|
321 | + $this->data['Secure'] = (bool)$secure; |
|
322 | 322 | } |
323 | 323 | |
324 | 324 | /** |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
343 | 343 | } |
344 | 344 | |
345 | - $this->data['Discard'] = (bool) $discard; |
|
345 | + $this->data['Discard'] = (bool)$discard; |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | /** |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
367 | 367 | } |
368 | 368 | |
369 | - $this->data['HttpOnly'] = (bool) $httpOnly; |
|
369 | + $this->data['HttpOnly'] = (bool)$httpOnly; |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | return false; |
437 | 437 | } |
438 | 438 | |
439 | - return (bool) \preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
|
439 | + return (bool)\preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
|
440 | 440 | } |
441 | 441 | |
442 | 442 | /** |
@@ -5,8 +5,7 @@ |
||
5 | 5 | /** |
6 | 6 | * Set-Cookie object |
7 | 7 | */ |
8 | -class SetCookie |
|
9 | -{ |
|
8 | +class SetCookie { |
|
10 | 9 | /** |
11 | 10 | * @var array |
12 | 11 | */ |
@@ -7,71 +7,71 @@ |
||
7 | 7 | */ |
8 | 8 | class SessionCookieJar extends CookieJar |
9 | 9 | { |
10 | - /** |
|
11 | - * @var string session key |
|
12 | - */ |
|
13 | - private $sessionKey; |
|
10 | + /** |
|
11 | + * @var string session key |
|
12 | + */ |
|
13 | + private $sessionKey; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var bool Control whether to persist session cookies or not. |
|
17 | - */ |
|
18 | - private $storeSessionCookies; |
|
15 | + /** |
|
16 | + * @var bool Control whether to persist session cookies or not. |
|
17 | + */ |
|
18 | + private $storeSessionCookies; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Create a new SessionCookieJar object |
|
22 | - * |
|
23 | - * @param string $sessionKey Session key name to store the cookie |
|
24 | - * data in session |
|
25 | - * @param bool $storeSessionCookies Set to true to store session cookies |
|
26 | - * in the cookie jar. |
|
27 | - */ |
|
28 | - public function __construct(string $sessionKey, bool $storeSessionCookies = false) |
|
29 | - { |
|
30 | - parent::__construct(); |
|
31 | - $this->sessionKey = $sessionKey; |
|
32 | - $this->storeSessionCookies = $storeSessionCookies; |
|
33 | - $this->load(); |
|
34 | - } |
|
20 | + /** |
|
21 | + * Create a new SessionCookieJar object |
|
22 | + * |
|
23 | + * @param string $sessionKey Session key name to store the cookie |
|
24 | + * data in session |
|
25 | + * @param bool $storeSessionCookies Set to true to store session cookies |
|
26 | + * in the cookie jar. |
|
27 | + */ |
|
28 | + public function __construct(string $sessionKey, bool $storeSessionCookies = false) |
|
29 | + { |
|
30 | + parent::__construct(); |
|
31 | + $this->sessionKey = $sessionKey; |
|
32 | + $this->storeSessionCookies = $storeSessionCookies; |
|
33 | + $this->load(); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Saves cookies to session when shutting down |
|
38 | - */ |
|
39 | - public function __destruct() |
|
40 | - { |
|
41 | - $this->save(); |
|
42 | - } |
|
36 | + /** |
|
37 | + * Saves cookies to session when shutting down |
|
38 | + */ |
|
39 | + public function __destruct() |
|
40 | + { |
|
41 | + $this->save(); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Save cookies to the client session |
|
46 | - */ |
|
47 | - public function save(): void |
|
48 | - { |
|
49 | - $json = []; |
|
50 | - /** @var SetCookie $cookie */ |
|
51 | - foreach ($this as $cookie) { |
|
52 | - if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
53 | - $json[] = $cookie->toArray(); |
|
54 | - } |
|
55 | - } |
|
44 | + /** |
|
45 | + * Save cookies to the client session |
|
46 | + */ |
|
47 | + public function save(): void |
|
48 | + { |
|
49 | + $json = []; |
|
50 | + /** @var SetCookie $cookie */ |
|
51 | + foreach ($this as $cookie) { |
|
52 | + if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
|
53 | + $json[] = $cookie->toArray(); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - $_SESSION[$this->sessionKey] = \json_encode($json); |
|
58 | - } |
|
57 | + $_SESSION[$this->sessionKey] = \json_encode($json); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Load the contents of the client session into the data array |
|
62 | - */ |
|
63 | - protected function load(): void |
|
64 | - { |
|
65 | - if (!isset($_SESSION[$this->sessionKey])) { |
|
66 | - return; |
|
67 | - } |
|
68 | - $data = \json_decode($_SESSION[$this->sessionKey], true); |
|
69 | - if (\is_array($data)) { |
|
70 | - foreach ($data as $cookie) { |
|
71 | - $this->setCookie(new SetCookie($cookie)); |
|
72 | - } |
|
73 | - } elseif (\strlen($data)) { |
|
74 | - throw new \RuntimeException('Invalid cookie data'); |
|
75 | - } |
|
76 | - } |
|
60 | + /** |
|
61 | + * Load the contents of the client session into the data array |
|
62 | + */ |
|
63 | + protected function load(): void |
|
64 | + { |
|
65 | + if (!isset($_SESSION[$this->sessionKey])) { |
|
66 | + return; |
|
67 | + } |
|
68 | + $data = \json_decode($_SESSION[$this->sessionKey], true); |
|
69 | + if (\is_array($data)) { |
|
70 | + foreach ($data as $cookie) { |
|
71 | + $this->setCookie(new SetCookie($cookie)); |
|
72 | + } |
|
73 | + } elseif (\strlen($data)) { |
|
74 | + throw new \RuntimeException('Invalid cookie data'); |
|
75 | + } |
|
76 | + } |
|
77 | 77 | } |
@@ -5,8 +5,7 @@ |
||
5 | 5 | /** |
6 | 6 | * Persists cookies in the client session |
7 | 7 | */ |
8 | -class SessionCookieJar extends CookieJar |
|
9 | -{ |
|
8 | +class SessionCookieJar extends CookieJar { |
|
10 | 9 | /** |
11 | 10 | * @var string session key |
12 | 11 | */ |
@@ -19,62 +19,62 @@ |
||
19 | 19 | */ |
20 | 20 | interface CookieJarInterface extends \Countable, \IteratorAggregate |
21 | 21 | { |
22 | - /** |
|
23 | - * Create a request with added cookie headers. |
|
24 | - * |
|
25 | - * If no matching cookies are found in the cookie jar, then no Cookie |
|
26 | - * header is added to the request and the same request is returned. |
|
27 | - * |
|
28 | - * @param RequestInterface $request Request object to modify. |
|
29 | - * |
|
30 | - * @return RequestInterface returns the modified request. |
|
31 | - */ |
|
32 | - public function withCookieHeader(RequestInterface $request): RequestInterface; |
|
22 | + /** |
|
23 | + * Create a request with added cookie headers. |
|
24 | + * |
|
25 | + * If no matching cookies are found in the cookie jar, then no Cookie |
|
26 | + * header is added to the request and the same request is returned. |
|
27 | + * |
|
28 | + * @param RequestInterface $request Request object to modify. |
|
29 | + * |
|
30 | + * @return RequestInterface returns the modified request. |
|
31 | + */ |
|
32 | + public function withCookieHeader(RequestInterface $request): RequestInterface; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Extract cookies from an HTTP response and store them in the CookieJar. |
|
36 | - * |
|
37 | - * @param RequestInterface $request Request that was sent |
|
38 | - * @param ResponseInterface $response Response that was received |
|
39 | - */ |
|
40 | - public function extractCookies(RequestInterface $request, ResponseInterface $response): void; |
|
34 | + /** |
|
35 | + * Extract cookies from an HTTP response and store them in the CookieJar. |
|
36 | + * |
|
37 | + * @param RequestInterface $request Request that was sent |
|
38 | + * @param ResponseInterface $response Response that was received |
|
39 | + */ |
|
40 | + public function extractCookies(RequestInterface $request, ResponseInterface $response): void; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Sets a cookie in the cookie jar. |
|
44 | - * |
|
45 | - * @param SetCookie $cookie Cookie to set. |
|
46 | - * |
|
47 | - * @return bool Returns true on success or false on failure |
|
48 | - */ |
|
49 | - public function setCookie(SetCookie $cookie): bool; |
|
42 | + /** |
|
43 | + * Sets a cookie in the cookie jar. |
|
44 | + * |
|
45 | + * @param SetCookie $cookie Cookie to set. |
|
46 | + * |
|
47 | + * @return bool Returns true on success or false on failure |
|
48 | + */ |
|
49 | + public function setCookie(SetCookie $cookie): bool; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Remove cookies currently held in the cookie jar. |
|
53 | - * |
|
54 | - * Invoking this method without arguments will empty the whole cookie jar. |
|
55 | - * If given a $domain argument only cookies belonging to that domain will |
|
56 | - * be removed. If given a $domain and $path argument, cookies belonging to |
|
57 | - * the specified path within that domain are removed. If given all three |
|
58 | - * arguments, then the cookie with the specified name, path and domain is |
|
59 | - * removed. |
|
60 | - * |
|
61 | - * @param string|null $domain Clears cookies matching a domain |
|
62 | - * @param string|null $path Clears cookies matching a domain and path |
|
63 | - * @param string|null $name Clears cookies matching a domain, path, and name |
|
64 | - */ |
|
65 | - public function clear(string $domain = null, string $path = null, string $name = null): void; |
|
51 | + /** |
|
52 | + * Remove cookies currently held in the cookie jar. |
|
53 | + * |
|
54 | + * Invoking this method without arguments will empty the whole cookie jar. |
|
55 | + * If given a $domain argument only cookies belonging to that domain will |
|
56 | + * be removed. If given a $domain and $path argument, cookies belonging to |
|
57 | + * the specified path within that domain are removed. If given all three |
|
58 | + * arguments, then the cookie with the specified name, path and domain is |
|
59 | + * removed. |
|
60 | + * |
|
61 | + * @param string|null $domain Clears cookies matching a domain |
|
62 | + * @param string|null $path Clears cookies matching a domain and path |
|
63 | + * @param string|null $name Clears cookies matching a domain, path, and name |
|
64 | + */ |
|
65 | + public function clear(string $domain = null, string $path = null, string $name = null): void; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Discard all sessions cookies. |
|
69 | - * |
|
70 | - * Removes cookies that don't have an expire field or a have a discard |
|
71 | - * field set to true. To be called when the user agent shuts down according |
|
72 | - * to RFC 2965. |
|
73 | - */ |
|
74 | - public function clearSessionCookies(): void; |
|
67 | + /** |
|
68 | + * Discard all sessions cookies. |
|
69 | + * |
|
70 | + * Removes cookies that don't have an expire field or a have a discard |
|
71 | + * field set to true. To be called when the user agent shuts down according |
|
72 | + * to RFC 2965. |
|
73 | + */ |
|
74 | + public function clearSessionCookies(): void; |
|
75 | 75 | |
76 | - /** |
|
77 | - * Converts the cookie jar to an array. |
|
78 | - */ |
|
79 | - public function toArray(): array; |
|
76 | + /** |
|
77 | + * Converts the cookie jar to an array. |
|
78 | + */ |
|
79 | + public function toArray(): array; |
|
80 | 80 | } |