/Learn
08 · Application · TCP 80

HTTP

Hypertext Transfer Protocol

The request and response language of the web.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a HTTP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System designProtocols

Study with AI

Part 1 of 5

Basics

What the protocol is for, and the ideas it is built on.

1.1What it does

The protocol in plain words, before any bytes.

HTTP is a simple conversation: the client sends a request naming a method and a resource, and the server returns a status code, headers and usually a body. Every web page, API call and file download follows this pattern.

HTTP/1.1 is plain text. HTTP/2 sends the same ideas as binary frames multiplexed on one connection, and HTTP/3 runs them over QUIC. The meaning of methods, status codes and headers stays the same.

1.2Key ideas

The handful of ideas everything else follows from.

Methods

GET reads, POST creates or submits, PUT replaces, PATCH changes, DELETE removes. GET, PUT and DELETE are meant to be idempotent.

Status classes

1xx info, 2xx success, 3xx redirect, 4xx client mistake, 5xx server failure.

Caching

Cache-Control, ETag and Last-Modified let clients and CDNs reuse responses safely.

Statelessness

Each request stands alone. Cookies and tokens carry session state.

Evolution

HTTP/2 multiplexes requests and compresses headers. HTTP/3 removes TCP head-of-line blocking.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. What is the difference between 301 and 302?
  2. Why is Host required in HTTP/1.1?

The answers are in part 4.

Part 3 of 5

Detailed answer

Watch a real exchange step by step, then take each message apart field by field.

3.1Follow the packets

Step through the exchange between the two sides.

Client
Server
GET /index.html
200 OK + headers + body
GET /style.css (same connection)
304 Not Modified
Step 1 of 4

Method, path, version and headers such as Host.

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

HTTP/1.1 is plain text over a TCP connection. Every line ends in CR LF (0d 0a); a blank line ends the headers.

0000474554202f696e6465782e68746d6c20GET /index.html
0010485454502f312e310d0a486f73743a20HTTP/1.1··Host:
00206578616d706c652e636f6d0d0a557365example.com··Use
0030722d4167656e743a206375726c2f382er-Agent: curl/8.
0040352e300d0a4163636570743a202a2f2a5.0··Accept: */*
00500d0a0d0a····
FieldRequest line
Length26 bytes
TextGET /index.html HTTP/1.1␍␊

Method, target and version. GET asks for a representation and must not change state.

FieldOffsetBytesHex
Request line02647 45 54 20 2f 69 6e 64 …
Host261948 6f 73 74 3a 20 65 78 …
User-Agent452455 73 65 72 2d 41 67 65 …
Accept691341 63 63 65 70 74 3a 20 …
End of headers8220d 0a

3.3Decode it in code

A small decoder, tested against the exact bytes shown above.

Parsing an HTTP/1.1 request

HTTP/1.1 is plain text, and this parses the request on this page.

http.tsTypeScript
1export interface HttpRequest {2  method: string;3  target: string;4  version: string;5  headers: Map<string, string>;6  body: string;7}8 9/** Lines end in CRLF, and a blank line ends the headers. */10export function parseRequest(text: string): HttpRequest {11  const split = text.indexOf("\r\n\r\n");12  if (split === -1) throw new Error("incomplete headers");13  const [requestLine, ...lines] = text.slice(0, split).split("\r\n");14  const [method, target, version] = requestLine.split(" ");15  const headers = new Map<string, string>();16  for (const line of lines) {17    const colon = line.indexOf(":");18    headers.set(line.slice(0, colon).toLowerCase(), line.slice(colon + 1).trim()); // names ignore case19  }20  const length = Number(headers.get("content-length") ?? 0);21  return { method, target, version, headers, body: text.slice(split + 4, split + 4 + length) };22}23 24/** A client may reuse the connection unless someone said otherwise. */25export function keepAlive(r: HttpRequest): boolean {26  const connection = r.headers.get("connection")?.toLowerCase();27  return r.version === "HTTP/1.1" ? connection !== "close" : connection === "keep-alive";28}
28 linesUTF-8TypeScript
text.indexOf("\r\n\r\n")
A blank line ends the headers.
toLowerCase()
Header names ignore case, and a parser that forgets this breaks in strange ways.
content-length
Says exactly where the body ends, which allows several requests on one connection.

Tested by npm run test:modules.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

What is the difference between 301 and 302?
301 is a permanent redirect that clients may cache. 302 is temporary.
Why is Host required in HTTP/1.1?
One IP address can serve many sites, and Host tells the server which one is wanted.
Part 5 of 5

Extra depth

The mistakes people make with this protocol in real systems.

5.1Watch out for

  • Plain HTTP is readable and modifiable by anyone on the path. Use HTTPS.
  • A missing Content-Length or chunked encoding mistake leaves the client waiting forever.
  • A retried POST can create duplicates. Use idempotency keys.
← TLSHTTPS →
AI Engineer · Protocol Explorer