fetch.bend relies on unsafe/foreign
raw source on the hub · import element-http@0.1.0.0/fetch.bend as Fetch
7 imports
import Base import ./sock.bend as Sock import element-rfc3986@0.1.0.1/url.bend as Url import ./http.bend as Http import element-text@0.1.0.0/utf8.bend as U import ./io.bend as Tls import ./cookie.bend as Ck
Types
type Page source · line 22 · raw
Data
Page@url:String -> @html:String -> Page
type RS source · line 31 · raw
Type
More: keep reading. Eof/Over/Err: stop (the socket is closed on the way out).
More@sock:Socket -> @chunks:List<&2, List<&2, U32>> -> @total:U32 -> @fr:0x475c2a8333a479bccfdd668aaa51426c/http.Fr -> RS
Eof@sock:Socket -> @chunks:List<&2, List<&2, U32>> -> RS
Over@sock:Socket -> RS
Err@sock:Socket -> @code:U32 -> @msg:String -> RS
type Req source · line 164 · raw
Data
A request to make: method, body and its type (a form POST; GET has none), and the User-Agent ("" = Element's) and extra header lines it carries, kept across redirects.
Req@method:String -> @body:String -> @ctype:String -> @ua:String -> @extra:String -> Req
type FS source · line 176 · raw
Data
Want@url:String -> @hops:U32 -> @req:Req -> FS
Plain@url:String -> @host:String -> @port:U32 -> @target:String -> @hops:U32 -> @tls:Bool -> @req:Req -> FS
Read@url:String -> @bytes:List<&2, U32> -> @hops:U32 -> @req:Req -> FS
Final@url:String -> @html:String -> FS
Refuse@msg:String -> FS
Got@url:String -> @status:U32 -> @ctype:String -> @body:String -> @acao:String -> @hdrs:String -> FS
type Visit source · line 213 · raw
Data
A page, where it ended up, and the cookie jar after every response on the way.
Visit@url:String -> @html:String -> @jar:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/cookie.Cookie> -> Visit
type W source · line 251 · raw
Type
WMoreW
WDone@r:Result<&1, &1, Pair(U32, String), List<&2, U32>> -> W
WShutW
type Answer source · line 418 · raw
Data
Answer@url:String -> @status:U32 -> @ctype:String -> @body:String -> @acao:String -> @jar:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/cookie.Cookie> -> @headers:String -> Answer
Definitions
def max_redirects source · line 25 · raw
U32
def rs_done source · line 37 · raw
@eof:Bool -> @over:Bool -> @s:Socket -> @bs:List<&2, U32> -> @chunks:List<&2, List<&2, U32>> -> @+total:U32 -> @fr:0x475c2a8333a479bccfdd668aaa51426c/http.Fr -> RS
def rs_of source · line 48 · raw
@got:Pair(Socket, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> @chunks:List<&2, List<&2, U32>> -> @+total:U32 -> @+cap:U32 -> @fr:0x475c2a8333a479bccfdd668aaa51426c/http.Fr -> RS
def close_with source · line 63 · raw
@s:Socket -> @r:Result<&1, &1, Pair(U32, String), List<&2, U32>> -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def rs_stop source · line 69 · raw
@st:RS -> @+cap:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def whole source · line 83 · raw
@done:Bool -> @s:Socket -> @+chunks:List<&2, List<&2, U32>> -> @+total:U32 -> @fr:0x475c2a8333a479bccfdd668aaa51426c/http.Fr -> RS
A whole response by its framing ends the read even when the server keeps the socket open (keep-alive hosts ignore Connection: close). The framing (Http.Fr) is fed each read's bytes once: the newest chunk.
def newest source · line 90 · raw
@chunks:List<&2, List<&2, U32>> -> List<&2, U32>
def framed source · line 97 · raw
@st:RS -> RS
def read_loop source · line 109 · raw
@k:Nat -> @st:RS -> @+cap:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def sock_of source · line 131 · raw
@sent:Pair(Socket, Result<&1, &1, Pair(U32, String), Unit>) -> Socket
def get_sock source · line 136 · raw
@r:Result<&1, &1, Pair(U32, String), Socket> -> @+req:String -> @+cap:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def get_ip source · line 146 · raw
@r:Result<&1, &1, Pair(U32, String), String> -> @port:U32 -> @+req:String -> @+cap:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def get source · line 155 · raw
@+host:String -> @port:U32 -> @+req:String -> @+cap:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
A host that does not resolve or a port that refuses is this request's error (the caller answers it), never the end of the program.
def get_req source · line 167 · raw
Req
def as_get source · line 171 · raw
@r:Req -> Req
The GET a redirect makes: same User-Agent and extra headers, no body.
def plan_scheme source · line 184 · raw
@+sch:String -> @+url:String -> @+hops:U32 -> @+req:Req -> FS
def plan source · line 190 · raw
@+url:String -> @+hops:U32 -> @+req:Req -> FS
def after_read source · line 194 · raw
@n:0x475c2a8333a479bccfdd668aaa51426c/http.Next -> @+url:String -> @+hops:U32 -> @+req:Req -> FS
A redirect is followed with GET (what browsers do after a form POST).
def read_result source · line 203 · raw
@r:Result<&1, &1, Pair(U32, String), List<&2, U32>> -> @+url:String -> @+hops:U32 -> @+req:Req -> FS
def out_of_steps source · line 216 · raw
IO(Result<&1, &1, Pair(U32, String), Visit>)
def path_of source · line 220 · raw
@+target:String -> String
The path part of a request target (cookies match on the path, not the query).
def send_now source · line 223 · raw
@tls:Bool -> @+host:String -> @port:U32 -> @+req:String -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def tick_ms source · line 242 · raw
U32
A host that accepts and then goes silent held a navigation forever. Each exchange (connect, TLS, request, whole answer) now has a deadline, and missing it is an error the caller sees, as Chrome's net::ERR_TIMED_OUT.
Not IO.within: it cannot cancel its loser, and when the answer wins the
loser is a 60 s sleep that a CLI run waited out before exiting (every
element --url took 60 s). Here the clock is a ticker of 200 ms slices
that sends each tick into the answer's channel; the waiter counts ticks,
and closes the channel as soon as the answer comes, so the ticker's next
send fails and it stops within one slice. A late exchange still runs on
(nothing can cancel it) and closes its own socket when the host ends it.
def deadline_ticks source · line 245 · raw
Nat
def deadline_s source · line 248 · raw
U32
def decide source · line 256 · raw
@m:Maybe<&1, Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>> -> W
def timed_out source · line 268 · raw
@+host:String -> @+ms:U32 -> Result<&1, &1, Pair(U32, String), List<&2, U32>>
(ms: the deadline this exchange had: 60 s, or a page's own timeout)
def late source · line 271 · raw
@+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> @+host:String -> @+ms:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def taken source · line 278 · raw
@r:Result<&1, &1, Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>, Unit> -> Bool
Chan.send (Base 2.0.36+): Done{} once the channel took the value, Fail{value} once it is closed; the ticker stops at the first refusal.
def ticker source · line 285 · raw
@n:Nat -> @go:Bool -> @+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> IO(Unit)
def settle source · line 297 · raw
@d:W -> @+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> @+host:String -> @+ms:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def wait source · line 310 · raw
@n:Nat -> @d:W -> @+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> @+host:String -> @+ms:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
n: how many more items to take. The ticker sends deadline_ticks() ticks at most, so the waiter never takes more than that and never blocks past it.
def answer_into source · line 324 · raw
@+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> @act:IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(Result<&1, &1, Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>, Unit>)
def race source · line 329 · raw
@+ticks:Nat -> @+c:Chan(Maybe<&1, Result<&1, &1, Pair(U32, String), List<&2, U32>>>) -> @act:IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> @+host:String -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
ticks slices of tick_ms(): deadline_ticks() (60 s), or fewer for a
page's own timeout (XMLHttpRequest.timeout)
def send_within source · line 338 · raw
@+ticks:Nat -> @tls:Bool -> @+host:String -> @port:U32 -> @+req:String -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def send_any source · line 347 · raw
@tls:Bool -> @+host:String -> @port:U32 -> @+req:String -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)
def request_of source · line 352 · raw
@r:Req -> @+host:String -> @+target:String -> @+cookie:String -> @+keep:Bool -> String
an https request through a pool asks the server to keep the connection
def url_host_of source · line 357 · raw
@+url:String -> String
def url_path_of source · line 360 · raw
@+url:String -> String
def visit source · line 391 · raw
@+url:String -> @+req:Req -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> @+jar:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/cookie.Cookie> -> @+now:U32 -> IO(Result<&1, &1, Pair(U32, String), Visit>)
Fetch with a cookie jar at time now, as GET or a form POST: 3 steps per
hop (plan, send, read), 6 hops at most.
def page_of source · line 394 · raw
@r:Result<&1, &1, Pair(U32, String), Visit> -> IO(Result<&1, &1, Pair(U32, String), Page>)
def fetch_as source · line 405 · raw
@url:String -> @+req:Req -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> IO(Result<&1, &1, Pair(U32, String), Page>)
Fetch with no cookies, as req says (a page's stylesheets carry its
User-Agent and extra headers).
def fetch source · line 409 · raw
@url:String -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> IO(Result<&1, &1, Pair(U32, String), Page>)
Fetch a page with no cookies (GET).
def hdr_line source · line 424 · raw
@h:0x475c2a8333a479bccfdd668aaa51426c/http.Hdr -> String
Every response header a script may read, as "name: value" lines (Set-Cookie and Set-Cookie2 never: Fetch's forbidden response header names). A page's Response and XMLHttpRequest answer from these.
def hdr_lines source · line 430 · raw
@hs:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/http.Hdr> -> String
def got_of source · line 437 · raw
@r:Result<&2, &2, String, 0x475c2a8333a479bccfdd668aaa51426c/http.Resp> -> @+url:String -> @+hops:U32 -> @+req:Req -> FS
def has_sub source · line 451 · raw
@s:String -> @+needle:String -> Bool
A refusal keeps only its words; one that says the deadline passed keeps its code (60) for the page (an XMLHttpRequest reports it as a timeout).
def refuse_code source · line 458 · raw
@+msg:String -> U32
def request source · line 487 · raw
@+url:String -> @+req:Req -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> @+jar:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/cookie.Cookie> -> @+now:U32 -> IO(Result<&1, &1, Pair(U32, String), Answer>)
def request_within source · line 493 · raw
@+url:String -> @+req:Req -> @+cap:U32 -> @+trust:0x475c2a8333a479bccfdd668aaa51426c/io.Store -> @+jar:List<&2, 0x475c2a8333a479bccfdd668aaa51426c/cookie.Cookie> -> @+now:U32 -> @+ms:U32 -> IO(Result<&1, &1, Pair(U32, String), Answer>)
A request with a page's own deadline (ms, XMLHttpRequest.timeout; 0: none), never past the 60 s every exchange has: each exchange of it (a redirect is another) gets that long. Its failure is code 60, ERR_TIMED_OUT.