URLs v0.1.0
HostKind
pub enum HostKind: uint8 {
None = 0,
Domain = 1,
Ipv4 = 2,
Ipv6 = 3
}
What a URL's host is. Copy.
Cases
| Name | Value | Description |
|---|---|---|
None | 0 | The URL has no authority, so no host. |
Domain | 1 | A registered name, such as example.com, or an empty host. |
Ipv4 | 2 | A dotted-decimal IPv4 address. |
Ipv6 | 3 | An 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.
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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | what the URL's text is kept in |
text | char8[..] | the URL |
Returns: the URL, or fails with what is wrong with it; a relative reference fails with MissingScheme
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.
| Name | Type | Description |
|---|---|---|
reference | char8[..] | an absolute URL or a relative reference |
Returns: the resolved URL, kept with this URL's allocator
Text
pub func Text(&self) -> char8[..]
The whole URL, in normalized form.
Returns: the text
Scheme
pub func Scheme(&self) -> char8[..]
The scheme, in lower case, without its :.
Returns: the scheme
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
Username
pub func Username(&self) -> char8[..]
The user name before @, percent-encoded, or the empty text when there is none.
Returns: the user name
Password
pub func Password(&self) -> char8[..]?
The password after the user name's :, percent-encoded, or none when there is none.
Returns: the password
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
HostKind
pub func HostKind(&self) -> HostKind
What kind of host the URL has.
Returns: the kind
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
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
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
PathSegments
pub func PathSegments(&self) -> SegmentIterator
The path's segments, between its /s.
Returns: a walk over the segments
Query
pub func Query(&self) -> char8[..]?
The query after ?, percent-encoded, or none when there is no ?.
Returns: the query
Fragment
pub func Fragment(&self) -> char8[..]?
The fragment after #, percent-encoded, or none when there is no #.
Returns: the fragment
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.
| Name | Type | Description |
|---|---|---|
path | char8[..] | the new path, percent-encoded or not |
Returns: nothing, or fails when the path cannot be normalized
SetQuery
pub func SetQuery(
&var self,
query: char8[..]?
) throws UrlError
Replaces the query, or removes it with none.
| Name | Type | Description |
|---|---|---|
query | char8[..]? | the new query without its ?, percent-encoded or not |
Returns: nothing, or fails when the query cannot be normalized
SetFragment
pub func SetFragment(
&var self,
fragment: char8[..]?
) throws UrlError
Replaces the fragment, or removes it with none.
| Name | Type | Description |
|---|---|---|
fragment | char8[..]? | the new fragment without its #, percent-encoded or not |
Returns: nothing, or fails when the fragment cannot be normalized
Equals
pub func Equals(&self, other: &Url) -> bool
Whether two URLs are the same, which after normalization means the same text.
| Name | Type | Description |
|---|---|---|
other | &Url | the URL to compare with |
Returns: true when they are equal
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
SegmentIterator
pub struct SegmentIterator {
// private fields
}
A walk over a path's segments, between its /s. Copy.
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
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