SyntaxPilot
Courses/Cybersecurity Fundamentals/Lesson 21

Cybersecurity Fundamentals · Lesson 21 of 90 · 14 min

HTTP from a security view

HTTP request and responsemethods (GET, POST, PUT, PATCH, DELETE, OPTIONS)safe and idempotent methodsstatus codesheaderscookiesstatelessnesssessionsURL anatomyquery stringsHTTP/1.1HTTP/2HTTP/3 and QUICforward proxy

These are the notes for Cybersecurity Fundamentals (defensive), episode 21. DNS found Ravi's shop and TCP connected to it. Today we invent, one problem at a time, what the browser and server actually say: the HTTP request and response, URL anatomy and query strings, the methods GET, POST, PUT, PATCH, DELETE and OPTIONS, safe and idempotent methods, status codes, headers, cookies, statelessness and sessions, HTTP/1.1, HTTP/2, HTTP/3 and QUIC, and the machines in the middle: forward proxy, reverse proxy, load balancer and CDN. Almost every web attack you will study later lives inside these messages. Every term is explained the first time it appears, and the Glossary at the end defines them all again.

Ravi's shop, the paths and every request are made up. The web server in the demos runs only on 127.0.0.1 (this machine) inside the demo itself; other addresses are private (RFC 1918) or documentation-only (RFC 5737), so no real website is contacted. Every code sample runs with Python 3.13 using only the standard library; outputs are real, copied from actual runs (dates and random session IDs differ on every run).

The problems you'll solve

Problem Idea it teaches
"cart please" gets a 400 HTTP, request line, method, path, version, headers, body, response, status line
Point at exactly the right thing URL, scheme, host, port, path, query string, fragment, URL encoding
A retry created two orders Methods GET, POST, PUT, PATCH, DELETE, OPTIONS; safe and idempotent
How does the server say what happened? Status codes 1xx–5xx, Location header
The server forgets you between requests Statelessness, cookies, Set-Cookie, sessions, session hijacking
One request at a time is slow HTTP/1.1, HTTP/2, HTTP/3, QUIC, head-of-line blocking
Machines in the middle Forward proxy, reverse proxy, load balancer, CDN, X-Forwarded-For

Problem 1: the request and the response

TCP is connected to the shop. The obvious way to ask for the cart: just send the words. Below, a real little web shop runs on port 8000 using Python's built-in http.server, and we talk to it over a raw TCP socket (episode 19).

Demo 1: "cart please" vs a real request

import socket, threading
from http.server import HTTPServer, BaseHTTPRequestHandler
class Shop(BaseHTTPRequestHandler):         # a tiny web shop
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.end_headers()
        self.wfile.write(b"your cart: 2 items")
    def log_message(self, *args): pass
