Expand description
A Rust port of Python’s urllib.parse.
Port target: the standard library urllib/parse.py (CPython 3.13). Behaviour is verified
against the reference module itself across a large corpus via differential testing.
Surface: the percent-encoding layer (quote, quote_plus, quote_from_bytes, unquote,
unquote_plus, unquote_to_bytes) and the URL splitting/joining layer (urlsplit,
urlunsplit, urlparse, urlunparse, urljoin, urldefrag, parse_qs/parse_qsl,
urlencode), including the SplitResult/ParseResult netloc accessors and urlsplit’s full
authority validation — bracketed IPv6/IPvFuture hosts plus the NFKC homograph check on
non-ASCII authorities (_checknetloc). That NFKC pass is the crate’s one dependency
(unicode-normalization, as used by url/idna), since Rust std has no Unicode normalization.
See STATUS.md.
Like urllib.parse, percent-encoding works on bytes: a str is first encoded as UTF-8,
every byte that is not “always safe” (A-Z a-z 0-9 _ . - ~) or in the caller’s safe set
becomes %XX (uppercase hex), and decoding reverses it (UTF-8, replacing invalid sequences).
Structs§
- Defrag
Result - A URL split into its defragmented part and its fragment, as returned by
urldefrag. Port ofDefragResult. - Parse
Result - The six components of a URL, as returned by
urlparse. Port ofParseResult(likeSplitResultbut with a;paramsfield split out of the path). - Split
Result - The five components of a URL, as returned by
urlsplit. Port ofSplitResult. - UrlError
- Error raised by the parsing functions, mirroring the
ValueErrors inurllib.parse.
Functions§
- parse_
qs - Parse a query string into a list of
(name, values)entries (insertion order, values grouped per name). Port ofparse_qs(which returns a dict). Seeparse_qslfor the arguments. - parse_
qsl - Parse a query string into a list of
(name, value)pairs. Port ofparse_qsl. - quote
- Percent-encode a string. Letters, digits and
_.-~are never quoted; everything else outsidesafebecomes%XX. The string is encoded as UTF-8 first. Port ofquote(str input). - quote_
from_ bytes - Like
quote, but takes a byte slice directly and does no string-to-bytes encoding. Always returns an ASCII string. Port ofquote_from_bytes. - quote_
plus - Like
quote, but also encodes' 'as'+', as required for HTML form values. Port ofquote_plus(Python defaultssafeto""). - unquote
- Replace
%xxescapes by their character equivalent, decoding as UTF-8 with invalid sequences replaced by U+FFFD (Python’serrors='replace'). Port ofunquote. - unquote_
plus - Like
unquote, but also replaces'+'with' ', as required for HTML form values. Port ofunquote_plus. - unquote_
to_ bytes - Replace
%xxescapes by the bytes they encode. Port ofunquote_to_bytes(the input string is encoded as UTF-8 first). - urldefrag
- Remove the fragment from a URL, returning the defragmented URL and the fragment. Port of
urldefrag. - urlencode
- Encode a sequence of
(key, value)pairs into akey=value&...query string, with each part run throughquote_plus. Port ofurlencode(withdoseq=False, the defaultquote_plus). - urlencode_
doseq - Like
urlencode, but each key maps to a sequence of values, emitting onekey=valuepair per element. Port ofurlencodewithdoseq=True. - urljoin
- Join a base URL and a possibly-relative URL into an absolute one. Port of
urljoin. - urlparse
- Parse a URL into 6 components:
scheme://netloc/path;params?query#fragment. Port ofurlparse. Likeurlsplit, but also splits;paramsoff the path for schemes inuses_params. - urlsplit
- Parse a URL into 5 components:
scheme://netloc/path?query#fragment. Port ofurlsplit. - urlunparse
- Combine 6 URL components into a string. Port of
urlunparse. - urlunsplit
- Combine 5 URL components into a string. Port of
urlunsplit.