forked from ringcentral/psr7
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
337e3ad
commit e98e3e6
Showing
5 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.7-dev" | ||
"dev-master": "1.9-dev" | ||
} | ||
}, | ||
"config": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Psr7; | ||
|
||
use Psr\Http\Message\UriInterface; | ||
|
||
/** | ||
* Provides methods to determine if a modified URL should be considered cross-origin. | ||
* | ||
* @author Graham Campbell | ||
*/ | ||
final class UriComparator | ||
{ | ||
/** | ||
* Determines if a modified URL should be considered cross-origin with | ||
* respect to an original URL. | ||
* | ||
* @return bool | ||
*/ | ||
public static function isCrossOrigin(UriInterface $original, UriInterface $modified) | ||
{ | ||
if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) { | ||
return true; | ||
} | ||
|
||
if ($original->getScheme() !== $modified->getScheme()) { | ||
return true; | ||
} | ||
|
||
if (self::computePort($original) !== self::computePort($modified)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
private static function computePort(UriInterface $uri) | ||
{ | ||
$port = $uri->getPort(); | ||
|
||
if (null !== $port) { | ||
return $port; | ||
} | ||
|
||
return 'https' === $uri->getScheme() ? 443 : 80; | ||
} | ||
|
||
private function __construct() | ||
{ | ||
// cannot be instantiated | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Tests\Psr7; | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use GuzzleHttp\Psr7\UriComparator; | ||
|
||
/** | ||
* @covers GuzzleHttp\Psr7\UriComparator | ||
*/ | ||
class UriComparatorTest extends BaseTest | ||
{ | ||
/** | ||
* @dataProvider getCrossOriginExamples | ||
*/ | ||
public function testIsCrossOrigin($originalUri, $modifiedUri, $expected) | ||
{ | ||
self::assertSame($expected, UriComparator::isCrossOrigin(new Uri($originalUri), new Uri($modifiedUri))); | ||
} | ||
|
||
public function getCrossOriginExamples() | ||
{ | ||
return [ | ||
['http://example.com/123', 'http://example.com/', false], | ||
['http://example.com/123', 'http://example.com:80/', false], | ||
['http://example.com:80/123', 'http://example.com/', false], | ||
['http://example.com:80/123', 'http://example.com:80/', false], | ||
['http://example.com/123', 'https://example.com/', true], | ||
['http://example.com/123', 'http://www.example.com/', true], | ||
['http://example.com/123', 'http://example.com:81/', true], | ||
['http://example.com:80/123', 'http://example.com:81/', true], | ||
['https://example.com/123', 'https://example.com/', false], | ||
['https://example.com/123', 'https://example.com:443/', false], | ||
['https://example.com:443/123', 'https://example.com/', false], | ||
['https://example.com:443/123', 'https://example.com:443/', false], | ||
['https://example.com/123', 'http://example.com/', true], | ||
['https://example.com/123', 'https://www.example.com/', true], | ||
['https://example.com/123', 'https://example.com:444/', true], | ||
['https://example.com:443/123', 'https://example.com:444/', true], | ||
]; | ||
} | ||
} |