Sub topic 8
The application layer: HTTP, DNS, TLS, and friends
This chapter assumes you know what a client and a server are, can run commands in a terminal, and have met the four TCP/IP layers (link, internet, transport, application) in earlier chapters. It covers the top layer and the protocols that live there.
What the application layer does
The application layer holds the protocols that programs use to talk to each other. Everything below it moves bytes between hosts and between processes. The application layer decides what those bytes mean: a request for a web page, a name lookup, a mail message.
In TCP/IP, this one layer covers three OSI layers: session (5), presentation (6), and application (7). TCP/IP does not separate them. Each application protocol handles its own session management and data formatting.
An application protocol defines:
- The message format, such as text lines, binary fields, or JSON.
- The order of messages, such as request then response.
- Error handling, such as status codes.
- Which transport it uses and on which port.
Common protocols
| Protocol | Purpose | Transport | Default port |
|---|---|---|---|
| HTTP | Web requests and APIs | TCP | 80 |
| HTTPS | HTTP carried inside TLS | TCP | 443 |
| DNS | Name to address lookup | UDP, or TCP for large answers | 53 |
| SMTP | Sending email between servers | TCP | 25 |
| SSH | Remote shell, file transfer | TCP | 22 |
| DHCP | Automatic address assignment | UDP | 67/68 |
The application layer does not choose how packets are routed or retransmitted. It picks a transport and relies on it. DNS usually picks UDP because a lookup is one small question and one small answer. HTTP picks TCP because pages must arrive complete and in order.
HTTP: a request and a response
HTTP is a text-based request and response protocol in versions 1.0 and 1.1. A client sends a request line, headers, and an optional body. The server replies with a status line, headers, and a body.
import socket
request = (
"GET / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n"
"\r\n"
).encode("ascii")
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
data = b""
while chunk := s.recv(4096):
data += chunk
print(data.split(b"\r\n")[0].decode())The first line of the reply looks like HTTP/1.1 200 OK. The code never builds a TCP or IP header. It writes only the application message, and the operating system handles the layers below.
DNS: names to addresses
DNS maps a name such as example.com to IP addresses. A program asks a resolver, which is a server that answers queries or finds the answer by asking other servers. You can see this with a terminal command.
dig +short example.com AThe output is one or more IPv4 addresses. Applications call DNS first, then open a connection to the address that came back. A DNS failure therefore looks like a connection failure to the user, even though no connection was attempted.
TLS: security between transport and application
TLS (Transport Layer Security) encrypts data and lets the client check the server's identity. HTTPS is HTTP sent through a TLS session. TLS runs on top of TCP and below HTTP, so it sits between the transport layer and the application protocol.
TLS does not fit cleanly into one layer. In OSI terms it resembles presentation and session functions. In TCP/IP terms it is usually counted with the application layer. A later chapter returns to this.
Common errors
- Name resolves, connection fails. DNS worked. Look at the transport layer or a firewall.
- Connection works, certificate error. TCP is fine. The TLS step failed, often because of an expired certificate or a wrong hostname.
- Connection works, HTTP 404 or 500. Every layer below works. The fault is in the application.
- Using a hostname that DNS cannot find. The program fails before any packet to the server is sent.
Exercise
A browser shows "certificate has expired" for a site. Which of these steps already succeeded: DNS lookup, TCP connection, TLS handshake, HTTP response?
Show answer
DNS lookup and TCP connection succeeded, because the client reached the server and began the TLS handshake. The TLS handshake failed at the certificate check. No HTTP response was received, because HTTP runs only after TLS completes.