Skip to content

RequestHandler

[Source]

The handler's interface to the connection.

Embedded in a user's handler actor (or used inline). Hides the connection protocol and tracks response state to prevent double-response.

For inline handlers, create a RequestHandler, call respond(), and let it go out of scope:

{(ctx) =>
  hobby.RequestHandler(consume ctx).respond(stallion.StatusOK, "Hello!")
} val

For async handlers, embed it in an actor:

actor MyHandler is hobby.HandlerReceiver
  embed _handler: hobby.RequestHandler

  new create(ctx: hobby.HandlerContext iso) =>
    _handler = hobby.RequestHandler(consume ctx)
    // ... start async work ...

  be result(value: String) =>
    _handler.respond(stallion.StatusOK, value)

  be dispose() => None
  be throttled() => None
  be unthrottled() => None
class ref RequestHandler

Constructors

create

[Source]

Create a handler from a consumed handler context.

Extracts all request data and protocol references from the context.

new ref create(
  ctx: HandlerContext iso)
: RequestHandler ref^

Parameters

Returns


Public Functions

respond

[Source]

Send a complete response with the given status and body.

Idempotent — the first call sends the response, subsequent calls are silently ignored. Content-Length is set automatically by the connection. For HEAD requests, the body is suppressed but Content-Length is preserved.

fun ref respond(
  status: Status val,
  body': (String val | Array[U8 val] val))
: None val

Parameters

Returns


respond_with_headers

[Source]

Send a complete response with explicit headers and body.

The caller is responsible for including Content-Length or any other required headers. For HEAD requests, the body is suppressed but all headers are preserved.

fun ref respond_with_headers(
  status: Status val,
  headers: Headers val,
  body': (String val | Array[U8 val] val))
: None val

Parameters

Returns


start_streaming

[Source]

Begin a streaming response using chunked transfer encoding.

Returns StreamingStarted on success, ChunkedNotSupported for HTTP/1.0 clients, or BodyNotNeeded for HEAD requests (the framework sends a headers-only response automatically).

After StreamingStarted, call send_chunk() to send data and finish() to complete the stream. If already responded, returns BodyNotNeeded.

For ChunkedNotSupported, respond() can still be called as a fallback.

fun ref start_streaming(
  status: Status val,
  headers: (Headers val | None val) = reference)
: (StreamingStarted val | ChunkedNotSupported val | BodyNotNeeded val)

Parameters

Returns


send_chunk

[Source]

Send a streaming chunk.

Only effective after start_streaming() returned StreamingStarted. Silently ignored otherwise.

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

Parameters

Returns


finish

[Source]

End the streaming response.

Sends the terminal chunk and signals completion. Only effective after start_streaming() returned StreamingStarted.

fun ref finish()
: None val

Returns


param

[Source]

Get a route parameter by name.

Errors if the parameter does not exist. Parameter names come from route definitions — :id in /users/:id is accessed as param("id").

fun box param(
  key: String val)
: String val ?

Parameters

Returns


body

[Source]

Return the accumulated request body bytes.

fun box body()
: Array[U8 val] val

Returns


request

[Source]

Return the HTTP request.

fun box request()
: Request val

Returns


is_head

[Source]

Return true if this is a HEAD request.

fun box is_head()
: Bool val

Returns