Skip to main content

Module url_codec

Module url_codec 

Source
Expand description

URL / IRI / URI helpers — url_encode, uri_to_iri, iri_to_uri, escape_uri_path, filepath_to_uri, urlsafe_base64_encode / _decode and friends. Pure std, so it is always compiled. URL codec helpers: application/x-www-form-urlencoded decoding, RFC 3986 percent encoding, and urlsafe base64.

URL decoders are an easy place to hide a security bug, so all of it lives here and a fix lands everywhere at once.

§Decoding rules

  • + becomes a space. This is the form convention, and every browser form encoder uses it.
  • %XX with two hex digits becomes that byte. Mixed case is fine.
  • A bad or truncated escape keeps the literal % and parsing goes on, like serde_urlencoded and RFC 3986 §2.1.
  • Bytes that are not valid UTF-8 become U+FFFD through String::from_utf8_lossy. A single bad byte must not wipe the whole output.

This is not a full RFC 3986 decoder: it does not treat reserved characters differently per URI part. Use url::Url to parse a whole URL; use this for form bodies and query-string values.

Functions§

escape_uri_path
Percent-encode the path part of a URI. / is kept, so the path structure survives.
filepath_to_uri
Turn a filesystem path into a URI path. Windows \ separators become /.
iri_to_uri
Turn an IRI (RFC 3987) into a plain URI (RFC 3986) by percent-encoding every byte outside the URI-safe set. Reserved syntax characters such as /, ?, # and % are kept, so a URI the caller built stays parseable.
percent_decode_path
Decode one path segment: %XX only, + stays literal.
uri_to_iri
Turn a URI back into IRI form: decode the escapes that make valid Unicode, and keep the URI’s structure.
url_decode
Decode a application/x-www-form-urlencoded string.
url_encode
Percent-encode every byte outside the RFC 3986 unreserved set (alphanumeric and - _ . ~).
urlsafe_base64_decode
Decode a URL-safe base64 string into raw bytes. Padding is optional, so senders that include = still work.
urlsafe_base64_encode
Base64-encode bytes with the URL-safe alphabet and the = padding stripped, so the result drops into a URL path or query value with no escaping. Used for the uidb64 part of /reset/<uidb64>/<token>/.