HyperText Transfer Protocol (HTTP)
1. Overview and Design Principles
The HyperText Transfer Protocol (HTTP) (RFC 2616 / 7230) is the foundational request-response application protocol that powers the World Wide Web.
Key Architectural Traits:
- Client-Server Model: A client (web browser) initiates a connection to an HTTP server and sends a request; the server processes the request and returns a response.
- Underlying Transport: Operates over TCP Port 80 (Unencrypted HTTP) and TCP Port 443 (Encrypted HTTPS over TLS).
- Stateless Protocol: The server retains zero memory (state) of previous requests from the same client. Each transaction is processed completely independently.
- State Management via Cookies: To maintain shopping carts, login states, and user sessions, web servers use Cookies (
Set-Cookieresponse header andCookierequest header).
2. HTTP Message Formats
HTTP messages are human-readable, plain ASCII text structures:
3. HTTP Methods and Idempotency
- Safe Method: A method that does not alter server state (read-only:
GET,HEAD). - Idempotent Method: Executing the request multiple times produces the exact same server state as executing it once (
GET,PUT,DELETE).
4. HTTP Status Code Categories
Status codes are 3-digit integers returned by the server:
5. Evolution: HTTP/1.0 vs. HTTP/1.1 vs. HTTP/2 vs. HTTP/3
- HTTP/1.0 (Non-Persistent): Every resource requires opening and closing a separate TCP connection. If a webpage contains 50 images, 50 full TCP handshakes are executed!
- HTTP/1.1 (Persistent): Introduced
Connection: keep-alive(default), keeping the TCP connection open for subsequent requests. Supports pipelining, but suffers from Application Head-of-Line Blocking (request #2 cannot complete until request #1 finishes). - HTTP/2: Replaces text with a Binary Framing Layer. Fully multiplexes multiple request/response streams concurrently over a single TCP connection, adds HPACK header compression, and supports Server Push.
- HTTP/3: Built on QUIC over UDP (Port 443), eliminating TCP-level packet loss Head-of-Line blocking and enabling 0-RTT handshakes.
Frequently Asked Questions
QWhy is HTTP described as a stateless protocol? How is state maintained?
HTTP servers do not retain information regarding past client transactions. State is maintained using HTTP Cookies and session tokens: the server sets a cookie in the response header (Set-Cookie), which the client sends back in all future request headers (Cookie).
QCompare Non-Persistent and Persistent HTTP connections.
Non-persistent HTTP (HTTP/1.0) creates and tears down a separate TCP connection for every single referenced web object, consuming per object. Persistent HTTP (HTTP/1.1) reuses a single open TCP connection to fetch multiple objects sequentially.
QWhat is the difference between HTTP/1.1 and HTTP/2?
HTTP/1.1 uses text-based formatting and processes requests sequentially (suffering Head-of-Line blocking). HTTP/2 uses binary framing and multiplexes multiple independent request streams simultaneously over a single TCP connection without blocking.
QWhat is the difference between a safe and an idempotent HTTP method?
A safe method (GET, HEAD) does not change server state. An idempotent method (GET, PUT, DELETE) gives the same server state whether it is executed once or many times. POST is neither.