srv = HTTPServer(("127.0.0.1", 8000), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

def ask(raw):                               # send raw bytes
    s = socket.create_connection(("127.0.0.1", 8000))
    s.sendall(raw)
    return s.makefile("rb").read().decode()  # read it all
print(ask(b"cart please\r\n\r\n").splitlines()[0])
print(ask(b"GET /cart HTTP/1.1\r\nHost: shop.example\r\n\r\n"))

Output:

HTTP/1.0 400 Bad HTTP/0.9 request type ('cart')
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.13.18
Date: Fri, 30 Oct 2026 23:31:41 GMT
Content-Type: text/plain

your cart: 2 items
  • "cart please" → 400 Bad Request. The server couldn't even guess what we meant (it tried to read it as an ancient HTTP/0.9 request).
  • The formatted request → 200 OK, a few lines of information, a blank line, and then the body your cart: 2 items. (This little server answers with version HTTP/1.0, which is fine.)

Two machines from different companies can only understand each other if they share one exact format. That format is HTTP, the Hypertext Transfer Protocol. Its meaning is defined in RFC 9110 (HTTP Semantics, June 2022).

The request

Think of a restaurant order slip: the first line says what to do, and to what ("BRING: table 7's bill"), then notes, one per line.

GET /cart HTTP/1.1            ← request line: method, path, version
Host: shop.example            ← headers: Name: value, one per line
User-Agent: Mozilla/5.0 ...
                              ← blank line = end of headers
(optional body)               ← data sent with POST/PUT/PATCH
  • Method: the action, e.g. GET (read).
  • Path: which thing on the server, e.g. /cart.
  • Version: HTTP/1.1.
  • Headers: extra details, Name: value. Host says which website you want, because one server can host many sites.
  • Body: data you send, such as a form or JSON.

The response

HTTP/1.1 200 OK               ← status line: version, status code, reason
Content-Type: text/plain      ← headers
                              ← blank line
your cart: 2 items            ← body: the page, image or data

Request, response: every click, every image and every API call is this pair.

Problem 2: URLs and query strings

A URL (Uniform Resource Locator) is the full address of one thing on the web:

https://shop.example.com:443/search?q=chai&page=2#reviews
└─┬─┘   └──────┬───────┘ └┬┘└──┬──┘ └─────┬──────┘ └──┬──┘
scheme        host      port path   query string  fragment
Part Meaning
Scheme How to talk: https (or http)
Host Which machine, found by DNS (episode 20)
Port Which door: 443 is the default for HTTPS (episode 19)
Path Which thing on that server
Query string Extra inputs after ?: key=value pairs joined by &
Fragment After #: a spot on the page; never sent to the server

Security rule: URLs are saved in browser history, server logs and proxy logs. Never put passwords, tokens or other secrets in a URL.

Demo 2: splitting a URL, and gluing user input into one

from urllib.parse import urlsplit, parse_qs, urlencode
u = urlsplit("https://shop.example.com:443"
             "/search?q=chai&page=2#reviews")
print("scheme:", u.scheme, "| host:", u.hostname,
      "| port:", u.port)
print("path:", u.path, "| query:", u.query,
      "| fragment:", u.fragment)
print("query as data:", parse_qs(u.query))

search = "chai & biscuits"           # what the user typed
bad = "/search?q=" + search          # the obvious way
print("bad: ", parse_qs(urlsplit(bad).query))
good = "/search?" + urlencode({"q": search})
print("good:", good, "->", parse_qs(urlsplit(good).query))

Output:

scheme: https | host: shop.example.com | port: 443
path: /search | query: q=chai&page=2 | fragment: reviews
query as data: {'q': ['chai'], 'page': ['2']}
bad:  {'q': ['chai ']}
good: /search?q=chai+%26+biscuits -> {'q': ['chai & biscuits']}

Gluing the user's text straight into the URL lost & biscuits: the server read the & as the start of a new parameter. The user's input changed the structure of the request. urlencode writes special characters as safe codes (& → %26, space → +): that is URL encoding (also called percent-encoding). User data leaking into structure is the root of injection attacks, later in this course.

Problem 3: methods, safe and idempotent

Ravi taps Buy on a train. The order reaches the shop, but the reply is lost in a tunnel; the app sees a timeout and retries. Is that safe? It depends on the method.

Demo 3: a retried POST vs a retried PUT

orders = {}                          # the shop's database
next_id = 1

def post_order(item):                # POST: create a NEW order
    global next_id
    orders[next_id] = item
    next_id += 1

def put_order(order_id, item):       # PUT: set THIS order
    orders[order_id] = item

post_order("phone")                  # reply lost on the way...
post_order("phone")                  # ...so the app retries
print("after POST + retry:", orders)

orders.clear()
put_order(1, "phone")                # same thing with PUT,
put_order(1, "phone")                # retried
print("after PUT + retry: ", orders)

Output:

after POST + retry: {1: 'phone', 2: 'phone'}
after PUT + retry:  {1: 'phone'}

POST means "create a new one" (the server picks the number), so the retry made a second order, and a second charge. PUT means "set order 1 to this", so doing it twice gave the same result as doing it once.

The methods

Method Meaning Safe? Idempotent?
GET Read something Yes Yes
HEAD Like GET, but headers only Yes Yes
OPTIONS Which methods may I use here? Yes Yes
POST Create / submit something No No
PUT Replace this thing completely No Yes
PATCH Change part of this thing No Not necessarily
DELETE Remove this thing No Yes
  • A safe method doesn't change anything on the server.
  • An idempotent method has the same effect whether it is sent once or ten times. RFC 9110: "PUT, DELETE, and safe request methods are idempotent." PATCH depends on the change: "set name to Ravi" is repeatable, "add 1 to the counter" is not.

Security rules:

  • A GET must never change anything (pay, delete, transfer). Browsers prefetch links, and attackers can make a victim's browser send GETs with a simple link or image.
  • Payment and order APIs add an idempotency key: a unique ID per action, so the server recognises a retried POST (Practice 3).

Problem 4: status codes

Demo 4: five requests, five codes

import threading, http.client
from http.server import HTTPServer, BaseHTTPRequestHandler
ROUTES = {"/cart": 200, "/old-cart": 301, "/admin": 403}
class Shop(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(ROUTES.get(self.path, 404))
        if self.path == "/old-cart":
            self.send_header("Location", "/cart")   # moved
        self.end_headers()
    def log_message(self, *args): pass
srv = HTTPServer(("127.0.0.1", 8000), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

for method, path in [("GET", "/cart"), ("GET", "/old-cart"),
                     ("GET", "/admin"), ("GET", "/nope"),
                     ("DELETE", "/cart")]:
    c = http.client.HTTPConnection("127.0.0.1", 8000)
    c.request(method, path); r = c.getresponse()
    print(f"{method:6} {path:9} -> {r.status} {r.reason}")

Output:

GET    /cart     -> 200 OK
GET    /old-cart -> 301 Moved Permanently
GET    /admin    -> 403 Forbidden
GET    /nope     -> 404 Not Found
DELETE /cart     -> 501 Unsupported method ('DELETE')
Class Meaning Common examples
1xx Informational: keep going 100 Continue
2xx Success 200 OK, 201 Created, 204 No Content
3xx Redirection: look elsewhere (the Location header says where) 301 Moved Permanently, 302 Found, 304 Not Modified
4xx Client error 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests
5xx Server error 500 Internal Server Error, 501 Not Implemented, 502 Bad Gateway, 503 Service Unavailable
  • 401 means "I don't know who you are: authenticate". 403 means "I know who you are, and the answer is no".
  • For defenders: sudden bursts of 401s (password guessing), 403s or 404s (probing for hidden paths) in logs are early warnings (see the mini project).

Problem 5: statelessness, cookies and sessions

RFC 9110: "HTTP is defined as a stateless protocol, meaning that each request message's semantics can be understood in isolation." Like a busy tea stall: "add sugar to my tea" – "which tea? who are you?"

So the stall hands you token 47 and writes "47 = one chai". On the web:

  1. The server sends a Set-Cookie header: Set-Cookie: session=7e14acf0f5c3eb1f.
  2. The browser stores this cookie (a small piece of data) and sends it back automatically on every later request: Cookie: session=7e14acf0f5c3eb1f.
  3. The data the server keeps for that token is a session; the token is the session ID.

Cookies are defined in RFC 6265 (April 2011), which describes them as "letting the servers maintain a stateful session over the mostly stateless HTTP protocol", and notes their security and privacy weaknesses.

Demo 5: a toy session system

import secrets
sessions = {}                    # server memory: id -> cart

def handle(req):                 # each request stands alone
    sid = req.get("Cookie", "").removeprefix("session=")
    if sid not in sessions:      # unknown visitor: new session
        sid = secrets.token_hex(8)
        sessions[sid] = []
    if req["path"] == "/add-chai":
        sessions[sid].append("chai")
    return {"Set-Cookie": f"session={sid}",
            "body": f"cart: {sessions[sid]}"}

r1 = handle({"path": "/add-chai"})
print("1st:", r1["body"], "| Set-Cookie:", r1["Set-Cookie"])
print("2nd, no cookie:   ", handle({"path": "/cart"})["body"])
mine = {"path": "/cart", "Cookie": r1["Set-Cookie"]}
print("3rd, with cookie: ", handle(mine)["body"])
print("thief with a copy:", handle(dict(mine))["body"])

Output:

1st: cart: ['chai'] | Set-Cookie: session=024aaaa0dd5b8b05
2nd, no cookie:    cart: []
3rd, with cookie:  cart: ['chai']
thief with a copy: cart: ['chai']

The thief holding a copy of the cookie gets the same cart: the server can't tell them apart. Stealing a session cookie is session hijacking. That's why session IDs must be long and random (secrets, not random), sent only over HTTPS, and hidden from page scripts; cookie attributes like Secure, HttpOnly and SameSite get a full episode (episode 28).

Headers you'll meet every day

Header Direction Meaning
Host Request Which website on this server
User-Agent Request Which browser or tool (as claimed by the client)
Content-Type Both What the body is: text/html, application/json
Content-Length Both Body size in bytes
Cookie / Set-Cookie Request / Response The session token
Location Response Where a redirect (3xx) or a new resource (201) is
Authorization Request Credentials or a token

Rule to remember: everything in a request (every header, parameter and cookie) is written by the client, and the client can write anything.

Problem 6: HTTP/1.1, HTTP/2, HTTP/3

Version How it works Supermarket picture
HTTP/1.1 Readable text; one request at a time per TCP connection One checkout lane, one customer at a time
HTTP/2 (RFC 9113, June 2022) A "binary framing layer over TCP": requests split into labelled frames, many requests interleaved on one TCP connection; HPACK header compression One shared belt with items from many carts, but a dropped item stops the belt
HTTP/3 (RFC 9114, June 2022) Runs on QUIC (RFC 9000, May 2021): "a UDP-based multiplexed and secure transport", independent streams, TLS 1.3 built into the handshake Many independent belts; a spill on one doesn't stop the others
  • Head-of-line blocking: in HTTP/2, TCP delivers bytes strictly in order, so one lost packet stalls every request sharing that connection. QUIC's independent streams avoid this.
  • For defenders: HTTP/3 is UDP on port 443, so firewalls, proxies and monitoring tools must understand it too, or traffic may bypass inspection or fail.

Problem 7: machines in the middle

Machine Acts for Everyday picture What it does
Forward proxy Clients An office's outgoing mail room Makes requests on behalf of users; companies use it to filter and log web traffic
Reverse proxy Servers A company's front reception Receives requests on behalf of servers; often handles TLS, caching and blocking bad requests
Load balancer Servers A bank's token counter Spreads requests across many servers; stops sending to servers that fail health checks
CDN (content delivery network) Servers An online store's local warehouses Keeps copies of content on servers close to users: faster, and absorbs attack traffic

The catch: behind a proxy, the server sees the proxy's address, not the client's. So proxies add a header, X-Forwarded-For, carrying the original client address. But a header is just text from the client.

Demo 6: trusting X-Forwarded-For from anyone

from collections import Counter
attempts = Counter()                # login tries per client IP

def client_ip(headers, peer):       # the obvious way:
    return headers.get("X-Forwarded-For", peer)  # trust it

def login(headers, peer):
    ip = client_ip(headers, peer)
    attempts[ip] += 1
    return "BLOCKED" if attempts[ip] > 3 else "try password"

for i in range(6):                  # one attacker, 6 tries,
    lie = {"X-Forwarded-For": f"10.9.9.{i}"}  # a new lie each
    print("try", i + 1, login(lie, peer="203.0.113.66"))

Output:

try 1 try password
try 2 try password
try 3 try password
try 4 try password
try 5 try password
try 6 try password

Six tries, never blocked: each lie looked like a new person.

Demo 7: trust it only from your own proxy

from collections import Counter
attempts = Counter()                # login tries per client IP
OUR_PROXY = "10.0.0.2"              # our own reverse proxy

def client_ip(headers, peer):       # the fix:
    if peer == OUR_PROXY:           # only our proxy may say
        return headers.get("X-Forwarded-For", peer)
    return peer                     # anyone else: real peer

def login(headers, peer):
    ip = client_ip(headers, peer)
    attempts[ip] += 1
    return "BLOCKED" if attempts[ip] > 3 else "try password"

for i in range(6):                  # same attacker, same lies
    lie = {"X-Forwarded-For": f"10.9.9.{i}"}
    print("try", i + 1, login(lie, peer="203.0.113.66"))

Output:

try 1 try password
try 2 try password
try 3 try password
try 4 BLOCKED
try 5 BLOCKED
try 6 BLOCKED

From anyone other than our own reverse proxy, we use the real address the TCP connection came from. The lies are ignored and the limit works: a trust boundary (episode 7).

The next problem

Every request we built today was plain text: paths, cookies, even passwords. Anyone on the same Wi-Fi, or anywhere on the path, can read them, or quietly change them. The fix is the padlock: TLS and HTTPS, next episode.

The whole chain

  1. "cart please" failed → request line, headers, body; status line
  2. Point exactly → URL, query string, URL encoding
  3. A retry made two orders → methods, safe, idempotent
  4. What happened? → status codes 1xx–5xx
  5. The server forgets → cookies and sessions
  6. Too slow → HTTP/2, HTTP/3 + QUIC
  7. Middle boxes → proxies, load balancers, CDNs; trust X-Forwarded-For only from your own proxy

...and you just invented HTTP.

Common mistakes (and what they print)

Mistake What you get Why Fix
Sending free text to a web server HTTP/1.0 400 Bad HTTP/0.9 request type ('cart') HTTP has an exact format A proper request line + headers, or an HTTP library
"/search?q=" + user_text {'q': ['chai ']}: input changed the request &, = and spaces have meaning in URLs urlencode() every user value
Secrets in the query string Tokens in history, server and proxy logs URLs are logged everywhere Send secrets in headers or the body, over HTTPS
Retrying a POST blindly {1: 'phone', 2: 'phone'}: charged twice POST is not idempotent PUT, or an idempotency key
A GET that changes data Triggered by prefetch or a hidden link GET must be safe POST/PUT/DELETE plus checks
Returning 401 for "logged in but not allowed" Confusing clients and logs 401 = unauthenticated, 403 = not permitted 403 for permission problems
Trusting X-Forwarded-For from anyone Rate limit never triggers Headers are typed by the client Trust it only from your own proxy
Unsupported method 501 Unsupported method ('DELETE') The server doesn't implement it Implement it, or return 405 with Allow

Mini project: Ravi's access-log triage

Web servers write an access log: one line per request. This tool groups a made-up log by client and raises two alerts: many 401s (password guessing) and many different missing paths, 404s (probing for forgotten files).

from collections import Counter, defaultdict

# made-up access log: client IP, method, path, status
log = """203.0.113.5 GET /cart 200
203.0.113.5 POST /orders 201
198.51.100.7 POST /login 401
198.51.100.7 POST /login 401
198.51.100.7 POST /login 401
198.51.100.7 POST /login 401
198.51.100.7 POST /login 401
198.51.100.8 GET /.env 404
198.51.100.8 GET /backup.zip 404
198.51.100.8 GET /admin 403
198.51.100.8 GET /wp-login.php 404
198.51.100.8 GET /.git/config 404
203.0.113.9 GET /old-cart 301
203.0.113.9 GET /cart 200"""

per_ip = defaultdict(Counter)                  # ip -> status counts
missing = defaultdict(set)                     # ip -> 404 paths
for line in log.splitlines():
    ip, method, path, status = line.split()
    per_ip[ip][int(status)] += 1
    if status == "404":
        missing[ip].add(path)

for ip, codes in per_ip.items():
    classes = Counter(f"{c // 100}xx" for c in codes.elements())
    note = ""
    if codes[401] >= 5:
        note = "ALERT: many 401s -> password guessing?"
    elif len(missing[ip]) >= 3:
        note = f"ALERT: {len(missing[ip])} missing paths -> probing?"
    print(f"{ip:13} {dict(classes)!s:22} {note}")

Output:

203.0.113.5   {'2xx': 2}             
198.51.100.7  {'4xx': 5}             ALERT: many 401s -> password guessing?
198.51.100.8  {'4xx': 5}             ALERT: 4 missing paths -> probing?
203.0.113.9   {'3xx': 1, '2xx': 1}

198.51.100.8 asked for /.env, /backup.zip, /.git/config and /wp-login.php: classic searches for forgotten secrets and admin pages. Ideas to extend it: add a time window, and count 403s on /admin paths.

Practice

1. Homework: add a do_POST to the demo shop. Accept a JSON order and answer 201 Created with a Location header.

Solution
import threading, http.client, json
from http.server import HTTPServer, BaseHTTPRequestHandler
orders = []

class Shop(BaseHTTPRequestHandler):
    def do_POST(self):
        size = int(self.headers.get("Content-Length", 0))
        body = json.loads(self.rfile.read(size))   # the request body
        orders.append(body)
        self.send_response(201)                    # 201 Created
        self.send_header("Location", f"/orders/{len(orders)}")
        self.end_headers()
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8000), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()
c = http.client.HTTPConnection("127.0.0.1", 8000)
c.request("POST", "/orders", body=json.dumps({"item": "chai", "qty": 2}),
          headers={"Content-Type": "application/json"})
r = c.getresponse()
print(r.status, r.reason, "| Location:", r.getheader("Location"))
print("orders on the server:", orders)

Output:

201 Created | Location: /orders/1
orders on the server: [{'item': 'chai', 'qty': 2}]

Content-Length tells the server how many body bytes to read; 201 Created plus Location is the standard answer to a successful create.

2. Homework: return 401 vs 403 for /admin.

Solution
import threading, http.client
from http.server import HTTPServer, BaseHTTPRequestHandler
USERS = {"token-ravi": "customer", "token-asha": "admin"}

class Shop(BaseHTTPRequestHandler):
    def do_GET(self):
        role = USERS.get(self.headers.get("Authorization", ""))
        if role is None:
            self.send_response(401)            # who are you?
        elif role != "admin":
            self.send_response(403)            # known, but no
        else:
            self.send_response(200)
        self.end_headers()
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8000), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()
for who in [None, "token-ravi", "token-asha"]:
    c = http.client.HTTPConnection("127.0.0.1", 8000)
    c.request("GET", "/admin", headers={"Authorization": who} if who else {})
    r = c.getresponse()
    print(f"{str(who):11} -> {r.status} {r.reason}")

Output:

None        -> 401 Unauthorized
token-ravi  -> 403 Forbidden
token-asha  -> 200 OK

No token: 401 (who are you?). A customer's token: 403 (known, not allowed). An admin's token: 200. (The tokens here are toy strings; real ones are long random values or signed tokens, episode 31.)

3. Homework: add an idempotency key to post_order.

Solution
orders, seen_keys = {}, {}          # database + idempotency keys
next_id = 1

def post_order(item, key):
    global next_id
    if key in seen_keys:            # a retry: same key as before
        return seen_keys[key]       # return the SAME order
    orders[next_id] = item
    seen_keys[key] = next_id
    next_id += 1
    return seen_keys[key]

print("first try -> order", post_order("phone", key="a1b2"))
print("retry     -> order", post_order("phone", key="a1b2"))
print("new order -> order", post_order("phone", key="c3d4"))
print("database:", orders)

Output:

first try -> order 1
retry     -> order 1
new order -> order 2
database: {1: 'phone', 2: 'phone'}

The app generates one key per purchase and sends it with every retry of that purchase; the server stores the key with the order and returns the same order for a repeat.

4. Comment question: why is GET never allowed to change data?

Answer

Because GET is defined as safe, and the whole web relies on that: browsers prefetch links, crawlers follow them, caches and proxies may repeat them, and an attacker can make a victim's browser send a GET just by showing an image or link. If a GET could pay, delete or transfer, any of those would trigger it.

5. Which parts of https://shop.example.com/search?q=chai#reviews does the server receive?

Answer

The path /search and the query string q=chai (in the request line), and the host shop.example.com (in the Host header and, with HTTPS, the TLS handshake). The fragment #reviews stays in the browser.

6. Your site sits behind a CDN. Where should a login rate-limiter get the client's IP?

Answer

From the forwarding header only when the TCP peer is one of your own proxies/CDN addresses (take the address that proxy added), otherwise from the TCP peer address itself. Never from a header that an arbitrary client could set.

Quick quiz

Ravi is logged in as a normal customer and opens GET /admin/reports. The server knows exactly who he is, but he isn't an admin. Which status code fits best?

A. 401 Unauthorized B. 403 Forbidden C. 404 Not Found D. 500 Internal Server Error

Answer

B. 403 Forbidden. 401 means "I don't know who you are". Ravi is already authenticated; he simply isn't allowed. Some sites return 404 on purpose to hide that the page exists at all.

Interview questions

  1. Describe an HTTP request and response. Request: request line (method, path, version), headers, blank line, optional body. Response: status line (version, code, reason), headers, blank line, body.
  2. What does "HTTP is stateless" mean, and how do sites remember users? Each request is understood on its own; servers set a cookie with a session ID (Set-Cookie), the browser sends it back (Cookie), and the server keeps session data for that ID.
  3. Safe vs idempotent methods? Safe methods don't change server state (GET, HEAD, OPTIONS); idempotent methods have the same effect once or many times (safe methods plus PUT and DELETE). POST is neither; PATCH may not be idempotent.
  4. 401 vs 403? 401: not authenticated. 403: authenticated (or not needed) but not permitted.
  5. Why shouldn't secrets go in URLs? URLs are stored in history, bookmarks, server and proxy logs, and can leak via the Referer header.
  6. What is URL encoding and why does it matter for security? Writing special characters as %XX codes so user data can't change the structure of a URL; failing to encode is a form of injection.
  7. HTTP/1.1 vs HTTP/2 vs HTTP/3? 1.1: text, one request at a time per connection. 2: binary frames multiplexed over one TCP connection, header compression, but TCP head-of-line blocking. 3: over QUIC on UDP with independent streams and built-in TLS 1.3.
  8. Forward vs reverse proxy? A forward proxy acts for clients (filtering, logging outbound traffic); a reverse proxy acts for servers (TLS termination, caching, load balancing, WAF).
  9. What is a CDN and why does it help security? A network of edge servers caching content near users; it reduces load and latency and absorbs DDoS traffic before it reaches the origin.
  10. What's the risk with X-Forwarded-For? It's client-controllable; trusting it from anyone lets attackers spoof their IP to bypass rate limits, allowlists and logging. Trust it only from known proxies.

Glossary

Term Meaning in plain words Example
HTTP Hypertext Transfer Protocol: the request/response format of the web (RFC 9110) GET /cart HTTP/1.1
Request What the client sends: request line, headers, optional body A browser asking for a page
Response What the server sends back: status line, headers, body 200 OK + the page
Request line The first line: method, path, version GET /cart HTTP/1.1
Method The action the client wants GET, POST
Path Which thing on the server /cart
Header An extra detail line, Name: value Host: shop.example
Body The data carried after the headers JSON order details
Status line / status code The first response line; a three-digit result code HTTP/1.1 404 Not Found
URL Uniform Resource Locator: the full address of a resource https://shop.example.com/cart
Scheme How to talk to the resource https
Host The machine's name shop.example.com
Query string Inputs after ?, as key=value pairs joined by & ?q=chai&page=2
Fragment The part after #, kept in the browser #reviews
URL encoding Writing special characters as safe codes & → %26, space → +
Injection User data changing the structure of a command or request & biscuits becoming a parameter
GET / HEAD Read a resource / read only its headers Opening a page
POST Create or submit; not idempotent Placing an order
PUT Replace a resource completely; idempotent Set order 1
PATCH Change part of a resource Update the address
DELETE Remove a resource; idempotent Cancel an order
OPTIONS Ask which methods are allowed Used by browsers before some requests
Safe method Doesn't change anything on the server GET
Idempotent method Same effect once or many times PUT, DELETE
Idempotency key A unique ID per action so retries are recognised key="a1b2"
Location header Where a redirect goes, or where a new resource lives Location: /cart
1xx–5xx Status classes: info, success, redirect, client error, server error 201, 301, 403, 503
401 vs 403 Not authenticated vs not permitted Logged out vs not admin
Stateless Each request understood on its own, with no memory HTTP
Cookie Small data the browser stores and sends back automatically (RFC 6265) session=7e14...
Set-Cookie Response header that creates or updates a cookie Set-Cookie: session=...
Session Data the server keeps for one visitor, found by a session ID The cart
Session ID The random token naming a session 16 hex characters
Session hijacking Using a stolen session ID to act as the victim The "thief with a copy"
User-Agent Header naming the client software (as claimed) Mozilla/5.0 ...
Content-Type Header saying what the body is application/json
HTTP/1.1 Text messages, one request at a time per connection Classic web
HTTP/2 Binary frames, many requests on one TCP connection (RFC 9113) Most HTTPS sites
HTTP/3 HTTP over QUIC (RFC 9114) UDP port 443
QUIC UDP-based, multiplexed, encrypted transport (RFC 9000) Under HTTP/3
Frame A small labelled chunk of an HTTP/2 or HTTP/3 message —
Head-of-line blocking One lost or slow item holds up everything behind it HTTP/2 over TCP
Forward proxy A middle server acting for clients Office web filter
Reverse proxy A middle server acting for servers In front of the shop
Load balancer Spreads requests across servers, skipping unhealthy ones Round robin with health checks
Health check A regular test that a server is working GET /health
CDN Content delivery network: copies content near users Edge servers
X-Forwarded-For Header in which proxies record the original client IP X-Forwarded-For: 203.0.113.5
Trust boundary The line where data crosses from less trusted to more trusted Client → your proxy
Access log The server's record of every request 198.51.100.7 POST /login 401

Summary

  • HTTP is an exact format: a request (method, path, version, headers, body) and a response (status line, headers, body).
  • A URL has scheme, host, port, path, query string and fragment. Encode user input (urlencode) and keep secrets out of URLs.
  • Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS. Safe = changes nothing; idempotent = same effect if repeated. Retried POSTs need idempotency keys; GET must never change data.
  • Status codes: 1xx info, 2xx success, 3xx redirect, 4xx client error, 5xx server error. 401 = who are you, 403 = no. Bursts of 4xx are a signal.
  • HTTP is stateless; cookies carry a session ID. A stolen cookie is a stolen session.
  • HTTP/2 multiplexes over one TCP connection; HTTP/3 runs over QUIC on UDP.
  • Proxies, load balancers and CDNs sit in the middle; trust X-Forwarded-For only from your own proxy.

Next episode: TLS and HTTPS in depth: what the padlock means, the TLS handshake, cipher suites, HSTS, downgrade attacks and mutual TLS.

Sources: RFC 9110, HTTP Semantics (June 2022, STD 97); RFC 6265, HTTP State Management Mechanism (A. Barth, Apr 2011); RFC 9113, HTTP/2 (M. Thomson, C. Benfield, June 2022); RFC 9114, HTTP/3 (M. Bishop, June 2022); RFC 9000, QUIC (May 2021); apyhub.com, "What Is an Idempotent Request?" (method safety and idempotency per RFC 9110). Code outputs from Python 3.13. Fetched 30 Oct 2026.