~/bend-docscommunity

src/wirespec.bend source

src/wirespec.bend on the hub · documented module

# ezhttp/src/wirespec: the text the wire effect reads, and what its answer# means, pure. Kept apart from wire.bend (the effect) so the proof gate can# import it. What the client sends for a URL (wire.exchange) and when it has# heard a whole answer (wire.read.next) are decided here; wire.bend and# client.bend act on them.## Spec: scheme, host, port each on their own line; everything after the third# newline is the request verbatim. The scheme line may carry a timeout after a# space: 32 binary digits, least significant first, in milliseconds. A scheme# line with no timeout means 60000 ms. SPEC.md HTTP-CLI lists what is# guaranteed.import Baseimport ./http.bend as Httpimport ./url.bend as Url# the word the effect reads for a scheme that wants TLSdef wire.scheme(secure: Bool) -> String:  match secure:    case True{}:      "tls"    case False{}:      "tcp"# the spec the effect readsdef wire.spec(secure: Bool, host: String, port: U32, req: String) -> String:  wire.scheme(secure) ++ "\n" ++ host ++ "\n" ++ U32.show(port) ++ "\n" ++ req# --- the timeout ---# the timeout a spec with none carries: 60 s, in millisecondsdef wire.ms.dflt() -> U32:  60000# one bit as a binary digitdef wire.bit(b: Bool) -> Char:  match b:    case True{}:      '1'    case False{}:      '0'# a word as binary digits, least significant firstdef wire.bits(n: Nat, w: Word(n)) -> String:  match n:    case 0n:      SNil{}    case 1n+p:      match w:        case WCon{b, t}:          SCon{wire.bit(b), wire.bits(p, t)}# binary digits, least significant first, as a word; a missing digit is 0def wire.bits.read(n: Nat, s: String) -> Word(n):  match n:    case 0n:      WNil{}    case 1n+p:      match s:        case SNil{}:          WCon{False{}, wire.bits.read(p, SNil{})}        case SCon{c, t}:          WCon{Char.is_eq(c, '1'), wire.bits.read(p, t)}# a timeout as the scheme line carries itdef wire.ms.text(ms: U32) -> String:  match ms:    case U32{w}:      wire.bits(32n, w)# the first line of a timed spec: the scheme, a space, the timeoutdef wire.head(secure: Bool, ms: U32) -> String:  wire.scheme(secure) ++ " " ++ wire.ms.text(ms)# the lines after the first: host, port, then the request verbatimdef wire.rest(host: String, port: U32, req: String) -> String:  host ++ "\n" ++ U32.show(port) ++ "\n" ++ req# the spec the effect reads, with a timeout in millisecondsdef wire.spec.timed(secure: Bool, ms: U32, host: String, port: U32,  req: String) -> String:  wire.head(secure, ms) ++ "\n" ++ wire.rest(host, port, req)# the timeout after the scheme word, or the default when there is nonedef wire.ms.words(ws: List<&2, String>) -> U32:  match ws:    case []:      wire.ms.dflt()    case _ <> []:      wire.ms.dflt()    case _ <> (bits <> _):      U32{wire.bits.read(32n, bits)}# the first line of a specdef wire.line(ls: List<&2, String>) -> String:  match ls:    case []:      ""    case l <> _:      l# the timeout the effect reads from a spec, in millisecondsdef wire.ms(spec: String) -> U32:  wire.ms.words(String.split(wire.line(String.split(spec, '\n')), ' '))# --- the answer ---# the wire status for an exchange that ran out of time (curl's# CURLE_OPERATION_TIMEDOUT)def wire.late() -> U32:  28# what a failed exchange is reported as: a timeout is marked, any other# failure's reason is kept as it isdef wire.why.of(why: String, late: Bool) -> String:  match late:    case True{}:      "timed out: " ++ why    case False{}:      why# a failed exchange's status and reason as the reason the client reportsdef wire.why(code: Maybe<&2, U32>, why: String) -> String:  match code:    case None{}:      why    case Some{n}:      wire.why.of(why, U32.is_eq(n, wire.late()))# whether a reported reason is a timeoutdef wire.timed_out(why: String) -> Bool:  String.starts_with(why, "timed out: ")# --- what is sent ---# what a request to a URL becomes: nothing when the URL did not parse, else# the scheme (secure or not), host, port and request text the effect is giventype Exchange is Data:  NoUrl{why: String}  Exchange{secure: Bool, host: String, port: U32, req: String}# the exchange once it is known whether the scheme wants TLSdef wire.exchange.at(method: String, +secure: Bool, +host: String, +port: U32,  path: String, headers: List<&2, Http.Header>, body: String) -> Exchange:  Exchange{secure, host, port,    Http.request.at(method, secure, host, port, path, headers, body)}# a request with this method, headers and body to a URL that came apartdef wire.exchange(method: String, l: Url.Loc, headers: List<&2, Http.Header>,  body: String) -> Exchange:  match l:    case Url.Bad{why}:      NoUrl{why}    case Url.Loc{scheme, host, port, path}:      +secure = String.eq(scheme, "https")      wire.exchange.at(method, secure, host, port, path, headers, body)# --- what is heard ---# an answer's status: the number on its first line, if it is onedef wire.code.of(c: Http.Cut, raw: String) -> Maybe<&2, U32>:  match c:    case Http.NoCut{}:      U32.read(raw)    case Http.Cut{before, after}:      U32.read(before)# an answer's status line as a numberdef wire.code(+raw: String) -> Maybe<&2, U32>:  wire.code.of(Http.divide(raw, "\n"), raw)# the text after an answer's first newlinedef wire.text.of(c: Http.Cut) -> String:  match c:    case Http.NoCut{}:      ""    case Http.Cut{before, after}:      after# the text after an answer's first newlinedef wire.text(raw: String) -> String:  wire.text.of(Http.divide(raw, "\n"))# whether a status is success (0)def wire.ok.of(m: Maybe<&2, U32>) -> Bool:  match m:    case None{}:      False{}    case Some{n}:      U32.is_eq(n, 0)# whether an answer has status 0def wire.ok(raw: String) -> Bool:  wire.ok.of(wire.code(raw))# what an open answered: a connection to read from, or the answer itselftype Opened is Data:  Refused{answer: String}  Open{conn: U32}# a connection number that read, or a malformed answer when it did notdef wire.opened.conn(m: Maybe<&2, U32>) -> Opened:  match m:    case None{}:      Refused{"22\na malformed connection number"}    case Some{n}:      Open{n}# an open's answer once it is known whether it succeededdef wire.opened.of(+got: String, ok: Bool) -> Opened:  match ok:    case False{}:      Refused{got}    case True{}:      wire.opened.conn(U32.read(wire.text(got)))# what an open's answer meansdef wire.opened(+got: String) -> Opened:  wire.opened.of(got, wire.ok(got))# after a read: read again with what has arrived, or answertype Read is Data:  Again{acc: String}  Heard{answer: String}# everything received, once it is known whether it is a whole responsedef wire.read.whole(all: String, done: Bool) -> Read:  match done:    case True{}:      Heard{"0\n" ++ all}    case False{}:      Again{all}# a read that brought text, or found the connection closeddef wire.read.got(+method: String, acc: String, text: String, closed: Bool)  -> Read:  match closed:    case True{}:      Heard{"0\n" ++ acc}    case False{}:      +all = acc ++ text      wire.read.whole(all, Http.whole(method, all))# a read's answer once it is known whether it succeededdef wire.read.of(+method: String, acc: String, +got: String, ok: Bool) -> Read:  match ok:    case False{}:      Heard{got}    case True{}:      +text = wire.text(got)      wire.read.got(method, acc, text, String.is_empty(text))# what to do after a read answered `got`, with `acc` received before it, for# a request with this method (SPEC.md HTTP-CLI-6)def wire.read.next(+method: String, acc: String, +got: String) -> Read:  wire.read.of(method, acc, got, wire.ok(got))# the answer when the reads run out before the response endsdef wire.spent() -> String:  "56\nthe answer did not end within 65536 reads"