Debian Codegraph @

vendor/guzzlehttp/guzzle/src/Handler/Proxy.php

raw
1 <?php
2
3 namespace GuzzleHttp\Handler;
4
5 use GuzzleHttp\Promise\PromiseInterface;
6 use GuzzleHttp\RequestOptions;
7 use Psr\Http\Message\RequestInterface;
8
9 /**
10 * Provides basic proxies for handlers.
11 *
12 * @final
13 */
14 class Proxy
15 {
16 /**
17 * Sends synchronous requests to a specific handler while sending all other
18 * requests to another handler.
19 *
20 * @param callable(RequestInterface, array): PromiseInterface $default Handler used for normal responses
21 * @param callable(RequestInterface, array): PromiseInterface $sync Handler used for synchronous responses.
22 *
23 * @return callable(RequestInterface, array): 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
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(RequestInterface, array): PromiseInterface $default Handler used for non-streaming responses
41 * @param callable(RequestInterface, array): PromiseInterface $streaming Handler used for streaming responses
42 *
43 * @return callable(RequestInterface, array): 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
52 /**
53 * Sends requests to a fallback handler when the default cURL handler cannot
54 * honor TLS 1.2 selection.
55 *
56 * @param callable(RequestInterface, array): PromiseInterface $default
57 * @param callable(RequestInterface, array): PromiseInterface $fallback
58 *
59 * @return callable(RequestInterface, array): PromiseInterface Returns the composed handler.
60 */
61 public static function wrapTlsFallback(callable $default, callable $fallback): callable
62 {
63 return static function (RequestInterface $request, array $options) use ($default, $fallback): PromiseInterface {
64 if (self::requiresTls12Fallback($options)) {
65 return $fallback($request, $options);
66 }
67
68 return $default($request, $options);
69 };
70 }
71
72 /**
73 * @param array<string, mixed> $options
74 */
75 private static function requiresTls12Fallback(array $options): bool
76 {
77 return isset($options[RequestOptions::CRYPTO_METHOD])
78 && $options[RequestOptions::CRYPTO_METHOD] === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
79 && !CurlVersion::supportsTls12();
80 }
81 }