Sikau

Computer networks · Day 12 · about 50 min

Capstone: A tiny browser

DNS lookup, an HTTP request over the wire, and following redirects.

What happens when you type a URL

The classic interview question, as code: parse the URL, look the host up in DNS (following CNAMEs), connect to that address, send a raw HTTP request with a Host header, parse the response — and if it's a redirect, do it all again for the new location.

The steps of a page load

Typing a URL sets off a chain you've now built piece by piece. Parse the URL into host and path. Resolve the host with DNS, following CNAMEs. Connect to that IP (TCP, and TLS for https). Send an HTTP request with a Host header. Parse the response. If it's a redirect, take the Location header and start again; otherwise, return the body.

  • new URL(url) gives host, path and protocol.
  • lookup(zones, host) gives the IP.
  • WORLD.servers[ip] stands in for opening a connection.
  • parseResponse(raw) gives status, headers and body.
  • Statuses 301, 302, 307, 308 with Location → follow.

Keep a list of every URL visited; it's the best debugging output and makes loop detection easy.

The rest of this lesson

  • Redirects and limitsexplain
  • What a real browser addsexplain
  • Typing a URLorder
  • Status to actionmatch
  • fetchUrlchallenge
  • Badge quizquiz
  • Computer networks: complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 12 lessons of Computer networks