Net.HTTP.Stream

lib/Net/HTTP/Stream.gob

A body that a program writes as it goes, for a server's response or a client's request: its length, if it knows it, and a function that writes the bytes into a Sink.

Stream(LengthUnknown, fun(sink) {
    for event in feed() {
        IO.writeText(sink, "data: " + event + "\n\n")?
        flush(sink)?
    }
    Right(Headers.empty)
})

The function runs after the head is sent. By the time it writes a byte, the peer has the status and the fields, so nothing it does can replace them. If it fails, or writes other than the length it declared, the message cannot be finished, and the connection under it is closed: the peer sees a message cut short, never a complete one with the wrong bytes.

Trailers. The function returns the fields to send after the body, such as a checksum or gRPC's status, or Headers.empty for none. HTTP/1 sends trailers only after a chunked body, so trailers from a LengthKnown stream, or from any stream sent to an HTTP/1.0 peer, are UnframedTrailers, and the message is left unfinished as after any other error.

Errors. A sink's writes fail with SomeError, and the function returns one, so that ? carries a failed write out of it, and another library's error enters with promote. The sink's own failures are HTTPErrors inside it, which cast recovers: Overrun, Truncated, UnframedTrailers, and Transport for the connection's own. A write after the message has ended, as a sink kept past its function's return would make, is an IOError of kind Closed.

type BodyLength = LengthKnown(Int) | LengthUnknown

How long a streamed body is: LengthKnown(n) bytes, or as long as it turns out to be.

type Stream = Stream(BodyLength, fun(Sink) -> Either SomeError Headers)

A body of the length given, written by the function, which is called at most once: never for a response to HEAD, and once the head is sent for any other message.

fun length : fun(Stream) -> BodyLength

Returns the length s declares.

type Sink

Where a Stream's function writes its body. It is a Write, and valid only while that function runs.

The message under it is reached through closures, so that a sink has one type whatever the connection is, and a function written once writes over TCP and TLS alike.

fun framing : fun(BodyLength, Framing) -> Framing

Returns the framing an HTTP/1 message gives a body of length l, with unknown for a body whose length is not known: Chunked, or for a response to an HTTP/1.0 request, which cannot read chunks, UntilClose. Panics on a negative length.

fun sink : [Write w] fun(BodyWriter w, fun(WriteError w) -> SomeError) -> Sink

Returns a sink that writes through w, an HTTP/1 body writer, with the connection's errors made SomeErrors by lift.

fun relayed : fun(Incoming) -> Stream

Returns a stream that passes b on as it is read: the length its framing declared, if any, the bytes as they come, with no limit of their own, and then the trailers it arrived with. Its function reads b, so it can run once.

fun send : fun(Stream, Sink) -> Either SomeError Unit

Runs s's function on into, then ends the body with the trailers it returned, and closes the sink either way. Returns the function's error if it failed, and then the body is left unended; or the end's: Truncated if a declared length was not reached, UnframedTrailers for trailers the framing has no place for. After any of these, the message cannot be finished, and the connection must be closed.

instance Write Sink