Learn R Programming

rurl (version 3.0.1)

format_url: Format a URL for safe human display

Description

Renders each URL as a string a person can read safely: credentials are redacted, invisible and bidirectional-override code points are made visible as <U+XXXX> tokens, percent-encoded delimiters are left encoded so decoding cannot fabricate structure, and an internationalized host is shown in both its Unicode and its ASCII (punycode) spelling whenever the two differ.

Usage

format_url(url, engine = NULL)

Value

A character vector the same length as url. NA_character_ for input the WHATWG parser does not accept.

Arguments

url

A character vector of URLs.

engine

Optional psl_engine object from pslr::psl_engine() for per-request Public Suffix List resolution. NULL (default) uses the session-global engine.

The result is not a URL

format_url() output is display only. It is not reparsable, has no round-trip guarantee, and must never be fed back into serialize_url(), into a comparison key, or into anything that treats it as an address. Use serialize_url() for a standard-exact full string and get_clean_url() for the cleaning surface.

See Also

serialize_url() for the standard-exact full string, get_clean_url() for the cleaning surface, and safe_parse_url() for the parsed components.

Examples

Run this code
# Credentials are redacted, never shown.
format_url("https://user:[email protected]/a")

# Percent-encoded delimiters stay encoded: decoding them would fabricate
# structure that the URL does not have.
format_url("https://example.com/a%2Fb?x=a%26b%3Dc")

# An internationalized host is shown in both spellings when they differ.
format_url("https://xn--mnchen-3ya.de/p")

# Invisible and bidirectional-override code points are made visible.
format_url("https://example.com/p?q=x#%E2%80%AE%E2%80%8Bevil")

# A byte no valid UTF-8 sequence can contain is emitted, not decoded.
format_url("https://example.com/%FF%00")

# Input the WHATWG parser rejects is NA, not a guess.
format_url("example.com/x")

Run the code above in your browser using DataLab