silk.http.client_native.
The native provider for the Http service of silk.http.client.
When to use#
Give NativeHttp one time, near main, with Effect.provideMut<Http>(&mut http). All code
that requires ? &mut Http then sends real requests over sockets and TLS.
Details#
The provider has its own clocks, random source, and trust store, so you give only an allocator.
The defaults are a deadline of 30 seconds for each request, a response body of at most 16 MiB,
up to 10 redirects, and the PEM bundle of the platform for HTTPS: /etc/ssl/cert.pem on macOS
and /etc/ssl/certs/ca-certificates.crt on Linux, or the logical Windows ROOT store.
NativeHttp.fromEnvironment uses the file
in SSL_CERT_FILE when it is set, and the proxies in HTTPS_PROXY, HTTP_PROXY, ALL_PROXY,
and NO_PROXY. Requests use HTTP/1.1 and send Accept-Encoding: identity, so the body
arrives as the server sent it. A direct connection stays open after a response and the next
request to the same origin reuses it. The client keeps one for each of up to 8 origins, and a new
origin past that closes the oldest.
Redirects follow the Fetch standard. After 303, and after 301 or 302 for a POST, the client
sends a GET without a body. After 307 and 308, it sends the same method and body. A redirect
from HTTPS to HTTP fails. A redirect to another origin does not send Authorization and
Cookie.
Gotchas#
The deadline covers the TLS handshake and the HTTP exchange, but not the name lookup and the TCP
connect. The macOS keychain is not used, only the PEM bundle. NativeHttp.make reads no
environment variables, so it never uses a proxy. Only http:// proxies are supported.
Windows imports root certificates rather than the Windows chain-engine policy; revocation,
store usage properties, and disallowed-store entries are not imported.
Examples#
Set up the client#
The request in this example fails before it connects, because ftp is not supported.
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
import silk.http.client { Http, HttpError, HttpErrorKind, Request, Response }
import silk.http.client_native { NativeHttp }
import silk.core.result { Result }
effect fn check() -> i32
! OutOfMemoryError
? &mut Http | &mut Allocator {
// A scheme other than http or https fails before any connection.
let sent = run Effect.result(Http.send(Request.get("ftp://example.com/version")))
return match move sent {
Result.Success {..} => 1
Result.Failure { error } => match move error {
HttpError failure => {
if failure.kind == HttpErrorKind.InvalidUrl {
return 0
}
return 1
}
OutOfMemoryError allocation => {
fail move allocation
}
}
}
}
pub effect fn main() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let mut http = NativeHttp.make().baseUrl("https://api.example.com").header(
"Accept",
"application/json"
).timeoutSeconds(10)
return run check() |> Effect.provideMut<Http>(&mut http)
|> Effect.provideMut<Allocator>(&mut allocator)
}Import as HttpClientNative with import silk.http.client_native as HttpClientNative.
Public declarations: 6.
DEFAULT_TIMEOUT_SECONDS#
pub const DEFAULT_TIMEOUT_SECONDS: u64The default deadline for one request, in seconds.
DEFAULT_MAX_BODY_BYTES#
pub const DEFAULT_MAX_BODY_BYTES: usizeThe default response body limit, in bytes.
DEFAULT_MAX_REDIRECTS#
pub const DEFAULT_MAX_REDIRECTS: usizeThe default number of redirects followed.
DEFAULT_CONCURRENCY#
pub const DEFAULT_CONCURRENCY: usizeThe default number of requests that Http.sendAll runs at the same time.
MAX_DEFAULT_HEADERS#
pub const MAX_DEFAULT_HEADERS: usizeThe maximum number of headers that NativeHttp.header adds.
NativeHttp#
pub struct NativeHttp<'config>A native HTTP client. It holds the defaults for all requests: the base URL, headers, limits, and trust.
When to use#
Give it one time, near main, with Effect.provideMut<Http>(&mut http). All code that
requires ? &mut Http then sends real requests over sockets and TLS.
Details#
The provider has its own clocks, random source, and trust store, so you give only an
allocator. The defaults are a deadline of 30 seconds for each request, a response body of at
most 16 MiB, up to 10 redirects, and the PEM bundle of the platform for HTTPS:
/etc/ssl/cert.pem on macOS and /etc/ssl/certs/ca-certificates.crt on Linux, or the logical
Windows ROOT certificate store. Requests use HTTP/1.1 and send Accept-Encoding: identity,
so the body arrives as the server sent it. The next request to an origin reuses an open
connection to it. The client keeps up to 64 open connections, several for one origin when
requests ran at the same time. It reads the trust bundle on its first HTTPS connection and
keeps the anchors for later ones.
Http.sendAll runs up to 32 requests at the same time on one thread, each on its own
connection. Set the number with NativeHttp.withConcurrency.
Redirects follow the Fetch standard. After 303, and after 301 or 302 for a POST, the client
sends a GET without a body. After 307 and 308, it sends the same method and body. A redirect
from HTTPS to HTTP fails. A redirect to another origin does not send Authorization and
Cookie.
Gotchas#
The deadline covers the TLS handshake and the HTTP exchange, but not the name lookup and the
TCP connect. A change to the trust bundle file takes effect in a new client. The macOS
keychain is not used, only the PEM bundle. NativeHttp.make reads no
environment variables, so it never uses a proxy. Only http:// proxies are supported.
Windows imports root certificates rather than chain-engine policy; revocation, store usage
properties, and disallowed-store entries are not imported.
Implementation NativeHttp<'config>#
impl<'config> NativeHttp<'config>Associated function make#
pub fn make() -> NativeHttp<'config>Returns a client with the default limits and platform trust. It reads nothing yet.
Details#
The defaults are a deadline of 30 seconds for each request, a response body of at most 16
MiB, up to 10 redirects, and the PEM bundle /etc/ssl/cert.pem on macOS or
/etc/ssl/certs/ca-certificates.crt on Linux, or the logical Windows ROOT store.
Windows imports certificate anchors without chain-engine policy, store usage properties,
disallowed-store entries, or revocation. Use NativeHttp.fromEnvironment to honor
SSL_CERT_FILE, which replaces platform trust with the configured PEM bundle.
Associated function fromEnvironment#
pub effect fn fromEnvironment() -> NativeHttp<'config>
! HttpError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut AllocatorMakes a client that uses SSL_CERT_FILE as its PEM bundle and the proxy variables when they
are set.
Details#
A relative SSL_CERT_FILE is resolved against the working directory. An empty value fails.
HTTPS requests go through HTTPS_PROXY, then https_proxy, ALL_PROXY, and all_proxy,
the first one that is set and not empty. Plain HTTP requests go through HTTP_PROXY, then
http_proxy, ALL_PROXY, and all_proxy. HTTPS uses a CONNECT tunnel, so TLS still
checks the server certificate. A proxy URL such as http://user:pass@proxy:3128 sends Basic
credentials to the proxy. A URL without a scheme is read as http://.
NO_PROXY or no_proxy lists hosts that are reached directly, separated by commas. An entry
matches the host and its subdomains, so example.com and .example.com both match
api.example.com. A port in an entry is ignored, and * matches every host. An address
range such as 10.0.0.0/8 matches nothing; list the addresses instead.
Errors#
A proxy URL that is not valid fails with InvalidUrl. Only http:// proxies are supported,
so an https:// or socks5:// proxy URL fails too.
Method baseUrl#
pub fn baseUrl(self: Self, url: string<'config>) -> NativeHttp<'config>Puts url before each request URL that has no scheme, such as "/repos".
Method header#
pub fn header(self: Self, name: string<'config>, value: string<'config>) -> NativeHttp<'config>Adds a header to each request. A header of the request with the same name replaces it.
Method timeoutSeconds#
pub fn timeoutSeconds(self: Self, seconds: u64) -> NativeHttp<'config>Sets the default deadline for each request.
Method maxBodyBytes#
pub fn maxBodyBytes(self: Self, limit: usize) -> NativeHttp<'config>Sets the default response body limit.
Method maxRedirects#
pub fn maxRedirects(self: Self, hops: usize) -> NativeHttp<'config>Sets how many redirects requests follow by default. With 0, a redirect response is returned as it is.
Method withConcurrency#
pub fn withConcurrency(self: Self, limit: usize) -> NativeHttp<'config>Sets how many requests Http.sendAll runs at the same time. A value of 0 counts as 1.
Method reportProgress#
pub fn reportProgress(self: Self, progress: BodyProgress) -> NativeHttp<'config>Counts the body of each successful final response that send reads in progress.
Details#
The client never opens or closes a step: open one with Progress.begin before send, such
as Downloading silk.tar.gz, and finish it after. While a step is open, the client sets its
total from the response's Content-Length and counts each body chunk as it arrives, so the
live line shows the bytes, the rate and the time left. Redirects, responses with a status
outside 200 to 299, and the requests of sendAll that run at the same time are not counted.
Reach the progress with NativeHttp.progress.
Method progress#
pub fn progress<'a>(self: &'a mut Self) -> &'a mut BodyProgressThe progress that NativeHttp.reportProgress gave the client, or one that writes nothing.
Method failOnStatus#
pub fn failOnStatus(self: Self) -> NativeHttp<'config>Makes each final response with a status outside 200 to 299 fail with a Status error.
Method trustBundle#
pub fn trustBundle(self: Self, path: string<'config>) -> NativeHttp<'config>Uses the PEM bundle at the absolute path path, such as "/etc/ssl/cert.pem", instead of
platform trust. On Windows, use an absolute native drive path or /C:/... machine path;
the bundle is opened on local NTFS without following reparse points.
Implementation Http for NativeHttp<'config>#
impl<'config> Http for NativeHttp<'config>Operation sendAll#
effect fn sendAll(self: &mut Self, requests: List<Request<'_>>) -> List<Result<Response, HttpError>>
! OutOfMemoryError
? &mut AllocatorOperation send#
effect fn send(self: &mut Self, request: Request<'_>) -> Response ! HttpError | OutOfMemoryError ? &mut AllocatorCategory: HTTP.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, x86_64-pc-windows-msvc.