~/bend-docscommunity

main.bend relies on unsafe/foreign

raw source on the hub · import ezhttp@0.24.0.0/main.bend as Main

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.

16 imports
import Base
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes
import ./src/url.bend as Url
import ./src/pct.bend as Pct
import ./src/http.bend as Http
import ./src/body.bend as Body
import ./src/form.bend as Form
import ./src/client.bend as Client
import ./src/server.bend as Server
import ./src/cookie.bend as Cookie
import ./src/cache.bend as Cache
import ./src/cors.bend as Cors
import ./src/html.bend as Html
import ./src/qs.bend as Qs
import ./src/unescape.bend as Unescape
import ./src/echo.bend as Echo

Definitions

def parse_url source · line 23 · raw

@url:String -> 0x8673751971e75cc0377d73b25bc4d890/src/url.Loc

URL parse re-export

def quote_path source · line 29 · raw

@text:String -> String

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_component source · line 34 · raw

@text:String -> String

percent-encode a path segment, query key or value (RFC 3986 §2.1), as urllib.parse.quote(text, safe=""): like quote_path, but "/" becomes %2F

def unquote source · line 41 · raw

@text:String -> String

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_plus source · line 45 · raw

@text:String -> String

percent-decode as urllib.parse.unquote_plus: + is a space, then unquote

def quote_plus source · line 50 · raw

@text:String -> String

percent-encode as urllib.parse.quote_plus: as quote_component, but a space is written + (so + itself is %2B)

def Kv source · line 54 · raw

Data

a name and a value, reusable: a query field or a cookie, written (k, v)

def urlencode source · line 59 · raw

@pairs:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/qs.Kv> -> String

a query string from name and value pairs, as urllib.parse.urlencode(pairs): name=value with both quote_plus'd, joined by &, in order

def parse_query source · line 67 · raw

@text:String -> List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/qs.Kv>

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 one

def query_get source · line 72 · raw

@pairs:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/qs.Kv> -> @name:String -> Maybe<&2, String>

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 be

def parse_response source · line 77 · raw

@raw:String -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Reply

response parse re-export; raw is octets, one character each (laws/OneOctetPerChar.bend)

def parse_response_to source · line 83 · raw

@method:String -> @raw:String -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Reply

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 reason_phrase source · line 89 · raw

@raw:String -> String

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 parse_request source · line 93 · raw

@raw:String -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Request

request parse re-export (server inbound)

def format_response source · line 97 · raw

@status:U32 -> @headers:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/http.Header> -> @body:String -> String

format a response message (server outbound)

def empty_body source · line 102 · raw

0x8673751971e75cc0377d73b25bc4d890/src/body.Body

empty request body

def text_body source · line 106 · raw

@text:String -> 0x8673751971e75cc0377d73b25bc4d890/src/body.Body

UTF-8 text request body

def octets_body source · line 110 · raw

@bytes:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> 0x8673751971e75cc0377d73b25bc4d890/src/body.Body

packed octets request body (bend-kit-bytes Bytes), sent unchanged

def octets_list_body source · line 114 · raw

@bs:List<&2, U32> -> 0x8673751971e75cc0377d73b25bc4d890/src/body.Body

octets request body from a list of octet values (each 0..255)

def form_field source · line 118 · raw

@name:String -> @value:String -> 0x8673751971e75cc0377d73b25bc4d890/src/form.Upload

a multipart/form-data text field (RFC 7578)

def form_file source · line 123 · raw

@name:String -> @filename:String -> @ctype:String -> @bytes:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> 0x8673751971e75cc0377d73b25bc4d890/src/form.Upload

a multipart/form-data file part: field name, filename, content type and the file's bytes (packed Bytes, sent unchanged)

def form_body source · line 130 · raw

@boundary:String -> @parts:List<&1, 0x8673751971e75cc0377d73b25bc4d890/src/form.Upload> -> 0x8673751971e75cc0377d73b25bc4d890/src/form.Form

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 bearer source · line 146 · raw

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

Bearer token header helper

def basic source · line 150 · raw

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

Basic credentials header (RFC 7617)

def cache_control source · line 166 · raw

@value:String -> 0x8673751971e75cc0377d73b25bc4d890/src/cache.Cc

parse a Cache-Control field value (RFC 9111)

def fresh source · line 170 · raw

@value:String -> @age:Nat -> Bool

freshness of a Cache-Control value at an age in seconds

def cors_reply source · line 174 · raw

@cfg:0x8673751971e75cc0377d73b25bc4d890/src/cors.Cfg -> @req:0x8673751971e75cc0377d73b25bc4d890/src/http.Request -> @reply:0x8673751971e75cc0377d73b25bc4d890/src/http.Reply -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Reply

CORS preflight or simple headers on a handler reply

def html_escape source · line 183 · raw

@text:String -> String

escape text for HTML as markupsafe and Jinja's autoescape do: & < > " ' as &amp; &lt; &gt; &#34; &#39;, every other character as itself

def html_unescape source · line 191 · raw

@text:String -> String

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 http.get source · line 206 · raw

@url:String -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

GET with no extra headers

def http.get_with source · line 210 · raw

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

GET with headers

def http.get_opts source · line 217 · raw

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

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.request_opts source · line 229 · raw

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

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 response

def http.post_opts source · line 241 · raw

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

POST with headers, a body and a timeout in milliseconds; redirects are not followed

