📘 Tutorial 4: The Web and HTTP Fundamentals

COMP347 (Revision 10) | TrustOpen University

📑 Table of Contents

🎯 Learning Objectives

Upon completion of this expanded tutorial, students will be able to:

🔭 Overview

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.


1. The World Wide Web – Architecture and Components

1.1 Client‑Server Model

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.

1.2 URL Structure and Encoding

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.

1.3 MIME Types and Content Negotiation

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).


2. HTTP Protocol Fundamentals

2.1 HTTP Versions

2.2 HTTP Message Structure

Both requests and responses share a common format:

Request-Line (e.g., "GET /index.html HTTP/1.1") Headers (e.g., "Host: www.example.com") CRLF (blank line) Message Body (optional)

The request‑line consists of: Method Request‑URI HTTP‑Version.

The response starts with a status‑line: HTTP‑Version Status‑Code Reason‑Phrase.

2.3 HTTP Methods and Idempotence

Idempotence means that making the same request multiple times has the same effect as making it once. This is important for retry logic.


3. HTTP Message Formats and Headers

3.1 Request Headers (Selected)

HeaderPurposeExample
HostVirtual host (required for HTTP/1.1)Host: www.example.com
User‑AgentClient identificationUser‑Agent: Mozilla/5.0 ...
AcceptPreferred media typesAccept: text/html, application/json;q=0.9
Accept‑EncodingCompression algorithms supportedAccept‑Encoding: gzip, deflate, br
Accept‑LanguagePreferred language(s)Accept‑Language: en‑US,en;q=0.9
ConnectionControl persistent connectionConnection: keep‑alive
CookieState information (client‑side)Cookie: session=abc123
RefererPrevious page URLReferer: https://www.example.com/prev
If‑Modified‑SinceConditional GET (date)If‑Modified‑Since: Mon, 23 May 2023 12:00:00 GMT
If‑None‑MatchConditional GET (ETag)If‑None‑Match: "abc123"

3.2 Response Headers (Selected)

HeaderPurposeExample
ServerServer softwareServer: Apache/2.4.54
Content‑TypeMedia type of response bodyContent‑Type: text/html; charset=UTF‑8
Content‑LengthSize in bytesContent‑Length: 1234
Content‑EncodingCompression appliedContent‑Encoding: gzip
Cache‑ControlCaching directivesCache‑Control: max‑age=3600, public
ExpiresAbsolute expiration timeExpires: Tue, 24 May 2023 12:00:00 GMT
ETagEntity tag (resource version)ETag: "abc123"
Last‑ModifiedModification timeLast‑Modified: Mon, 22 May 2023 14:30:00 GMT
LocationRedirect targetLocation: https://www.example.com/new
Set‑CookieStore state on clientSet‑Cookie: session=xyz; HttpOnly; Secure

3.3 Status Codes – Detailed Categorisation


4. Connection Management: Non‑Persistent vs Persistent

4.1 Non‑Persistent Connections (HTTP/1.0 default)

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).

4.2 Persistent Connections (HTTP/1.1 default)

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.

4.3 Persistent Connections with Pipelining

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.


5. HTTP Performance Modelling

5.1 Page Load Time Model

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:

\( T = RTT_{handshake} + RTT_{request} + T_{trans} + (N-1) \cdot (RTT_{request} + T_{trans}) \)
\( = (N+1) \cdot RTT + N \cdot T_{trans} \)

For non‑persistent:

\( T = N \cdot (2 \cdot RTT + T_{trans}) = 2N \cdot RTT + N \cdot T_{trans} \)

Thus, persistent connections reduce the \( RTT \) term from \( 2N \) to \( N+1 \), which is significant for large \( N \).

5.2 Parallel Connections

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.


6. Web Caching and Conditional Requests

6.1 Cache Types

6.2 Cache Control Headers

6.3 Conditional GET

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.

6.4 ETag vs Last‑Modified


7. HTTP State Management: Cookies

7.1 Cookie Attributes

7.2 Cookie Flow

  1. Server sends Set‑Cookie header in response.
  2. Browser stores the cookie.
  3. Browser includes the cookie in subsequent requests via Cookie header.

Cookies are the primary mechanism for session management, personalisation, and tracking.


8. Introduction to HTTP/2 and HTTP/3

8.1 HTTP/2 (RFC 7540)

8.2 HTTP/3 (RFC 9114)


📝 Quiz: Tutorial 4

Q1: What is the default port for HTTP?

Answer

Port 80.

Q2: What is the purpose of the Host header in HTTP/1.1?

Answer

It specifies the virtual host name to allow multiple domains to share the same IP address.

Q3: What does the status code 304 indicate?

Answer

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?

Answer

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?

Answer

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?

Answer

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?

Answer

It prevents client‑side scripts (JavaScript) from accessing the cookie, mitigating XSS attacks.

Q8: Which HTTP method is idempotent but not safe?

Answer

PUT (and DELETE also idempotent, but not safe).

Q9: What is the primary advantage of HTTP/2's multiplexing over HTTP/1.1 pipelining?

Answer

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?

Answer

QUIC (which is based on UDP).

Q11: What is the purpose of the Referer header?

Answer

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)?

Answer

2 RTT (one for TCP handshake, one for request/response).


✏️ Exercises: Tutorial 4

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).

Sample Solution

(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.

Sample Solution
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?

Sample Solution

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.

Sample Solution

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.

Sample Solution

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.

Sample Solution

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: Tutorial 4

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.

Guidance

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.

Guidance

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.

Guidance

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.

Guidance

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.

Guidance

Follow best practices: use nouns, plural resource names, proper methods, and meaningful status codes.


📌 Summary

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.