v0.4.0

URLs v0.1.0

Source
Parsing a URL into its parts and reading them back.

HostKind

pub enum HostKind: uint8 {
    None = 0,
    Domain = 1,
    Ipv4 = 2,
    Ipv6 = 3
}

What a URL's host is. Copy.

Source

Cases

NameValueDescription
None0The URL has no authority, so no host.
Domain1A registered name, such as example.com, or an empty host.
Ipv42A dotted-decimal IPv4 address.
Ipv63An IPv6 address, written between brackets in the URL.

Url

pub struct Url {
    // private fields
}

An absolute URL in normalized form. Read it with Parse and resolve references against it with Resolve. Every component is a view of the URL's own text, still percent-encoded; Decode turns one into text. A URL owns its text, so it is move-only, and Clone makes an independent copy.

Source

Associated functions

Parse

pub func Parse(
    allocator: Allocator,
    text: char8[..]
) -> Url throws UrlError

Reads text as an absolute URL and puts it in normalized form. Spaces and control characters around the text are ignored. A character a component cannot hold as itself, such as a space or a non-ASCII byte, is percent-encoded; a host is not, and refuses one.

NameTypeDescription
allocatorAllocatorwhat the URL's text is kept in
textchar8[..]the URL

Returns: the URL, or fails with what is wrong with it; a relative reference fails with MissingScheme

Source

Methods

Resolve

pub func Resolve(
    &self,
    reference: char8[..]
) -> Url throws UrlError

Resolves reference against this URL, as a browser resolves a link on a page, following section 5 of RFC 3986. An absolute reference stands on its own; //host/path, /path, path, ?query and #fragment replace what they name and keep the rest of this URL.

NameTypeDescription
referencechar8[..]an absolute URL or a relative reference

Returns: the resolved URL, kept with this URL's allocator

Source

Text

pub func Text(&self) -> char8[..]

The whole URL, in normalized form.

Returns: the text

Source

Scheme

pub func Scheme(&self) -> char8[..]

The scheme, in lower case, without its :.

Returns: the scheme

Source

HasAuthority

pub func HasAuthority(&self) -> bool

Whether the URL has an authority: a // with a host, as http: URLs do and mailto: URLs do not.

Returns: true when it has one

Source

Username

pub func Username(&self) -> char8[..]

The user name before @, percent-encoded, or the empty text when there is none.

Returns: the user name

Source

Password

pub func Password(&self) -> char8[..]?

The password after the user name's :, percent-encoded, or none when there is none.

Returns: the password

Source

Host

pub func Host(&self) -> char8[..]?

The host: a lower-case name, a dotted IPv4 address, or an IPv6 address without its brackets. None when the URL has no authority.

Returns: the host

Source

HostKind

pub func HostKind(&self) -> HostKind

What kind of host the URL has.

Returns: the kind

Source

Port

pub func Port(&self) -> uint16?

The port written in the URL, or none when there is none or it is the scheme's default, which normalizing leaves out.

Returns: the port

Source

PortOrDefault

pub func PortOrDefault(&self) -> uint16?

The port a connection uses: the one written, or the scheme's default, or none for a scheme without one.

Returns: the port

Source

Path

pub func Path(&self) -> char8[..]

The path, percent-encoded, without dot segments. A URL with an http-like scheme always has at least /.

Returns: the path

Source

PathSegments

pub func PathSegments(&self) -> SegmentIterator

The path's segments, between its /s.

Returns: a walk over the segments

Source

Query

pub func Query(&self) -> char8[..]?

The query after ?, percent-encoded, or none when there is no ?.

Returns: the query

Source

Fragment

pub func Fragment(&self) -> char8[..]?

The fragment after #, percent-encoded, or none when there is no #.

Returns: the fragment

Source

SetPath

pub func SetPath(&var self, path: char8[..]) throws UrlError

Replaces the path. A relative path is made absolute when the URL has an authority.

NameTypeDescription
pathchar8[..]the new path, percent-encoded or not

Returns: nothing, or fails when the path cannot be normalized

Source

SetQuery

pub func SetQuery(
    &var self,
    query: char8[..]?
) throws UrlError

Replaces the query, or removes it with none.

NameTypeDescription
querychar8[..]?the new query without its ?, percent-encoded or not

Returns: nothing, or fails when the query cannot be normalized

Source

SetFragment

pub func SetFragment(
    &var self,
    fragment: char8[..]?
) throws UrlError

Replaces the fragment, or removes it with none.

NameTypeDescription
fragmentchar8[..]?the new fragment without its #, percent-encoded or not

Returns: nothing, or fails when the fragment cannot be normalized

Source

Equals

pub func Equals(&self, other: &Url) -> bool

Whether two URLs are the same, which after normalization means the same text.

NameTypeDescription
other&Urlthe URL to compare with

Returns: true when they are equal

Source

Clone

pub func Clone(&self) -> Url

An independent copy, with text of its own from the same allocator. A URL owns its text, so = cannot copy it; this is how to keep two. The copy is equal to this URL and changing either leaves the other as it was. Terminates the program when its text cannot be stored.

Returns: the copy

Source

SegmentIterator

pub struct SegmentIterator {
    // private fields
}

A walk over a path's segments, between its /s. Copy.

Source

Methods

Iterate

pub func Iterate(&self) -> SegmentIterator

The walk itself, so that for segment in url.PathSegments() reads every segment.

Returns: a copy of this walk

Source

Implements Iterator

SegmentIterator conforms to Iterator.

Next

pub func Next(&var self) -> char8[..]?

The next segment, still percent-encoded, or none after the last.

Returns: the segment

Source