Net.HTTP.ServerResponse

lib/Net/HTTP/ServerResponse.gob

What a server sends in response to a request: a status, header fields, and a body, which Net.HTTP.Server's respond writes.

(GET, "/health") -> respond(r, body(OK, MediaType.plainText, "ok\n"))
(GET, "/events") -> respond(r, stream(OK, MediaType.eventStream, LengthUnknown, feed))

Abstract, where ClientRequest is a record. A rule that fits inside one field's type is kept by that type, so ClientRequest b exports its constructor: a Method cannot put a body on a GET, and Headers cannot hold a field the library writes. A response's rule spans two fields: whether it may have a body depends on its status, since a 1xx, 204 or 304 response has none. No field's type can enforce that, so a response is made only by the functions here, which check it, and a record would let a program build one they refuse.

The library never picks a media type: a response has the Content-Type its constructor was given, or none. Header fields are added with withHeaders(response, field(h, v)).

A stream's function runs while respond writes, after the head is on the wire, so a failure in it can no longer change the status: the connection is closed, and the client sees a body cut short. Its function returns the trailers to send after the body, Headers.empty for none.

type ServerResponse
type Outgoing = Empty | Whole(Buffer) | Streamed(Stream) | Switched(Token, fun(Duplex) -> Either SomeError Unit)

A response's body, as the server writes it.

  • Empty: no bytes. A 204 or 304 is framed as having no body at all; anything else declares a length of 0.
  • Whole(b): the bytes of b.
  • Streamed(s): written by s's function.
  • Switched(protocol, f): a 101 to protocol, then f with the connection.
fun status : fun(ServerResponse) -> Net.HTTP.Status.Status

Returns the response's status.

fun outgoing : fun(ServerResponse) -> Outgoing

Returns the response's body, for the server that writes it.

class Content c

What body sends: text as its UTF-8 bytes, or the bytes of a Buffer's window.

fun contentBytes : [Content c] fun(c) -> Buffer
fun body : [Content c] fun(Net.HTTP.Status.Status, MediaType, c) -> ServerResponse

Returns a response of s with content as its body, of type t.

fun stream : fun(Net.HTTP.Status.Status, MediaType, BodyLength, fun(Sink) -> Either SomeError Headers) -> ServerResponse

Returns a response of s, of type t, whose body write writes, length bytes of it, once the head is sent, and whose trailers it returns. For a response to HEAD, write is never called.

fun statusOnly : fun(Net.HTTP.Status.Status) -> ServerResponse

Returns a response of s with no body: framed as having none for a 204 or 304, and with a length of 0 otherwise. Panics on a 1xx, which inform or switch sends.

fun noContent : fun() -> ServerResponse

Returns a 204 response, which has no body.

fun notModified : fun() -> ServerResponse

Returns a 304 response, which has no body. Add the fields a 200 would have had, ETag among them, with withHeaders.

fun switch : fun(Token, Headers, fun(Duplex) -> Either SomeError Unit) -> ServerResponse

Returns a response that switches the connection to protocol: a 101 with headers, and Upgrade and Connection written by the server. Once it is sent, run has the connection as a Duplex until it returns, and then the connection is closed.

A request whose Upgrade field does not offer protocol, or that does not let its connection be switched (an HTTP/1.0 request, or one whose body could not be read to its end), gets a 500 instead, and the server reports it to onError: a 101 that the client did not ask for is a mistake in the handler.

fun relay : fun(Response Incoming) -> ServerResponse

Returns resp, a response a client received, to be sent on as it is read: its status, its fields, its body, read as it is written and with no limit of its own, and the trailers that followed it. A 204 or 304 is sent without a body. A failure to read the body once the head is sent closes the connection, as a stream's does.

The client took out the fields that frame the message and those Connection names, so resp.headers holds only what a proxy passes on. Panics on a 1xx: an interim response is relayed with the server's inform, and a 101 is a switch.

class ToResponse r

What respond takes: anything that becomes a response.

  • ServerResponse: itself.
  • Status: statusOnly of it.
  • Either l r, when both sides are a ToResponse: the side it holds. So a handler computes an Either APIError ServerResponse, uses ? on its way, and its error type's own ToResponse instance chooses the response each error becomes.
fun toResponse : [ToResponse r] fun(r) -> ServerResponse
instance HasHeaders ServerResponse
instance Content String
instance Content Buffer

The response keeps the buffer, which must not be changed until it is sent.

instance ToResponse ServerResponse
instance ToResponse Net.HTTP.Status.Status
instance ToResponse (Either l r) : ToResponse l, ToResponse r