Skip to content

Commit

Permalink
Release 1.9.0 (guzzle#520)
Browse files Browse the repository at this point in the history
(cherry picked from commit e98e3e6)
  • Loading branch information
GrahamCampbell authored and TimWolla committed Jun 21, 2022
1 parent 986596d commit 3dffed9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PSR-7 Message Implementation

This repository contains a full [PSR-7](http://www.php-fig.org/psr/psr-7/)
This repository contains a full [PSR-7](https://www.php-fig.org/psr/psr-7/)
message implementation, several stream decorators, and some helpful
functionality like query string parsing.

Expand Down Expand Up @@ -595,7 +595,7 @@ manually but instead is used indirectly via `Psr\Http\Message\UriInterface::__to

`public static function fromParts(array $parts): UriInterface`

Creates a URI from a hash of [`parse_url`](http://php.net/manual/en/function.parse-url.php) components.
Creates a URI from a hash of [`parse_url`](https://www.php.net/manual/en/function.parse-url.php) components.


### `GuzzleHttp\Psr7\Uri::withQueryValue`
Expand All @@ -620,6 +620,16 @@ associative array of key => value.
Creates a new URI with a specific query string value removed. Any existing query string values that exactly match the
provided key are removed.

## Cross-Origin Detection

`GuzzleHttp\Psr7\UriComparator` provides methods to determine if a modified URL should be considered cross-origin.

### `GuzzleHttp\Psr7\UriComparator::isCrossOrigin`

`public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool`

Determines if a modified URL should be considered cross-origin with respect to an original URL.

## Reference Resolution

`GuzzleHttp\Psr7\UriResolver` provides methods to resolve a URI reference in the context of a base URI according
Expand Down
55 changes: 55 additions & 0 deletions src/UriComparator.php
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
}
}
42 changes: 42 additions & 0 deletions tests/UriComparatorTest.php
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],
];
}
}

0 comments on commit 3dffed9

Please sign in to comment.