Handles the subset of RFC 3986 URIs relevant to HTTP clients:

scheme://[user:pass@]`host[:port]`/path[?query][#fragment]

Only http and https schemes are supported. Fragment is parsed but ignored for request purposes (not sent to server per RFC 7230 Β§5.1).

Example:

    var u = Url.parse("https://api.example.com:8443/v1/items?filter=active")
    print(u.host)   # api.example.com
    print(u.port)   # 8443
    print(u.path)   # /v1/items
    print(u.query)  # filter=active
    ```

Structs

struct UrlParseError

struct UrlParseError

Raised when a URL string cannot be parsed.

Fields

message String

Methods

fn __init__ static

__init__(out self, message: String)

Args

message String
self Self out

Returns

Self

fn write_to

write_to[W: Writer](self, mut writer: W)

Type Parameters

W Writer

Args

self Self
writer W mut

struct Url

struct Url

A parsed HTTP/HTTPS URL.

Fields: scheme: "http" or "https". host: Hostname or IP, without brackets (IPv6 brackets stripped). port: Numeric port; defaults to 80 (http) or 443 (https). path: URL path including leading /; "/" if absent. query: Query string without leading ?; "" if absent. fragment: Fragment without leading #; "" if absent.

This type is Movable but not Copyable.

Fields

scheme String
host String
port UInt16
path String
query String
fragment String

Methods

fn __init__ static

__init__(out self, scheme: String, host: String, port: UInt16, path: String, query: String = "", fragment: String = "")

Args

scheme String
host String
port UInt16
path String
query String
Default: ""
fragment String
Default: ""
self Self out

Returns

Self

fn __init__ static

__init__(out self, *, deinit take: Self)

Args

take Self deinit
self Self out

Returns

Self

fn parse static

parse(raw: String) -> Self

Parse a URL string into a ``Url``.

Args

raw String

Full URL string (http://... or https://...).

Returns

Self

Parsed Url.

⚠️ Raises

UrlParseError: If the URL is missing a scheme, host, or uses an unsupported scheme.

fn request_target

request_target(self) -> String

Return the request-target for the HTTP request line.

Args

self Self

Returns

String

/path?query or /path if query is empty.

fn is_tls

is_tls(self) -> Bool

Return True if the scheme is ``https``.

Args

self Self

Returns

Bool

True for https, False for http.