src/server.bend relies on unsafe/foreign
raw source on the hub · import ezhttp@0.25.0.0/src/server.bend as Server
ezhttp/src/server: listen, accept, and serve each connection on its own
fork: parse one request, write one response (Connection: close). Sockets
are Base's (listen) and bend-kit-wire's (accept with a deadline, recv,
send.words), whose effects park on the event loop, so connections overlap.
A _with serve hands the handler a context value and the request as text
with its body's octets, and takes a reply as text, packed octets or a
stream of octet chunks; the plain serves are those with a text handler.
Every decision after an effect is step.bend's or http.bend's, proved under
laws/ (the server's guarantees, such as laws/BadRequestAnswers400.bend);
this module runs the effects in order (trusted below).
Trusted, not proved: Base's TCP effects (listen, accept, send) and
bend-kit-wire's recv on an accepted socket report a failure as Fail
and never end the process themselves; recv answers the octets that
arrived, one character each, and no octets once the peer has closed. They
are foreign C in Bend's runtime (effs/tcp_*.c) and in bend-kit-wire
(effs/wire.c), which no law reaches.
Trusted, not proved: this module acts on step.bend's answers in order. It
listens, then accepts with bend-kit-wire's accept.deadline one
connection at a time, and serves each accepted connection on its own fork
(IO.spawn); with a concurrency limit max other than 0 it takes a slot
before forking and gives it back when the connection is closed, so at most
max connections are served at once. On each connection it receives the
request until the octets step.need names have arrived (the head, its
blank line and its Content-Length), the peer closes, or 65536 receives
have been made, parses it, calls the handler only for a Call, with the
serve's context and the request step.text and step.octets give, writes
the reply step.bare shapes (a text reply as the UTF-8 of respond, a
packed octets reply as respond.head and its octets, a streamed one as
respond.stream, then each chunk the channel gives as chunk.frame
writes it until the channel is closed, then chunk.end) with
bend-kit-wire's send.words, and closes the socket; a lost connection's
line is written to stderr and the socket closed. http.serve,
http.serve_with, http.serve_io and http.serve_io_with stop accepting
after limit connections, a failed accept counting as one and an accept
whose deadline passed not counting, and return once every connection they
forked is closed; http.serve_forever and http.serve_forever_with do
not stop. It is IO sequencing over Base's and bend-kit-wire's effects and
the runtime's IO.spawn and channels: the checker cannot run an IO
program and no law can state what happens after an effect, or which fork
runs when. The serve_forever defs are also @unsafe recursion, outside
the termination check.
7 imports
import Base import bend-kit-wire@0.4.6.1/wire.bend as W import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes import ./http.bend as Http import ./cors.bend as Cors import ./step.bend as Step import ./wirespec.bend as Spec
Types
type Req source · line 54 · raw
Type
what a _with handler gets: the request as text (step.text) and its
body's octets as they arrived (step.octets)
Req@method:String -> @target:String -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:String -> @bytes:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> Req
type Answer source · line 62 · raw
Type
what a _with handler answers: a text body (written as UTF-8), packed
octets written unchanged, or a stream of octet chunks from a channel,
written chunked until the channel is closed
(laws/ReplyHeadFramesBody.bend, laws/StreamedRepliesAreChunked.bend)
TextReply@status:U32 -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @body:String -> Answer
OctetsReply@status:U32 -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @bytes:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> Answer
StreamReply@status:U32 -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @chunks:Chan(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> Answer
Definitions
def server.max source · line 69 · raw
U32
the most octets one receive asks for
def server.reads source · line 73 · raw
Nat
the most receives one request takes
def server.wait source · line 77 · raw
U32
how long one accept waits before it is asked again, in ms
def server.idles source · line 81 · raw
Nat
how many idle accepts in a row a limited serve allows before it stops
def server.pause source · line 85 · raw
U32
how long a failed accept waits before the next, so EMFILE does not spin
def server.listen source · line 89 · raw
@host:String -> @port:U32 -> IO(Listener)
listen on a host address and port, e.g. "127.0.0.1" or "0.0.0.0"
def server.accept.un source · line 93 · raw
@r:Pair(Listener, Result<&1, &1, Pair(U32, String), Socket>) -> IO(Pair(Listener, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Accept<Socket>))
accept one connection, waiting at most server.wait() ms
def server.accept source · line 99 · raw
@listener:Listener -> IO(Pair(Listener, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Accept<Socket>))
def server.lose source · line 105 · raw
@sock:Socket -> @line:String -> IO(Unit)
a lost connection logs its line and closes
def server.read.done source · line 112 · raw
@sock:Socket -> @ps:List<&2, String> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
the request once it has all arrived (or the peer closed, or the receives ran out): its pieces put together and parsed
def server.read.needs source · line 120 · raw
@m:Maybe<&2, Nat> -> @ps:List<&2, String> -> Maybe<&2, Nat>
what the request needs, kept once its head has ended
def server.read.whole source · line 131 · raw
@m:Maybe<&2, Nat> -> @+have:Nat -> Bool
whether as much has arrived as the request needs
def server.read.more source · line 139 · raw
@sock:Socket -> @+ps:List<&2, String> -> @+have:Nat -> @+need:Maybe<&2, Nat> -> @whole:Bool -> @rest:(@_:Socket -> @_:List<&2, String> -> @_:Nat -> @_:Maybe<&2, Nat> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))) -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
octets that arrived: stop when the request is whole, else read again
def server.read.add source · line 154 · raw
@sock:Socket -> @+ps:List<&2, String> -> @+have:Nat -> @need:Maybe<&2, Nat> -> @rest:(@_:Socket -> @_:List<&2, String> -> @_:Nat -> @_:Maybe<&2, Nat> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))) -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
octets added to the pieces: is the request now whole?
def server.read.text source · line 165 · raw
@sock:Socket -> @+ps:List<&2, String> -> @+have:Nat -> @need:Maybe<&2, Nat> -> @+text:String -> @closed:Bool -> @rest:(@_:Socket -> @_:List<&2, String> -> @_:Nat -> @_:Maybe<&2, Nat> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))) -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
octets that arrived, or none once the peer has closed
def server.read.got source · line 183 · raw
@+ps:List<&2, String> -> @+have:Nat -> @need:Maybe<&2, Nat> -> @r:Pair(Socket, Result<&1, &1, Pair(U32, String), String>) -> @rest:(@_:Socket -> @_:List<&2, String> -> @_:Nat -> @_:Maybe<&2, Nat> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))) -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
a receive's answer: a failure loses the connection, no octets is the peer closing, octets go onto the pieces
def server.read.go source · line 200 · raw
@fuel:Nat -> @sock:Socket -> @+ps:List<&2, String> -> @+have:Nat -> @+need:Maybe<&2, Nat> -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
receive until the head has ended and its Content-Length has arrived, the peer closes, or the receives run out
def server.read source · line 218 · raw
@sock:Socket -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>))
receive then parse an HTTP request (RFC 9112 §3)
def server.sent.un source · line 224 · raw
@r:Pair(Socket, Result<&1, &1, Pair(U32, String), Unit>) -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<Unit>))
after a send: the socket, or the line of a lost connection
def server.send source · line 231 · raw
@sock:Socket -> @b:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<Unit>))
send octets
def server.send.text source · line 237 · raw
@sock:Socket -> @text:String -> IO(Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<Unit>))
send text as its UTF-8 octets
def server.close source · line 241 · raw
@r:Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<Unit>) -> IO(Unit)
after the last send: close, logging first when it failed
def server.then source · line 250 · raw
@r:Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<Unit>) -> @more:(@_:Socket -> IO(Unit)) -> IO(Unit)
after a send in the middle: go on, or lose the connection
def server.answer source · line 263 · raw
@sock:Socket -> @method:String -> @reply:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply -> IO(Unit)
a text reply shaped for the request's method (laws/HeadAndNoContentHaveNoBody.bend), then close
def server.octets.go source · line 271 · raw
@sock:Socket -> @+status:U32 -> @headers:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> @r:Pair(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes, U32) -> @bare:Bool -> IO(Unit)
an octets reply once its length is known: its head, then its octets unless it is bare (laws/ReplyHeadFramesBody.bend, laws/BareRepliesWriteHeadOnly.bend)
def server.chunks source · line 291 · raw
Nat
the most chunks one streamed reply writes
def server.chunk.put source · line 295 · raw
@sock:Socket -> @c:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Chunk -> @b:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> @more:(@_:Socket -> IO(Unit)) -> IO(Unit)
one chunk the channel gave: framed and sent, or skipped when empty
def server.chunk.sized source · line 311 · raw
@sock:Socket -> @r:Pair(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes, U32) -> @more:(@_:Socket -> IO(Unit)) -> IO(Unit)
a chunk with its length known
def server.chunk.got source · line 320 · raw
@sock:Socket -> @m:Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes> -> @more:(@_:Socket -> IO(Unit)) -> IO(Unit)
what the channel gave: a chunk, or its close, which ends the body
def server.stream.go source · line 333 · raw
@fuel:Nat -> @sock:Socket -> @+ch:Chan(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> IO(Unit)
every chunk the channel gives, until it is closed
def server.stream.body source · line 347 · raw
@sock:Socket -> @+ch:Chan(0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes) -> @bare:Bool -> IO(Unit)
a streamed reply after its head: its chunks, or nothing when it is bare
def server.reply source · line 361 · raw
@sock:Socket -> @+method:String -> @a:Answer -> IO(Unit)
a handler's answer, shaped for the request's method (laws/HeadAndNoContentHaveNoBody.bend, laws/ReplyHeadFramesBody.bend, laws/StreamedRepliesAreChunked.bend, laws/BareRepliesWriteHeadOnly.bend)
def server.req source · line 377 · raw
@req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> @+octets:String -> Req
the handler's request: the request as text and its body's octets
def server.slot.give source · line 432 · raw
@+slots:Chan(Unit) -> @limited:Bool -> IO(Unit)
give a slot back
def server.slot.take source · line 441 · raw
@+slots:Chan(Unit) -> @limited:Bool -> IO(Unit)
take a slot, waiting while every one is taken
def server.done source · line 450 · raw
@+done:Chan(Unit) -> @tracked:Bool -> IO(Unit)
say a fork is done, when the serve waits for its forks
def server.join source · line 484 · raw
@n:Nat -> @+done:Chan(Unit) -> IO(Unit)
wait for n forks to say they are done
def server.slots source · line 547 · raw
@+max:U32 -> IO(Chan(Unit))
the slots a concurrency limit allows (max 0: no limit)
def server.plain source · line 626 · raw
@r:Req -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request
a _with request as the text request a plain handler takes
def server.text source · line 631 · raw
@reply:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply -> IO(Answer)
a text reply as an answer
Templates
template server.step source · line 387 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @sock:Socket -> @+method:String -> @+octets:String -> @step:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Step -> IO(Unit)
answer now, or call the handler: step.bend decides which (laws/BadRequestAnswers400.bend, laws/HandlerGetsRequestText.bend, laws/WithServesGetOctetsAndContext.bend)
template server.conn.got source · line 404 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @r:Pair(Socket, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Got<0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request>) -> IO(Unit)
after the read: a lost connection, or a request to step
template server.conn source · line 420 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @sock:Socket -> IO(Unit)
one connection: read, decide, maybe handle, write, close
template server.fork.body source · line 459 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @sock:Socket -> @+pool:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Pool -> IO(Unit)
a connection's fork: serve it, give its slot back, and say it is done
template server.fork source · line 472 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @sock:Socket -> @+pool:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Pool -> IO(Unit)
serve an accepted connection on its own fork, a slot taken first
template server.loop.got source · line 495 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @+pool:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Pool -> @r:Pair(Listener, 0x0b59049ea5717d6f2fd30293c7271ede/src/step.Accept<Socket>) -> @took:(@_:Listener -> IO(Unit)) -> @idle:(@_:Listener -> IO(Unit)) -> @missed:(@_:Listener -> IO(Unit)) -> IO(Unit)
what an accept brought, for the loop: took serves a connection and goes
on with one connection fewer, idle accepts again, missed logs, pauses
and goes on with one fewer (a failed accept counts toward the limit)
template server.loop.go source · line 519 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @fuel:Nat -> @idles:Nat -> @+forks:Nat -> @+pool:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Pool -> @listener:Listener -> IO(Unit)
the limited accept loop: fuel connections left, idles idle accepts in
a row left, forks forked so far; its pool is tracked, so the loop joins
its forks on done before it returns
template server.serve_io_with source · line 553 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @host:String -> @port:U32 -> @limit:U32 -> @+max:U32 -> IO(Unit)
listen and serve up to limit connections, each on its own fork, at most
max at once (0: no limit), with a context value for the handler; returns
once every fork is done (trusted above)
template server.lift.with source · line 570 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> Answer) -> @+ctx:C -> @req:Req -> IO(Answer)
a pure handler as an IO one
template server.serve_with source · line 579 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> Answer) -> @+ctx:C -> @host:String -> @port:U32 -> @limit:U32 -> @max:U32 -> IO(Unit)
listen and serve with a pure handler and a context value
template server.wrap.io source · line 639 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)) -> @r:Req -> IO(Answer)
a plain IO handler as a _with one
template server.serve_io source · line 646 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)) -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)
listen and serve up to limit connections with an IO handler
template server.lift source · line 656 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)
a pure handler as an IO one
template server.serve source · line 663 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)
listen and serve up to limit connections
template server.once source · line 672 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @host:String -> @port:U32 -> IO(Unit)
serve exactly one connection then stop
template server.dispatch source · line 691 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply
turn a bad request into 400; otherwise call the handler
template server.reply.built source · line 702 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @method:String -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply
apply HEAD body stripping once the method token is known
template server.reply.text source · line 710 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply
HEAD responses are written with an empty body (RFC 9110 §9.3.2)
template server.cors_reply source · line 722 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply) -> @cfg:0x0b59049ea5717d6f2fd30293c7271ede/src/cors.Cfg -> @+req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply
CORS preflight or simple headers, after HEAD bodies are dropped
Unsafe
unsafe server.forever.go source · line 593 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @+pool:0x0b59049ea5717d6f2fd30293c7271ede/src/step.Pool -> @listener:Listener -> IO(Unit)
the accept loop with no limit: it runs until the process is stopped; its pool is untracked, so its forks never say they are done
unsafe server.serve_forever_with source · line 608 · raw
@-C:Data -> @-handle:(@_:C -> @_:Req -> IO(Answer)) -> @+ctx:C -> @host:String -> @port:U32 -> @+max:U32 -> IO(Unit)
listen and serve with an IO handler and a context value until the process
is stopped, at most max connections at once (0: no limit)
unsafe server.serve_forever source · line 680 · raw
@-handle:(@_:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)) -> @host:String -> @port:U32 -> IO(Unit)
listen and serve with an IO handler until the process is stopped