COMP347 (Revision 10) | TrustOpen University
Upon completion of this expanded tutorial, students will be able to:
The Hypertext Transfer Protocol (HTTP) is the foundation of the World Wide Web. This tutorial provides a rigorous, university‑level examination of HTTP, covering not only the syntax and semantics but also the performance implications of connection management, caching, and the evolution of the protocol.
We begin with the Web's client‑server architecture and the role of URIs and MIME types. We then dissect HTTP request and response messages, paying close attention to header fields and their impact on caching, security, and content negotiation. A significant portion is devoted to connection management: we develop a formal performance model for non‑persistent versus persistent connections, and we explore pipelining and its limitations. We also delve into caching mechanics, conditional requests, and the cookie mechanism that enables session state. Finally, we introduce the innovations in HTTP/2 and HTTP/3 that address the performance bottlenecks of HTTP/1.1.
The Web is a distributed hypermedia system built on the client‑server paradigm. Clients (web browsers) send requests for resources; servers (web servers) listen for requests and respond with the requested resource or an error. Resources are identified by Uniform Resource Locators (URLs), which specify the scheme (e.g., http, https), hostname, port, path, and optional query parameters.
A generic URL follows:
scheme://[userinfo@]host[:port]/path[?query][#fragment]
Percent‑encoding (URL encoding) is used to represent reserved or unsafe characters (e.g., space → %20). The query string is a sequence of key‑value pairs separated by &, with keys and values also percent‑encoded.
HTTP uses MIME (Multipurpose Internet Mail Extensions) types to indicate the media type of a resource (e.g., text/html, image/png, application/json). Content negotiation allows clients to specify preferences via the Accept header; servers may respond with the best matching representation or a 300 Multiple Choices (or 406 Not Acceptable).
Both requests and responses share a common format:
The request‑line consists of: Method Request‑URI HTTP‑Version.
The response starts with a status‑line: HTTP‑Version Status‑Code Reason‑Phrase.
Idempotence means that making the same request multiple times has the same effect as making it once. This is important for retry logic.
| Header | Purpose | Example |
|---|---|---|
| Host | Virtual host (required for HTTP/1.1) | Host: www.example.com |
| User‑Agent | Client identification | User‑Agent: Mozilla/5.0 ... |
| Accept | Preferred media types | Accept: text/html, application/json;q=0.9 |
| Accept‑Encoding | Compression algorithms supported | Accept‑Encoding: gzip, deflate, br |
| Accept‑Language | Preferred language(s) | Accept‑Language: en‑US,en;q=0.9 |
| Connection | Control persistent connection | Connection: keep‑alive |
| Cookie | State information (client‑side) | Cookie: session=abc123 |
| Referer | Previous page URL | Referer: https://www.example.com/prev |
| If‑Modified‑Since | Conditional GET (date) | If‑Modified‑Since: Mon, 23 May 2023 12:00:00 GMT |
| If‑None‑Match | Conditional GET (ETag) | If‑None‑Match: "abc123" |
| Header | Purpose | Example |
|---|---|---|
| Server | Server software | Server: Apache/2.4.54 |
| Content‑Type | Media type of response body | Content‑Type: text/html; charset=UTF‑8 |
| Content‑Length | Size in bytes | Content‑Length: 1234 |
| Content‑Encoding | Compression applied | Content‑Encoding: gzip |
| Cache‑Control | Caching directives | Cache‑Control: max‑age=3600, public |
| Expires | Absolute expiration time | Expires: Tue, 24 May 2023 12:00:00 GMT |
| ETag | Entity tag (resource version) | ETag: "abc123" |
| Last‑Modified | Modification time | Last‑Modified: Mon, 22 May 2023 14:30:00 GMT |
| Location | Redirect target | Location: https://www.example.com/new |
| Set‑Cookie | Store state on client | Set‑Cookie: session=xyz; HttpOnly; Secure |
Each request opens a new TCP connection. The overhead is significant: each connection requires a 3‑way handshake (1 RTT) plus possibly a slow‑start penalty. For a page with many embedded objects (images, scripts), this leads to high latency.
Response time formula (ignoring transmission time): \( T_{non-persistent} = (2 \times RTT) \times N \) for \( N \) objects, assuming sequential requests, because each object requires a new TCP connection (SYN, SYN‑ACK) and then the request/response (another RTT). Actually, it's 2 RTT per object: one for handshake, one for request/response. So total = \( 2N \cdot RTT \) (plus transmission).
A single TCP connection can be reused for multiple requests. This eliminates the handshake overhead for subsequent requests. The connection is kept alive (keep‑alive) for a period of idle time, after which it may be closed.
Response time (no pipelining): \( T_{persistent} = (1 \times RTT) + (N \times RTT) \) — but actually, the first request requires handshake (1 RTT) and request/response (1 RTT). Subsequent requests each require 1 RTT (request/response). So total = \( 2 \times RTT + (N-1) \times RTT = (N+1) \times RTT \). That's a reduction.
Pipelining allows multiple requests to be sent without waiting for each response. In theory, this can reduce the total time to just \( 2 \times RTT + \text{transmission} \) for all objects (since responses are sent back‑to‑back). However, pipelining has head‑of‑line blocking at the HTTP layer: if the first request is slow (e.g., a large dynamic response), subsequent responses are delayed. HTTP/2 solved this with multiplexing.
Let there be \( N \) objects on a page. Let \( RTT \) be the round‑trip time, and \( T_{trans} \) be the transmission time per object (assumed equal). For persistent connections without pipelining:
For non‑persistent:
Thus, persistent connections reduce the \( RTT \) term from \( 2N \) to \( N+1 \), which is significant for large \( N \).
Modern browsers open multiple parallel TCP connections (typically 6‑8 per domain) to fetch objects concurrently. This reduces perceived latency but can cause congestion and does not eliminate the handshake overhead for each connection. HTTP/2's multiplexing over a single connection is more efficient.
Cache‑Control: public (cacheable by any cache), private (only browser cache), no‑cache (must revalidate), no‑store (do not store at all), max‑age=<seconds>, must‑revalidate (stale objects must be revalidated with origin).Expires: absolute expiration date (obsolete in HTTP/1.1 but still used).
A conditional GET uses If‑Modified‑Since (based on Last‑Modified) or If‑None‑Match (based on ETag) to ask the server if the resource has changed. If not, the server responds with 304 Not Modified (no body), saving bandwidth and latency.
Set‑Cookie header in response.Cookie header.Cookies are the primary mechanism for session management, personalisation, and tracking.
Q1: What is the default port for HTTP?
Port 80.
Q2: What is the purpose of the Host header in HTTP/1.1?
It specifies the virtual host name to allow multiple domains to share the same IP address.
Q3: What does the status code 304 indicate?
Not Modified – used in conditional GET when the resource has not changed.
Q4: What is the difference between no‑cache and no‑store in Cache‑Control?
no‑cache requires revalidation with the origin server before use; no‑store means do not store the response at all.
Q5: Why is pipelining in HTTP/1.1 considered problematic?
It suffers from head‑of‑line blocking: if the first request is slow, all subsequent responses are delayed.
Q6: What is the difference between Last‑Modified and ETag for cache validation?
Last‑Modified is based on a timestamp; ETag is an opaque identifier (often a hash) that can detect changes more reliably.
Q7: What is the HttpOnly attribute of a cookie used for?
It prevents client‑side scripts (JavaScript) from accessing the cookie, mitigating XSS attacks.
Q8: Which HTTP method is idempotent but not safe?
PUT (and DELETE also idempotent, but not safe).
Q9: What is the primary advantage of HTTP/2's multiplexing over HTTP/1.1 pipelining?
Multiplexing interleaves frames from multiple streams, so a slow stream does not block others (no head‑of‑line blocking).
Q10: What transport protocol does HTTP/3 use?
QUIC (which is based on UDP).
Q11: What is the purpose of the Referer header?
It indicates the URL of the page that made the request (used for analytics, anti‑hotlinking).
Q12: In the response time model for non‑persistent HTTP, how many RTTs are needed for each object (assuming no transmission time)?
2 RTT (one for TCP handshake, one for request/response).
Exercise 1 – Page Load Time Calculation
A page has 10 objects. RTT = 50 ms. Transmission time per object = 10 ms. Calculate load time for (a) non‑persistent, (b) persistent no pipelining, (c) persistent with pipelining (ideal).
(a) Non‑persistent: 10 * (2*50 + 10) = 10*110 = 1100 ms.
(b) Persistent: (10+1)*50 + 10*10 = 550+100 = 650 ms.
(c) Persistent with pipelining: 1 RTT for handshake, then send all requests, then receive all responses (one RTT). Total = 2*50 + 10*10 = 100+100 = 200 ms.
Exercise 2 – HTTP Request Construction
Construct the HTTP request for `https://api.example.com/v1/data?limit=5` using a browser that supports HTTP/1.1 and gzip.
GET /v1/data?limit=5 HTTP/1.1 Host: api.example.com User-Agent: Mozilla/5.0 ... Accept: application/json, text/plain Accept-Encoding: gzip, deflate Connection: keep-alive [blank line]
Exercise 3 – Status Code Analysis
You receive a 302 Found with a Location header. What should the client do?
The client should follow the redirect (usually a GET request to the new location). 302 is a temporary redirect; the client should continue using the original URI for future requests (unless changed).
Exercise 4 – Cache Validation
Explain the sequence of headers for a conditional GET using ETag.
Initial response: ETag: "abc". Later request: If-None-Match: "abc". Server checks; if unchanged, returns 304 Not Modified with no body; if changed, returns 200 with new body and new ETag.
Exercise 5 – Cookie Flow
Describe the cookie exchange when a user logs in and then navigates to a protected page.
1. Login POST: server validates credentials, sets Set-Cookie: session=xyz; HttpOnly; Secure.
2. Browser stores cookie.
3. GET /protected: browser sends Cookie: session=xyz.
4. Server validates session, returns protected content.
Exercise 6 – HTTP/2 vs HTTP/1.1
Explain how HTTP/2's multiplexing addresses the head‑of‑line blocking problem of HTTP/1.1 pipelining.
In HTTP/1.1 pipelining, responses must be returned in request order; if the first request is slow (e.g., a large database query), subsequent responses are delayed even if they are ready. In HTTP/2, frames from multiple streams are interleaved, so a slow stream does not block others; the client can process responses out of order.
Homework 1 – HTTP/2 and HTTP/3 Research
Write a detailed report comparing HTTP/2 and HTTP/3, focusing on: (a) transport differences, (b) multiplexing, (c) header compression, (d) security, (e) performance under packet loss.
Include references to RFCs 7540 and 9114.
Homework 2 – Cache Invalidation Strategy
Design a caching strategy for a dynamic web application where content updates frequently (every few minutes). Specify Cache‑Control headers, versioning scheme, and cache invalidation approach.
Use short max‑age, ETag, and versioned URLs (e.g., /v2/resource) to force cache updates.
Homework 3 – HTTP Security Headers
Research and explain the purpose of: Content‑Security‑Policy, Strict‑Transport‑Security, X‑Frame‑Options, X‑Content‑Type‑Options.
Provide configuration examples and discuss the threats they mitigate.
Homework 4 – Performance Analysis with DevTools
Use browser DevTools to load a popular website and analyse the waterfall chart. Identify the number of requests, connection types, and caching statuses. Suggest optimisations.
Look for parallel connections, time to first byte, and resource sizes.
Homework 5 – REST API Design
Design a RESTful API for a blog, including endpoints for posts, comments, and users. Specify HTTP methods, status codes, and example request/response JSON.
Follow best practices: use nouns, plural resource names, proper methods, and meaningful status codes.
This expanded tutorial has provided a rigorous foundation in HTTP and the World Wide Web. Key takeaways:
These concepts are essential for understanding web performance, security, and the evolution of web applications.