web-url
This library aids in processing web-based URLs — URLs in the format scheme://host:port/path?query#fragment — with
strict validation, normalizing parsers, & borrowed component types.
Usage
= "0.11.0"
Example
use FromStr;
use ;
// Parsing normalizes the URL: the scheme & host are lowercased & the port is rewritten.
let url = from_str.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// URLs can be mutated in place; a URL parsed without a path gets the path '/'.
let url = from_str.unwrap.with_param;
assert_eq!;
Features
This crate has no features. The only dependency is the address crate.
The URL Format
scheme://host:port/path?query#fragment
The format is a subset of the RFC 3986 URI syntax: the authority is required, user info is rejected, & the path is never empty. The port, query, & fragment are optional.
The accepted chars follow RFC 3986: the path accepts the pchar chars plus '/', & the query & fragment also accept
'?'. A '%' char begins a percent-encoded octet & must be followed by two hex digits, so %zz is rejected.
Normalization
Parsed URLs are always normalized: the scheme & host are lowercased, an IP address host is rewritten in the canonical
address form ([0:0:0:0:0:0:0:1] -> [::1]), an empty port is dropped along with its ':', a port with leading
zeros is rewritten, a URL parsed without a path gets the path '/', & the path dot-segments are removed
(/a/../b -> /b). The query & fragment are preserved exactly. The percent-encoding is validated but not
normalized, so %7e & ~ stay distinct.
Parsing with TryFrom<String> reuses the allocation when the URL is already normalized & recovers the original
string on error.
Component Types
The component types are borrowed, validated views. The WebUrl accessors return them borrowing from the URL string
& they can also be created from their own strings.
Scheme: A lowercase URL scheme, withHTTP&HTTPSconstants.Path: A URL path starting with '/', with an iterator for its segments.Query: A URL query starting with '?', with an iterator for itsParams.Param: A query parameter with a name & an optional value.Fragment: A URL fragment starting with '#'.
The host is an address::HostRef, either a domain name or an IP address. The address crate is re-exported as
web_url::address.
Mutations
URLs can be mutated in place & every mutation keeps the URL normalized:
set_scheme,set_host,set_port,set_path,set_query, &set_fragmentset their component. The port, query, & fragment are removed by setting them toNone.add_paramappends a query parameter,remove_paramsremoves every parameter with a name, &replace_paramsreplaces every parameter with a name with a single parameter.
The with_scheme, with_host, with_port, with_path, with_query, with_fragment, with_param,
without_params, & with_replaced_params variants chain on owned URLs.