LESSON 06 · Downloader and parser

Fetch imperfect pages safely

Bound network and parsing work, and classify outcomes before following links.

SYSTEM VIEW

The crawl path

Diagram view
Body—
Redirects—
Blocked—

Watch a fetch worker meet a decompression bomb and a redirect to a private address, then parse and render what it keeps.

  1. The Fetch worker resolves example.org to 203.0.113.7, a public address the Address check lets through, and fetches /guide under a clear crawler identity and example bounds. It gets a 200 response of 180 KB. Concurrency helps, but each worker has finite memory, bandwidth and file descriptors.

    Timeout
    30 s
    Redirects
    5 at most
    Unpacked
    20 MB at most

    What changed

    • example.org → 203.0.113.7, public
    • GET /guide: 200 · 180 KB
  2. Another response from example.org is a 10 MB gzip body. Gzip can expand about 1,000 times, so it would become 10 GB in memory. The Fetch worker caps the decompressed size, not just the transfer: at 20 MB it closes the stream and records why.

    Gzip can expand
    about 1,000×
    Cap
    20 MB unpacked

    What changed

    • 10 MB gzip response
    • Stream closed at 20 MB unpacked
    • Reason recorded
  3. A public page on example.org answers 301 to 169.254.169.254, where cloud instances read metadata, which may include credentials. That is SSRF, server-side request forgery: an attacker gets your server to call addresses it can reach and they cannot. The Address check validates the new destination and refuses it.

    Redirect to
    169.254.169.254
    Serves
    instance metadata

    What changed

    • 301 → 169.254.169.254
    • Redirect target checked
    • Hop refused before connecting
  4. Next, example.net resolves to 203.0.113.9, a public address, so a check at that moment passes. Moments later the name resolves to 169.254.169.254. The Address check validates the address actually connected to, not the earlier answer, so it blocks this connection too.

    At check
    203.0.113.9
    At connect
    169.254.169.254

    What changed

    • New fetch: example.net
    • example.net now resolves to 169.254.169.254
    • Connection blocked at the Address check
  5. The Fetch worker hands the 180 KB of HTML from /guide to the Parser. Real pages are often malformed, so it is a tolerant parser, not a regular expression. It finds 40 links, resolves them against the final URL, and drops unsupported schemes such as mailto:.

    Links resolved
    final URL, valid base
    Kept
    status, type, fetch time

    What changed

    • Parser
    • 40 links found
    • Unsupported schemes dropped
  6. Some pages show content only after JavaScript runs. If one is in scope, the Parser sends it to the Renderer, a headless browser that uses far more CPU and memory than parsing. Google renders pages with headless Chromium in a stage after crawling. Don’t render every page by default.

    Renderer
    headless browser
    Default
    parse, don’t render

    What changed

    • JS-rendered page to the Renderer
    • Far more CPU and memory
    • Other pages only parsed

Space plays · ← → step · Home resets

THE IDEA

How it works

Use DNS and HTTP clients with timeouts, redirect limits, response-size caps, accepted content types, and bounded decompression. A gzip response can expand about 1,000 times, so a 10 MB download may become 10 GB in memory; cap the decompressed size, not just the transfer. Set a clear crawler identity, track response status, and close sockets and streams on failure. Concurrency helps throughput, but each worker has finite memory, bandwidth, and file descriptors.

Validate destinations after every redirect and DNS resolution. This defends against SSRF (server-side request forgery), where an attacker gets your server to call addresses it can reach and they cannot. A public page could redirect to 169.254.169.254, the address where cloud instances read metadata, which may include credentials. Check the address actually connected to, because a hostname can resolve to a public address at check time and a private one moments later.

Parse HTML with a tolerant parser, not a regular expression, because real pages are often malformed. Resolve relative links using the final document URL and any valid base URL, filter unsupported schemes, and keep metadata such as status, content type, fetch time, and canonical hints. JavaScript-rendered pages require a separate, more expensive rendering path if they are in scope: a headless browser uses far more CPU and memory than parsing the same page. Google renders pages with headless Chromium in a stage after crawling. Do not render every page by default.

DECISION POINT

A fetched public URL redirects to a private IP address. What should the downloader do?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Bound redirects, time, bytes, decompression, and destinations.
  • HTML parsing and browser rendering have different costs and coverage.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 2
RecallQUESTION 01

Which bound protects a downloader from a decompression bomb?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 2 checkpoints answered correctly to complete this lesson.