Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: fix WHATWG URL host formatting error caused by port string #20493

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Object.defineProperties(URL.prototype, {
}, options);
const ctx = this[context];
var ret = ctx.scheme;
if (ctx.host !== null) {
if (ctx.hostname !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be ctx.host !== null. The hostname change only applies below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, URLContext uses null:

const { URL } = require('url');

const myURL = new URL('https://nodejs.org/en/');

const kContext = Object.getOwnPropertySymbols(myURL)[0];

console.log(myURL[kContext]);
URLContext {
  flags: 400,
  scheme: 'https:',
  username: '',
  password: '',
  host: 'nodejs.org',
  port: null,
  path: [ 'en', '' ],
  query: null,
  fragment: null }

ret += '//';
const has_username = ctx.username !== '';
const has_password = ctx.password !== '';
Expand All @@ -400,7 +400,9 @@ Object.defineProperties(URL.prototype, {
ret += '@';
}
ret += options.unicode ?
domainToUnicode(this.host) : this.host;
domainToUnicode(this.hostname) : this.hostname;
if (ctx.port !== '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be ctx.port !== null?

Copy link
Contributor Author

@peakji peakji May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WHATWG URL API uses '' as the empty value, not null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> new url.URL("https://nodejs.org/en/")
URL {
  href: 'https://nodejs.org/en/',
  origin: 'https://nodejs.org',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'nodejs.org',
  hostname: 'nodejs.org',
  port: '',
  pathname: '/en/',
  search: '',
  searchParams: URLSearchParams {},
  hash: '' }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad! This is the internal context...

ret += `:${ctx.port}`
} else if (ctx.scheme === 'file:') {
ret += '//';
}
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-url-format-whatwg.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ assert.strictEqual(
url.format(myURL, { unicode: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(new URL('http://xn--0zwm56d.com:8080/path'), { unicode: true }),
'http://测试.com:8080/path'
);