Net.HTTP.H1.Head

lib/Net/HTTP/H1/Head.gob

Stage 2 of the HTTP/1 head parser: one pass over a complete head, by hand, with the byte classes of Net.HTTP.H1.Classes. Net.HTTP.H1.Input is stage 1, which finds where the head ends.

Strict, with no leniency to turn on. Each rule below is one an HTTP implementation's CVE showed was needed:

Checks run in a fixed order, so a head that breaks two rules is always refused for the same one:

  1. The grammar, left to right. Any byte it cannot take is Malformed, except that a head with more than fields field lines is HeadTooLarge at the first line past the limit.
  2. Then, on a head the grammar accepts: a first line over lineBytes (LineTooLong); a version other than 1.0 and 1.1 (UnsupportedVersion); a target whose form the method does not allow (Malformed); the fields, one at a time, where a malformed or repeated Content-Length, a malformed Transfer-Encoding or one that continues after chunked, or a malformed Connection is Malformed; then a Host missing from 1.1, repeated or malformed (Malformed), a Transfer-Encoding that does not end in chunked (Malformed), a coding other than chunked (UnknownCoding), Content-Length beside Transfer-Encoding (Malformed), a Host that disagrees with the target (Malformed), and last an Expect other than 100-continue (UnmetExpectation).

Nothing is copied but names and the target. Header values are views into the head's bytes, which stage 1 never writes again. A name is copied because it is stored lowercase.

The reserved fields are taken out. Content-Length, Transfer-Encoding, Host, Connection, Expect, Upgrade and TE become the head's framing, target, persistence, expectation, upgrade and trailer acceptance; Keep-Alive, Trailer and Proxy-Connection are dropped. None of them reaches headers, which holds only what a program could have written itself.

So are the fields Connection names. Connection: X-Foo says that X-Foo is for this hop only (RFC 9110 ยง7.6.1), so no X-Foo field reaches headers either. They are removed here, on receipt, before any program sees the message, rather than when a proxy sends it on: removed on sending, a peer's Connection could name a field the program added itself, and strip it. The cost is that a program that is not a proxy cannot read such a field either. A name in Connection that is a reserved field changes nothing, since the framing was read before anything was removed.

type Framing = ContentLength(Int) | Chunked | UntilClose | NoBody

How a message's body is delimited on the wire.

  • ContentLength(n): exactly n bytes follow the head.
  • Chunked: chunks follow, the last one empty, then trailers.
  • UntilClose: the body is everything until the connection closes. Only a response can be framed this way.
  • NoBody: nothing follows the head.
type Persistence = KeepAlive | Close

Whether the connection may carry another message after this one.

type Version = HTTP10 | HTTP11
type RequestHead = RequestHead { method : Method Unit, target : RequestTarget, headers : Headers, version : Version, framing : Framing, persistence : Persistence, expectContinue : Bool, upgrade : Array HeaderValue, trailers : Bool }

A request head, read and checked.

method carries () where the method has a body, for the server to replace with the body itself (map). An extension method carries Some(()) when the framing says a body follows. A method without a body slot, such as GET, may still have a body by its framing; it is never ignored, but drained or refused as an unread body is.

target.authority is the request's Host, or the authority of an absolute-form or CONNECT target, which Host must agree with. For CONNECT, target.path is empty.

persistence is Close for HTTP/1.0, and for Connection: close. expectContinue is whether an HTTP/1.1 client waits for 100 Continue before it sends the body. upgrade is each Upgrade line's value, for the server to decide on. trailers is whether TE lists trailers: the client reads trailers after a chunked response.

type ResponseHead = ResponseHead { status : Net.HTTP.Status.Status, headers : Headers, version : Version, framing : Framing, persistence : Persistence, upgrade : Array HeaderValue }

A response head, read and checked, with the framing its request implies: no body for a response to HEAD, for 1xx, 204 and 304, and for a 2xx to CONNECT, whose connection becomes a tunnel; otherwise chunked, a length, or until the connection closes.

fun parseRequest : fun(Array Byte, Int, Int, Limits) -> Either Refusal RequestHead

Returns the request head s[at..to), which ends in its empty line, or why it is refused, checking in the order the module comment gives.

fun protocols : fun(Array HeaderValue) -> Array Token

Returns the protocols Upgrade lines name, in order. An element that is not a token, such as one with a version (HTTP/2.0), is left out.

fun control : fun(RequestHead) -> Control

Returns what h's reserved fields said, for the request a server hands on.

fun parseResponse : fun(Array Byte, Int, Int, Limits, Method b) -> Either Refusal ResponseHead

Returns the response head s[at..to) to a request with method request, or why it is refused: the request head's checks, in the same order, where they apply to a response.

fun fieldLine : fun(Array Byte, Int, Int, Vec Int) -> Int

Reads one field-line CRLF at p, pushing its four offsets into fields, and returns where it ends, or -1 if it is malformed.

field-line = field-name ":" OWS field-value OWS

fun isReservedName : fun(Array Byte, Int, Int) -> Bool

Whether s[from..to) is a reserved field name, in any case: one a trailer may not hold either.

instance Show Version