Copied!

Provides methods to normalize and compare URIs.

  • author Tobias Schultze
  • see https://datatracker.ietf.org/doc/html/rfc3986#section-6
CloneableFinal
Constants
public GuzzleHttp\Psr7\UriNormalizer::CAPITALIZE_PERCENT_ENCODING = 1
 

All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.

public GuzzleHttp\Psr7\UriNormalizer::CONVERT_EMPTY_PATH = 4
 

Converts the empty path to "/" for http and https URIs.

public GuzzleHttp\Psr7\UriNormalizer::DECODE_UNRESERVED_CHARACTERS = 2
 

Decodes percent-encoded octets of unreserved characters.

For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers.

Example: http://example.org/%7Eusern%61me/http://example.org/~username/

public GuzzleHttp\Psr7\UriNormalizer::PRESERVING_NORMALIZATIONS = 63
 

Default normalizations which only include the ones that preserve semantics.

public GuzzleHttp\Psr7\UriNormalizer::REMOVE_DEFAULT_HOST = 8
 

Removes the default host of the given URI scheme from the URI.

Only the "file" scheme defines the default host "localhost". All of file:/myfile, file:///myfile, and file://localhost/myfile are equivalent according to RFC 3986. The first format is not accepted by PHPs stream functions and thus already normalized implicitly to the second format in the Uri class. See GuzzleHttp\Psr7\Uri::composeComponents.

Example: file://localhost/myfile → file:///myfile

public GuzzleHttp\Psr7\UriNormalizer::REMOVE_DEFAULT_PORT = 16
 

Removes the default port of the given URI scheme from the URI.

public GuzzleHttp\Psr7\UriNormalizer::REMOVE_DOT_SEGMENTS = 32
 

Removes unnecessary dot-segments.

Dot-segments in relative-path references are not removed as it would change the semantics of the URI reference.

Example: http://example.org/../a/b/../c/./d.htmlhttp://example.org/a/c/d.html

public GuzzleHttp\Psr7\UriNormalizer::REMOVE_DUPLICATE_SLASHES = 64
 

Paths which include two or more adjacent slashes are converted to one.

Webservers usually ignore duplicate slashes and treat those URIs equivalent. But in theory those URIs do not need to be equivalent. So this normalization may change the semantics. Encoded slashes (%2F) are not removed.

Example: http://example.org//foo///bar.htmlhttp://example.org/foo/bar.html

public GuzzleHttp\Psr7\UriNormalizer::SORT_QUERY_PARAMETERS = 128
 

Sort query parameters with their values in alphabetical order.

However, the order of parameters in a URI may be significant (this is not defined by the standard). So this normalization is not safe and may change the semantics of the URI.

Example: ?lang=en&article=fred → ?article=fred&lang=en

Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly.

Methods
public static isEquivalent(Psr\Http\Message\UriInterface $uri1, Psr\Http\Message\UriInterface $uri2, int $normalizations = 63self::PRESERVING_NORMALIZATIONS) : bool
 

Whether two URIs can be considered equivalent.

Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also accepts relative URI references and returns true when they are equivalent. This of course assumes they will be resolved against the same base URI. If this is not the case, determination of equivalence or difference of relative references does not mean anything.

  • param \UriInterface $uri1 An URI to compare
  • param \UriInterface $uri2 An URI to compare
  • param int $normalizations A bitmask of normalizations to apply, see constants
  • see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1
public static normalize(Psr\Http\Message\UriInterface $uri, int $flags = 63self::PRESERVING_NORMALIZATIONS) : Psr\Http\Message\UriInterface
 

Returns a normalized URI.

The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. This methods adds additional normalizations that can be configured with the $flags parameter.

PSR-7 UriInterface cannot distinguish between an empty component and a missing component as getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are treated equivalent which is not necessarily true according to RFC 3986. But that difference is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.

  • param \UriInterface $uri The URI to normalize
  • param int $flags A bitmask of normalizations to apply, see constants
  • see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2
Methods
private __construct()
private static capitalizePercentEncoding(Psr\Http\Message\UriInterface $uri) : Psr\Http\Message\UriInterface
private static decodeUnreservedCharacters(Psr\Http\Message\UriInterface $uri) : Psr\Http\Message\UriInterface
Methods
private static capitalizePercentEncoding(Psr\Http\Message\UriInterface $uri) : Psr\Http\Message\UriInterface
private static decodeUnreservedCharacters(Psr\Http\Message\UriInterface $uri) : Psr\Http\Message\UriInterface
public static isEquivalent(Psr\Http\Message\UriInterface $uri1, Psr\Http\Message\UriInterface $uri2, int $normalizations = 63self::PRESERVING_NORMALIZATIONS) : bool
 

Whether two URIs can be considered equivalent.

Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also accepts relative URI references and returns true when they are equivalent. This of course assumes they will be resolved against the same base URI. If this is not the case, determination of equivalence or difference of relative references does not mean anything.

  • param \UriInterface $uri1 An URI to compare
  • param \UriInterface $uri2 An URI to compare
  • param int $normalizations A bitmask of normalizations to apply, see constants
  • see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1
public static normalize(Psr\Http\Message\UriInterface $uri, int $flags = 63self::PRESERVING_NORMALIZATIONS) : Psr\Http\Message\UriInterface
 

Returns a normalized URI.

The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. This methods adds additional normalizations that can be configured with the $flags parameter.

PSR-7 UriInterface cannot distinguish between an empty component and a missing component as getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are treated equivalent which is not necessarily true according to RFC 3986. But that difference is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.

  • param \UriInterface $uri The URI to normalize
  • param int $flags A bitmask of normalizations to apply, see constants
  • see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration