Skip to content

Commit 6b579d6

Browse files
committed
Fix webview address when using a proxy
1 parent d4ed2ef commit 6b579d6

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/server.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export abstract class Server {
119119
protected readonly server: http.Server | https.Server;
120120
protected rootPath = path.resolve(__dirname, "../../../..");
121121
private listenPromise: Promise<string> | undefined;
122-
public readonly protocol: string;
122+
public readonly protocol: "http" | "https";
123123
public readonly options: ServerOptions;
124124

125125
public constructor(options: ServerOptions) {
@@ -157,17 +157,12 @@ export abstract class Server {
157157
}
158158

159159
/**
160-
* The local address of the server. If you pass in a request, it will use the
161-
* request's host if listening on a port (rather than a socket). This enables
162-
* setting the webview endpoint to the same host the browser is using.
160+
* The *local* address of the server.
163161
*/
164-
public address(request?: http.IncomingMessage): string {
162+
public address(): string {
165163
const address = this.server.address();
166164
const endpoint = typeof address !== "string"
167-
? (request
168-
? request.headers.host!.split(":", 1)[0]
169-
: (address.address === "::" ? "localhost" : address.address)
170-
) + ":" + address.port
165+
? (address.address === "::" ? "localhost" : address.address) + ":" + address.port
171166
: address;
172167
return `${this.protocol}://${endpoint}`;
173168
}
@@ -189,15 +184,17 @@ export abstract class Server {
189184
return { content: await util.promisify(fs.readFile)(filePath), filePath };
190185
}
191186

187+
protected withBase(request: http.IncomingMessage, path: string): string {
188+
return `${this.protocol}://${request.headers.host}${this.options.basePath}${path}`;
189+
}
190+
192191
private onRequest = async (request: http.IncomingMessage, response: http.ServerResponse): Promise<void> => {
193192
try {
194193
const payload = await this.preHandleRequest(request);
195194
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
196195
"Cache-Control": "max-age=86400", // TODO: ETag?
197196
"Content-Type": getMediaMime(payload.filePath),
198-
...(payload.redirect ? {
199-
Location: `${this.protocol}://${request.headers.host}${this.options.basePath}${payload.redirect}`,
200-
} : {}),
197+
...(payload.redirect ? { Location: this.withBase(request, payload.redirect) } : {}),
201198
...payload.headers,
202199
});
203200
response.end(payload.content);
@@ -464,11 +461,11 @@ export class MainServer extends Server {
464461
]);
465462
const environment = this.services.get(IEnvironmentService) as IEnvironmentService;
466463
const locale = environment.args.locale || await getLocaleFromConfig(environment.userDataPath);
467-
const webviewEndpoint = this.address(request) + "/webview/";
468464
const cwd = process.env.VSCODE_CWD || process.cwd();
469465
const workspacePath = parsedUrl.query.workspace as string | undefined;
470466
const folderPath = !workspacePath ? parsedUrl.query.folder as string | undefined || this.options.folderUri || cwd: undefined;
471467
const remoteAuthority = request.headers.host as string;
468+
const webviewEndpoint = this.withBase(request, "/webview/");
472469
const transformer = getUriTransformer(remoteAuthority);
473470
const options: Options = {
474471
WORKBENCH_WEB_CONGIGURATION: {

0 commit comments

Comments
 (0)