RequestMatcher.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\HttpFoundation;
  11. /**
  12. * RequestMatcher compares a pre-defined set of checks against a Request instance.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class RequestMatcher implements RequestMatcherInterface
  17. {
  18. private ?string $path = null;
  19. private ?string $host = null;
  20. private ?int $port = null;
  21. /**
  22. * @var string[]
  23. */
  24. private array $methods = [];
  25. /**
  26. * @var string[]
  27. */
  28. private array $ips = [];
  29. /**
  30. * @var string[]
  31. */
  32. private array $attributes = [];
  33. /**
  34. * @var string[]
  35. */
  36. private array $schemes = [];
  37. /**
  38. * @param string|string[]|null $methods
  39. * @param string|string[]|null $ips
  40. * @param string|string[]|null $schemes
  41. */
  42. public function __construct(string $path = null, string $host = null, string|array $methods = null, string|array $ips = null, array $attributes = [], string|array $schemes = null, int $port = null)
  43. {
  44. $this->matchPath($path);
  45. $this->matchHost($host);
  46. $this->matchMethod($methods);
  47. $this->matchIps($ips);
  48. $this->matchScheme($schemes);
  49. $this->matchPort($port);
  50. foreach ($attributes as $k => $v) {
  51. $this->matchAttribute($k, $v);
  52. }
  53. }
  54. /**
  55. * Adds a check for the HTTP scheme.
  56. *
  57. * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  58. */
  59. public function matchScheme(string|array|null $scheme)
  60. {
  61. $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  62. }
  63. /**
  64. * Adds a check for the URL host name.
  65. */
  66. public function matchHost(?string $regexp)
  67. {
  68. $this->host = $regexp;
  69. }
  70. /**
  71. * Adds a check for the the URL port.
  72. *
  73. * @param int|null $port The port number to connect to
  74. */
  75. public function matchPort(?int $port)
  76. {
  77. $this->port = $port;
  78. }
  79. /**
  80. * Adds a check for the URL path info.
  81. */
  82. public function matchPath(?string $regexp)
  83. {
  84. $this->path = $regexp;
  85. }
  86. /**
  87. * Adds a check for the client IP.
  88. *
  89. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  90. */
  91. public function matchIp(string $ip)
  92. {
  93. $this->matchIps($ip);
  94. }
  95. /**
  96. * Adds a check for the client IP.
  97. *
  98. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  99. */
  100. public function matchIps(string|array|null $ips)
  101. {
  102. $ips = null !== $ips ? (array) $ips : [];
  103. $this->ips = array_reduce($ips, static function (array $ips, string $ip) {
  104. return array_merge($ips, preg_split('/\s*,\s*/', $ip));
  105. }, []);
  106. }
  107. /**
  108. * Adds a check for the HTTP method.
  109. *
  110. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  111. */
  112. public function matchMethod(string|array|null $method)
  113. {
  114. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  115. }
  116. /**
  117. * Adds a check for request attribute.
  118. */
  119. public function matchAttribute(string $key, string $regexp)
  120. {
  121. $this->attributes[$key] = $regexp;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function matches(Request $request): bool
  127. {
  128. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  129. return false;
  130. }
  131. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  132. return false;
  133. }
  134. foreach ($this->attributes as $key => $pattern) {
  135. $requestAttribute = $request->attributes->get($key);
  136. if (!\is_string($requestAttribute)) {
  137. return false;
  138. }
  139. if (!preg_match('{'.$pattern.'}', $requestAttribute)) {
  140. return false;
  141. }
  142. }
  143. if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
  144. return false;
  145. }
  146. if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
  147. return false;
  148. }
  149. if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
  150. return false;
  151. }
  152. if (IpUtils::checkIp($request->getClientIp() ?? '', $this->ips)) {
  153. return true;
  154. }
  155. // Note to future implementors: add additional checks above the
  156. // foreach above or else your check might not be run!
  157. return 0 === \count($this->ips);
  158. }
  159. }