From e51852d0fc91c10b8eac641d137d10c9348589ec Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Wed, 4 Apr 2018 21:43:25 -0700 Subject: [PATCH] Use Anchor tag to normalize urls(#1828) --- clients/ts/signalr/src/HttpConnection.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/clients/ts/signalr/src/HttpConnection.ts b/clients/ts/signalr/src/HttpConnection.ts index 4f6fb08152..e8c489828f 100644 --- a/clients/ts/signalr/src/HttpConnection.ts +++ b/clients/ts/signalr/src/HttpConnection.ts @@ -274,20 +274,16 @@ export class HttpConnection implements IConnection { throw new Error(`Cannot resolve '${url}'.`); } - const parser = window.document.createElement("a"); - parser.href = url; + // Setting the url to the href propery of an anchor tag handles normalization + // for us. There are 3 main cases. + // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b" + // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b" + // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b" + const aTag = window.document.createElement("a"); + aTag.href = url; - const baseUrl = (!parser.protocol || parser.protocol === ":") - ? `${window.document.location.protocol}//${(parser.host || window.document.location.host)}` - : `${parser.protocol}//${parser.host}`; - - if (!url || url[0] !== "/") { - url = "/" + url; - } - - const normalizedUrl = baseUrl + url; - this.logger.log(LogLevel.Information, `Normalizing '${url}' to '${normalizedUrl}'.`); - return normalizedUrl; + this.logger.log(LogLevel.Information, `Normalizing '${url}' to '${aTag.href}'.`); + return aTag.href; } private resolveNegotiateUrl(url: string): string {