fix: Correctly parse URL domain

This commit is contained in:
Joachim Van Herwegen 2020-10-16 10:08:37 +02:00
parent 4df26454d4
commit 5fa068687b

View File

@ -1,5 +1,4 @@
import type { TLSSocket } from 'tls'; import type { TLSSocket } from 'tls';
import { format } from 'url';
import type { HttpRequest } from '../../server/HttpRequest'; import type { HttpRequest } from '../../server/HttpRequest';
import { toCanonicalUriPath } from '../../util/Util'; import { toCanonicalUriPath } from '../../util/Util';
import type { ResourceIdentifier } from '../representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../representation/ResourceIdentifier';
@ -23,11 +22,11 @@ export class BasicTargetExtractor extends TargetExtractor {
throw new Error('Missing host.'); throw new Error('Missing host.');
} }
const isHttps = input.connection && (input.connection as TLSSocket).encrypted; const isHttps = input.connection && (input.connection as TLSSocket).encrypted;
const path = format({
protocol: `http${isHttps ? 's' : ''}`, // URL object applies punycode encoding to domain
host: toCanonicalUriPath(input.headers.host), const base = `http${isHttps ? 's' : ''}://${input.headers.host}`;
pathname: toCanonicalUriPath(input.url), const url = toCanonicalUriPath(input.url);
}); const path = new URL(url, base).href;
return { path }; return { path };
} }