~/bend-docscommunity

src/client.bend relies on unsafe/foreign

raw source on the hub · import ezhttp@0.25.0.0/src/client.bend as Client

ezhttp/src/client: fetch a URL. URL and HTTP parsing joined to the wire effect. Answers a structured Response (status + headers + body), not a hub-style "0\n…" string contract.

Trusted, not proved: this module and src/wire.bend act on src/wirespec.bend's answers in order. For a NoUrl they answer Err with its reason and open nothing; otherwise they resolve the exchange's host (Dns.resolve.literal.all, else Dns.lookup.all), connect to its addresses in turn until one connects, run the TLS handshake with the host when the exchange is secure, and send wire.payload of the request followed by the body's octets (body.shape), each call with the deadline wire.ms.of gives; a failed call is answered wire.fail of its phase, its error number and its reason (the last address's, for the connect), and nothing after it is done. Then, from the state wire.got(method) for the exchange's method, they receive up to 65536 octets at a time and append each receive's octets, packed, to those before it (Bytes.append); they judge each receive with wire.read.count of its count of octets, and only when that answers none with wire.read.next of a copy of its octets as text, dropped after the judging; they read again with the state the judging gives back until it answers Heard, closing the socket on every path, and give up with status 56 after 65536 receives. A failed exchange is Err with the reason wire.failed reads from its answer. Otherwise the answer is the octets received, and the response is what packed.reply reads from them by the exchange's method: a torn one as Err with its reason, any other as a Response with its status, its header fields and reason phrase as text (Qs.octets.headers, Qs.octets.text), and its body's octets, packed, as bytes; body_text (client.text) decodes them as UTF-8 only when called. Every client function builds its exchange with wire.exchange.body from the shape of the body it is given. http.request_opts sends the method, headers and body it is given, and http.post_opts, http.put_opts and http.delete_opts are it with POST, PUT and DELETE and follow unset. http.timed_out is false for a Response and wire.timed_out of an Err's reason. It is IO sequencing over bend-kit-wire's effects, which no law can state, and this module reaches those effects, so no law file can import even its pure helpers (http.answer, client.timed_out). Every decision it acts on is src/wirespec.bend's, proved under laws/ (the client's guarantees, such as laws/RequestLineAndHost.bend).

11 imports
import Base
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes
import ./url.bend as Url
import ./http.bend as Http
import ./body.bend as Body
import ./wire.bend as Wire
import ./wirespec.bend as Spec
import ./redirect.bend as Redirect
import ./auth.bend as Auth
import ./qs.bend as Qs
import ./packed.bend as Packed

Types

type Response source · line 56 · raw

Type

structured client result: transport/parse error, or an HTTP response: its status, its header fields and reason phrase as text (decoded from the octets received as UTF-8, laws/OctetsDecodeAsUtf8.bend, laws/ReasonPhraseFromFinalHead.bend), and its body's octets as they arrived, packed (client.text decodes them when asked)

Definitions

def client.text source · line 62 · raw

@b:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> String

a body's octets decoded as UTF-8 (laws/OctetsDecodeAsUtf8.bend)

def from.got source · line 67 · raw

@g:0x0b59049ea5717d6f2fd30293c7271ede/src/packed.Got -> Response

a response read off its octets as a client Response, its head's octet text decoded

def http.answer source · line 77 · raw

@+method:String -> @a:0x0b59049ea5717d6f2fd30293c7271ede/src/wire.Answer -> Response

an exchange's answer as a client Response, read by the request's method (packed.bend packed.reply: interim 1xx heads skipped, no body for HEAD, 1xx, 204 or 304); a failed exchange keeps its reason (wirespec.bend wire.failed)

def http.send source · line 86 · raw

@ex:0x0b59049ea5717d6f2fd30293c7271ede/src/wirespec.Exchange -> @tail:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> IO(Response)

an exchange as wirespec.bend wrote it, with the body's octets after it: sent and read by its own method, or the URL's reason

def http.loc.shaped source · line 96 · raw

