module std::httpsource
HTTP/1.1 requests (alloy-ui's decisions/F12.md, "Network in std"): a
client in Alloy over std::net and, for https://, std::tls. send
waits for the whole response on whichever task calls it, and another
task may follow or cancel an exchange through its Control.
It writes the request with Host, Content-Length, `Accept-Encoding:
gzip and User-Agent: alloy` unless the request has its own; reads the
status line, headers and a body sized by its length, chunked or ending
with the connection; inflates a gzip body (std::deflate); and follows
redirections as browsers do: 303, and 301 or 302 after a POST, become
a GET without a body, and Authorization, Proxy-Authorization and
Cookie are dropped when the origin changes. A Client keeps the
connections servers leave open, at most 6 an origin, each for a minute
after its last response, for the requests sent through it after; send
makes a client for one request, whose redirections still reuse its
connection on its origin. A status of 404 or 500 is a response, not an
error; past redirects redirections the last is the response.
The timeout covers the whole exchange, connecting included, through
the socket's own timeouts against its deadline. Natively only: under
WASI and in the interpreter send gives Unsupported, and on the web's
page, which may not wait, it is a fault.
import std::result;
import std::string;
var request = Request::get(&"ftp://tickets.example/shows.json");
request.add_header(&"Accept", &"application/json");
request.timeout = Duration::from_seconds(10.0);
match (send(&request)) {
::Ok |&response| {
expect(response.ok());
}
::Err |&error| {
expect_equal(&f"{error}", &String::from(&"not an http:// or https:// URL"));
}
}
MethodnameHeaderRequestRequest::getRequest::post_jsonadd_headerResponseheaderokjsonHttpErrorformatProgressControlControl::createcancelprogresssendsend_withClientdisposesendsend_withExchangestartcancelprogress
type Methodsource
exp type Method = enum
Variants
GetHeadPostPutPatchDelete
Functions
fn namesource
exp fn name(self method: &Method) -> &[u8]
type Headersource
exp type Header = struct
Fields
exp name: Stringexp value: String
type Requestsource
exp type Request = struct
Fields
exp method: Method = Method::Getexp url: Stringexp headers: Vector<Header> = Vector<Header>::empty()exp body: Vector<u8> = Vector<u8>::empty()exp timeout: Duration = Duration::from_seconds(30.0)the whole exchange, connecting included
exp limit: u64 = 64 << 20the most bytes of body taken; more is TooLarge
exp redirects: u8 = 5the most followed
Functions
fn Request::getsource
exp fn Request::get(url: &[u8]) -> Request
fn Request::post_jsonsource
exp fn Request::post_json(url: &[u8], body: &Json) -> Request
A POST of the JSON, with Content-Type: application/json.
fn add_headersource
exp fn add_header(self request: &var Request, name: &[u8], value: &[u8])
The name and value are written as they are: send gives
HttpError::Header when the name is empty or holds ':', or either holds
a CR, LF or NUL.
type Responsesource
exp type Response = struct
Fields
exp status: u16exp headers: Vector<Header>exp body: Vector<u8>decoded: chunks joined, gzip inflated
exp url: Stringwhere it came from, after redirections
Functions
fn headersource
exp fn header(self response: &Response, name: &[u8]) -> Option<&[u8]>
The first header of the name, compared without case.
fn oksource
exp fn ok(self response: &Response) -> bool
A status from 200 to 299.
fn jsonsource
exp fn json(self response: &Response) -> Option<Json>
The body as JSON, None when it is not.
type HttpErrorsource
exp type HttpError : Format = enum
Variants
Addressnot an http:// or https:// URL
Headera header's name or value cannot be written (
add_header)Unsupportedno network here: WASI, the interpreter
Resolvethe host has no address
Connectno address took the connection
Certificate: Stringthe server's certificate is not trusted for the host: why
Tlsthe handshake failed otherwise
TimeoutCancelledClosedthe connection ended before the response did
Malformedwhat came back is not HTTP/1.1
TooLarge
Functions
fn formatsource
exp fn format(self error: &HttpError, out: &var String)
type Progresssource
exp type Progress = struct
How far an exchange has gone.
Fields
exp sent: u64exp to_send: u64exp received: u64exp total: Option<u64>the response's Content-Length, when it gave one
type Controlsource
exp type Control : Shared = struct
What another task sees of an exchange, and how it stops one. A control
follows one exchange at a time, its progress the latest one's; once
cancelled it stays cancelled, as a browser's AbortSignal does, so a
cancel that comes before or between exchanges is not lost and every
later send_with through it gives Cancelled at once: the next
exchange takes a new control.
Functions
fn Control::createsource
exp fn Control::create() -> Control
fn cancelsource
exp fn cancel(self control: &Control)
Stops the exchange: its connection is shut down, and send_with
returns Cancelled, now and through this control from now on.
fn progresssource
exp fn progress(self control: &Control) -> Progress
fn sendsource
exp fn send(request: &Request) -> Result<Response, HttpError>
Sends the request and waits for the whole response, on connections of its own.
fn send_withsource
exp fn send_with(request: &Request, control: &Control) -> Result<Response, HttpError>
Sends the request as send does, another task following it or
cancelling it through the control.
type Clientsource
exp type Client : Shared + Disposable = struct
Connections kept open between requests, by origin: a request takes one
to its origin the client keeps, else connects, and gives it back once
its response ended where it said and the server keeps it open, the
client keeping at most 6 an origin, each closed once idle for
idle_for. Tasks share a client: each request takes its own
connection. Disposing the client closes those it keeps.
import std::result;
import std::string;
var client = Client {};
const result = client.send(&Request::get(&"ftp://tickets.example/shows.json"));
expect(result.is_err());
client.dispose();
Fields
exp idle_for: Duration = Duration::from_seconds(60.0)
Functions
fn disposesource
exp fn dispose(self client: &var Client)
fn sendsource
exp fn send(self client: &Client, request: &Request) -> Result<Response, HttpError>
Sends the request as http::send does, on a connection the client
keeps when it has one to the origin.
fn send_withsource
exp fn send_with(
self client: &Client,
request: &Request,
control: &Control
) -> Result<Response, HttpError>
Sends the request as send_with does, on a connection the client keeps
when it has one to the origin.
type Exchangesource
exp type Exchange = struct
An exchange start handed to the browser, which another part of the
program follows or cancels by it.
Functions
fn startsource
exp fn start(request: Request, done: |=| (Result<Response, HttpError>)) -> Exchange
web-wasm32 only: hands the request to the browser's fetch and returns
at once; done runs from the browser's loop once the exchange ends,
cancelled too. The browser follows redirections and inflates the body
itself, and a URL relative to the page is taken. Elsewhere a program
waits with send on a task of its own, and this panics.
fn cancelsource
exp fn cancel(self exchange: &Exchange)
Stops the exchange: done gets Cancelled, unless it ended first.
fn progresssource
exp fn progress(self exchange: &Exchange) -> Progress
How far the exchange has gone; nothing once it ended. Bytes sent jump
from none to all, as fetch tells no more of an upload.