JFLib

netio/url.rs#

Source: lib/netio/url.rs

url -- absolute http(s) URLs, references resolved against them, percent encoding, query
strings

  Url::parse("https://host:9443/api?x=1")   the host checked: a name of letters, digits,
       - . _ ~ (no IDN: give its A-label, "xn--..."), an IPv4 address in dotted form, or an IPv6
       literal in brackets; a name that ends in a number must be a dotted IPv4 address (no
       "0x7f.1" or "2130706433", which some resolvers read as 127.0.0.1); no user:password@
  url.join(reference)   RFC 3986 5.2 (netpkt::uri): "https://other/x", "//host/x", "/x", "../x"
  url.same_origin(&other), url.authority() ("host:port", for CONNECT), url.path()

Bytes beyond ASCII and " < > ` { } in a path or query are percent-encoded; a fragment is
dropped (it never goes to a server).
  • struct Url an absolute URL: scheme://host[:port][/path][?query]
    • scheme: String -- "http" or "https" (lower case)
    • host: String -- host name or IP literal (IPv6 without brackets)
    • port: u16
    • target: String -- the path and query ("/" at least)

impl Url

  • fn parse(s: &str) -> Option<Url> parse; the port defaults to 80 / 443

  • fn is_https(&self) -> bool

  • fn host_header(&self) -> String "host:port" for the Host header (port left out when it is the default)

  • fn authority(&self) -> String "host:port" with the port always (a CONNECT request's target)

  • fn with_target(&self, target: &str) -> Url the URL with another target ("/api/jobs")

  • fn origin(&self) -> String "https://host:port"

  • fn same_origin(&self, other: &Url) -> bool the same scheme, host and port (RFC 6454): what a redirect must keep for credentials to go along

  • fn path(&self) -> &str the path without the query

  • fn join(&self, reference: &str) -> Option<Url> a reference resolved against this URL as RFC 3986 5.2 does it (netpkt::uri): absolute ("https://other/x"), network-path ("//host/x"), absolute path ("/x"), relative path ("x", "../x"), query ("?q") or empty; dot segments removed, the fragment dropped. None for another scheme, a reference with spaces or controls, or a URL Url::parse refuses

  • fn percent_encode(s: &str) -> String percent-encode everything but the unreserved characters (RFC 3986)

  • fn percent_decode(s: &str, plus: bool) -> String decode %XX (and '+' as a space in query strings when plus); invalid escapes stay as they are, invalid UTF-8 becomes U+FFFD

  • fn query_pairs(q: &str) -> Vec<(String, String)> the (key, value) pairs of a query string ("a=1&b=x%20y"), decoded

  • fn split_target(target: &str) -> (&str, &str) split a target into its path and query ("" when there is none)

Source: docs/API.md in the JFLib repository.