Net.HTTP.H1.Head
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:
- CRLF only, everywhere: a bare LF, or a CR without its LF, is refused.
- No whitespace between a field name and its colon, and none before the first field (obs-fold).
- Content-Length is
1*DIGIT, fits anInt, and appears at most once. - Transfer-Encoding is a list of tokens ending in exactly
chunked; nothing may followchunked. - Content-Length beside Transfer-Encoding is refused, not resolved.
- No control byte in a value: no NUL, no DEL, no CR or LF.
- A body is never ignored: a request's framing is what its fields say, whatever its method.
Checks run in a fixed order, so a head that breaks two rules is always refused for the same one:
- The grammar, left to right. Any byte it cannot take is
Malformed, except that a head with more thanfieldsfield lines isHeadTooLargeat the first line past the limit. - 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 afterchunked, or a malformed Connection isMalformed; then a Host missing from 1.1, repeated or malformed (Malformed), a Transfer-Encoding that does not end inchunked(Malformed), a coding other thanchunked(UnknownCoding), Content-Length beside Transfer-Encoding (Malformed), a Host that disagrees with the target (Malformed), and last anExpectother than100-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.
How a message's body is delimited on the wire.
ContentLength(n): exactlynbytes 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.
Whether the connection may carry another message after this one.
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.
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.
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.
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.
Returns what h's reserved fields said, for the request a server hands
on.
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.
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
Whether s[from..to) is a reserved field name, in any case: one a trailer
may not hold either.