| @@ -26,14 +26,21 @@ |
| 26 | 26 | /// (`Transfer-Encoding: chunked`), up to a size limit (413 beyond it); a |
| 27 | 27 | /// malformed request gets an error response, and the connection is |
| 28 | 28 | /// closed; |
| 29 | | −/// - the server sets `Content-Length` and `Connection` on responses. |
| 29 | +/// - the server sets `Content-Length` and `Connection` on responses; |
| 30 | +/// - a response can be **streamed** (ADR 0053): the head goes out with |
| 31 | +/// `Transfer-Encoding: chunked`, the handler's stream fn writes chunks |
| 32 | +/// (each flushed, each write timed per chunk; the stream between chunks |
| 33 | +/// is not timed), and keep-alive continues after the final chunk. |
| 34 | +/// Server-Sent Events are a streamed response that stays open. |
| 30 | 35 | /// |
| 31 | 36 | /// Request heads and chunk sizes are parsed by httparse (picohttpparser's |
| 32 | 37 | /// design) in the runtime. |
| 33 | 38 | |
| 39 | +import std/int |
| 34 | 40 | import std/io |
| 35 | 41 | import std/list |
| 36 | 42 | import std/option |
| 43 | +import std/result |
| 37 | 44 | import std/string |
| 38 | 45 | import std/bytes.{type Builder} |
| 39 | 46 | import std/net.{type TcpListener, type TcpStream} |
| @@ -48,7 +55,81 @@ pub type Request { |
| 48 | 55 | } |
| 49 | 56 | |
| 50 | 57 | pub type Response { |
| 51 | | − Response(status: Int, headers: List<(String, String)>, body: Bytes) |
| 58 | + Response(status: Int, headers: List<(String, String)>, body: Body) |
| 59 | +} |
| 60 | + |
| 61 | +/// What a response's body is (ADR 0053). |
| 62 | +pub type Body { |
| 63 | + /// The whole body, in hand. |
| 64 | + Whole(Bytes) |
| 65 | + /// Written chunk by chunk: the server sends the head with |
| 66 | + /// `Transfer-Encoding: chunked`, then calls `write` with a `Writer` |
| 67 | + /// and sends the final chunk when it returns. Between chunks the |
| 68 | + /// connection is not timed; each chunk's write is, per chunk, by the |
| 69 | + /// write timeout. A HEAD request sends the head only (`write` is not |
| 70 | + /// called). If `write` unwinds, the connection dies without the final |
| 71 | + /// chunk: the client sees a truncated stream, the only honest end |
| 72 | + /// once the status line is sent. |
| 73 | + Streamed(write: fn(Writer) -> () / <io, proc, clock, random, ffi>) |
| 74 | +} |
| 75 | + |
| 76 | +/// What a streamed response writes with (ADR 0053). |
| 77 | +pub opaque type Writer { |
| 78 | + Writer(conn: Resource<TcpStream>, activity: Subject<Activity>) |
| 79 | +} |
| 80 | + |
| 81 | +/// Write one chunk, flushed (one frame, one socket write). `Error` says |
| 82 | +/// the client is gone: stop writing. |
| 83 | +pub fn write_chunk(w: Writer, chunk: Bytes) -> Result<(), String> / <io, proc> { |
| 84 | + let Writer(conn, activity) = w |
| 85 | + process.send(activity, Writing) |
| 86 | + match net.write(conn, frame(chunk)) { |
| 87 | + Ok(()) -> { |
| 88 | + process.send(activity, Streaming) |
| 89 | + Ok(()) |
| 90 | + }, |
| 91 | + Error(e) -> Error(e), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Write one chunk of text, flushed. |
| 96 | +pub fn write_string(w: Writer, text: String) -> Result<(), String> / <io, proc> { |
| 97 | + write_chunk(w, bytes.from_string(text)) |
| 98 | +} |
| 99 | + |
| 100 | +/// A chunk's frame: its length in hex, CRLF, the data, CRLF. |
| 101 | +fn frame(chunk: Bytes) -> Bytes { |
| 102 | + let head = to_hex(bytes.length(chunk)) <> "\r\n" |
| 103 | + bytes.concat_all([bytes.from_string(head), chunk, bytes.from_string("\r\n")]) |
| 104 | +} |
| 105 | + |
| 106 | +fn to_hex(n: Int) -> String { |
| 107 | + if n <= 0 { "0" } else { hex_up(n, "") } |
| 108 | +} |
| 109 | + |
| 110 | +fn hex_up(n: Int, acc: String) -> String { |
| 111 | + if n == 0 { acc } else { hex_up(int.shift_right(n, 4), hex_digit(int.and(n, 15)) <> acc) } |
| 112 | +} |
| 113 | + |
| 114 | +fn hex_digit(d: Int) -> String { |
| 115 | + match d { |
| 116 | + 0 -> "0", |
| 117 | + 1 -> "1", |
| 118 | + 2 -> "2", |
| 119 | + 3 -> "3", |
| 120 | + 4 -> "4", |
| 121 | + 5 -> "5", |
| 122 | + 6 -> "6", |
| 123 | + 7 -> "7", |
| 124 | + 8 -> "8", |
| 125 | + 9 -> "9", |
| 126 | + 10 -> "a", |
| 127 | + 11 -> "b", |
| 128 | + 12 -> "c", |
| 129 | + 13 -> "d", |
| 130 | + 14 -> "e", |
| 131 | + _ -> "f", |
| 132 | + } |
| 52 | 133 | } |
| 53 | 134 | |
| 54 | 135 | /// Change the defaults with a record update: |
| @@ -105,7 +186,31 @@ pub fn form_body(req: Request) -> List<(String, String)> { |
| 105 | 186 | |
| 106 | 187 | /// A response with `body` of type `content_type`. |
| 107 | 188 | pub fn response(status: Int, content_type: String, body: Bytes) -> Response { |
| 108 | | − Response(status, [("Content-Type", content_type)], body) |
| 189 | + Response(status, [("Content-Type", content_type)], Whole(body)) |
| 190 | +} |
| 191 | + |
| 192 | +/// A 200 response written chunk by chunk (ADR 0053): the head goes out |
| 193 | +/// first (`Transfer-Encoding: chunked`), then `write` runs with a |
| 194 | +/// `Writer`; each `write_chunk`/`write_string` flushes. Between chunks |
| 195 | +/// the connection is not timed; each write is, per chunk. Keep-alive |
| 196 | +/// continues after the final chunk. |
| 197 | +/// |
| 198 | +/// http.stream("text/html; charset=utf-8", fn(w) { |
| 199 | +/// let _ = http.write_string(w, html.render(shell_top)) |
| 200 | +/// // each component, as it finishes |
| 201 | +/// let _ = http.write_string(w, "</body></html>") |
| 202 | +/// }) |
| 203 | +pub fn stream(content_type: String, write: fn(Writer) -> () / <io, proc, clock, random, ffi>) -> Response { |
| 204 | + Response(200, [("Content-Type", content_type)], Streamed(write)) |
| 205 | +} |
| 206 | + |
| 207 | +/// The body a response holds, if it is a whole one (a streamed response's |
| 208 | +/// body is written, not held). |
| 209 | +pub fn whole_body(resp: Response) -> Option<Bytes> { |
| 210 | + match resp.body { |
| 211 | + Whole(body) -> Some(body), |
| 212 | + Streamed(_) -> None, |
| 213 | + } |
| 109 | 214 | } |
| 110 | 215 | |
| 111 | 216 | /// A 200 response with a rendered page. |
| @@ -192,6 +297,9 @@ type Activity { |
| 192 | 297 | Handling |
| 193 | 298 | /// It sends a response: timed. |
| 194 | 299 | Writing |
| 300 | + /// A streamed response, between chunks: not timed (a stream may be |
| 301 | + /// quiet arbitrarily long); each chunk's write is `Writing`. |
| 302 | + Streaming |
| 195 | 303 | /// It stopped (also by crashing). |
| 196 | 304 | Stopped |
| 197 | 305 | } |
| @@ -229,6 +337,7 @@ fn watch(conn, settings: Settings, events, timeout) { |
| 229 | 337 | Some(HeadStarted) -> watch(conn, settings, events, settings.head_timeout), |
| 230 | 338 | Some(Handling) -> watch(conn, settings, events, -1), |
| 231 | 339 | Some(Writing) -> watch(conn, settings, events, settings.write_timeout), |
| 340 | + Some(Streaming) -> watch(conn, settings, events, -1), |
| 232 | 341 | Some(Stopped) -> (), |
| 233 | 342 | } |
| 234 | 343 | } |
| @@ -253,11 +362,25 @@ fn serve_requests(conn: Resource<TcpStream>, activity: Subject<Activity>, max_bo |
| 253 | 362 | match read_request(conn, activity, max_body, buffer, at) { |
| 254 | 363 | Ok(Incoming(req, keep_alive, buffer, at)) -> { |
| 255 | 364 | process.send(activity, Handling) |
| 365 | + // HEAD is GET without the body (RFC 9110 §9.3.2): the handler sees |
| 366 | + // a GET, and the body is not sent (a stream fn is not called). |
| 256 | 367 | let with_body = req.method != "HEAD" |
| 368 | + let req = if with_body { req } else { Request(..req, method: "GET") } |
| 257 | 369 | let resp = handler(req) |
| 258 | 370 | let closes = find_header(resp.headers, "connection") |> option.unwrap_or("") |> has_token("close") |
| 259 | 371 | let keep = keep_alive && !closes |
| 260 | | − match send_response(conn, activity, serialize(resp, keep, with_body)) { |
| 372 | + let sent = |
| 373 | + match resp.body { |
| 374 | + Whole(_) -> send_response(conn, activity, serialize(resp, keep, with_body)), |
| 375 | + Streamed(write) -> |
| 376 | + if with_body { |
| 377 | + serve_streamed(conn, activity, resp, keep, write) |
| 378 | + } else { |
| 379 | + // HEAD: the head only; the stream fn is not called. |
| 380 | + send_response(conn, activity, serialize_stream_head(resp, keep)) |
| 381 | + }, |
| 382 | + } |
| 383 | + match sent { |
| 261 | 384 | Ok(_) -> if keep { serve_requests(conn, activity, max_body, handler, buffer, at) } else { () }, |
| 262 | 385 | Error(_) -> (), |
| 263 | 386 | } |
| @@ -270,6 +393,17 @@ fn serve_requests(conn: Resource<TcpStream>, activity: Subject<Activity>, max_bo |
| 270 | 393 | } |
| 271 | 394 | } |
| 272 | 395 | |
| 396 | +/// A streamed response (ADR 0053): the head (chunked, no length), the |
| 397 | +/// stream fn, the final chunk. Between chunks the connection is untimed; |
| 398 | +/// each write is timed per chunk by the watchdog. |
| 399 | +fn serve_streamed(conn: Resource<TcpStream>, activity: Subject<Activity>, resp: Response, keep: Bool, write: fn(Writer) -> () / <io, proc, clock, random, ffi>) -> Result<(), String> / <io, proc, clock, random, ffi> { |
| 400 | + use () <- result.try(send_response(conn, activity, serialize_stream_head(resp, keep))) |
| 401 | + process.send(activity, Streaming) |
| 402 | + write(Writer(conn, activity)) |
| 403 | + process.send(activity, Writing) |
| 404 | + net.write(conn, bytes.from_string("0\r\n\r\n")) |
| 405 | +} |
| 406 | + |
| 273 | 407 | /// Write a response (timed by the watchdog). |
| 274 | 408 | fn send_response(conn: Resource<TcpStream>, activity: Subject<Activity>, data: Bytes) -> Result<(), String> / <io, proc> { |
| 275 | 409 | process.send(activity, Writing) |
| @@ -518,22 +652,33 @@ type Chunk { |
| 518 | 652 | fn parse_chunk(buffer: Bytes, at: Int) -> Chunk |
| 519 | 653 | |
| 520 | 654 | /// The head and (unless `with_body` is false, for HEAD requests) the body, |
| 521 | | −/// in one buffer so they leave in one write. |
| 655 | +/// in one buffer so they leave in one write. The whole-body case. |
| 522 | 656 | fn serialize(resp: Response, keep_alive: Bool, with_body: Bool) -> Bytes { |
| 523 | | − let own = list.filter(resp.headers, fn(h) { !is_framing(string.to_lower(h.0)) }) |
| 524 | | − let head = string.concat([ |
| 657 | + let body = option.unwrap_or(whole_body(resp), bytes.empty()) |
| 658 | + let head = head_bytes(resp.status, resp.headers, keep_alive, "Content-Length: " <> show(bytes.length(body))) |
| 659 | + let out = bytes.append_string(bytes.builder(), head) |
| 660 | + bytes.build(if with_body { bytes.append(out, body) } else { out }) |
| 661 | +} |
| 662 | + |
| 663 | +/// A streamed response's head: `Transfer-Encoding: chunked`, no length. |
| 664 | +fn serialize_stream_head(resp: Response, keep_alive: Bool) -> Bytes { |
| 665 | + bytes.from_string(head_bytes(resp.status, resp.headers, keep_alive, "Transfer-Encoding: chunked")) |
| 666 | +} |
| 667 | + |
| 668 | +/// The status line, the caller's headers (framing filtered), the framing |
| 669 | +/// header, and the keep-alive line. |
| 670 | +fn head_bytes(status: Int, headers: List<(String, String)>, keep_alive: Bool, framing: String) -> String { |
| 671 | + let own = list.filter(headers, fn(h) { !is_framing(string.to_lower(h.0)) }) |
| 672 | + string.concat([ |
| 525 | 673 | "HTTP/1.1 ", |
| 526 | | − show(resp.status), |
| 674 | + show(status), |
| 527 | 675 | " ", |
| 528 | | − reason(resp.status), |
| 676 | + reason(status), |
| 529 | 677 | "\r\n", |
| 530 | 678 | string.concat(list.map(own, fn(h) { h.0 <> ": " <> h.1 <> "\r\n" })), |
| 531 | | − "Content-Length: ", |
| 532 | | − show(bytes.length(resp.body)), |
| 679 | + framing, |
| 533 | 680 | if keep_alive { "\r\nConnection: keep-alive\r\n\r\n" } else { "\r\nConnection: close\r\n\r\n" }, |
| 534 | 681 | ]) |
| 535 | | − let out = bytes.append_string(bytes.builder(), head) |
| 536 | | − bytes.build(if with_body { bytes.append(out, resp.body) } else { out }) |
| 537 | 682 | } |
| 538 | 683 | |
| 539 | 684 | /// Headers the server sets itself. |