From fb74ef0ed87ecfc36a415a2d272fdcbae4ee4bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Mar=C3=A9chal?= Date: Wed, 12 Jan 2022 13:49:20 -0500 Subject: [PATCH] core: properly handle localhost uris on Electron (#10590) When running Electron `window.location` is not a standard HTTP(S) URL, so we cannot use it to determine the host where the backend is running. Resolve the remote host to `localhost` when running Electron. --- .../core/src/browser/external-uri-service.ts | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/core/src/browser/external-uri-service.ts b/packages/core/src/browser/external-uri-service.ts index e949e8ad5fd4b..35f03c6ddcdb5 100644 --- a/packages/core/src/browser/external-uri-service.ts +++ b/packages/core/src/browser/external-uri-service.ts @@ -14,11 +14,17 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ +import { environment } from '@theia/application-package/lib/environment'; import { injectable } from 'inversify'; -import URI from '../common/uri'; import { MaybePromise } from '../common/types'; +import URI from '../common/uri'; import { Endpoint } from './endpoint'; +export interface AddressPort { + address: string + port: number +} + @injectable() export class ExternalUriService { @@ -31,29 +37,20 @@ export class ExternalUriService { * Use `parseLocalhost` to retrieve localhost address and port information. */ resolve(uri: URI): MaybePromise { - const localhost = this.parseLocalhost(uri); - if (localhost) { - return this.toRemoteUrl(uri, localhost); + const address = this.parseLocalhost(uri); + if (address) { + return this.toRemoteUrl(uri, address); } return uri; } - protected toRemoteUrl(uri: URI, localhost: { address: string, port: number }): URI { - const host = this.toRemoteHost(localhost); - return new Endpoint({ host }).getRestUrl().withPath(uri.path).withFragment(uri.fragment).withQuery(uri.query); - } - - protected toRemoteHost(localhost: { address: string, port: number }): string { - return `${window.location.hostname}:${localhost.port}`; - } - - parseLocalhost(uri: URI): { address: string, port: number } | undefined { + parseLocalhost(uri: URI): AddressPort | undefined { if (uri.scheme !== 'http' && uri.scheme !== 'https') { - return undefined; + return; } const localhostMatch = /^(localhost|127\.0\.0\.1|0\.0\.0\.0):(\d+)$/.exec(uri.authority); if (!localhostMatch) { - return undefined; + return; } return { address: localhostMatch[1], @@ -61,4 +58,22 @@ export class ExternalUriService { }; } + protected toRemoteUrl(uri: URI, address: AddressPort): URI { + return new Endpoint({ host: this.toRemoteHost(address) }) + .getRestUrl() + .withPath(uri.path) + .withFragment(uri.fragment) + .withQuery(uri.query); + } + + protected toRemoteHost(address: AddressPort): string { + return `${this.getRemoteHost()}:${address.port}`; + } + + /** + * @returns The remote host (where the backend is running). + */ + protected getRemoteHost(): string { + return environment.electron.is() ? 'localhost' : window.location.hostname; + } }