Rust

url

Module

parse_url and the PHP 8.5 URI extension (Uri\Rfc3986\Uri).

PHPRust
parse_url($url)parse_url → Url
parse_url($url, PHP_URL_*)parse_url + Url::component (Component)
Uri\Rfc3986\Uri::parse($uri)rfc3986::Uri::parse

Uri\WhatWg\Url is not reimplemented: no Utopia library uses it.

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations url.*).

Contents

1 submodule, and what the module defines itself.

use php_std::url::Url;
Rust
pub struct Url<'a>

A URL split by PHP's parse_url (php_url_parse_ex2 in ext/standard/url.c).

Components borrow from the input. PHP replaces ASCII control characters (iscntrl: 0x00–0x1F and 0x7F) in every component with _; the accessors do the same, allocating only when a component contains one. Components are byte strings because PHP strings are: when the input is valid UTF-8, so is every component (the parser only splits at ASCII bytes).

use php_std::url::Component;
Rust
pub enum Component

A PHP_URL_* selector for parse_url($url, $component).

PHP throws a ValueError for any other integer; the enum makes that unrepresentable (a recorded deviation).

use php_std::url::ComponentValue;
Rust
pub enum ComponentValue<'a>

The value parse_url($url, $component) returns for a component that is present: a string, or an integer for the port.

use php_std::url::parse_url;
Rust
pub fn parse_url(url: &[u8]) -> Option<Url<'_>>

PHP parse_url($url): None where PHP returns false (a malformed URL, such as an invalid port or an empty host after //).

url::rfc3986

Own page

Uri\Rfc3986\Uri from the PHP 8.5 URI extension.

PHP parses with uriparser 1.0 (uriParseSingleUriExMmA, the RFC 3986 URI-reference grammar), keeps the parsed URI for the getRaw*() getters, and reads the normalized getters (getScheme(), getHost()) from a copy passed through uriNormalizeSyntaxExMmA with every normalization enabled. This module ports the parser state machine (UriParse.c) and the scheme and host normalization (UriNormalize.c, UriCommon.c) byte for byte, including their quirks:

  • host lowercasing stops at a % in the last three bytes of the host;
  • a port longer than 20 digits or above PHP_INT_MAX makes parse() return null (PHP's own check).

The normalized user info, path, port, query and fragment getters, toString()/toRawString(), base URIs (parse($uri, $baseUrl)), resolve() and the with*() modifiers are not ported: no Utopia library uses them.

use php_std::url::rfc3986::Uri;
Rust
pub struct Uri<'a>

A URI parsed by Uri\Rfc3986\Uri::parse().