Skip to content

Responder

[Source]

Sends an HTTP response for a single request.

Each request receives its own Responder, delivered via both HTTPServerLifecycleEventReceiver.on_request() and on_request_complete(). The Responder buffers response data through the connection's response queue, which ensures pipelined responses are sent in request order.

Two response modes are available:

Complete response — build the full response with ResponseBuilder and send it via respond():

let body: String val = "Hello!"
let response = ResponseBuilder(StatusOK)
  .add_header("Content-Length", body.size().string())
  .finish_headers()
  .add_chunk(body)
  .build()
responder.respond(response)

Streaming response — use chunked transfer encoding for large or incrementally-generated bodies. start_chunked_response() returns a StartChunkedResponseResult indicating success or the reason for failure:

match responder.start_chunked_response(StatusOK, headers)
| StreamingStarted =>
  let token1 = responder.send_chunk("chunk 1")
  let token2 = responder.send_chunk("chunk 2")
  responder.finish_response()
| ChunkedNotSupported =>
  // HTTP/1.0 — fall back to a complete response
  responder.respond(fallback_response)
| AlreadyResponded => None
end

Each send_chunk() returns a ChunkSendToken (or None if the call was a no-op). A matching on_chunk_sent(token) callback fires when the OS accepts the data, enabling flow-controlled streaming.

Responders are created internally by HTTPServer. Application code should not attempt to construct them directly.

class ref Responder

Public Functions

respond

[Source]

Send a pre-serialized HTTP response, bypassing internal serialization.

The raw bytes must be a complete HTTP response: status line, headers, blank line separator, and optional body. No headers are injected and no validation is performed — the caller is fully responsible for correct HTTP formatting. Use ResponseBuilder to construct well-formed raw responses.

Only valid when no response has been started. Subsequent calls are silently ignored (both after respond() and after any other response method).

fun ref respond(
  raw: (String val | Array[U8 val] val))
: None val

Parameters

Returns


start_chunked_response

[Source]

Begin a streaming response using chunked transfer encoding.

Sends the status line and headers immediately (with Transfer-Encoding: chunked added automatically). Follow with send_chunk() calls and a final finish_response().

Returns StreamingStarted on success, ChunkedNotSupported for HTTP/1.0 requests (which do not support chunked transfer encoding — use respond() with a ResponseBuilder-constructed response instead), or AlreadyResponded when a response has already been started or completed.

fun ref start_chunked_response(
  status: Status val,
  headers: (Headers val | None val) = reference)
: ((StreamingStarted val | AlreadyResponded val | ChunkedNotSupported val) & _StartChunkedResponseResult val)

Parameters

Returns


send_chunk

[Source]

Send a chunk of response body data.

The data is wrapped in chunked transfer encoding format. Empty data is silently ignored — use finish_response() to send the terminal chunk.

Returns ChunkSendToken when the chunk was queued — a matching on_chunk_sent() callback will fire when the OS accepts the data. Returns None when the call was a no-op (wrong state, empty data, or HTTP/1.0 request where chunked encoding was rejected).

Only valid after start_chunked_response(). Calls in other states are silently ignored.

fun ref send_chunk(
  data: (String val | Array[U8 val] val))
: (ChunkSendToken val | None val)

Parameters

Returns


finish_response

[Source]

Finish a streaming response by sending the terminal chunk.

Only valid after start_chunked_response(). Calls in other states are silently ignored.

fun ref finish_response()
: None val

Returns