Home / Docs / Specification / 31. HTTP servers

31.5 Writing a reply

Release v0.4.0
  1. Checks. Before any byte is written, a reply MUST have a status from 200 to 599; no body on a 204 or a 304; header names that are tokens, in any case; header values with no CR, LF, NUL or other control character but tab, and no 0x7F; none of Connection, Content-Length, Date, Keep-Alive, Proxy-Connection, TE, Trailer, Transfer-Encoding and Upgrade, in any letter case; at most 100 headers, and at most 64 KiB of them, each counted as name: value with its CRLF. Otherwise reply fails with BadReply, nothing is sent, and the reply right stays held.
  2. The bytes. HTTP/1.1 <status> <reason>, with the reason phrase of RFC 9110 §15 (and of RFC 6585 for 429 and 431), or an empty one for any other status; then Date as an IMF-fixdate; Content-Length with the length of the body given, except on a 204 or a 304; the program's headers, in order, as given; content-type: text/plain; charset=utf-8 when the reply names no content type and carries a body; Connection: close when the connection closes after the reply; the empty line; and the body, unless the request was HEAD or the status is 204 or 304. A HEAD reply keeps the Content-Length of the body given and gets no default content type.
  3. The host's own answers (refusals, 421, 503, 500) have an empty body, Content-Length: 0, Retry-After: 1 where ADR 0123 rule 14 asks for it, and Connection: close when the connection closes, as it does after every refusal of §3 and §4. 100 Continue is the status line and the empty line alone.
  4. Cost. Checking and writing a reply of r bytes is O(r), into one buffer reserved once.