Net.HTTP.H1.Input

lib/Net/HTTP/H1/Input.gob

The reading side of an HTTP/1 connection: the bytes read from a stream and not yet consumed, and stage 1 of the head parser, which finds where a head ends.

Stage 1 (readHead) scans each newly read window for the end of the head, remembering how far it has scanned, so a head that arrives a byte at a time costs one pass, not one per read. It refuses a head over the byte limit, and a first line over the line limit, as soon as the bytes show it, without waiting for the rest. A head ends at the first LF that follows an LF, alone or with a CR between; stage 2 then refuses the bare LF, so a malformed head is refused at once rather than waited on until it reaches the limit.

The bytes are never overwritten while a value views them. A head is handed on as a span of the array it arrived in, and stage 2's header values view that array. So once a head has been handed out, the array is pinned: nothing rewinds or compacts it, and when it is full the window moves to a new array. An array nobody views is reused in place. This is what lets values be views without copies, and it costs one array per buffer of heads, not one per head.

Body bytes go through the same window, so that what one message read ahead of its end is where the next message starts. A body read with nothing held goes from the stream straight into the reader's buffer.

type CodecError e = Either HTTPError e

What reading through the codec fails with: the codec's own error on the left, or the stream's on the right.

type Input r
type HeadBytes = HeadBytes { bytes : Array Byte, from : Int, to : Int }

A head stage 1 found: bytes[from..to), ending in its empty line. The array is never written again. It has no Eq, since equal heads may lie at different places in different arrays. A head's raw bytes may carry credentials, so !Debug keeps them out of a derived Debug.

fun input : fun(r, Limits) -> Input r

Returns an input over source, with room for a head of the limit's size.

fun source : fun(Input r) -> r
fun held : fun(Input r) -> Int

Returns how many bytes are held, read and not yet consumed.

fun readHead : [Read r] fun(Input r, Limits) -> Either (Either HTTPError (ReadError r)) (Option HeadBytes)

Returns the next head, None if the stream ended cleanly before its first byte, or why there is none: HeadTooLarge over headBytes, LineTooLong for a first line over lineBytes, Truncated if the stream ended inside it, or the stream's own error.

fun pin : fun(Input r) -> Unit

Marks the window's array as viewed by a value, so that it is never written again.

fun take : [Read r] fun(Input r, Buffer, Int) -> Either (Either HTTPError (ReadError r)) Int

Moves up to max body bytes into into, at least 1 unless the stream has ended: from what is held if anything is, or else from the stream straight into into. Returns how many moved, 0 at the end of the stream.

fun skip : fun(Input r, Int) -> Unit

Drops n held bytes, which the caller has read.

fun line : [Read r] fun(Input r, Int) -> Either (Either HTTPError (ReadError r)) Net.HTTP.H1.Input.Line

Returns the window's array and the offsets of the next line, from to the LF that ends it, reading more of the stream until a whole line of at most max bytes, CRLF included, is held. TooLong if the line is longer, or EndOfStream if the stream ends first. The line stays held; the caller drops it with skip.

type Line = Whole(Array Byte, Int, Int) | TooLong | EndOfStream

What line found: a line bytes[from..lf], a line over the limit, or the end of the stream before a whole line. It has no Eq, for the reason HeadBytes has none. !Debug, as HeadBytes is: a line is raw bytes of a head.