http.bend checks
raw source on the hub · import element-http@0.1.0.0/http.bend as Http
4 imports
import Base import element-compress@0.1.0.1/gzip.bend as Gzip import element-rfc3986@0.1.0.1/url.bend as Url import element-text@0.1.0.0/utf8.bend as U
Types
type Hdr source · line 11 · raw
Data
Hdr@name:String -> @value:String -> Hdr
type Resp source · line 14 · raw
Data
Resp@status:U32 -> @headers:List<&2, Hdr> -> @body:List<&2, U32> -> Resp
type Split source · line 73 · raw
Data
Split@head:List<&2, U32> -> @body:List<&2, U32> -> @found:Bool -> Split
type CS source · line 248 · raw
Data
The chunked state after bs, as in dechunk: 4 is the last chunk seen.
after counts bytes after mode 4 and w keeps the last four bytes, so
the end of the trailers (an empty line) is seen.
CS@mode:U32 -> @left:U32 -> @after:U32 -> @w:U32 -> CS
type Fr source · line 377 · raw
Data
complete reads the whole response again; asked after every read, a body
of n bytes over r reads cost n * r steps (github.com's 367 KB chunked page:
10.8 s of CPU for one fetch, 0.47 s for a 100 KB one). Fr is the framing
after the bytes seen so far, and each read feeds it only the bytes it
added: fr_done(fr_feed(...)) over the reads is complete over their
concatenation.
FHead@head:List<&2, U32> -> Fr
FLen@left:U32 -> Fr
FChunk@st:CS -> Fr
FOpenFr
FDoneFr
type Next source · line 512 · raw
Data
What a response means at url: a page (decoded HTML), a redirect to an
absolute URL, or an error.
Page@html:String -> Next
Go@url:String -> Next
Bad@msg:String -> Next
type Rec source · line 540 · raw
Data
Rec@url:String -> @resp:String -> Rec
Definitions
def pick source · line 17 · raw
@c:Bool -> @a:U32 -> @b:U32 -> U32
def default_ua source · line 22 · raw
String
Any request Element sends: GET or POST, a Cookie header when one applies, and a body with its type (Content-Length counts UTF-8 bytes).
def accept_after_nl source · line 25 · raw
@s:String -> Bool
def has_accept source · line 35 · raw
@+extra:String -> Bool
Is one of these header lines ("Name: value\r\n" each) an Accept header? Names are case-insensitive (RFC 9110 5.1).
def accept_unless source · line 39 · raw
@has:Bool -> String
def accept_line source · line 50 · raw
@+extra:String -> String
The Accept a request sends when extra names none: a navigation asks for
a document. A subresource says what it wants in extra (a script */*, a
stylesheet text/css, as Chrome does), and a server that negotiates on
Accept answers what was asked for.
def request_with source · line 57 · raw
@+conn:String -> @+host:String -> @+method:String -> @+target:String -> @+cookie:String -> @+ctype:String -> @+body:String -> @+ua:String -> @+extra:String -> String
ua replaces the User-Agent when not empty; extra is header lines
("Name: value\r\n" each) sent as given (CDP's setExtraHTTPHeaders).
conn is the Connection header: "close", or "keep-alive" for a request
that goes out through tls/io.bend's pool.
def request_full source · line 65 · raw
@+host:String -> @+method:String -> @+target:String -> @+cookie:String -> @+ctype:String -> @+body:String -> @+ua:String -> @+extra:String -> String
def request source · line 68 · raw
@host:String -> @target:String -> String
def split_go source · line 79 · raw
@bs:List<&2, U32> -> @found:Bool -> @+w:U32 -> @+head:List<&2, U32> -> Split
w holds the last three bytes seen (newest in the low byte) to spot CRLFCRLF.
found is computed by the caller and matched, so the head is reversed once
(a Bool.pick would build the found-branch, a reverse, at every byte).
def lines_go source · line 93 · raw
@s:String -> @+cur:List<&2, Char> -> @+out:List<&2, String> -> List<&2, String>
def trim_left source · line 105 · raw
@s:String -> String
def rtrim source · line 112 · raw
@+s:String -> String
def header_of source · line 115 · raw
@+line:String -> Hdr
def keep_header source · line 118 · raw
@+h:String -> List<&2, Hdr>
def headers_of source · line 123 · raw
@xs:List<&2, String> -> List<&2, Hdr>
(Not Bool.pick over the recursive call: both arguments of a call are evaluated, which made this exponential in the number of headers.)
def header source · line 130 · raw
@hs:List<&2, Hdr> -> @+name:String -> String
def all source · line 141 · raw
@hs:List<&2, Hdr> -> @+name:String -> List<&2, String>
"HTTP/1.1 301 Moved" -> 301 Every value of a header that may repeat (Set-Cookie).
def resp_headers source · line 151 · raw
@r:Result<&2, &2, String, Resp> -> List<&2, Hdr>
def status_of source · line 160 · raw
@+line:String -> U32
def take source · line 165 · raw
@n:Nat -> @bs:List<&2, U32> -> List<&2, U32>
def hex_val source · line 176 · raw
@+b:U32 -> U32
def is_hex source · line 179 · raw
@+b:U32 -> Bool
def dechunk source · line 186 · raw
@bs:List<&2, U32> -> @+mode:U32 -> @+left:U32 -> @+out:List<&2, U32> -> List<&2, U32>
Chunked transfer coding (RFC 9112 7.1), one fold over the bytes:
mode 0: reading the size in hex 1: skipping to the end of the size line
2: left data bytes to copy 3: skipping the CRLF after the data
4: done (last chunk seen; trailers are ignored)
def by_length source · line 209 · raw
@m:Maybe<&2, U32> -> @+raw:List<&2, U32> -> List<&2, U32>
Content-Length bytes of raw; a length that does not read as a number
frames nothing, and the body is what came before the close. (It read as
Url.port_default's 80, and such a body was cut to 80 bytes.)
def body_of source · line 216 · raw
@+hs:List<&2, Hdr> -> @+raw:List<&2, U32> -> List<&2, U32>
def first_line source · line 221 · raw
@xs:List<&2, String> -> String
def rest_lines source · line 228 · raw
@xs:List<&2, String> -> List<&2, String>
def chunk_state source · line 251 · raw
@bs:List<&2, U32> -> @st:CS -> CS
def chunked_done source · line 276 · raw
@st:CS -> Bool
Done when the last chunk was seen and either no trailer followed (just CRLF) or the trailers ended with an empty line (CRLF CRLF).
def no_body source · line 281 · raw
@+status:U32 -> Bool
def have_length source · line 286 · raw
@m:Maybe<&2, U32> -> @+n:U32 -> Bool
Content-Length bytes received. A length that does not read as a number frames nothing: the body then runs to the close.
def chunked_complete source · line 294 · raw
@chunked:Bool -> @+body:List<&2, U32> -> Bool
the walk over the body runs only for a chunked one (a match, not a pick)
def complete_by source · line 301 · raw
@+status:U32 -> @+hs:List<&2, Hdr> -> @+body:List<&2, U32> -> Bool
def complete_lines source · line 310 · raw
@+ls:List<&2, String> -> @+body:List<&2, U32> -> Bool
def complete_split source · line 313 · raw
@sp:Split -> Bool
def complete source · line 323 · raw
@bs:List<&2, U32> -> Bool
Whether bs (from the first byte of the response) is a whole response.
def reusable_lines source · line 331 · raw
@+ls:List<&2, String> -> Bool
Whether the server keeps the connection after the response bs: an
HTTP/1.1 response unless it says Connection: close, an HTTP/1.0 one only
when it says keep-alive. Reads the head only (split_go stops there).
def reusable_split source · line 337 · raw
@sp:Split -> Bool
def reusable source · line 346 · raw
@bs:List<&2, U32> -> Bool
def is_get source · line 350 · raw
@bs:List<&2, U32> -> Bool
a request's bytes start "GET " (only GETs go out twice on a stale connection)
def fr_start source · line 384 · raw
Fr
def fr_len source · line 387 · raw
@m:Maybe<&2, U32> -> Fr
def fr_framing source · line 396 · raw
@+status:U32 -> @+hs:List<&2, Hdr> -> Fr
As complete_by: a 1xx is followed by the real response (never complete), 204/304 have no body, then chunked, then Content-Length, else the close.
def fr_of_lines source · line 402 · raw
@+ls:List<&2, String> -> Fr
def fr_rest source · line 406 · raw
@fr:Fr -> @+bs:List<&2, U32> -> Fr
the body's bytes, once the framing is known
def fr_w source · line 421 · raw
@n:Nat -> @head:List<&2, U32> -> U32
split_go's window over the head kept newest first: its first four bytes
def fr_split source · line 435 · raw
@sp:Split -> Fr
the head up to CRLFCRLF (split_go, carried on from the bytes seen), then what follows it as the body
def fr_feed source · line 444 · raw
@fr:Fr -> @+bs:List<&2, U32> -> Fr
def fr_done source · line 457 · raw
@fr:Fr -> Bool
def coded source · line 477 · raw
@+hs:List<&2, Hdr> -> String
Element asks for Accept-Encoding: gzip, deflate (since 2026-10-27; it asked for identity before, and python.org compressed anyway, curl confirms). A gzip, x-gzip or deflate body is decoded (compress; the trailer's checksum verified); bytes Element cannot decode (br, zstd, a corrupt stream) are not a page: the response is refused, never shown as text. The headers stay as sent (Content-Encoding and Content-Length describe the wire, the body is the content).
def resp_body source · line 481 · raw
@r:Result<&2, &2, String, List<&2, U32>> -> @+ls:List<&2, String> -> @+enc:String -> Result<&2, &2, String, Resp>
def resp_build source · line 488 · raw
@+ls:List<&2, String> -> @+raw:List<&2, U32> -> Result<&2, &2, String, Resp>
def resp_lines source · line 493 · raw
@+text:String -> @+raw:List<&2, U32> -> Result<&2, &2, String, Resp>
def resp_of source · line 496 · raw
@sp:Split -> Result<&2, &2, String, Resp>
def parse source · line 505 · raw
@bs:List<&2, U32> -> Result<&2, &2, String, Resp>
def is_redirect source · line 517 · raw
@+s:U32 -> Bool
def next_of source · line 520 · raw
@r:Resp -> @+url:String -> Next
def next source · line 529 · raw
@r:Result<&2, &2, String, Resp> -> @+url:String -> Next
def lookup source · line 543 · raw
@t:List<&2, Rec> -> @+u:String -> String
def is_go source · line 552 · raw
@n:Next -> Bool
def go_url source · line 561 · raw
@n:Next -> String
def final_of source · line 570 · raw
@n:Next -> String
def follow source · line 579 · raw
@fuel:Nat -> @+table:List<&2, Rec> -> @+url:String -> String
def recorded source · line 589 · raw
List<&2, Rec>
A recorded site: / -> 301 /a (relative Location) -> 200 chunked page; /loop -> 302 /loop forever.