v157 · url host parsing

Do the parsers agree?

A browser has more than one place that turns a string into a host: the URL constructor, an anchor element's href, the Request constructor that fetch() uses, and the WebSocket constructor. When they disagree about which host a string names, a validator and a fetcher can be looking at different servers. That is why this shipped — and it is checkable, right here.

Run one string through four parsers

Each parser's view of the same string
parseroutcomehost it resolved

code path

// Four entry points, one host parser underneath. The WebSocket and
// Request constructors are the interesting ones: they throw on a bad
// URL rather than quietly normalising it, so they were the tests that
// Chromium failed before this change.
new URL(candidate).host;
Object.assign(document.createElement("a"), { href: candidate }).host;
new Request(candidate).url;
new WebSocket(candidate);   // wss:// and ws:// only

see also