Speed

A Recording Is Bytes

A recording is a list of segments, each with a connection, a direction and bytes; one connection can carry several exchanges.

The question

When a client and a server talk over HTTP, what can be written down afterwards, and in what shape? This book reads one real session. Nothing runs live in the page: the bytes were captured once, on 2026-11-21, between the command-line client curl 8.5.0 and a scripted server, lmmock 0.1.0, on one machine over the loopback interface. The host name www.example.test was set with --connect-to and never looked up. What does such a recording hold?

Work it by hand

A recording is a list of segments. Each segment is one line of the form connection|direction|bytes in hex. The direction c2s is client to server, so the segment is a request; s2c is server to client, a response. The first segment of connection 1 starts 1|c2s|474554202f68656c6c6f20. The bytes 47 45 54 are the letters G, E, T; 20 is a space; 2f 68 65 6c 6c 6f is /hello.

Count the whole request by hand. The line GET /hello HTTP/1.1 is 19 characters and ends with the two bytes 0d 0a (carriage return, line feed), so 21 bytes. Host: www.example.test is 22 plus 2, User-Agent: curl/8.5.0 is 22 plus 2, Accept: */* is 11 plus 2, and the empty line is 0d 0a again. The sum is 21 + 24 + 24 + 13 + 2 = 84 bytes, 168 hex digits.

Run it

The proxy that wrote the segments down is a witness: it sat between the two programs, passed every byte on unchanged and noted what it received (the file recording.txt of the fixture says that nothing was edited; the page does not run that check). The recording has 8 connections, 9 requests and 18 segments, because connection 2 carried two requests.

A message-flow diagram with four arrows on one connection: the client sends GET /old HTTP/1.1, 82 bytes; the server answers HTTP/1.1 301 Moved Permanently, 108 bytes; the client sends GET /hello HTTP/1.1, 84 bytes; the server answers HTTP/1.1 200 OK, 106 bytes. The answer is 214, the bytes the server sent.
Watch the steps (recorded video)
let lines = str_split(segments, "\n");
let m = str_list(); let sent = 0; let got = 0;
for (k in range(0, lines.len())) {
  let p = str_split(lines.at(k), "|");
  let hex = p.at(2);
  let end = 0;
  while (hex_slice(hex, end, 2) != "0d0a") { end = end + 1; }
  let first = ssl.unhex(hex_slice(hex, 0, end));
  let route = "0>1";
  if (p.at(1) == "s2c") { route = "1>0"; got = got + hex_len(hex); }
  else { sent = sent + hex_len(hex); }
  m.add(route + "|" + first + "|" + str(hex_len(hex)) + " bytes");
}
let answer = got;
lmcode_return_put("messages", m);
lmcode_return_put("client_bytes", sent);
lmcode_return_put("segments", lines.len());
{
  "answer": 214,
  "messages": [
    "0>1|GET /old HTTP/1.1|82 bytes",
    "1>0|HTTP/1.1 301 Moved Permanently|108 bytes",
    "0>1|GET /hello HTTP/1.1|84 bytes",
    "1>0|HTTP/1.1 200 OK|106 bytes"
  ],
  "client_bytes": 166,
  "segments": 4
}

The demo reads connection 2, the -L follow of a redirect. For each segment it cuts the bytes at the first 0d 0a (lines 6 to 8), keeps that first line as the label and chooses the arrow by the direction (lines 9 to 11). Line 12 adds one message for the picture. The answer, set on line 14, is the number of bytes the server sent.

You can change segments to other lines of the same form. A line with fewer than three parts stops the run with "StrList.at: index out of range: 2"; bytes that are not hex digits with "Ssl.unhex: invalid hex digit"; bytes without a 0d 0a, such as 4142, with "bytes 1 to 2 are past the end of the data".

A last odd digit is ignored. Capital hex digits (0D0A) are not found as a line end and stop the run with the same past-the-end message (measured with 41420D0A: "bytes 3 to 4 are past the end of the data"). The picture draws the first ten messages and, with more than eight, leaves the byte counts out.

Your turn

The program below should say how many requests each connection of a recording carries. Several candidate programs are offered. Read them, run them on recordings of your own, and decide which of them is right for every recording. Some cases are recorded connections and some are constructed from recorded messages; the page says which when it draws them.

A recording text of six segment lines for connections 9 and 7 (each line: connection, direction, bytes in hex), and the list of numbers that the shown program returns, with the number of connections it found.
let total = int_list(); let seen = int_list();
for (i in range(0, 10)) { total.add(0); seen.add(0); }
let lines = str_split(segments, "\n");
for (k in range(0, lines.len())) {
  let p = str_split(lines.at(k), "|");
  let c = str_to_int(p.at(0));
  seen.set(c, 1);
  if (p.at(1) == "c2s") { total.set(c, total.at(c) + 1); }
}
let answer = int_list();
for (i in range(0, 10)) { if (seen.at(i) == 1) { answer.add(total.at(i)); } }
lmcode_return_put("connections", answer.len());
{
  "answer": [
    1,
    2
  ],
  "connections": 2
}

Return the number of requests on each connection, in the order in which the connections first appear in the text, as a list of integers. The text segments has one segment per line, written connection|direction|bytes in hex; the direction c2s is a segment sent by the client (a request), s2c one sent by the server (a response); connection numbers are from 1 to 9. A connection that has only responses has 0 requests. Check runs your program on this recording and on five others.

Read the result carefully

A recording is not the exchange. It is what one witness wrote down, in the order it saw the pieces, and here every piece is one whole message only because each read of the proxy returned one. A real capture may cut a message into several segments or join two, and then counting segments says nothing about requests.

This server is a script with three routes, not a model of any real site, and the client is curl, not any other tool. Connection numbers are the order in which the proxy accepted them. The picture of a constructed connection is made from recorded messages: no such connection happened. For one request and its answer, see the request line and the Host field.

Recap

  • A segment is a connection number, a direction and bytes.
  • A request is what the client sent, a response what the server sent.
  • Each line of a message ends with 0d 0a; the empty line ends the head.
  • One connection can carry several exchanges: connection 2 carried two.