flare_mlake · API · http

flare.http.static_response

Pre-encoded literal HTTP responses for the fastest possible fast path.

Handlers that always return the same bytes (health checks, TFB plaintext, fixed "hello" endpoints) pay for no reason on every request:

  • A Response struct is constructed.
  • Headers are looked up / appended.
  • Status line, header block, and body are re-serialised on every call.

StaticResponse does all of that once at module-initialisation time and hands the reactor a frozen buffer. HttpServer.serve_static[resp] then skips the parser's handler-dispatch step entirely and memcpys the buffer straight into the per-connection write queue.

Shape
from flare.http import StaticResponse, precompute_response

var HELLO = precompute_response(
    status=200,
    content_type="text/plain; charset=utf-8",
    body="Hello, World!",
)
# HELLO holds two wire-form buffers:
# keepalive — with "Connection: keep-alive"
# close — with "Connection: close"
# The reactor picks the right one per request based on Connection:.
Semantics

Every request still gets parsed far enough to find the ``

header terminator and any declaredContent-Length(so the server can skip the body bytes before starting the next pipelined request on a keep-alive connection). The parser does **not** build aRequest`` struct and never calls a handler.

HTTP/1.0 close semantics, Connection: close, and the max_keepalive_requests cap are all still honoured — the only saving is that we don't re-build identical bytes per request.

A HEAD request is answered with the pre-encoded head only (the bytes up to and including the first CRLFCRLF): a response to HEAD carries no content (RFC 9110 §9.3.2), and queuing the body would let a keep-alive client read it as the start of the next response.

The pre-encoded buffer is a plain List[UInt8] rather than an Array so the reactor can memcpy from it at the same cost as a stack_allocation source.

Structs

StaticResponse

struct StaticResponse

A pair of pre-encoded HTTP/1.1 response buffers.

keepalive_bytes is emitted when the connection will stay open after this response; close_bytes is emitted when the server has decided to close after this write (HTTP/1.0 without keep-alive, explicit Connection: close request, max_keepalive_requests cap reached, or server shutdown).

Both buffers end at the final body byte; the reactor concatenates nothing — it copies the buffer verbatim into the socket write queue.

Implements Copyable, Movable

Fields
keepalive_bytes: List[UInt8]
close_bytes: List[UInt8]
body_length: Int: Exposed so the config-gate logic (e.g. Content-Length > max_body_size) can re-check the precomputed size if it ever needs to — the reactor only reads keepalive_bytes / close_bytes.

__init__

def __init__(out self, var keepalive_bytes: List[UInt8], var close_bytes: List[UInt8], body_length: Int)

copy

def copy(self) -> Self

Functions

precompute_response

def precompute_response(status: Int, content_type: String, body: String) -> StaticResponse

Build a pre-encoded static response for a known (status, body).

Returns a StaticResponse holding both the Connection: keep-alive and Connection: close wire forms. The reactor picks one per request based on the parsed Connection: header.

Args
status: HTTP status code (200, 204, 404, …). Reason phrase is looked up from the built-in table; callers who need a custom reason should pass their own StaticResponse instance.
content_type: Full Content-Type header value (e.g. "text/plain; charset=utf-8").
body: Response body (UTF-8 string). Content-Length is derived from its byte length.