Net.HTTP.ServerResponse
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.
bodyandstreamhave a body, of the media type the caller names, and panic on a status that cannot: a status written in a program that cannot carry its body is a mistake in the program, as a bad literal is.statusOnly(s)isswith no body.noContent()is a 204, andnotModified()a 304.switchis a 101 that hands the connection to another protocol.relay(resp)is a response a client received, passed on as it is read: a proxy's.- A 1xx other than 101 is not a response here. The server's
informsends one, before the final response.
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.
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 ofb.Streamed(s): written bys's function.Switched(protocol, f): a 101 toprotocol, thenfwith the connection.
Returns the response's status.
Returns the response's body, for the server that writes it.
What body sends: text as its UTF-8 bytes, or the bytes of a
Buffer's window.
Returns a response of s with content as its body, of type t.
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.
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.
Returns a 204 response, which has no body.
Returns a 304 response, which has no body. Add the fields a 200 would have
had, ETag among them, with withHeaders.
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.
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.
What respond takes: anything that becomes a response.
ServerResponse: itself.Status:statusOnlyof it.Either l r, when both sides are aToResponse: the side it holds. So a handler computes anEither APIError ServerResponse, uses?on its way, and its error type's ownToResponseinstance chooses the response each error becomes.
The response keeps the buffer, which must not be changed until it is sent.