Speed

The Head Ends at the Empty Line

A message is a start line, field lines and an empty line, then the body; the first line of a response gives the status code.

The question

A response arrives as one run of bytes. Where does the part that describes it stop, and where does the content start? And what does the first line say? The recording of this book (made on 2026-11-21 with curl 8.5.0 and the scripted server lmmock 0.1.0, loopback only, host name www.example.test) holds nine such responses. Each one can be cut at the same place.

Work it by hand

RFC 9112, section 2.1, gives the shape of an HTTP message: a start line, then any number of field lines, then an empty line, then an optional body, and every line ends with CRLF, the bytes 0d 0a. For a response, section 4 says the first line is HTTP-version SP status-code SP [ reason-phrase ].

Cut a constructed response by hand: HTTP/1.1 200 OK, CRLF, Content-Length: 3, CRLF, the empty line (just CRLF), then abc. The status line has 15 characters and CRLF, so 17 bytes; the field has 17 characters and CRLF, 19 bytes; the empty line is 2 bytes: the head is 17 + 19 + 2 = 38 bytes. The body is 3 bytes, the message 41. The status code is the three characters after the first space: 200. There is 1 field line; the empty line is not one.

Run it

The demo cuts the 404 response of connection 7 where the bytes 0d 0a 0d 0a first appear.

The 404 response of connection 7: the start line HTTP/1.1 404 Not Found, 4 field lines, a head of 157 bytes up to and including the empty line, a body of 19 bytes shown as hex starting 34 30 34 20, and 176 bytes in all. The answer is 157.
Watch the steps (recorded video)
let text = ssl.unhex(response);
let cut = str_find(text, "\r\n\r\n");
if (cut < 0) { out.fail("no empty line in the message"); }
let head = str_split(str_substr(text, 0, cut), "\r\n");
let body = str_substr(text, cut + 4, str_len(text) - cut - 4);
let answer = cut + 4;
lmcode_return_put("start_line", head.at(0));
lmcode_return_put("field_lines", head.len() - 1);
lmcode_return_put("body_bytes", str_len(body));
lmcode_return_put("body_hex", str_hex_encode(body));
lmcode_return_put("total_bytes", str_len(text));
{
  "answer": 157,
  "start_line": "HTTP/1.1 404 Not Found",
  "field_lines": 4,
  "body_bytes": 19,
  "body_hex": "3430342070616765206e6f7420666f756e640a",
  "total_bytes": 176
}

Line 1 turns the hex input into bytes and line 2 finds the first empty line: the end of the last field line and the empty line make the four bytes. Lines 4 and 5 split the text into head lines and body. The answer, set on line 6, is the head length counted through the empty line: 157 bytes.

The start line is HTTP/1.1 404 Not Found, there are 4 field lines, the body has 19 bytes (the text 404 page not found and a line feed), and the message has 176 bytes.

The line feed at the end of that body is part of the content, not a line end of the head. A bare line feed as a line end is not in this recording.

You can change response to another message. A message with no empty line stops the run on line 3 with "no empty line in the message". A head that is not text stops it at the return, measured as "lmcode return: invalid JSON value at /start_line: utf8" for the bytes ff 0d 0a 0d 0a. The page adds two lines above the program, so the run reports line numbers two higher.

Your turn

The program below should read a response and report its status code, the number of its field lines and the length of its body. Several candidate programs are offered. Read them, run them on messages of your own, and decide which of them is right for every response. Some cases are recorded responses and some are constructed; the picture says which one it draws.

A recorded response message of 106 bytes whose first bytes are 48 54 54 50 (HTTP), and the list of three numbers that the shown program returns.
let n = hex_len(message);
let found = 0 - 1;
for (i in range(0, n - 3)) {
  if (found < 0 && hex_slice(message, i, 4) == "0d0a0d0a") { found = i; }
}
let ends = 0;
for (i in range(0, found + 2)) {
  if (hex_slice(message, i, 2) == "0d0a") { ends = ends + 1; }
}
let code = str_to_int(ssl.unhex(hex_slice(message, 9, 3)));
let answer = int_list();
answer.add(code); answer.add(ends - 1); answer.add(n - found - 2);
{
  "answer": [
    200,
    3,
    7
  ]
}

Return [status code, number of field lines, body length in bytes] for the response in the hex input message, as a list of three integers. A response is a status line, field lines, an empty line and the body, and every line ends with the two bytes 0d 0a; the field lines are the lines between the status line and the empty line, and the empty line is not one of them. The message has a status line and an empty line. Check runs your program on this message and on five others.

Read the result carefully

RFC 9112, section 2.2, says that a recipient MAY recognize a single LF as a line terminator and ignore a preceding CR. This lesson does not: it looks for 0d 0a 0d 0a, the exercise grades only messages that use CRLF, and the recording has no bare-LF message. A message made with bare line feeds would need another search, and this page does not test one.

Counting is also not understanding. The program finds a head and a body; it does not check that the status code is a known one, that Content-Length says the truth, or that the body is the whole body. The next lesson reads the length field. The server is a script: its pages say what that script sent, not what other servers send. For the request line and the Host field, see Host Header Picks the Site.

Recap

  • A message is a start line, field lines, an empty line and an optional body.
  • Every line of the head ends with the bytes 0d 0a.
  • The status code is the three digits after the first space of the status line.
  • The empty line is not a field line, and the head includes it.