3. HTTP Protocol Deep Dive (HTTP/1.1, HTTP/2, HTTP/3)
HTTP Request Structure
An HTTP request is the message sent by a client to a server. It consists of a request line (Method, Path, Version), a set of headers, and an optional body. The Method defines the intent, such as GET to retrieve resources or POST to submit data.
The header section follows the request line, containing metadata like Host, User-Agent, and Content-Type. The body, separated by an empty line, carries the payload data for methods like POST or PUT.
import socket def craft_http_request(host, path): request = f'GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n' return request print(craft_http_request('example.com', '/'))
HTTP Response Structure
HTTP responses are the server's reply to a client request. The core of a response is the Status Line, which includes a numeric status code indicating success or failure (e.g., 200 OK, 404 Not Found, 500 Internal Server Error).
Status codes are categorized: 2xx (Success), 3xx (Redirection), 4xx (Client Errors), and 5xx (Server Errors). These help security analysts detect anomalous activity, such as brute force attempts (401 Unauthorized) or unauthorized scanning.
# Analyze headers and response structure using curl curl -I https://www.google.com
Evolution of HTTP (1.1, 2, 3)
HTTP/1.1 suffered from head-of-line blocking, where one slow request would stall subsequent ones. HTTP/2 introduced multiplexing, allowing multiple streams over a single TCP connection. HTTP/3 moves to QUIC, which operates over UDP to solve blocking issues at the transport layer.
| Protocol | Key Innovation |
|---|---|
| HTTP/1.1 | Persistent connections |
| HTTP/2 | Multiplexing |
| HTTP/3 | QUIC over UDP |
import time # Simulating basic serialization vs multiplexing overhead requests = [1, 2, 3] def simulate_v1(reqs): return sum(reqs) # Sequential blocking def simulate_v2(reqs): return max(reqs) # Multiplexed speed print(f'V1 time: {simulate_v1(requests)}') print(f'V2 time: {simulate_v2(requests)}')
Inspecting HTTP with cURL
cURL is the industry-standard tool for interacting with web servers. The -v (verbose) flag shows the entire handshake and exchange, while -I shows only headers.
Security professionals use these commands to verify security headers like Content-Security-Policy or Strict-Transport-Security during a network audit.
# Verbose request to see handshake and header exchange curl -v https://voidx-academy.internal
Knowledge Check
Ready to test your understanding of 3. HTTP Protocol Deep Dive (HTTP/1.1, HTTP/2, HTTP/3)?