main.bend source
main.bend on the hub · documented module
# ezhttp: HTTP/1.1 client and server for Bend 2, with auth, cookie and CORS helpers.## Shared message types, BYO JSON via encode/decode. TLS for the client stays# at the wire/runtime layer.import Baseimport bend-kit-bytes@0.3.2.0/bytes.bend as Bytesimport ./src/url.bend as Urlimport ./src/pct.bend as Pctimport ./src/http.bend as Httpimport ./src/body.bend as Bodyimport ./src/form.bend as Formimport ./src/client.bend as Clientimport ./src/server.bend as Serverimport ./src/cookie.bend as Cookieimport ./src/cache.bend as Cacheimport ./src/cors.bend as Corsimport ./src/html.bend as Htmlimport ./src/qs.bend as Qsimport ./src/unescape.bend as Unescapeimport ./src/echo.bend as Echo# URL parse re-exportdef parse_url(url: String) -> Url.Loc: # noqa: L001 re-export of Url.parse Url.parse(url)# percent-encode a path (RFC 3986 §2.1), as Python's urllib.parse.quote(text):# unreserved characters and "/" stay, every other character becomes %XX per# UTF-8 byte, upper-case hex (laws/QuoteKeepsUnreserved.bend)def quote_path(text: String) -> String: # noqa: L001 re-export of Pct.quote (the path form) Pct.quote(True{}, text)# percent-encode a path segment, query key or value (RFC 3986 §2.1), as# urllib.parse.quote(text, safe=""): like quote_path, but "/" becomes %2Fdef quote_component(text: String) -> String: # noqa: L001 re-export of Pct.quote (the component form) Pct.quote(False{}, text)# percent-decode a string as urllib.parse.unquote: %XX escapes (either case)# become bytes, decoded as UTF-8 with U+FFFD for bytes that are not; an escape# that is not two hex digits is kept as written, and characters outside ASCII# are kept (laws/UnquoteReadsEscapes.bend)def unquote(text: String) -> String: # noqa: L001 re-export of Qs.unquote Qs.unquote(text)# percent-decode as urllib.parse.unquote_plus: `+` is a space, then unquotedef unquote_plus(text: String) -> String: # noqa: L001 re-export of Qs.unquote.plus Qs.unquote.plus(text)# percent-encode as urllib.parse.quote_plus: as quote_component, but a space# is written `+` (so `+` itself is %2B)def quote_plus(text: String) -> String: # noqa: L001 re-export of Qs.quote.plus Qs.quote.plus(text)# a name and a value, reusable: a query field or a cookie, written (k, v)def Kv() -> Data: # noqa: L001 re-export of the type Qs.Kv Qs.Kv()# a query string from name and value pairs, as urllib.parse.urlencode(pairs):# `name=value` with both quote_plus'd, joined by `&`, in orderdef urlencode(pairs: List<&2, Qs.Kv()>) -> String: # noqa: L001 re-export of Qs.urlencode Qs.urlencode(pairs)# a query string's name and value pairs, as# urllib.parse.parse_qsl(text, keep_blank_values=True): split on `&`, empty# pieces skipped, each cut at its first `=` (a piece without one is a name# with an empty value), both unquote_plus'd. Every pair is kept, in order,# duplicate names included; query_get reads the last onedef parse_query(text: String) -> List<&2, Qs.Kv()>: # noqa: L001 re-export of Qs.parse Qs.parse(text)# the value of the last pair with this name (names compared exactly), none# when no pair has it: last-wins, as a dict(parse_qsl(...)) would bedef query_get(pairs: List<&2, Qs.Kv()>, name: String) -> Maybe<&2, String>: # noqa: L001 re-export of Qs.get Qs.get(pairs, name)# response parse re-export; `raw` is octets, one character each# (laws/OneOctetPerChar.bend)def parse_response(raw: String) -> Http.Reply: # noqa: L001 re-export of Http.parse Http.parse(raw)# a response as the client reads it for a request with this method: interim# 1xx heads skipped, and no body for HEAD or a 1xx, 204 or 304 status,# whatever Content-Length says (laws/InterimResponsesSkipped.bend)def parse_response_to(method: String, raw: String) -> Http.Reply: # noqa: L001 re-export of Http.parse.to Http.parse.to(method, raw)# the reason phrase of a response's status line (RFC 9112 §4), read from the# head parse_response_to stops at; empty when there is none# (laws/ReasonPhraseFromFinalHead.bend)def reason_phrase(raw: String) -> String: # noqa: L001 re-export of Http.phrase.to Http.phrase.to(raw)# request parse re-export (server inbound)def parse_request(raw: String) -> Http.Request: # noqa: L001 re-export of Http.ask Http.ask(raw)# format a response message (server outbound)def format_response( # noqa: L001 re-export of Http.respond status: U32, headers: List<&2, Http.Header>, body: String) -> String: Http.respond(status, headers, body)# empty request bodydef empty_body() -> Body.Body: # noqa: L001 re-export of Body.body.empty Body.body.empty()# UTF-8 text request bodydef text_body(text: String) -> Body.Body: # noqa: L001 re-export of Body.body.text Body.body.text(text)# packed octets request body (bend-kit-bytes Bytes), sent unchangeddef octets_body(bytes: Bytes.Bytes) -> Body.Body: # noqa: L001 re-export of Body.body.octets Body.body.octets(bytes)# octets request body from a list of octet values (each 0..255)def octets_list_body(bs: List<&2, U32>) -> Body.Body: # noqa: L001 re-export of Body.body.list Body.body.list(bs)# a multipart/form-data text field (RFC 7578)def form_field(name: String, value: String) -> Form.Upload: # noqa: L001 re-export of Form.form.field Form.form.field(name, value)# a multipart/form-data file part: field name, filename, content type and the# file's bytes (packed Bytes, sent unchanged)def form_file( # noqa: L001 re-export of Form.form.file name: String, filename: String, ctype: String, bytes: Bytes.Bytes) -> Form.Upload: Form.form.file(name, filename, ctype, bytes)# a multipart/form-data body (RFC 7578) with this boundary: the Content-Type# field value to send and an octet body, or why the boundary cannot be used# (laws/FormRefusesBadRequests.bend)def form_body(boundary: String, parts: List<&1, Form.Upload>) -> Form.Form: # noqa: L001 re-export of Form.form.body Form.form.body(boundary, parts)# BYO JSON (or any codec): encode a value with the caller's to_textdef encode(~A: Data, ~to_text: A -> String, value: A) -> Body.Body: # noqa: L001 re-export of Body.body.encode Body.body.encode(A, to_text, value)# BYO JSON (or any codec): decode body text with the caller's from_textdef decode( # noqa: L001 re-export of Body.body.decode ~A: Data, ~from_text: String -> Maybe<&2, A>, text: String) -> Maybe<&2, A>: Body.body.decode(A, from_text, text)# Bearer token header helperdef bearer(token: String) -> Http.Header: # noqa: L001 re-export of client.bearer = Auth.auth.bearer Client.client.bearer(token)# Basic credentials header (RFC 7617)def basic(user: String, password: String) -> Http.Header: # noqa: L001 re-export of client.basic = Auth.auth.basic Client.client.basic(user, password)# Set-Cookie header from a parsed cookie (RFC 6265)def set_cookie(cookie: Cookie.Cookie) -> Http.Header: # noqa: L001 re-export of Cookie.cookie.set Cookie.cookie.set(cookie)# parse one Set-Cookie field valuedef parse_cookie(line: String) -> Maybe<&2, Cookie.Cookie>: # noqa: L001 re-export of Cookie.cookie.parse Cookie.cookie.parse(line)# Cookie request header for a list of cookiesdef cookie_header(cs: List<&2, Cookie.Cookie>) -> Http.Header: # noqa: L001 re-export of Cookie.cookie.request Cookie.cookie.request(cs)# parse a Cache-Control field value (RFC 9111)def cache_control(value: String) -> Cache.Cc: # noqa: L001 re-export of Cache.cache.parse Cache.cache.parse(value)# freshness of a Cache-Control value at an age in secondsdef fresh(value: String, age: Nat) -> Bool: # noqa: L001 Cache.cache.fresh of Cache.cache.parse Cache.cache.fresh(Cache.cache.parse(value), age)# CORS preflight or simple headers on a handler replydef cors_reply( # noqa: L001 re-export of Cors.cors.on cfg: Cors.Cfg, req: Http.Request, reply: Http.Reply) -> Http.Reply: Cors.cors.on(cfg, req, reply)# escape text for HTML as markupsafe and Jinja's autoescape do: & < > " ' as# & < > " ', every other character as itselfdef html_escape(text: String) -> String: # noqa: L001 re-export of Html.escape Html.escape(text)# HTML character references back to characters, as Python's html.unescape:# decimal and hex numeric references (with the HTML Standard's replacements)# and the HTML Standard's 2231 named references, the legacy ones also# without `;`; anything that is not a reference is kept as written# (laws/UnescapeFindsReferences.bend)def html_unescape(text: String) -> String: # noqa: L001 re-export of Unescape.unescape Unescape.unescape(text)# the Cookie header value to send back after these Set-Cookie field values# (RFC 6265 §5.2, §5.4): each one's name=value, attributes ignored; a later# cookie with the same name replaces the earlier one's value in its place;# `name=value` pairs joined by `; ` (laws/CookieHeaderEchoesStore.bend)def cookie_from_set_cookies(values: List<&2, String>) -> String: # noqa: L001 re-export of Echo.echo Echo.echo(values)# the same, from a response's header fields (its Set-Cookie fields, in order)def cookie_from_headers(headers: List<&2, Http.Header>) -> String: # noqa: L001 Echo.echo of Echo.values Echo.echo(Echo.values(headers))# GET with no extra headersdef http.get(url: String) -> IO(Client.Response): Client.client.get(url, [])# GET with headersdef http.get_with( url: String, headers: List<&2, Http.Header>) -> IO(Client.Response): Client.client.get(url, headers)# GET with headers and a timeout in milliseconds that bounds connecting,# sending and receiving; with `follow`, redirects are followed (GET only, at# most 10 hops, see src/redirect.bend)def http.get_opts( url: String, headers: List<&2, Http.Header>, timeout_ms: U32, follow: Bool) -> IO(Client.Response): Client.client.get.opts(url, headers, timeout_ms, follow)# a request with any method, headers, a body and a timeout in milliseconds# that bounds connecting, sending and receiving; with `follow`, a GET follows# redirects (at most 10 hops, see src/redirect.bend) and every other method# answers a redirect as its responsedef http.request_opts( method: String, url: String, headers: List<&2, Http.Header>, body: Body.Body, timeout_ms: U32, follow: Bool) -> IO(Client.Response): Client.client.request.opts(method, url, headers, body, timeout_ms, follow)# POST with headers, a body and a timeout in milliseconds; redirects are not# followeddef http.post_opts( url: String, headers: List<&2, Http.Header>, body: Body.Body, timeout_ms: U32) -> IO(Client.Response): http.request_opts("POST", url, headers, body, timeout_ms, False{})# PUT with headers, a body and a timeout in milliseconds; redirects are not# followeddef http.put_opts( url: String, headers: List<&2, Http.Header>, body: Body.Body, timeout_ms: U32) -> IO(Client.Response): http.request_opts("PUT", url, headers, body, timeout_ms, False{})# DELETE with headers, a body and a timeout in milliseconds; redirects are# not followeddef http.delete_opts( url: String, headers: List<&2, Http.Header>, body: Body.Body, timeout_ms: U32) -> IO(Client.Response): http.request_opts("DELETE", url, headers, body, timeout_ms, False{})# whether a response is an exchange that ran out of timedef http.timed_out(r: Client.Response) -> Bool: Client.client.timed_out(r)# a response body's octets (a Response's `bytes`) decoded as UTF-8, done only# when called (laws/OctetsDecodeAsUtf8.bend)def body_text(bytes: Bytes.Bytes) -> String: # noqa: L001 re-export of Client.client.text Client.client.text(bytes)# HEAD with no extra headersdef http.head(url: String) -> IO(Client.Response): Client.client.head(url, [])# HEAD with headersdef http.head_with( url: String, headers: List<&2, Http.Header>) -> IO(Client.Response): Client.client.head(url, headers)# POST with a body and no extra headersdef http.post(url: String, body: Body.Body) -> IO(Client.Response): Client.client.post(url, [], body)# POST with headers and a bodydef http.post_with( url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Client.Response): Client.client.post(url, headers, body)# PUT with a body and no extra headersdef http.put(url: String, body: Body.Body) -> IO(Client.Response): Client.client.put(url, [], body)# PUT with headers and a bodydef http.put_with( url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Client.Response): Client.client.put(url, headers, body)# DELETE with a body and no extra headersdef http.delete(url: String, body: Body.Body) -> IO(Client.Response): Client.client.delete(url, [], body)# DELETE with headers and a bodydef http.delete_with( url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Client.Response): Client.client.delete(url, headers, body)# OPTIONS with no extra headersdef http.options(url: String) -> IO(Client.Response): Client.client.options(url, [])# OPTIONS with headersdef http.options_with( url: String, headers: List<&2, Http.Header>) -> IO(Client.Response): Client.client.options(url, headers)# listen on host:port and serve up to `limit` connectionsdef http.serve( ~handle: Http.Request -> Http.Reply, host: String, port: U32, limit: U32) -> IO(Unit): Server.server.serve(handle, host, port, limit)# listen on host:port and serve up to `limit` connections with a handler that# does IO; a failed connection is logged and the next one is serveddef http.serve_io( ~handle: Http.Request -> IO(Http.Reply), host: String, port: U32, limit: U32) -> IO(Unit): Server.server.serve_io(handle, host, port, limit)# listen on host:port and serve with an IO handler until the process stops@unsafe def http.serve_forever( ~handle: Http.Request -> IO(Http.Reply), host: String, port: U32) -> IO(Unit): Server.server.serve_forever(handle, host, port)# listen on host:port and serve up to `limit` connections, each on its own# fork and at most `max` at once (0: no limit), calling a pure handler with# `ctx` and the request (Server.Req: text, and its body's octets); returns# once every connection is closed (laws/BadRequestAnswers400.bend, trusted# in src/server.bend)def http.serve_with( ~C: Data, ~handle: C -> Server.Req -> Server.Answer, ctx: C, host: String, port: U32, limit: U32, max: U32) -> IO(Unit): Server.server.serve_with(~C, handle, ctx, host, port, limit, max)# the same with a handler that does IOdef http.serve_io_with( ~C: Data, ~handle: C -> Server.Req -> IO(Server.Answer), ctx: C, host: String, port: U32, limit: U32, max: U32) -> IO(Unit): Server.server.serve_io_with(~C, handle, ctx, host, port, limit, max)# the same with no limit: it serves until the process is stopped@unsafe def http.serve_forever_with( ~C: Data, ~handle: C -> Server.Req -> IO(Server.Answer), ctx: C, host: String, port: U32, max: U32) -> IO(Unit): Server.server.serve_forever_with(~C, handle, ctx, host, port, max)# a reply with a text body, written as UTF-8def text_reply( # noqa: L001 constructor of Server.Answer, which holds Bytes status: U32, headers: List<&2, Http.Header>, body: String) -> Server.Answer: Server.TextReply{status, headers, body}# a reply whose body is packed octets, written unchangeddef octets_reply( # noqa: L001 constructor of Server.Answer, which holds Bytes status: U32, headers: List<&2, Http.Header>, bytes: Bytes.Bytes) -> Server.Answer: Server.OctetsReply{status, headers, bytes}# a reply streamed chunked: each chunk the channel gives is written as it# comes, and the body ends when the channel is closeddef stream_reply( # noqa: L001 constructor of Server.Answer, which holds a channel status: U32, headers: List<&2, Http.Header>, chunks: Chan(Bytes.Bytes)) -> Server.Answer: Server.StreamReply{status, headers, chunks}# listen on host:port, serve one connection, then stopdef http.serve_once( ~handle: Http.Request -> Http.Reply, host: String, port: U32) -> IO(Unit): Server.server.once(handle, host, port)