Net.HTTP.Syntax

lib/Net/HTTP/Syntax.gob

The small grammars of RFC 9110 that header values are written in: tokens, quoted strings, parameters and comma-separated lists.

Every function works on bytes, not characters. The grammar is defined over octets, and each byte it gives a meaning to is ASCII, so a cut made next to one of those bytes is never inside a UTF-8 sequence.

A value is read once, whole, and is a few hundred bytes at most, so the grammars are written as a cursor over the bytes with ? on Option rather than with combinators: each rule is a few lines that read as the RFC's ABNF does. URL shares the byte helpers.

fun isTokenByte : fun(Int) -> Bool
fun isFieldVChar : fun(Int) -> Bool

field-vchar = VCHAR / obs-text. Bytes from 0x80 are obs-text, which the RFC tells a recipient to treat as opaque; they arrive here only as parts of UTF-8 sequences, since a String holds nothing else.

fun isQuotable : fun(Int) -> Bool

What a quoted string can carry, unescaped or after a backslash: HTAB, SP, VCHAR and obs-text. Every other control byte, CR, LF and NUL among them, has no spelling inside quotes.

fun byteAt : fun(String, Int) -> Int
fun byteFind : fun(String, String, Int) -> Int

Returns the offset of the first needle at or after from, or the length of s if there is none, so that the result is always a place to cut.

fun isToken : fun(String) -> Bool
fun isFieldValue : fun(String) -> Bool

field-value = *field-content, which neither starts nor ends with whitespace: SP and HTAB are allowed only between visible bytes.

fun canQuote : fun(String) -> Bool
fun quote : fun(String) -> String

Returns s as a quoted-string, with " and \ escaped. s must pass canQuote; the callers check it before they build the value that is written.

fun asciiLower : fun(String) -> String

Returns s with ASCII letters lowercased and every other byte unchanged.

fun sliceBytes : fun(String, Int, Int) -> String

Returns bytes start up to stop of s. Both must be next to an ASCII byte or at an end, which every cut the grammars make is.

type Cursor = Cursor { text : String, var at : Int }

A position in a value being read.

fun cursor : fun(String) -> Cursor
fun atEnd : fun(Cursor) -> Bool
fun peek : fun(Cursor) -> Int

Returns the byte at the cursor, or -1 at the end.

fun advance : fun(Cursor) -> Unit
fun skipSpace : fun(Cursor) -> Unit

OWS = *( SP / HTAB )

fun expect : fun(Cursor, Int) -> Option Unit

Consumes b if it is next.

fun token : fun(Cursor) -> Option String

token = 1*tchar

fun quotedString : fun(Cursor) -> Option String

quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE, returned with its quotes removed and its escapes undone.

fun tokenOrQuoted : fun(Cursor) -> Option String
fun rest : fun(Cursor) -> String

Returns the bytes from the cursor to the end, and moves it there.

fun listItems : fun(String) -> Option (Vec String)

Returns the elements of a comma-separated list (RFC 9110 ยง5.6.1), with the whitespace around each removed. Commas inside a quoted string do not separate. Empty elements are dropped, as a recipient must accept a, , b as a, b. Returns None if a quoted string is not closed.

fun escaped : fun(String) -> String

Returns s with every byte outside printable ASCII written as \xNN, for a diagnostic that quotes text it refused: a refused value may hold the very CR and LF that make it dangerous, and a message must not carry them on.