Systems and protocols · Intermediate

HTTP/1.1: identifying message boundaries

Understand framing, persistent connections and ambiguous message lengths without equating a TCP read with an HTTP message.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Framing a byte stream
  2. Count octets in a concrete response
  3. Chunking and persistent connections
  4. Ambiguity is a security problem
  5. Ask Logeia about this articleCheck your understanding

Framing a byte stream

HTTP/1.1 places message structure on top of an ordered byte stream. A start line and header fields end at an empty line, after which a body may follow under the applicable framing rules. A TCP read can contain part of a header, several pieces of a body, or bytes belonging to a later message. It is not an HTTP message boundary. Correct parsing must distinguish the header terminator from the body length. This article focuses on the version’s wire syntax and connection behavior rather than redefining HTTP method semantics.

Count octets in a concrete response

Suppose a response declares Content-Length: 5 and carries hello. Receiving he in one read and llo in another still yields one five-octet body. If another response follows on the persistent connection, the parser must stop at the correct boundary instead of consuming its first bytes. Content length counts octets, not displayed characters; a five-character Unicode string can require more than five bytes. Method and status rules can also establish that a response has no body, so a parser cannot use one header field in isolation from the message context.

Chunking and persistent connections

Chunked transfer coding can delimit a body whose total size is not known when the headers are sent. Chunk sizes and the terminating chunk supply framing independently of TCP segmentation. Persistent connections allow reuse, but they do not permit arbitrary interleaving of response bytes. In particular, pipelined requests still impose response-order requirements. A close-delimited response uses connection termination as part of its boundary, affecting whether that connection can carry another message. Ask how the parser knows the body has ended before reasoning about the benefit of reusing the connection.

Ambiguity is a security problem

If a proxy and an origin disagree about the length of the same request, later bytes can be interpreted as a different message. Conflicting transfer-length indicators therefore need the specification’s careful error and forwarding rules, not an ad hoc choice of whichever length is convenient. A short teaching parser is not a production implementation. HTTP/1.1 framing also does not itself encrypt traffic; HTTPS combines HTTP with a secure transport layer. Keep parsing consistency, connection management and confidentiality separate when reviewing either an implementation or a packet capture.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 10

Choose a learning path