UriComparator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\UriInterface;
  4. /**
  5. * Provides methods to determine if a modified URL should be considered cross-origin.
  6. *
  7. * @author Graham Campbell
  8. */
  9. final class UriComparator
  10. {
  11. /**
  12. * Determines if a modified URL should be considered cross-origin with
  13. * respect to an original URL.
  14. *
  15. * @return bool
  16. */
  17. public static function isCrossOrigin(UriInterface $original, UriInterface $modified)
  18. {
  19. if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) {
  20. return true;
  21. }
  22. if ($original->getScheme() !== $modified->getScheme()) {
  23. return true;
  24. }
  25. if (self::computePort($original) !== self::computePort($modified)) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /**
  31. * @return int
  32. */
  33. private static function computePort(UriInterface $uri)
  34. {
  35. $port = $uri->getPort();
  36. if (null !== $port) {
  37. return $port;
  38. }
  39. return 'https' === $uri->getScheme() ? 443 : 80;
  40. }
  41. private function __construct()
  42. {
  43. // cannot be instantiated
  44. }
  45. }