RFC 9110 HTTP Content Negotiation Lab

Hyper Content Negotiation Lab

Returning full styled HTML pages to automated readers wastes tokens on navigation chrome and forces brittle scraping. Test how one canonical URI serves text/html, text/markdown, application/ld+json, and application/json using standard Accept negotiation with Vary: Accept and alternate Link headers.

Hypothesis

Standard RFC 9110 Accept negotiation (Vary: Accept) on a single canonical URL shrinks automated retrieval payloads significantly without breaking browser HTML or duplicating URL trees.

Confirm signal

text/markdown and application/ld+json measure 50%–85% fewer bytes and estimated tokens than full text/html while preserving Vary: Accept and alternate Link discovery headers.

Falsify signal

Fine-grained custom headers (such as Accept-Prompt) collapse shared CDN caches under Vary and expose user preferences on the first request.

Quick Experiments

Run live requests against the Worker endpoints and inspect exact wire headers.

Interactive Negotiation Workbench

Ready

1. Canonical Resource or Remote HTTPS URL

Select a built-in /docs/:slug resource or fetch any public https:// page through the SSRF-guarded /negotiate endpoint.

2. HTTP Accept & Optional Accept-Prompt Header

Send a real Accept header via fetch() to inspect RFC 9110 q-weight matching, or attach an experimental Accept-Prompt header to test cache-key fragmentation.

Built-in Canonical Resource

Loading resource...

Representation Payload Comparison

Comparing UTF-8 bytes, gzip bytes, and estimated tokens (~chars/4) against the HTML baseline.

Measuring payload reduction and cache-key behavior...

Cache-Key Cardinality & Fingerprinting Check

Vary: Accept (4 bounded keys)

Coarse RFC 9110 Accept negotiation keeps shared CDN caches bounded to 4 canonical variants (html, markdown, jsonld, json).

Negotiated Representation Body text/markdown 0 B · 0 tokens
GET /docs/hyper-content-negotiation
Loading...
Live HTTP Response Headers & RFC 9110 Match HTTP 200 Vary: Accept
Matched rule: Accept negotiation
Loading headers...
Ready-to-Run curl Commands CLI 4 media types
Live Network Wire Telemetry Log 0 requests Observed fetch() headers
No requests recorded yet.

Canonical Negotiation Flow

HTTP Content Negotiation Architecture A single canonical URI (/docs/hyper-content-negotiation) or SSRF-guarded /negotiate proxy passes through an RFC 9110 Accept parser with Vary: Accept and branches into HTML, Markdown, and JSON-LD representations. SINGLE CANONICAL URI GET /docs/hyper-content-negotiation Accept: <media-range>;q=... REMOTE PROXY (SSRF-GUARDED) GET /negotiate?url=https://... HTTPS only · 512 KiB cap · 5s · 2 hops CLOUDFLARE WORKER EDGE RFC 9110 Negotiator parseAcceptHeader(Accept, ?format) Vary: Accept Link: <?format=markdown>; rel=alternate Content-Type: text/html Browser Document Styled layout, nav chrome, JSON-LD head Content-Type: text/markdown Automated Text Reader (~80% smaller) YAML frontmatter + headings + links Content-Type: application/ld+json Typed Schema.org Article Graph Structured entities, citations, sections
Figure 1: One canonical path branches on RFC 9110 Accept precedence and emits Vary: Accept so shared caches store separate entries per media representation without exploding cache keys on custom prompt headers.
How This Is Set Up

Implementation & Wire Contract

One Cloudflare Worker serves styled HTML, compact Markdown, Schema.org JSON-LD, and JSON from /docs/:slug and /negotiate using the RFC 9110 parser in src/negotiate.ts, emitting Vary: Accept and alternate Link headers.

Without content negotiation
// Scraping styled HTML or maintaining parallel format-specific URL trees
const html = await fetch("https://site.example/docs/architecture").then((r) => r.text());
const doc = new DOMParser().parseFromString(html, "text/html");
// Brittle selectors pull navigation, footers, and chrome alongside prose
const body = doc.querySelector("main article")?.textContent ?? "";

// Or the origin duplicates routes per format:
// GET /docs/architecture         -> HTML
// GET /api/v1/architecture.md    -> Markdown
// GET /api/v1/architecture.json  -> JSON
With RFC 9110 Accept negotiation
// One canonical URI (/docs/:slug) responds according to Accept q-values
const mdRes = await fetch("/docs/hyper-content-negotiation", {
  headers: { Accept: "text/markdown, text/html;q=0.8" },
});
// Content-Type: text/markdown; charset=utf-8
// Vary: Accept
// Link: </docs/...?format=markdown>; rel="alternate"; type="text/markdown", ...
const markdown = await mdRes.text();

const ldRes = await fetch("/docs/hyper-content-negotiation", {
  headers: { Accept: "application/ld+json" },
});
const graph = await ldRes.json();
Complete Page & Worker Wiring (public/index.html + src/negotiate.ts + src/worker.ts)

Matches the live Worker routes in src/worker.ts, the SUPPORTED_MEDIA_TYPES precedence table in src/negotiate.ts, and the browser fetch() calls in public/app.js.

<!-- 1. Page HTML Setup (public/index.html) -->
<form id="accept-form">
  <input id="accept-input" name="accept" type="text" value="text/markdown, text/html;q=0.8" required>
  <input id="accept-prompt-input" name="acceptPrompt" type="text" placeholder="Optional Accept-Prompt">
  <button type="submit">Send Request</button>
</form>
<pre id="wire-body-view"><code id="wire-body-code">Loading...</code></pre>
<script type="module" src="/app.js"></script>

// 2. Media-Type Precedence Table (src/negotiate.ts)
export const FORMAT_CONTENT_TYPES = {
  html: "text/html; charset=utf-8",
  markdown: "text/markdown; charset=utf-8",
  jsonld: "application/ld+json; charset=utf-8",
  json: "application/json; charset=utf-8",
};
// parseAcceptHeader() ranks candidates by (b.q - a.q) || (b.specificity - a.specificity) || (a.serverPriority - b.serverPriority)

// 3. Edge Route & Coarse Cache Policy (src/worker.ts -> handleDocsRoute)
const negotiation = parseAcceptHeader(request.headers.get("Accept"), url.searchParams.get("format"));
const rendered = renderRepresentation(doc, negotiation.format, canonicalUrl);
const cacheAnalysis = analyzeCacheKeyImpact(request.headers.get("Accept-Prompt"));
const headers = new Headers({
  "Content-Type": rendered.contentType,
  Vary: "Accept", // Never vary on free-form Accept-Prompt strings
  "X-Negotiated-Format": rendered.format,
  "X-Negotiation-Acceptable": String(negotiation.acceptable),
  "X-Matched-Accept-Range": negotiation.matchedRange,
  "X-Cache-Key-Strategy": "coarse-accept (4 variants)",
  Link: buildAlternateLinkHeader(canonicalUrl),
});
if (cacheAnalysis.promptProvided) {
  headers.set("X-Accept-Prompt-Status", cacheAnalysis.promptPolicy);
}
What disappears

Fragile HTML scraping selectors and parallel /api/*.md endpoint trees that split canonical URLs and duplicate indexing.

What becomes explicit

Client media-type priority via Accept (q-weights and specificity), bounded CDN cache keys via Vary: Accept, and machine-readable alternates via Link.

Platform constraints

Browser address bars send a fixed Accept: text/html,... header, and free-form headers like Accept-Prompt cannot safely enter Vary without collapsing shared CDN cache hit rates.