Net.HTTP.Client
An HTTP/1.1 client: requests sent over a pool of connections, each one
reaching only what a ConnectPolicy allows.
let c = Client.client(Client.defaultConfig)?
send(c, request(GET, "https://api.example.com/users/1"), Duration.seconds(10), fun(resp) {
expectStatus(resp, OK)?
receive(resp.body, 1048576)
})
The response is the callback's. send calls use with the response
once its head has arrived, and the body can be read only inside use.
When use returns, what it left unread is read and thrown away if that is
cheap (the limits' drainBytes), so the connection can carry the next request;
otherwise the connection is closed. A body read after use returned fails
with Transport of an IOError whose kind is Closed. use's result,
and its error, are send's.
Every head, pulled. exchange calls use with the exchange instead,
and next returns each head the server responds with: every interim (1xx)
response as Informational, then the final one as Final, whose body is
read as send's is. A proxy relays a 103 that way. send is exchange
with the interim responses skipped. A 100 Continue is never returned: the
server sends it in response to the client's own Expect. A server may
send at most 16 interim responses before the final one; more, or a 101 to
a request that asked for no upgrade, is BadResponse.
Errors come back through FromHTTP, so ? lifts them into the
caller's own error type. A request that got no response is SendFailed
of a ClientError, which records whether the server may have seen it:
NotSent, MaybeSent or BadResponse.
Deadlines. The timeout covers everything: resolving, connecting, the
TLS handshake, sending, the response's heads, every redirect send
follows, the one retry, and each read of the body inside use. The
...Unbounded calls have no total limit, for a transfer whose length
cannot be known ahead. Two limits hold for both: resolving, connecting and
the handshake together within the connect limit (30 s), and the limits'
stall (30 s), which closes a connection on which a read or write has waited
that long while no byte moved either way. A deadline that passes part-way
through a message closes the connection.
Connections are pooled per origin -- scheme, host and port -- and a
connection is reused only once both directions ended exactly: the request
was written whole, and its response was read to its exact end with
nothing after it. A byte past the end would be read as the next response,
so a connection that has one is closed. Up to 64 idle connections are kept
per origin and 100 in all, the oldest closed first past that, each for up
to the limits' idleTimeout (60 s). A Client is safe to share between
tasks.
One retry. A pooled connection may be stale: closed by the server
while it was idle, which the client learns only by using it, when it
ends before any byte of a response. Then the request is sent once more
on a new connection, if it is safe to: it was NotSent, or it was
MaybeSent and its method is idempotent or it has an Idempotency-Key;
and its body can be sent again. Nothing else is retried.
Redirects are responses like any other: a 3xx reaches use as it is.
redirect(resp, req) builds the request that follows one, under the rules
its comment gives, so a program that must see each hop loops itself.
A client built with follow(n) has send run that loop for up to n hops; the hop
past n is NotSent(BadRedirect(TooManyRedirects)), and one redirect
refuses for a reason other than NotRedirect, NoLocation or
NotReplayable is NotSent(BadRedirect(...)). Each hop goes through
Net.resolve and the policy again, as any request does. A response's
url is the URL it came from, and its timings how long each step of its
hop took.
Upgrades. sendUpgrade offers to switch the connection to another
protocol (WebSocket, for one), and on a 101 hands use the connection as a
Duplex: bytes the server sent after its 101 are read first, and
shutdownWrite ends this side's output while the server goes on. A 101
must name only protocols the request offered, or it is BadResponse. The
timeout ends with the 101; after it, the new protocol keeps its own time,
with the deadlines use sets, and the stall limit no longer applies. The
connection is closed when use returns, never pooled. A proxy calls the
server's switch inside use, so both connections close together.
Bodies. A request's method carries its body as a Payload, made by
newPOST, newPUT, newPATCH or newExtension from any Body
(Net.HTTP.Body), which holds its length and whether it can be sent
again. A Stream body sends the trailers its function returns, and a
relayed Incoming the trailers it came with. A body that fails, or does
not match the length it declared, fails the request, and the connection
is closed. A response's trailers are trailers(resp.body) once its body
has been read to the end.
On the wire. HTTP/1.1, one request at a time per connection, never
pipelined. The client writes Host (hostField), the framing
(Content-Length from the body's length, contentLength, or chunked when
it has none), Expect: 100-continue, and nothing else of its own. A
request body is written by a task of its own while the response is read,
so a response the server sends before reading the whole body (a 413) is
read.
TLS offers ALPN http/1.1.
Expect: 100-continue goes with a body of 1 MiB or more, and with a
relayed Incoming whose sender waits for a 100 itself. The body is sent
once the server's 100 arrives, or after 1 s without one; a final response
first means it is not sent, and the connection is closed. A relayed body
is read only when it is sent, and that first read is what sends its
sender's 100, so a server's refusal reaches the sender before it has sent
a byte.
Overrides of a client's settings, each one None to keep the default. A
monoid: defaultConfig changes nothing, and a <> b takes each
setting from b where b sets it and from a where it does not, so
settings can be named, kept and combined in any grouping. Each function
below returns a Config that sets one thing, and has the name of the
field it sets. limits is itself a set of overrides, which combine the
same way.
The Config that sets nothing, so that every setting has its default.
Returns a Config that connects only where p allows. The default is
Net.publicInternet, so that a client reaches a loopback or private
address only where a program allows it.
Returns a Config that reads responses under l's overrides of the
limits. Of the limits, the client uses the head, line, chunk-line, trailer
and drain sizes; stall, for each read and write; and idleTimeout, for
how long a pooled connection is kept. Combining two such Configs
combines their limits.
Returns a Config that trusts r rather than the system's roots: a
test's own CA, or a service's.
Returns a Config under which send follows up to n redirects per
request. The default is 0. Panics if n is negative.
Returns a Config that allows d for resolving, connecting and the TLS
handshake together. The default is 30 s. Panics if d is not positive.
Returns a Config that keeps up to n idle connections per origin; 0
keeps none. The default is 64. Panics if n is negative.
Returns a Config that keeps up to n idle connections in all, across
every origin, closing the oldest past it; 0 keeps none. The default is
100. Panics if n is negative.
Returns a client with c's settings, and the defaults for those it does
not set, or why its TLS configuration cannot be made: a roots file that
does not load.
Closes the client's idle connections and frees its TLS configuration. A request in flight finishes; one sent later fails.
Returns Right(()) if the response's status is status, and otherwise
UnexpectedStatus with the status it has.
Returns the Host value the client writes for a request to u, for a
program that signs the request's fields.
Returns the Content-Length the client writes for req's body, for a
program that signs the request's fields: None if its method has no body
or the body's length is not known before it is sent, and so it goes
chunked.
A head the server sent in response to a request: an interim (1xx) one, of which there may be several, or the final response.
Sends req and calls use with the exchange, from which next reads
each head the server responds with, everything within timeout. See the
module comment.
exchange with no total limit: only the connect limit and the stall
limit.
Sends req and calls use with its final response, everything within
timeout. Interim (1xx) responses are skipped, and redirects followed as
far as the client's follow allows. See the module comment.
send with no total limit: only the connect limit and the stall limit.
Sends req offering to switch its connection to one of protocols, in
order of preference, and calls use with the final response and the
switched connection. A 101 is the switch: use reads and writes the new
protocol through the Duplex, and the connection is closed when use
returns. Any other final response reaches use as send's would, with
a duplex whose every read and write fails with an IOError of kind
Closed.
timeout covers everything up to the final response's head; the duplex is
bounded only by the deadlines use sets on it. Panics if protocols is
empty.
sendUpgrade with no limit on reaching the final response but the
connect limit and the stall limit. Panics if protocols is empty.
Returns whether the client sends req again after it failed with e on a
stale connection: when sending it again cannot repeat an effect, because
none of it was sent, or its method is idempotent, or it has an
Idempotency-Key; and its body, if it has one, can be sent again.
One request on one connection, and the heads read in response to it.
Returns the next head the server sent in response to the exchange's
request: each interim (1xx) one, then the final response. After the final
one, and once the exchange's callback has returned, fails with Transport
of an IOError whose kind is Closed.
Returns the request that follows the redirect resp to from, or why
there is none:
- a 301, 302 or 303 becomes a GET without a body, unless
fromis a GET or a HEAD, which stays as it is; a 307 or 308 keeps the method and the body, and isNotReplayableif the body cannot be sent again; - the target is
Locationresolved againstresp.url, and must behttporhttps, and nothttpafterhttps; Authorization,CookieandProxy-Authorizationare dropped when the scheme, host or port changes, andContent-Typewith the body.
The request it returns is sent through the client's ConnectPolicy like
any other.