Skip to content

Commit cd54aec

Browse files
committed
Fix login redirect when not using https
1 parent 078af59 commit cd54aec

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/server.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ export abstract class Server {
111111
protected readonly server: http.Server | https.Server;
112112
protected rootPath = path.resolve(__dirname, "../../../..");
113113
private listenPromise: Promise<string> | undefined;
114+
private readonly protocol: string;
114115

115116
public constructor(public readonly options: ServerOptions) {
117+
this.protocol = this.options.allowHttp ? "http" : "https";
116118
if (this.options.cert && this.options.certKey) {
117119
useHttpsTransformer();
118120
const httpolyglot = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/httpolyglot/lib/index")) as typeof import("httpolyglot");
@@ -153,7 +155,7 @@ export abstract class Server {
153155
: (address.address === "::" ? "localhost" : address.address)
154156
) + ":" + address.port
155157
: address;
156-
return `${this.options.allowHttp ? "http" : "https"}://${endpoint}`;
158+
return `${this.protocol}://${endpoint}`;
157159
}
158160

159161
protected abstract handleRequest(
@@ -173,7 +175,9 @@ export abstract class Server {
173175
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
174176
"Cache-Control": "max-age=86400", // TODO: ETag?
175177
"Content-Type": getMediaMime(payload.filePath),
176-
...(payload.redirect ? { Location: payload.redirect } : {}),
178+
...(payload.redirect ? {
179+
Location: `${this.protocol}://${request.headers.host}${payload.redirect}`,
180+
} : {}),
177181
...payload.headers,
178182
});
179183
response.end(payload.content);
@@ -189,7 +193,7 @@ export abstract class Server {
189193
private async preHandleRequest(request: http.IncomingMessage): Promise<Response> {
190194
const secure = (request.connection as tls.TLSSocket).encrypted;
191195
if (!this.options.allowHttp && !secure) {
192-
return { redirect: "https://" + request.headers.host + request.url };
196+
return { redirect: request.url };
193197
}
194198

195199
const parsedUrl = url.parse(request.url || "", true);
@@ -215,7 +219,7 @@ export abstract class Server {
215219
if (requestPath === "/favicon.ico") {
216220
return this.getResource(path.join(this.rootPath, "/out/vs/server/src/favicon", requestPath));
217221
} else if (!this.authenticate(request)) {
218-
return { redirect: "https://" + request.headers.host + "/login" };
222+
return { redirect: "/login" };
219223
}
220224
break;
221225
case "/login":
@@ -240,13 +244,13 @@ export abstract class Server {
240244
private async tryLogin(request: http.IncomingMessage): Promise<Response> {
241245
if (this.authenticate(request)) {
242246
this.ensureGet(request);
243-
return { redirect: "https://" + request.headers.host + "/" };
247+
return { redirect: "/" };
244248
}
245249
if (request.method === "POST") {
246250
const data = await this.getData<LoginPayload>(request);
247251
if (this.authenticate(request, data)) {
248252
return {
249-
redirect: "https://" + request.headers.host + "/",
253+
redirect: "/",
250254
headers: {"Set-Cookie": `password=${data.password}` }
251255
};
252256
}
@@ -384,12 +388,7 @@ export class MainServer extends Server {
384388
case "/node_modules":
385389
case "/out":
386390
return this.getResource(path.join(this.rootPath, base, requestPath));
387-
// TODO: this setup means you can't request anything from the root if it
388-
// starts with /node_modules or /out, although that's probably low risk.
389-
// There doesn't seem to be a really good way to solve this since some
390-
// resources are requested by the browser (like the extension icon) and
391-
// some by the file provider (like the extension README). Maybe add a
392-
// /resource prefix and a file provider that strips that prefix?
391+
// TODO: make this a /resources endpoint instead. Will require patching?
393392
default: return this.getResource(path.join(base, requestPath));
394393
}
395394
}

0 commit comments

Comments
 (0)