Salta al contenuto
Note per Studenti HTTP and the Web Report

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
e-mail 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

E-mail is in E-mail - SMTP, POP3, IMAP and MIMEE-mail is a one-way transaction, so intermediate servers are used: two user agents (UA), push client-server programs (MTA, protocol SMTP, from sender to its server and between servers) and a pull program (MAA, POP3 or IMAP4, from the recipient's server to the recipient); SMTP uses TCP port 25 with commands HELO, MAIL FROM, RCPT TO, DATA, QUIT and three-digit replies (220 ready, 250 ok, 354 start mail input, 221 closing, 421 unavailable) in three phases (opening, transfer, closing), POP3 (port 110) downloads messages one by one after login while IMAP4 (port 143) adds header preview, search, partial download and server-side folders, and MIME turns non-ASCII data into 7-bit ASCII, for example with base64 (3 bytes become 4 characters).E-mail - SMTP, POP3, IMAP and MIME →.

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/path or protocol://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.

HTTP/1.1 messages

Request: request line, headers (one per line, Name: value), a blank line, optional body; each line ends with CR LF.

http
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-alive

Response: status line (version, code, phrase), headers, blank line, body.

http
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 nn 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 L/RL/R: 2 RTT+L/R2\,\text{RTT}+L/R.

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/R

Persistent 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 NN embedded objects, N+1N+1 objects in all, transmission times neglected).

  • non-persistent: T=2(N+1) RTTT=2(N+1)\,\text{RTT};
  • persistent without pipelining: T=(2+N) RTTT=(2+N)\,\text{RTT};
  • persistent with pipelining: T=3 RTTT=3\,\text{RTT}.

Non-persistent: each object costs 1+1=21+1=2 RTT and objects are N+1N+1, one after the other. Persistent: the page costs 22 RTT, then each object 1 RTT. Pipelining: page 2 RTT, then all NN requests together in 1 RTT. With N=10N=10: 2222, 1212, 33 RTT. With RTT=100\text{RTT}=100 ms: 2.22.2 s, 1.21.2 s, 0.30.3 s. Persistence saves 2(N+1)−(2+N)=N2(N+1)-(2+N)=N RTT.

With transmission times. HTML page 50 kB and 10 images of 100 kB on a 10 Mbit/s link, RTT =100=100 ms: thtml=50 000⋅8/107=0.04t_{\text{html}}=50\,000\cdot8/10^7=0.04 s, tobj=0.08t_{\text{obj}}=0.08 s (multiply bytes by 8 to get bits). Non-persistent: 0.2+0.04+10(0.2+0.08)=3.040.2+0.04+10(0.2+0.08)=3.04 s. Persistent: 0.2+0.04+10(0.1+0.08)=2.040.2+0.04+10(0.1+0.08)=2.04 s. Pipelined: 0.2+0.04+0.1+10⋅0.08=1.140.2+0.04+0.1+10\cdot0.08=1.14 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 the Set-Cookie header; the browser stores it and returns it in every later request to the same site with the Cookie header. The server uses it as a key into its database.

http
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 request

Four 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 S=106S=10^6 open sessions and an 8-digit decimal cookie (10810^8 values) a random guess hits a valid session with probability S/108=1%S/10^8=1\%; with 64 random bits it is S/264≈5⋅10−14S/2^{64}\approx5\cdot10^{-14}. 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 hh: Tˉ=h Thit+(1−h) Tmiss\bar T=h\,T_{\text{hit}}+(1-h)\,T_{\text{miss}}, where Tmiss=Thit+ToriginT_{\text{miss}}=T_{\text{hit}}+T_{\text{origin}} (the cache is always crossed).

Example. Thit=10T_{\text{hit}}=10 ms, Torigin=200T_{\text{origin}}=200 ms, h=0.4h=0.4: Tˉ=0.4⋅10+0.6⋅210=130\bar T=0.4\cdot10+0.6\cdot210=130 ms instead of 200 ms. Since Tˉ=210−200 h\bar T=210-200\,h, the cache pays off only if h>10/200=5%h>10/200=5\%.

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 L/RL/R terms when data sizes are given.

Versione ripasso

Non-persistent 2(N+1) RTT2(N+1)\,\text{RTT} (object =2 RTT+L/R=2\,\text{RTT}+L/R); persistent (N+2) RTT(N+2)\,\text{RTT}; persistent + pipelining 3 RTT3\,\text{RTT}. N=10N=10: 2222, 1212, 33 RTT. With sizes: 3.043.04, 2.042.04, 1.141.14 s (50 kB page, 10 images of 100 kB, 10 Mbit/s, RTT 100 ms).

  • Cookies: Set-Cookie in the response, Cookie in later requests, file in the browser, database at the site; random long values (S/108=1%S/10^8=1\% guess for 8 digits). HTTP gains memory only through cookies.
  • Proxy/web cache: Tˉ=h Thit+(1−h)(Thit+Torigin)\bar T=h\,T_{\text{hit}}+(1-h)(T_{\text{hit}}+T_{\text{origin}}); example 130130 ms vs 200200; useful only if h>5%h>5\%. 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 L/RL/R.

Teoria collegata