URL parsing, building, relative resolution, and percent-encoding per RFC 3986.
Provides parse to decompose a URL into its components, build to
reassemble a component table back into a string, absolute to resolve
relative URLs against a base, and escape/unescape for percent-encoding
and decoding.
| Name | Signature |
|---|---|
escape | escape(s) -> encoded |
unescape | unescape(s) -> decoded |
parse | parse(url_str) -> parsed, err |
build | build(parsed) -> url_str |
absolute | absolute(base_url, relative_url) -> resolved |
escape(
s) ->encoded
Percent-encode a string per RFC 3986
Replaces every byte that is not in the RFC 3986 unreserved set
(A-Z a-z 0-9 - . _ ~) with its %XX hex-encoded form.
This is not application/x-www-form-urlencoded encoding
(spaces become %20, not +).
unescape(
s) ->decoded
Percent-decode a string
Replaces every %XX hex-encoded sequence with the corresponding byte.
Does not decode + as space — that is application/x-www-form-urlencoded
behavior, not generic percent-decoding.
parse(
url_str) ->parsed,err
Parse a URL into its component parts per RFC 2396
Decomposes a URL string into a table with the following fields
(all nil when absent):
scheme — e.g. "https"
authority — e.g. "user:pass@host:443"
userinfo — e.g. "user:pass"
user — e.g. "user"
password — e.g. "pass"
host — e.g. "example.com" (IPv6 brackets stripped)
port — e.g. "443" (string, not number)
path — e.g. "/index.html"
params — semicolon-separated parameters (RFC 2396 ;params)
query — e.g. "key=val&foo=bar" (without leading ?)
fragment — e.g. "section1" (without leading #)
Returns nil, "invalid url" for empty or nil input.
build(
parsed) ->url_str
Rebuild a URL string from its parsed components
Accepts a table with the same fields returned by parse() and
reassembles them into a URL string. Fields that are nil are omitted.
When both host and authority are present, host (plus port,
user, password) takes precedence for rebuilding the authority portion.
IPv6 hosts are automatically wrapped in brackets.
absolute(
base_url,relative_url) ->resolved
Resolve a relative URL against a base URL per RFC 2396 section 5.2
If relative_url has a scheme, it is returned as-is (already absolute).
Otherwise, missing components are inherited from base_url following
the RFC priority: scheme, then authority, then path (merged via
dot-component normalization), then params, then query. Fragment is
always from the relative URL. base_url may also be a parsed table.