~/bend-docscommunity

server.bend relies on unsafe/foreign

raw source on the hub · import gralha-http-server@0.1.0.0/server.bend as Server

An HTTP/1.1 server over Base's TCP: one request per connection, answered and closed, one connection at a time. The app's state threads through the handler, a template (~), so it runs inline and may run every time.

2 imports
import Base
import gralha-http-core@0.1.0.0/http.bend as Http

Definitions

def read.chunk source · line 10 · raw

@socket:Socket -> @+text:String -> @+max:U32 -> @next:(@_:Socket -> @_:String -> IO(Pair(Socket, String))) -> IO(Pair(Socket, String))

def read.got source · line 15 · raw

@got:Pair(Socket, Result<&1, &1, Pair(U32, String), Maybe<&1, String>>) -> @acc:String -> @+max:U32 -> @next:(@_:Socket -> @_:String -> IO(Pair(Socket, String))) -> IO(Pair(Socket, String))

def read.text source · line 33 · raw

@socket:Socket -> @+max:U32 -> IO(Pair(Socket, String))

The request's text: received until it is whole (Http.request.complete), the peer stops sending, or more than max bytes arrived; "" on an error.

def read.parse source · line 36 · raw

@got:Pair(Socket, String) -> IO(Pair(Socket, Maybe<&2, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Request>))

def read source · line 41 · raw

@socket:Socket -> @+max:U32 -> IO(Pair(Socket, Maybe<&2, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Request>))

The request read and parsed, or None if it never became one.

def send.done source · line 50 · raw

@got:Pair(Socket, Result<&1, &1, Pair(Pair(U32, String), List<&2, U32>), Unit>) -> IO(Unit)

A failed send also hands back the unsent rest, which a closing reply drops.

def send.text source · line 55 · raw

@socket:Socket -> @text:String -> IO(Unit)

Sends text as it is and closes the socket.

def closing source · line 61 · raw

@r:0x59279b68893c8c86cbcf854fcfc9db2f/http.Response -> 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response

The response with Connection: close, unless it names a Connection.

def send source · line 69 · raw

@socket:Socket -> @r:0x59279b68893c8c86cbcf854fcfc9db2f/http.Response -> IO(Unit)

Sends a response (Http.response.text, with Connection: close) and closes the socket.

def answer.sent source · line 75 · raw

@-S:Type -> @socket:Socket -> @out:Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response) -> IO(S)

def loop.next source · line 146 · raw

@-S:Type -> @got:Pair(Listener, S) -> @next:(@_:Listener -> @_:S -> IO(Pair(Listener, S))) -> IO(Pair(Listener, S))

Templates

template answer source · line 81 · raw

@-S:Type -> @-handle:(@_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response))) -> @got:Pair(Socket, Maybe<&2, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Request>) -> @state:S -> IO(S)

template answer.socket source · line 94 · raw

@-S:Type -> @-reply:(@_:Socket -> @_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(S)) -> @got:Pair(Socket, Maybe<&2, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Request>) -> @state:S -> IO(S)

template accepted source · line 105 · raw

@-S:Type -> @-handle:(@_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response))) -> @got:Pair(Listener, Result<&1, &1, Pair(U32, String), Socket>) -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

template accepted.socket source · line 117 · raw

@-S:Type -> @-reply:(@_:Socket -> @_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(S)) -> @got:Pair(Listener, Result<&1, &1, Pair(U32, String), Socket>) -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

template step source · line 132 · raw

@-S:Type -> @-handle:(@_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response))) -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

Accepts one connection, reads its request (up to max bytes), answers it with handle(request)(state) and closes it; a request that never parses gets a 400. A failed accept leaves the state as it was.

template step.socket source · line 140 · raw

@-S:Type -> @-reply:(@_:Socket -> @_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(S)) -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

As step, but reply(socket)(request)(state) answers on the socket itself and must close it: for bodies the app streams through its own effect.

template serve.for source · line 151 · raw

@-S:Type -> @-handle:(@_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response))) -> @n:Nat -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

Serves n connections, then hands back the listener and the state.

template serve.socket.for source · line 161 · raw

@-S:Type -> @-reply:(@_:Socket -> @_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(S)) -> @n:Nat -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

Unsafe

unsafe read.go source · line 26 · raw

@socket:Socket -> @+max:U32 -> @acc:String -> IO(Pair(Socket, String))

unsafe serve source · line 172 · raw

@-S:Type -> @-handle:(@_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(Pair(S, 0x59279b68893c8c86cbcf854fcfc9db2f/http.Response))) -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))

Serves connections forever, one at a time (unsafe: it never ends).

unsafe serve.socket source · line 178 · raw

@-S:Type -> @-reply:(@_:Socket -> @_:0x59279b68893c8c86cbcf854fcfc9db2f/http.Request -> @_:S -> IO(S)) -> @listener:Listener -> @state:S -> @+max:U32 -> IO(Pair(Listener, S))