sigildocs

(sigil http response)

(sigil http response) - HTTP Response Types and Serialization

Provides the <http-response> record and functions for serializing HTTP/1.1 responses to sockets.

Exports

http-responseprocedure

HTTP Response Record

Test if a value is a http-response struct.

Get the status field of a http-response struct.

Get the headers field of a http-response struct.

Get the body field of a http-response struct.

HTTP-OKvariable

Status code constants

Get the human-readable message for an HTTP status code.

(http-status-message 200)  ; => "OK"
(http-status-message 404)  ; => "Not Found"

Convenience constructors Create a plain text response.

(http-response/text 200 "Hello, world!")

Create an HTML response.

(http-response/html 200 "<h1>Hello</h1>")

Create a JSON response.

The body should be an already-encoded JSON string.

(http-response/json 200 (json-encode #{ name: "Alice" }))

Create a 404 Not Found response with a default HTML body.

Create a redirect response.

Defaults to 302 Found. Pass a status code to override.

(http-response/redirect "/login")
(http-response/redirect "/new-url" 301)

Create an error response with an HTML body.

(http-response/error 500 "Something went wrong")
body-lengthprocedure

Get body length for Content-Length header For strings, we need byte length (UTF-8), not character count

Check if body is a streaming producer

Build status line: "HTTP/1.1 200 OKrn"

Build headers string Converts keyword keys to HTTP header format

Ensure required headers are present

Write an HTTP response to a socket.

The write function should accept (socket data) and return bytes written or #f. Returns #t on success, #f on error.

Convert an HTTP response to a string (for debugging).

Parse an HTTP Range header value into (start . end). Returns #f if the header is absent or malformed.

(parse-range-header "bytes=0-499")    ; => (0 . 499)
(parse-range-header "bytes=500-")     ; => (500 . #f)
(parse-range-header #f)               ; => #f

Serve a file with automatic MIME type and optional Range support. Uses streaming I/O to send files in chunks without loading the entire file into memory.

(http-response/file "/path/to/video.mp4")
(http-response/file "/path/to/video.mp4" range-start: 0 range-end: 499)

Create a Server-Sent Events response.

The producer is called with two arguments:

  • send: (lambda (event-type data) ...) - sends an event
  • close: (lambda () ...) - closes the stream

Example:

(http-response/sse
  (lambda (send close)
    (send "message" (dict text: "Hello"))
    (send "update" (dict count: 42))
    ;; Keep connection open, send more events...
    ))

Create an SSE response that broadcasts from a channel.

Subscribes to a broadcast channel and sends events for each message. The handler transforms messages into SSE event strings.

Parameters: broadcast: The broadcast channel to subscribe to handler: (lambda (msg) ...) -> string or #f Returns SSE event string to send, or #f to skip buffer-size: Subscriber buffer size (default: 32) on-connect: Optional (lambda () ...) called on connect Returns initial event string or #f

Example:

(http-response/sse-broadcast chat-broadcast
  (lambda (msg)
    (sse-event "message" (json-encode msg))))
sse-eventprocedure

Format a Server-Sent Event with event type and data.

Returns a string in SSE format: event: <type> data: <json>

The data is JSON-encoded if it's not already a string.

sse-dataprocedure

Format a simple data-only SSE event (no event type).

Returns a string in SSE format: data: <content>