(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-responseprocedureHTTP Response Record
http-response?procedureTest if a value is a http-response struct.
http-response-statusprocedureGet the status field of a http-response struct.
http-response-headersprocedureGet the headers field of a http-response struct.
http-response-bodyprocedureGet the body field of a http-response struct.
HTTP-OKvariableStatus code constants
http-status-messageprocedureGet the human-readable message for an HTTP status code.
(http-status-message 200) ; => "OK"
(http-status-message 404) ; => "Not Found"http-response/textprocedureConvenience constructors Create a plain text response.
(http-response/text 200 "Hello, world!")http-response/htmlprocedureCreate an HTML response.
(http-response/html 200 "<h1>Hello</h1>")http-response/jsonprocedureCreate a JSON response.
The body should be an already-encoded JSON string.
(http-response/json 200 (json-encode #{ name: "Alice" }))http-response/not-foundprocedureCreate a 404 Not Found response with a default HTML body.
http-response/redirectprocedureCreate a redirect response.
Defaults to 302 Found. Pass a status code to override.
(http-response/redirect "/login")
(http-response/redirect "/new-url" 301)http-response/errorprocedureCreate an error response with an HTML body.
(http-response/error 500 "Something went wrong")body-lengthprocedureGet body length for Content-Length header For strings, we need byte length (UTF-8), not character count
streaming-body?procedureCheck if body is a streaming producer
build-status-lineprocedureBuild status line: "HTTP/1.1 200 OKrn"
build-headers-stringprocedureBuild headers string Converts keyword keys to HTTP header format
ensure-headersprocedureEnsure required headers are present
write-http-responseprocedureWrite 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.
http-response->stringprocedureConvert an HTTP response to a string (for debugging).
parse-range-headerprocedureParse 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) ; => #ffile-chunk-sizevariableServe 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)http-response/sseprocedureCreate 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...
))http-response/sse-broadcastvariableCreate 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-eventprocedureFormat 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-dataprocedureFormat a simple data-only SSE event (no event type).
Returns a string in SSE format: data: <content>