Expand description
§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
web-url = "0.11.0"§Example
use std::str::FromStr;
use web_url::{Param, WebUrl};
// Parsing normalizes the URL: the scheme & host are lowercased & the port is rewritten.
let url = WebUrl::from_str("HTTPS://Example.com:0443/path?key=value#section").unwrap();
assert_eq!(url.as_str(), "https://example.com:443/path?key=value#section");
assert_eq!(url.scheme().as_str(), "https");
assert_eq!(url.host().to_string(), "example.com");
assert_eq!(url.port(), Some(443));
assert_eq!(url.path().as_str(), "/path");
assert_eq!(url.query().unwrap().as_str(), "?key=value");
assert_eq!(url.fragment().unwrap().as_str(), "#section");
// URLs can be mutated in place; a URL parsed without a path gets the path '/'.
let url = WebUrl::from_str("https://example.com").unwrap().with_param(Param::try_from("page=2").unwrap());
assert_eq!(url.as_str(), "https://example.com/?page=2");§Features
This crate has no features. The only dependency is the address crate.
§The URL Format
scheme://host:port/path?query#fragmentThe 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.
Re-exports§
pub use address;
Structs§
- Fragment
- A web-based URL fragment.
- Invalid
UrlError - An error parsing a web-based URL from an owned string.
- Param
- A web-based URL query parameter.
- Path
- A web-based URL path.
- Piece
Iterator - Responsible for iterating over the separated pieces of a string.
- Query
- A web-based URL query.
- Scheme
- A web-based URL scheme.
- WebUrl
- A web-based URL.
Enums§
- Error
- An error parsing a web-based URL.