chore: Clean up code related to headers.

This commit is contained in:
Ruben Verborgh
2020-11-22 21:58:19 +01:00
committed by Joachim Van Herwegen
parent f08617b1c9
commit 30ee0f8dc6
5 changed files with 34 additions and 39 deletions

View File

@@ -95,7 +95,7 @@ class WebSocketListener extends EventEmitter {
throw new Error(`Mismatched protocol: ${resolved.protocol} instead of ${this.protocol}`);
}
// Subscribe to the URL
const url = resolved.toString();
const url = resolved.href;
this.subscribedPaths.add(url);
this.sendMessage('ack', url);
this.logger.debug(`WebSocket subscribed to changes on ${url}`);

View File

@@ -13,23 +13,25 @@ import { TargetExtractor } from './TargetExtractor';
export class BasicTargetExtractor extends TargetExtractor {
protected readonly logger = getLoggerFor(this);
public async handle(request: HttpRequest): Promise<ResourceIdentifier> {
if (!request.url) {
public async handle({ url, headers: { host }, connection }: HttpRequest): Promise<ResourceIdentifier> {
if (!url) {
this.logger.error('The request has no URL');
throw new Error('Missing URL');
}
if (!request.headers.host) {
if (!host) {
this.logger.error('The request has no Host header');
throw new Error('Missing Host header');
}
if (/[/\\*]/u.test(host)) {
throw new Error(`The request has an invalid Host header: ${host}`);
}
const isHttps = request.connection && (request.connection as TLSSocket).encrypted;
const isHttps = (connection as TLSSocket)?.encrypted;
this.logger.debug(`Request is using HTTPS: ${isHttps}`);
// URL object applies punycode encoding to domain
const base = `http${isHttps ? 's' : ''}://${request.headers.host}`;
const url = toCanonicalUriPath(request.url);
const path = new URL(url, base).href;
const base = `http${isHttps ? 's' : ''}://${host}`;
const path = new URL(toCanonicalUriPath(url), base).href;
return { path };
}