@method:String -> @l:0x0b59049ea5717d6f2fd30293c7271ede/src/url.Loc -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @r:Pair(0x0b59049ea5717d6f2fd30293c7271ede/src/body.Shape, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> IO(Response)

an exchange for a body of this shape, sent with the body's octets

def http.loc source · line 106 · raw

@method:String -> @l:0x0b59049ea5717d6f2fd30293c7271ede/src/url.Loc -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> IO(Response)

a URL that came apart (or not) into an HTTP exchange with this body

def http.send.timed source · line 116 · raw

@ms:U32 -> @ex:0x0b59049ea5717d6f2fd30293c7271ede/src/wirespec.Exchange -> @tail:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> IO(Response)

an exchange as wirespec.bend wrote it, bounded by a timeout, sent and read by its own method

def client.timed.shaped source · line 131 · raw

@method:String -> @url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @ms:U32 -> @r:Pair(0x0b59049ea5717d6f2fd30293c7271ede/src/body.Shape, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> IO(Response)

a timed exchange for a body of this shape

def client.timed source · line 144 · raw

@method:String -> @url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> @ms:U32 -> IO(Response)

a request with this method, headers and body, bounded by a timeout in milliseconds

def client.get.timed source · line 154 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @ms:U32 -> IO(Response)

GET bounded by a timeout in milliseconds

def client.hop source · line 163 · raw

@left:Nat -> @url:String -> @r:Response -> Pair(0x0b59049ea5717d6f2fd30293c7271ede/src/redirect.Hop, Response)

the next hop after a response (redirect.bend), and the response; a failed exchange stops

def client.after source · line 172 · raw

@p:Pair(0x0b59049ea5717d6f2fd30293c7271ede/src/redirect.Hop, Response) -> @go:(@_:String -> IO(Response)) -> IO(Response)

a response, or the GET its hop asks for

def client.follow source · line 196 · raw

@+left:Nat -> @+url:String -> @+headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> @+ms:U32 -> IO(Response)

GET, following redirects while left hops remain; body goes with this hop only. The answer is the last response, which is a redirect only when the hops ran out or its Location is not one redirect.bend follows.

Trusted, not proved: it sends a GET to the URL it is given (with the caller's body on the first hop only), asks redirect.plan about the response with the hops it has left (10 to start), sends the next GET to a Go target with the same headers and timeout and one hop fewer, and answers the response on a Stop, on a failed exchange, or when no hops are left. It is IO sequencing over the wire effect, which no law can state; every decision it acts on is src/redirect.bend's, proved under laws/ (the redirect guarantees, such as laws/RedirectsFollowLocation.bend).

def client.request.go source · line 213 · raw

@go:Bool -> @method:String -> @url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> @ms:U32 -> IO(Response)

a request once it is known whether it follows redirects

def client.request.opts source · line 230 · raw

@+method:String -> @url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> @ms:U32 -> @follow:Bool -> IO(Response)

a request with a method, headers, body and timeout; it follows redirects when follow is set and the method is GET (redirect.bend redirect.follows), and answers its first response otherwise

def client.get.opts source · line 242 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @ms:U32 -> @follow:Bool -> IO(Response)

GET with headers and a timeout, following redirects when follow is set

def client.timed_out source · line 251 · raw

@r:Response -> Bool

whether a response is the failure of an exchange that ran out of time

def client.get source · line 259 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> IO(Response)

GET

def client.head source · line 263 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> IO(Response)

HEAD (RFC 9110 §9.3.2)

def client.post source · line 267 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> IO(Response)

POST

def client.put source · line 275 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> IO(Response)

PUT

def client.delete source · line 283 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:0x0b59049ea5717d6f2fd30293c7271ede/src/body.Body -> IO(Response)

DELETE

def client.options source · line 291 · raw

@url:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> IO(Response)

OPTIONS

def client.bearer source · line 295 · raw

@token:String -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header

Authorization: Bearer … (auth.bend)

def client.basic source · line 299 · raw

@user:String -> @password:String -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header

Authorization: Basic … (auth.bend; RFC 7617, RFC 4648 Base64)