def http.put_opts source · line 251 · raw

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

PUT with headers, a body and a timeout in milliseconds; redirects are not followed

def http.delete_opts source · line 261 · raw

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

DELETE with headers, a body and a timeout in milliseconds; redirects are not followed

def http.timed_out source · line 270 · raw

@r:0x8673751971e75cc0377d73b25bc4d890/src/client.Response -> Bool

whether a response is an exchange that ran out of time

def body_text source · line 275 · raw

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

a response body's octets (a Response's bytes) decoded as UTF-8, done only when called (laws/OctetsDecodeAsUtf8.bend)

def http.head source · line 279 · raw

@url:String -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

HEAD with no extra headers

def http.head_with source · line 283 · raw

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

HEAD with headers

def http.post source · line 288 · raw

@url:String -> @body:0x8673751971e75cc0377d73b25bc4d890/src/body.Body -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

POST with a body and no extra headers

def http.post_with source · line 292 · raw

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

POST with headers and a body

def http.put source · line 300 · raw

@url:String -> @body:0x8673751971e75cc0377d73b25bc4d890/src/body.Body -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

PUT with a body and no extra headers

def http.put_with source · line 304 · raw

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

PUT with headers and a body

def http.delete source · line 309 · raw

@url:String -> @body:0x8673751971e75cc0377d73b25bc4d890/src/body.Body -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

DELETE with a body and no extra headers

def http.delete_with source · line 313 · raw

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

DELETE with headers and a body

def http.options source · line 321 · raw

@url:String -> IO(0x8673751971e75cc0377d73b25bc4d890/src/client.Response)

OPTIONS with no extra headers

def http.options_with source · line 325 · raw

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

OPTIONS with headers

def text_reply source · line 396 · raw

@status:U32 -> @headers:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/http.Header> -> @body:String -> 0x8673751971e75cc0377d73b25bc4d890/src/server.Answer

a reply with a text body, written as UTF-8

def octets_reply source · line 401 · raw

@status:U32 -> @headers:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/http.Header> -> @bytes:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> 0x8673751971e75cc0377d73b25bc4d890/src/server.Answer

a reply whose body is packed octets, written unchanged

def stream_reply source · line 408 · raw

@status:U32 -> @headers:List<&2, 0x8673751971e75cc0377d73b25bc4d890/src/http.Header> -> @chunks:Chan(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> 0x8673751971e75cc0377d73b25bc4d890/src/server.Answer

a reply streamed chunked: each chunk the channel gives is written as it comes, and the body ends when the channel is closed

Templates

template encode source · line 134 · raw

@-A:Data -> @-to_text:(@_:A -> String) -> @value:A -> 0x8673751971e75cc0377d73b25bc4d890/src/body.Body

BYO JSON (or any codec): encode a value with the caller's to_text

template decode source · line 138 · raw

@-A:Data -> @-from_text:(@_:String -> Maybe<&2, A>) -> @text:String -> Maybe<&2, A>

BYO JSON (or any codec): decode body text with the caller's from_text

template http.serve source · line 330 · raw

@-handle:(@_:0x8673751971e75cc0377d73b25bc4d890/src/http.Request -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Reply) -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)

listen on host:port and serve up to limit connections

template http.serve_io source · line 340 · raw

@-handle:(@_:0x8673751971e75cc0377d73b25bc4d890/src/http.Request -> IO(0x8673751971e75cc0377d73b25bc4d890/src/http.Reply)) -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)

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 served

template http.serve_with source · line 361 · raw

@-C:Data -> @-handle:(@_:C -> @_:0x8673751971e75cc0377d73b25bc4d890/src/server.Req -> 0x8673751971e75cc0377d73b25bc4d890/src/server.Answer) -> @ctx:C -> @host:String -> @port:U32 -> @limit:U32 -> @max:U32 -> IO(Unit)

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)

template http.serve_io_with source · line 373 · raw

@-C:Data -> @-handle:(@_:C -> @_:0x8673751971e75cc0377d73b25bc4d890/src/server.Req -> IO(0x8673751971e75cc0377d73b25bc4d890/src/server.Answer)) -> @ctx:C -> @host:String -> @port:U32 -> @limit:U32 -> @max:U32 -> IO(Unit)

the same with a handler that does IO

template http.serve_once source · line 414 · raw

@-handle:(@_:0x8673751971e75cc0377d73b25bc4d890/src/http.Request -> 0x8673751971e75cc0377d73b25bc4d890/src/http.Reply) -> @host:String -> @port:U32 -> IO(Unit)

listen on host:port, serve one connection, then stop

Unsafe

unsafe http.serve_forever source · line 349 · raw

@-handle:(@_:0x8673751971e75cc0377d73b25bc4d890/src/http.Request -> IO(0x8673751971e75cc0377d73b25bc4d890/src/http.Reply)) -> @host:String -> @port:U32 -> IO(Unit)

listen on host:port and serve with an IO handler until the process stops

unsafe http.serve_forever_with source · line 385 · raw

@-C:Data -> @-handle:(@_:C -> @_:0x8673751971e75cc0377d73b25bc4d890/src/server.Req -> IO(0x8673751971e75cc0377d73b25bc4d890/src/server.Answer)) -> @ctx:C -> @host:String -> @port:U32 -> @max:U32 -> IO(Unit)

the same with no limit: it serves until the process is stopped