Proxy.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Promise\PromiseInterface;
  4. use GuzzleHttp\RequestOptions;
  5. use Psr\Http\Message\RequestInterface;
  6. /**
  7. * Provides basic proxies for handlers.
  8. *
  9. * @final
  10. */
  11. class Proxy
  12. {
  13. /**
  14. * Sends synchronous requests to a specific handler while sending all other
  15. * requests to another handler.
  16. *
  17. * @param callable(RequestInterface, array): PromiseInterface $default Handler used for normal responses
  18. * @param callable(RequestInterface, array): PromiseInterface $sync Handler used for synchronous responses.
  19. *
  20. * @return callable(RequestInterface, array): PromiseInterface Returns the composed handler.
  21. */
  22. public static function wrapSync(callable $default, callable $sync): callable
  23. {
  24. return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface {
  25. return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options);
  26. };
  27. }
  28. /**
  29. * Sends streaming requests to a streaming compatible handler while sending
  30. * all other requests to a default handler.
  31. *
  32. * This, for example, could be useful for taking advantage of the
  33. * performance benefits of curl while still supporting true streaming
  34. * through the StreamHandler.
  35. *
  36. * @param callable(RequestInterface, array): PromiseInterface $default Handler used for non-streaming responses
  37. * @param callable(RequestInterface, array): PromiseInterface $streaming Handler used for streaming responses
  38. *
  39. * @return callable(RequestInterface, array): PromiseInterface Returns the composed handler.
  40. */
  41. public static function wrapStreaming(callable $default, callable $streaming): callable
  42. {
  43. return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface {
  44. return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options);
  45. };
  46. }
  47. /**
  48. * Sends requests to a fallback handler when the default cURL handler cannot
  49. * honor TLS 1.2 selection.
  50. *
  51. * @param callable(RequestInterface, array): PromiseInterface $default
  52. * @param callable(RequestInterface, array): PromiseInterface $fallback
  53. *
  54. * @return callable(RequestInterface, array): PromiseInterface Returns the composed handler.
  55. */
  56. public static function wrapTlsFallback(callable $default, callable $fallback): callable
  57. {
  58. return static function (RequestInterface $request, array $options) use ($default, $fallback): PromiseInterface {
  59. if (self::requiresTls12Fallback($options)) {
  60. return $fallback($request, $options);
  61. }
  62. return $default($request, $options);
  63. };
  64. }
  65. /**
  66. * @param array<string, mixed> $options
  67. */
  68. private static function requiresTls12Fallback(array $options): bool
  69. {
  70. return isset($options[RequestOptions::CRYPTO_METHOD])
  71. && $options[RequestOptions::CRYPTO_METHOD] === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
  72. && !CurlVersion::supportsTls12();
  73. }
  74. }