(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-status-messageprocedureGet human-readable status message
http-response/textprocedureConvenience constructors
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 HTTP response to socket write-fn: (socket data) -> bytes-written or #f Returns #t on success, #f on error
http-response->stringprocedureConvert response to string (for debugging)
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...
))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>