Sub topic 1
Anatomy of a URL
Prerequisites
This chapter assumes you can use a web browser, recognize HTML, CSS, and JavaScript, and understand that an IP address identifies a device on a network.
URL parts
A URL (Uniform Resource Locator) tells the browser what resource to request and how to reach it:
https://example.com:8443/docs/intro?lang=en#setuphttpsis the scheme. It selects the communication protocol.example.comis the host name.8443is the port. It identifies a service on that host./docs/introis the path to the resource.?lang=enis the query string. It carries input for the server.#setupis the fragment. The browser uses it after receiving the resource, often to jump to a page section. It is not sent in the HTTP request.
The URL class exposes these parts:
const url = new URL("https://example.com:8443/docs/intro?lang=en#setup");
console.log(url.protocol); // "https:"
console.log(url.hostname); // "example.com"
console.log(url.port); // "8443"
console.log(url.pathname); // "/docs/intro"
console.log(url.search); // "?lang=en"
console.log(url.hash); // "#setup"From keystrokes to navigation
When you press Enter, the browser first decides whether the text is a URL. Text such as example.com is usually treated as a navigation target. Text such as weather tomorrow is sent to the configured search engine instead.
The browser then normalizes the target. It may add a scheme, encode characters, or select a default port. The final URL determines the next steps.
Checking local state before the network
Before contacting a server, the browser can check its own memory cache, disk cache, service workers, and security rules such as HSTS. A valid cached response can avoid a network request. HSTS can also change an http navigation into https.
IP addresses and ports
Computers route packets using IP addresses, such as 203.0.113.10, rather than host names. A port selects a program on that machine. HTTPS commonly uses port 443, while HTTP commonly uses port 80. If a URL omits the port, the scheme supplies the default.
Finding the server with DNS
DNS (Domain Name System) translates a host name into an IP address. The browser and operating system may have cached the answer. Otherwise, a recursive DNS resolver asks authoritative name servers and returns an address. A host can have several addresses for resilience or load distribution.
Opening a TCP connection
For HTTP/1.1 or HTTP/2 over TCP, the browser opens a connection to the resolved IP address and port. TCP uses a three-way handshake: the client sends SYN, the server replies SYN-ACK, and the client sends ACK. This establishes a reliable, ordered byte stream. HTTPS performs its TLS handshake over this connection before HTTP data is exchanged.
Exercise
Which URL component is used by the browser but omitted from the HTTP request?
Show answer
The fragment, such as #setup, is used for client-side navigation after the resource arrives.