HTTP and the Web
In questa pagina 12
The application layer
The application layer provides services to the user over a logical connection: the two applications behave as if directly linked. It is the top layer: its protocols offer services to no other protocol and only receive transport services.
| application | protocol | transport |
|---|---|---|
| SMTP | TCP | |
| remote terminal | Telnet | TCP |
| World Wide Web | HTTP | TCP |
| file transfer | FTP | TCP |
| streaming multimedia | proprietary | TCP or UDP |
| Internet telephony | proprietary | typically UDP |
| name resolution | DNS (DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →) | typically UDP |
The World Wide Web
The idea dates from 1989 at CERN. The Web is an archive of web pages distributed worldwide and linked by hypertext. It is a distributed client-server service: a client (a browser) accesses a service offered by a server; the service is spread over many sites, each with one or more pages (files with a name and an address).
Example. A document (file A) refers to a text (file C) and a large image (file B), with A and B on one site and C on another: three transactions, one per file.
A browser has a controller, client protocols and interpreters (e.g. for HTML). The server keeps requested files in a memory cache and can serve several requests at once (multiprocessing, multithreading).
URL
A page needs a unique identifier: host, port, path, plus the protocol.
Definition (URL).
protocol://host/pathorprotocol://host:port/path.
- Protocol: the client-server program used, usually HTTP.
- Host: IP address or (usually) domain name, translated by DNS.
- Port: 16-bit integer, normally the default of the application (HTTP: 80), written only if different.
- Path: location and name of the file on the server (in UNIX folders separated by
/).
Example. http://www.example.it:8080/corsi/internet.html: protocol http, host www.example.it, port 8080, path /corsi/internet.html; the request has GET /corsi/internet.html HTTP/1.1 and Host: www.example.it:8080.
A web page is an HTML file with embedded objects (images, style sheets, scripts), each with its own URL and fetched by a separate request: a page with 1 HTML file and 10 images needs 11 requests.
HTTP
HTTP (HyperText Transfer Protocol) defines how clients and servers retrieve pages: the client sends a request, the server returns a response.
- The server uses port 80; the client a temporary port (Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →).
- It uses TCP (TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control →): connection set-up and release are needed; errors and losses are TCP's concern.
- Stateless: the server remembers nothing about earlier requests; for memory (cart, login) cookies are used.
- In versions 1.x messages are ASCII text (readable with
telnetorncon port 80).
HTTP/1.1 messages
Request: request line, headers (one per line, Name: value), a blank line, optional body; each line ends with CR LF.
GET /corsi/internet.html HTTP/1.1
Host: www.example.it
User-Agent: Mozilla/5.0 (X11; Linux x86_64) Firefox/130.0
Accept: text/html,application/xhtml+xml;q=0.9,*/*;q=0.8
Accept-Language: it-IT,it;q=0.9,en;q=0.5
Connection: keep-aliveResponse: status line (version, code, phrase), headers, blank line, body.
HTTP/1.1 200 OK
Date: Fri, 09 Oct 2026 10:15:30 GMT
Server: Apache/2.4
Content-Type: text/html; charset=UTF-8
Content-Length: 44
<html><body><h1>Internet</h1></body></html>Content-Length is the body size in bytes; it lets the client know where the body ends without closing the connection. Host is mandatory in HTTP/1.1: one IP can host many sites (virtual hosting), and the site name is visible only here.
Main headers. Request: Host, User-Agent, Accept, Accept-Language (with weights q), Cookie. Response: Date, Server, Set-Cookie, Location (for redirects). Both: Content-Type, Content-Length, Connection (keep-alive or close).
Example. A POST sending a form: Content-Type: application/x-www-form-urlencoded, Content-Length: 17 for the body nome=Mario&eta=30.
Methods
| method | use | body in request | safe | idempotent |
|---|---|---|---|---|
| GET | read a resource | no | yes | yes |
| HEAD | like GET, response has only headers | no | yes | yes |
| POST | send data to process (form, create a resource) | yes | no | no |
| PUT | replace/create the resource at the URL | yes | no | yes |
| DELETE | delete the resource | usually no | no | yes |
Definition. A method is safe if it does not change server state; idempotent if repeating it times has the same effect as once.
DELETE /foto/7 twice leaves the same state (second reply may be 404): idempotent; two POST /ordini create two orders: not idempotent (hence the browser warning on reload). With GET the parameters are in the URL query (visible, in logs); with POST in the body.
Status codes
First digit: 1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error.
| code | phrase | meaning |
|---|---|---|
| 200 | OK | success, body has the resource |
| 201 | Created | resource created (POST/PUT) |
| 204 | No Content | success, no body |
| 301 | Moved Permanently | new permanent URL in Location |
| 302 | Found | temporary move, Location |
| 304 | Not Modified | the cached copy is still valid (no body) |
| 400 | Bad Request | malformed request |
| 401 | Unauthorized | credentials needed |
| 403 | Forbidden | understood but refused |
| 404 | Not Found | resource does not exist |
| 500 | Internal Server Error | server fault |
| 503 | Service Unavailable | overloaded or in maintenance |
A 301/302 has Location: new-URL and the browser automatically makes a new request: one click can cost several RTTs. Example: http://www.example.it/ gets 301 with Location: https://www.example.it/: besides the 2 RTT already spent, a new TCP connection to port 443 (1 RTT), the new request (at least 1 RTT) and the TLS handshake.
Non-persistent and persistent connections
Objects on different servers need separate TCP connections. For objects on the same server:
Non-persistent HTTP: at most one object per TCP connection (open, request, response, close). Each object costs 1 RTT for the three-way handshake (the final ACK travels with the request), 1 RTT for request and start of the response, plus the transmission time : .
non-persistent, one object persistent, several objects
client server client server
|--- SYN ------------->| |--- SYN ------------->|
|<-- SYN+ACK ----------| 1 RTT |<-- SYN+ACK ----------| 1 RTT (only once)
|--- ACK + GET ------->| |--- ACK + GET page -->|
|<-- response ---------| 1 RTT + L/R |<-- page -------------| 1 RTT + L/R
| (connection closed) |--- GET object 1 ---->|
next object: SYN again... |<-- object 1 ---------| 1 RTT + L/RPersistent HTTP: several objects over the same TCP connection; the server closes it on request or after a timeout. HTTP/1.1 is persistent by default (Connection: keep-alive, closed with Connection: close). The handshake is done once. Without pipelining the next request starts only when the previous response arrives (1 RTT per object); with pipelining the client sends all requests without waiting (about 1 RTT for all). Browsers disabled pipelining in practice (head-of-line blocking, buggy servers).
Formula (page with embedded objects, objects in all, transmission times neglected).
- non-persistent: ;
- persistent without pipelining: ;
- persistent with pipelining: .
Non-persistent: each object costs RTT and objects are , one after the other. Persistent: the page costs RTT, then each object 1 RTT. Pipelining: page 2 RTT, then all requests together in 1 RTT. With : , , RTT. With ms: s, s, s. Persistence saves RTT.
With transmission times. HTML page 50 kB and 10 images of 100 kB on a 10 Mbit/s link, RTT ms: s, s (multiply bytes by 8 to get bits). Non-persistent: s. Persistent: s. Pipelined: s (the images still arrive one after another on the link). The protocol reduces only the round trips, not the transmission time. New TCP connections also restart with a small congestion window (TCP Congestion ControlCongestion arises when fast links feed a slow one, router queues fill and packets are lost or delayed, possibly up to network collapse; TCP controls its congestion window cwnd using loss feedback (timeout or three duplicate ACKs): slow start doubles cwnd every RTT up to the threshold ssthresh, then congestion avoidance adds 1 MSS per RTT, and on a loss ssthresh becomes W/2; variants differ in reaction to three dupACKs: Tahoe restarts from cwnd=1, Reno uses fast recovery (ssthresh=cwnd/2, cwnd=ssthresh+3, +1 per further dupACK), NewReno handles partial ACKs and recovers several losses in one window, SACK acknowledges received blocks and resends only what is missing.TCP Congestion Control →), while a persistent one has already "taken speed".
Cookies
The Web was born stateless, but many functions need memory (shops, portals, advertising).
Definition (cookie). A small string, e.g.
sessione=18988466, sent by the server in a response with theSet-Cookieheader; the browser stores it and returns it in every later request to the same site with theCookieheader. The server uses it as a key into its database.
GET /ntw/index.html HTTP/1.1 <- first request, no cookie
HTTP/1.1 200 OK
Set-Cookie: 18988466 <- the server assigns the cookie
GET /ntw/carrello/index.html HTTP/1.1
Cookie: 18988466 <- the client sends it back
GET /image.gif HTTP/1.1
Cookie: 18988466 <- and with every later requestFour components: (1) a header line in the HTTP response (Set-Cookie); (2) a header line in the HTTP request (Cookie); (3) a file kept on the user's computer and managed by the browser; (4) a database behind the site.
Uses: session management (login), personalisation (language, theme), tracking and statistics across sites, shopping cart.
A session cookie must be random and long: a guessable identifier allows session hijacking. With open sessions and an 8-digit decimal cookie ( values) a random guess hits a valid session with probability ; with 64 random bits it is . A cookie travels in clear with plain HTTP; HTTPS protects it.
Cache and proxy
A proxy (web cache) is a computer that keeps copies of responses to recent requests. The client sends the request to the proxy (the browser is configured for it and writes the full URL: GET http://www.example.it/ HTTP/1.1); the proxy checks its cache and, if the response is absent, asks the real server; responses are stored for future requests of other clients. It reduces load on the origin, reduces traffic and improves latency. The same principle is used by CDNs.
Formula (mean time with a cache). With hit ratio : , where (the cache is always crossed).
Example. ms, ms, : ms instead of 200 ms. Since , the cache pays off only if .
HTTPS
HTTPS is HTTP carried inside a TLS connection, normally on port 443: the same messages, encrypted, with the server identity proved by a certificate. The TLS handshake is added before the HTTP request, and the Secure cookie attribute makes sense because the browser checks the certificate before sending cookies and passwords.
Common mistakes
Saying HTTP "keeps state" (cookies do, not the protocol); counting one RTT per object in non-persistent mode (it is 2); counting 3 RTT per object with pipelining (handshake and page are paid once, then all objects cost 1 RTT together); forgetting the blank line between headers and body; forgetting the terms when data sizes are given.
Versione ripasso
- Application layer = top of the stack, uses transport only. Web = distributed client-server hypertext; URL
protocol://host:port/path(HTTP default port 80); a page = HTML plus embedded objects, each its own request (1 HTML + 10 images = 11 requests). - HTTP: over TCP, server port 80, stateless, ASCII messages: request/status line, headers (
Hostmandatory in 1.1), blank line, body;Content-Length,Content-Type,Connection. - Methods: GET (safe, idempotent), HEAD, POST (not idempotent), PUT (idempotent), DELETE (idempotent). Safe = no state change; idempotent = repeating equals once.
- Codes: 1xx info, 2xx success (200, 201, 204), 3xx redirect (301 permanent, 302 temporary, 304 not modified), 4xx client (400, 401, 403, 404), 5xx server (500, 503). Redirect costs extra RTTs.
- Page time with embedded objects (transmission neglected), see TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control → for the handshake:
Non-persistent (object ); persistent ; persistent + pipelining . : , , RTT. With sizes: , , s (50 kB page, 10 images of 100 kB, 10 Mbit/s, RTT 100 ms).
- Cookies:
Set-Cookiein the response,Cookiein later requests, file in the browser, database at the site; random long values ( guess for 8 digits). HTTP gains memory only through cookies. - Proxy/web cache: ; example ms vs ; useful only if . CDNs same idea.
- HTTPS = HTTP in TLS, port 443.
- Name lookup before connecting: DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →.
- Typical errors: 1 RTT per non-persistent object (it is 2); 3 RTT per object with pipelining; missing blank line; forgetting .