Skip to content

Commit 9d868c6

Browse files
committed
url: ensure host setter matches parse for file url
Technically, file URLs are not permitted to have a port. There is currently an ambiguity in the URL spec. In the current spec having a port in a file URL is undefined behavior. In the current implementation, the port is ignored and handled as if it were part of the host name. This will be changing once the ambiguity is resolved in the spec. The spec change may involve either ignoring the port if specified or throwing with an Invalid URL error if the port is specified. For now, this test verifies the currently implemented behavior. Fixes: #10608
1 parent bde26a6 commit 9d868c6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/internal/url.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,9 @@ Object.defineProperties(URL.prototype, {
434434
ctx.flags &= ~binding.URL_FLAGS_HAS_HOST;
435435
return;
436436
}
437-
binding.parse(host, binding.kHost, null, ctx,
437+
const override = ctx.scheme === 'file:' ?
438+
binding.kFileHost : binding.kHost;
439+
binding.parse(host, override, null, ctx,
438440
onParseHostComplete.bind(this));
439441
}
440442
},
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
// Technically, file URLs are not permitted to have a port. There is
4+
// currently an ambiguity in the URL spec. In the current spec
5+
// having a port in a file URL is undefined behavior. In the current
6+
// implementation, the port is ignored and handled as if it were part
7+
// of the host name. This will be changing once the ambiguity is
8+
// resolved in the spec. The spec change may involve either ignoring the
9+
// port if specified or throwing with an Invalid URL error if the port
10+
// is specified. For now, this test verifies the currently implemented
11+
// behavior.
12+
13+
require('../common');
14+
const URL = require('url').URL;
15+
const assert = require('assert');
16+
17+
const u = new URL('file://example.net:80/foo');
18+
19+
assert.strictEqual(u.hostname, 'example.net:80');
20+
assert.strictEqual(u.port, '');
21+
22+
u.host = 'example.com:81';
23+
24+
assert.strictEqual(u.hostname, 'example.com:81');
25+
assert.strictEqual(u.port, '');
26+
27+
u.hostname = 'example.org:81';
28+
29+
assert.strictEqual(u.hostname, 'example.org');
30+
assert.strictEqual(u.port, '');

0 commit comments

Comments
 (0)