Guide
Streaming, files and uploads
Streaming, files and uploads
Streamed responses
A streaming route returns a body.Stream: a status, fields, and a producer
that writes through an Output.
module main
import pooka/int
import pooka/otp
import whiplash/body
countdown() -> body.Stream {
body.Stream(200, [#("content-type", "text/plain")], fn(output) { count(output, 10) })
}
count(output: body.Output, n: Int) -> Result(Nil, String) {
match n {
0 -> output.send(<<"lift-off\n":utf8>>)
_ -> {
let _ = output.send(<<(int.to_string(n) + "\n"):utf8>>)?
otp.sleep(200)
count(output, n - 1)
}
}
}
The producer runs in its own linked process. Each send may wait: the
producer is held back while too much is queued for a slow client (about 256
KiB on HTTP/1; on HTTP/2 and HTTP/3, while the client's flow-control windows
are closed). A send that fails means the client has gone: return the error
and the producer ends. Without a content-length field the body is chunked
on HTTP/1; with one, the producer must write exactly that many bytes, or the
response is cut off (HTTP/1) or its stream reset (HTTP/2, HTTP/3).
body.chunks(status, fields, chunks) streams a list you already have.
Server-sent events
body.event_headers() gives the fields of an event stream;
output.event(body.Event(data, id, event, retry)) writes one event (multi-line
data is split for you) and output.heartbeat(text) writes a comment, which
keeps proxies from closing a quiet connection. body.event(data) is an event
with data only, body.events(list) a stream of events you already have.
module main
import pooka/int
import pooka/otp
import whiplash/body
ticks() -> body.Stream {
body.Stream(200, body.event_headers(), fn(output) { tick(output, 1) })
}
tick(output: body.Output, n: Int) -> Result(Nil, String) {
let _ = output.event(body.Event("tick", Some(int.to_string(n)), Some("tick"), None))?
otp.sleep(1000)
tick(output, n + 1)
}
Files
body.file(path, content_type) is a stream of a regular file. Whiplash keeps
a VM-wide cache like nginx's open_file_cache: a file's size and modification
time for one second, and the bytes of files up to 128 KiB while those are
current (256 files at most). A changed file is served within a second, not
instantly. Files up to 128 KiB leave in one write with their head; larger
ones go through sendfile on cleartext HTTP/1 and through the producer
elsewhere.
Uploads
An upload route gets the request with an empty body and a body.Reader:
module main
import pooka/bit_array
import pooka/http
import pooka/int
import whiplash/body
uploads(request: http.Request) -> Option(fn(http.Request, body.Reader) -> http.Response) {
match request.path {
"/count" -> Some(fn(_, reader: body.Reader) {
match count(reader, 0) {
Ok(bytes) -> http.text(200, int.to_string(bytes) + " bytes")
Error(reason) -> http.text(400, reason)
}
})
_ -> None
}
}
count(reader: body.Reader, bytes: Int) -> Result(Int, String) {
match reader.read(10000)? {
Some(piece) -> count(reader, bytes + bit_array.byte_size(piece))
None -> Ok(bytes)
}
}
reader.read(timeout) returns the next piece, or None at the end;
reader.all(limit, timeout) the whole body. Pieces arrive as the client sends
them: a chunked body, or one larger than 64 KiB, reaches the handler while it
is still arriving, and a slow handler holds the client back (by TCP on
HTTP/1, by flow control on HTTP/2 and HTTP/3). A body with a declared length
of at most 64 KiB is handed over once it has ended. Every upload counts
against max_body: beyond it the answer is 413.