Response
is a response data type. It holds the code of the request, the
version, the message, the cookies, the headers, and the body.
chunked
(Fn [Int, String, (Ref (Array String) a)] Response)
(chunked code content-type chunks)
creates a response with Transfer-Encoding: chunked.
The body is built from an array of chunk strings, each encoded with the
chunked framing (<hex-length>\r\n<data>\r\n) and terminated by a
final 0\r\n\r\n.
(Response.chunked 200 @"text/plain"
&[@"first chunk" @"second chunk"])
The chunks are pre-encoded into a single buffer. For truly lazy streaming where chunks are generated on demand, a future version will provide a callback-based API.
client-error?
(Fn [(Ref Response a)] Bool)
(client-error? r)
checks whether the status code is between 400 and 500.
content-type-for
(Fn [(Ref String a)] String)
(content-type-for path)
infers an HTTP Content-Type for a file path
based on its extension. Unknown extensions return application/octet-stream.
cookies
(Fn [(Ref Response a)] (Ref (Array Cookie) a))
gets the cookies property of a Response.
file
(Fn [(Ref String a)] Response)
(file path)
creates a 200 OK response by reading path from disk. Returns
a 404 Not Found response if the file is missing or unreadable. The
Content-Type header is inferred from the file extension via
content-type-for.
header
(Fn [(Ref Response a), (Ref String b)] (Maybe String))
(header r name)
gets the first value of a header by name (case-insensitive).
Returns (Maybe String).
headers
(Fn [(Ref Response a)] (Ref (Map String (Array String)) a))
gets the headers property of a Response.
init
(Fn [Int, String, String, (Array Cookie), (Map String (Array String)), String] Response)
creates a Response.
parse
(Fn [(Ref String a)] (Result Response String))
(parse txt)
parses a HTTP response from a string txt.
Returns a (Error String) holding the error message if it fails, otherwise
it will return a (Success Response).
sendfile
(Fn [(Ref String a)] Response)
(sendfile path)
creates a 200 OK response that signals the server to
transfer the file at path directly to the socket via sendfile(2),
avoiding the user-space copy. Falls back to file if the
server does not detect the X-Sendfile header.
The Content-Type is inferred from the extension. The server sets
Content-Length from the file size.
server-error?
(Fn [(Ref Response a)] Bool)
(server-error? r)
checks whether the status code is between 500 and 600.
set-body!
(Fn [(Ref Response a), String] ())
sets the body property of a Response in place.
set-code!
(Fn [(Ref Response a), Int] ())
sets the code property of a Response in place.
set-cookie
(Fn [Response, Cookie] Response)
(set-cookie resp cookie)
adds a Set-Cookie header to the response. The cookie
argument is a Cookie value from the http library.
(Response.set-cookie resp
(Cookie.init @"session" @"abc" @"/"
(Maybe.Nothing) (Maybe.Nothing) true true (SameSite.Lax)))
For simple name/value cookies, use set-simple-cookie.
set-cookies
(Fn [Response, (Array Cookie)] Response)
sets the cookies property of a Response.
set-cookies!
(Fn [(Ref Response a), (Array Cookie)] ())
sets the cookies property of a Response in place.
set-headers
(Fn [Response, (Map String (Array String))] Response)
sets the headers property of a Response.
set-headers!
(Fn [(Ref Response a), (Map String (Array String))] ())
sets the headers property of a Response in place.
set-message!
(Fn [(Ref Response a), String] ())
sets the message property of a Response in place.
set-simple-cookie
(Fn [Response, String, String] Response)
(set-simple-cookie resp name value)
adds a Set-Cookie header with a name and value.
Uses Path=/, HttpOnly, and SameSite=Lax as defaults.
set-version!
(Fn [(Ref Response a), String] ())
sets the version property of a Response in place.
update-body
(Fn [Response, (Ref (Fn [String] String a) b)] Response)
updates the body property of a Response using a function f.
update-code
(Fn [Response, (Ref (Fn [Int] Int a) b)] Response)
updates the code property of a Response using a function f.
update-cookies
(Fn [Response, (Ref (Fn [(Array Cookie)] (Array Cookie) a) b)] Response)
updates the cookies property of a Response using a function f.
update-headers
(Fn [Response, (Ref (Fn [(Map String (Array String))] (Map String (Array String)) a) b)] Response)
updates the headers property of a Response using a function f.
update-message
(Fn [Response, (Ref (Fn [String] String a) b)] Response)
updates the message property of a Response using a function f.
update-version
(Fn [Response, (Ref (Fn [String] String a) b)] Response)
updates the version property of a Response using a function f.
with-header
(Fn [Response, String, String] Response)
(with-header r k v)
adds a header to a response.
with-status
(Fn [Response, Int, String] Response)
(with-status r code msg)
sets the status code and message of a response.