Home / Docs / Specification / 31. HTTP servers
31.3 Reading a request
Release v0.4.0
The host refuses every request that two HTTP implementations might read differently, and never repairs one. Each refusal answers the status shown, with Connection: close, and closes the connection after it, so the host never looks for where a broken request ends.
- Request line. It MUST be
method SP target SP version CRLF, with single spaces and nothing before the method; an empty line before it is refused, not skipped. A line that is not three parts split by single spaces, a method that is not a token (RFC 9110 §5.6.2), or a target with a byte outside the printable ASCII range 0x21 to 0x7E is400. A version other thanHTTP/1.1andHTTP/1.0is505. A token that is not one ofGET,HEAD,POST,PUT,PATCH,DELETEandOPTIONS, in upper case, is501,CONNECTandTRACEincluded. A request line longer thanline, its CRLF included, is414, refused as soon as it cannot fit and before its bytes are kept. - Line ends. Every line MUST end with CRLF. A bare LF, or a CR not followed at once by LF, anywhere in the request line, the header section or the chunked framing, is
400. - Header lines.
- A header line is a name that is a token, followed at once by
:, then a value. A line that starts with a space or a tab (obsolete folding) is400; whitespace before the colon is400. - A value is trimmed of spaces and tabs at both ends. A byte below 0x20 other than tab, or 0x7F, in a value is
400. A value that is not UTF-8 is400, and is never repaired. - A
Connectionheader is a list of tokens separated by commas, with optional spaces and tabs around each. Every token, compared in any letter case, MUST beclose,keep-alive, or a header that belongs to the connection:connection,keep-alive,proxy-connection,te,trailer,transfer-encoding,upgradeandcontent-length. Any other token, or an empty element, is400, so a peer can never make the host remove a header that a front proxy added, orhost. - A header name that differs from
host,content-encoding,expector a header of rule 3.3 only by_in place of-, such asTransfer_Encoding, is400: a proxy that maps one to the other would read a framing or a host that the server does not. - The header section's lines, its trailers' included, count against
headers, and its bytes, each line with its CRLF and the empty line that ends it included, againstheader. Over either,431, refused before the bytes over the limit are kept.
- A header line is a name that is a token, followed at once by
- Framing, the defense against smuggling. In this order:
Content-LengthandTransfer-Encodingtogether:400.Transfer-Encodingin anHTTP/1.0request:400.- Two
Transfer-Encodingheaders:400. - A
Transfer-Encodingwhose whole value is notchunked, in any letter case:400when it is malformed (empty, an empty element, an element that is not a bare token, parameters included) or nameschunkedtwice; else501, since it names a coding the host does not implement. - Two
Content-Lengthheaders, even with equal values:400. - A
Content-Lengthvalue that is not 1 to 19 ASCII digits alone, with no sign, whitespace or list:400. A value abovebody:413, before any byte of the body is read. - Neither header: an empty body.
- Chunked bodies. Each chunk size MUST be 1 to 16 hex digits alone, with no whitespace, sign or extension; anything else is
400. This departs from RFC 9112 §7.1.1, which asks a recipient to ignore unknown chunk extensions, because lenient reading there has been a route for smuggling. The line after a chunk's data MUST be empty. A chunk whose size would take the body pastbodyis413at its size line, before any of its data is kept. Trailers are header lines of rule 3, read and dropped; their lines and bytes count against what the header section left ofheadersandheader, not a fresh allowance.
Host. A request MUST carry exactly oneHost,HTTP/1.0requests included, else400. Its value is a host and an optional:with a port of 1 to 5 digits, at most 65535. The host is a registered name of letters, digits,-,_and., with no empty label; an IPv4 address in dotted-quad form; or an IPv6 address in brackets, without a zone. A registered name whose last label is all digits, or starts with0x, and which is not an IPv4 address, such as127.1, is400, since resolvers read such names as addresses in different ways. Anything else, such as an empty value,a/b@c, or an empty port, is400. One trailing.of a registered name is removed, and the name is compared in ASCII lower case; the program sees the value as sent. Which names a grant accepts (421) is the grant's (§2 rule 5).- Encodings and expectations. A
Content-Encodingother thanidentity, in any letter case, is415. In anHTTP/1.1request,Expect: 100-continue, in any letter case, asks the host to send100 Continueonce it admits a body; any other expectation is417. In anHTTP/1.0request,Expectis ignored. - Order. The request line is checked when it ends. The whole head is checked when its empty line arrives, before any byte of the body is read, in this order: the framing of rule 4,
Connection,Host, the target of §4,Content-Encoding,Expect. - The body is read to the end its framing gives, and no further: the bytes after it belong to the next request, which is read only after the reply to this one is written. A body that is not valid UTF-8 is
400. - What the program sees. The headers of rule 3.3 that belong to the connection are removed. The other names are in ASCII lower case, in the order received, with their trimmed values. The connection closes after the reply when
Connectionlistsclose, or when the request isHTTP/1.0. - Splits. Reading is incremental, and a request split at any byte boundary reads exactly as in one piece. Each byte is examined a bounded number of times, so reading is linear in the bytes read.