silk.http.origin.
The origin of an HTTP request: the scheme, host, and port that a connection goes to.
When to use#
Use Origin.fromUri to get the origin of a URL. The client connects to the host and port of
the origin, and for HTTPS checks the server certificate against the host. Use
Origin.authorityInto to write the Host value.
Details#
An Origin has no path, and does not allocate. A missing port becomes 80 for http and 443 for
https. Origin.authorityInto writes a lowercase domain, the canonical IP text, and the port
only when it is not the default. A Host field that you give never changes the host that TLS
checks.
Errors#
OriginErrorif the scheme is nothttporhttps, the URI has user information, or the host or port is not valid.
Examples#
Get the origin of a URL#
import silk.http.origin { Origin, OriginError }
import silk.net.address { Port }
import silk.core.result { Result }
import silk.uri.uri { Uri }
import silk.uri.reference { ParseError }
pub fn main() -> i32 {
let uri = match Uri.parse("https://Example.COM/docs?page=2") {
Result<Uri, ParseError>.Success { value } => value
Result<Uri, ParseError>.Failure { error: _ } => {
return 1
}
}
let origin = match Origin.fromUri(&uri) {
Result<Origin, OriginError>.Success { value } => value
Result<Origin, OriginError>.Failure { error: _ } => {
return 1
}
}
if !Origin.secure(&origin) || Port.value(&Origin.port(&origin)) != 443 {
return 1
}
// The default port is not written, and the domain is lowercase.
let mut output: [u8; 64] = [0; 64]
let length = match Origin.authorityInto(&origin, &mut output) {
Result<usize, OriginError>.Success { value } => value
Result<usize, OriginError>.Failure { error: _ } => {
return 1
}
}
if length != 11 || output[0] != 101 {
return 1
}
if !Origin.matchesAuthority(&origin, "example.com:443") {
return 1
}
return 0
}Import as Origin with import silk.http.origin { Origin }.
Public declarations: 2.
OriginError#
pub enum OriginErrorThe reason that an origin is not valid, or that its text does not fit: the scheme is not http
or https, the URI has user information, the host or port is not valid, or the output is too
small.
InvalidScheme#
InvalidSchemeThe scheme is not http or https.
UserInfo#
UserInfoThe URI has user information, as in user@host.
MissingHost#
MissingHostThe URI has no host.
InvalidHost#
InvalidHostThe host is not a valid domain name or IP address. See Host.parse in silk.net.address.
InvalidPort#
InvalidPortThe port is empty, not a number, or not from 1 to 65535.
OutputTooSmall#
OutputTooSmallThe output buffer is too small. Nothing was written.
Implementation Debug for OriginError#
impl Debug for OriginErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as InvalidScheme.
Origin#
pub struct OriginThe scheme, host, and port of an HTTP server. Make it with Origin.fromUri or
Origin.make.
Implementation Copy for Origin#
impl Copy for OriginImplementation Origin#
impl OriginAssociated function make#
pub fn make(host: Host, port: Port, secure: bool) -> Result<Origin, OriginError>Returns the origin for host and port. secure selects HTTPS. Port 0 returns
InvalidPort.
Associated function fromUri#
pub fn fromUri(uri: &Uri) -> Result<Origin, OriginError>Returns the origin of an http or https URI.
Details#
A missing port becomes 80 for http and 443 for https. The path, query, and fragment are
ignored. User information returns UserInfo.
Method host#
pub fn host(self: &Self) -> HostReturns the host. The client resolves it, and TLS checks the certificate against it.
Method port#
pub fn port(self: &Self) -> PortReturns the port, with the default port if the URI had none.
Method secure#
pub fn secure(self: &Self) -> boolReturns true for HTTPS.
Method equals#
pub fn equals(self: &Self, other: &Self) -> boolReturns true if the scheme, the port, and the host are equal. Domains are compared without
case.
Method authorityInto#
pub fn authorityInto(self: &Self, output: &mut [u8]) -> Result<usize, OriginError>Writes host or host:port to the start of output, and returns the length. The port is
written only when it is not the default. If output is too small, it writes nothing.
Method authorityWithPortInto#
pub fn authorityWithPortInto(self: &Self, output: &mut [u8]) -> Result<usize, OriginError>Writes host:port to the start of output, and returns the length. The port is always
written. If output is too small, it writes nothing.
Method matchesAuthority#
pub fn matchesAuthority(self: &Self, text: string) -> boolReturns true if text names this origin.
Details#
text is a host with an optional port. The default port can be written or not. Domains are
compared without case, and equal IP addresses match in any spelling.
Category: HTTP.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.