From 0ab5603d3221b41c84e4e04a920e7b1547e95a5f Mon Sep 17 00:00:00 2001 From: Charles Brubaker Date: Sat, 18 May 2024 17:14:29 -0400 Subject: [PATCH] fix: Connection.resolveExternalWSUri should return ws or wss schema --- src/connectionInfo/connection.ts | 6 ++++-- src/editorPreview/webviewComm.ts | 2 +- src/server/serverUtils/HTMLInjector.ts | 4 +--- src/test/suite/connectionInfo.test.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/connectionInfo/connection.ts b/src/connectionInfo/connection.ts index 7e8a8bb2..1115eb23 100644 --- a/src/connectionInfo/connection.ts +++ b/src/connectionInfo/connection.ts @@ -102,8 +102,10 @@ export class Connection extends Disposable { if (!wsPath) { wsPath = this.wsPath; } - const wsPortUri = this.constructLocalUri(wsPort); - return vscode.Uri.joinPath(await vscode.env.asExternalUri(wsPortUri),wsPath); // ensure that this pathname is retained, as the websocket server must see this in order to authorize + const httpPortUri = this.constructLocalUri(wsPort); + const externalHttpUri = await vscode.env.asExternalUri(httpPortUri); + const externalWsUri = externalHttpUri.with({scheme : externalHttpUri.scheme === 'https' ? 'wss' : 'ws'}); + return vscode.Uri.joinPath(externalWsUri, wsPath); // ensure that this pathname is retained, as the websocket server must see this in order to authorize } /** diff --git a/src/editorPreview/webviewComm.ts b/src/editorPreview/webviewComm.ts index f2a57c24..e20f2d2d 100644 --- a/src/editorPreview/webviewComm.ts +++ b/src/editorPreview/webviewComm.ts @@ -132,7 +132,7 @@ export class WebviewComm extends Disposable { this._panel.webview.html = this._getHtmlForWebview( webview, url, - `ws://${wsURI.authority}${wsURI.path}`, + `${wsURI.scheme}://${wsURI.authority}${wsURI.path}`, `${httpHost.scheme}://${httpHost.authority}` ); diff --git a/src/server/serverUtils/HTMLInjector.ts b/src/server/serverUtils/HTMLInjector.ts index 170668e5..6b5abb3d 100644 --- a/src/server/serverUtils/HTMLInjector.ts +++ b/src/server/serverUtils/HTMLInjector.ts @@ -75,9 +75,7 @@ export class HTMLInjector extends Disposable { wsUri = await this._connection.resolveExternalWSUri(); } - // if the HTTP scheme uses SSL, the WS scheme must also use SSL - const wsURL = `${httpUri.scheme === 'https' ? 'wss' : 'ws'}://${wsUri.authority - }${wsUri.path}`; + const wsURL = `${wsUri.scheme}://${wsUri.authority}${wsUri.path}`; let httpURL = `${httpUri.scheme}://${httpUri.authority}`; if (httpURL.endsWith('/')) { diff --git a/src/test/suite/connectionInfo.test.ts b/src/test/suite/connectionInfo.test.ts index 72b5dc5b..4c303ea8 100644 --- a/src/test/suite/connectionInfo.test.ts +++ b/src/test/suite/connectionInfo.test.ts @@ -55,7 +55,7 @@ describe('ConnectionInfo', () => { assert.ok(target.calledOnce); const httpUri = vscode.Uri.parse('http://127.0.0.1:3000'); - const wsUri = vscode.Uri.parse('http://127.0.0.1:3001/1234'); + const wsUri = vscode.Uri.parse('ws://127.0.0.1:3001/1234'); assert.deepStrictEqual([ { httpURI: httpUri, @@ -103,7 +103,7 @@ describe('ConnectionInfo', () => { await connection.connected(); assert(connection.workspace === undefined); const httpUri = vscode.Uri.parse('http://127.0.0.1:3000'); - const wsUri = vscode.Uri.parse('http://127.0.0.1:3001/1234'); + const wsUri = vscode.Uri.parse('ws://127.0.0.1:3001/1234'); assert.ok(target.calledOnce); assert.deepStrictEqual([