NoPrivateNetworkHttpClient.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  14. use Symfony\Component\HttpClient\Exception\TransportException;
  15. use Symfony\Component\HttpFoundation\IpUtils;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  19. use Symfony\Contracts\Service\ResetInterface;
  20. /**
  21. * Decorator that blocks requests to private networks by default.
  22. *
  23. * @author Hallison Boaventura <hallisonboaventura@gmail.com>
  24. */
  25. final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
  26. {
  27. use HttpClientTrait;
  28. /**
  29. * @var \Symfony\Contracts\HttpClient\HttpClientInterface
  30. */
  31. private $client;
  32. /**
  33. * @var string|mixed[]|null
  34. */
  35. private $subnets;
  36. /**
  37. * @param string|mixed[] $subnets String or array of subnets using CIDR notation that will be used by IpUtils.
  38. If null is passed, the standard private subnets will be used.
  39. * @param \Symfony\Contracts\HttpClient\HttpClientInterface $client
  40. */
  41. public function __construct($client, $subnets = null)
  42. {
  43. if (!class_exists(IpUtils::class)) {
  44. throw new \LogicException(sprintf('You cannot use "%s" if the HttpFoundation component is not installed. Try running "composer require symfony/http-foundation".', __CLASS__));
  45. }
  46. $this->client = $client;
  47. $this->subnets = $subnets;
  48. }
  49. /**
  50. * @param string $method
  51. * @param string $url
  52. * @param mixed[] $options
  53. */
  54. public function request($method, $url, $options = [])
  55. {
  56. $onProgress = $options['on_progress'] ?? null;
  57. if (null !== $onProgress && !\is_callable($onProgress)) {
  58. throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress)));
  59. }
  60. $subnets = $this->subnets;
  61. $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets): void {
  62. static $lastPrimaryIp = '';
  63. if ($info['primary_ip'] !== $lastPrimaryIp) {
  64. if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? IpUtils::PRIVATE_SUBNETS)) {
  65. throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url']));
  66. }
  67. $lastPrimaryIp = $info['primary_ip'];
  68. }
  69. null !== $onProgress && $onProgress($dlNow, $dlSize, $info);
  70. };
  71. return $this->client->request($method, $url, $options);
  72. }
  73. /**
  74. * @param \Symfony\Contracts\HttpClient\ResponseInterface|mixed[] $responses
  75. * @param float|null $timeout
  76. */
  77. public function stream($responses, $timeout = null)
  78. {
  79. return $this->client->stream($responses, $timeout);
  80. }
  81. /**
  82. * @param \Psr\Log\LoggerInterface $logger
  83. */
  84. public function setLogger($logger)
  85. {
  86. if ($this->client instanceof LoggerAwareInterface) {
  87. $this->client->setLogger($logger);
  88. }
  89. }
  90. /**
  91. * @return $this
  92. * @param mixed[] $options
  93. */
  94. public function withOptions($options)
  95. {
  96. $clone = clone $this;
  97. $clone->client = $this->client->withOptions($options);
  98. return $clone;
  99. }
  100. public function reset()
  101. {
  102. if ($this->client instanceof ResetInterface) {
  103. $this->client->reset();
  104. }
  105. }
  106. }