Skip to content

Commit 601f57a

Browse files
committed
Make redirects consistent
They will preserve the trailing slash if there is one.
1 parent ecdbe0f commit 601f57a

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/node/http.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export const relativeRoot = (originalUrl: string): string => {
139139
}
140140

141141
/**
142-
* Redirect relatively to `/${to}`. Query variables on the current URI will be preserved.
143-
* `to` should be a simple path without any query parameters
142+
* Redirect relatively to `/${to}`. Query variables on the current URI will be
143+
* preserved. `to` should be a simple path without any query parameters
144144
* `override` will merge with the existing query (use `undefined` to unset).
145145
*/
146146
export const redirect = (
@@ -288,3 +288,10 @@ export const getCookieOptions = (req: express.Request): express.CookieOptions =>
288288
sameSite: "lax",
289289
}
290290
}
291+
292+
/**
293+
* Return the full path to the current page, preserving any trailing slash.
294+
*/
295+
export const self = (req: express.Request): string => {
296+
return normalize(`${req.baseUrl}${req.originalUrl.endsWith("/") ? "/" : ""}`, true)
297+
}

src/node/routes/domainProxy.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Request, Router } from "express"
22
import { HttpCode, HttpError } from "../../common/http"
3-
import { normalize } from "../../common/util"
4-
import { authenticated, ensureAuthenticated, redirect } from "../http"
3+
import { authenticated, ensureAuthenticated, redirect, self } from "../http"
54
import { proxy } from "../proxy"
65
import { Router as WsRouter } from "../wsRouter"
76

@@ -56,7 +55,7 @@ router.all("*", async (req, res, next) => {
5655
return next()
5756
}
5857
// Redirect all other pages to the login.
59-
const to = normalize(`${req.baseUrl}${req.path}`)
58+
const to = self(req)
6059
return redirect(req, res, "login", {
6160
to: to !== "/" ? to : undefined,
6261
})

src/node/routes/pathProxy.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as path from "path"
33
import * as qs from "qs"
44
import * as pluginapi from "../../../typings/pluginapi"
55
import { HttpCode, HttpError } from "../../common/http"
6-
import { normalize } from "../../common/util"
7-
import { authenticated, ensureAuthenticated, redirect } from "../http"
6+
import { authenticated, ensureAuthenticated, redirect, self } from "../http"
87
import { proxy as _proxy } from "../proxy"
98

109
const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
@@ -25,7 +24,7 @@ export function proxy(
2524
if (!authenticated(req)) {
2625
// If visiting the root (/:port only) redirect to the login page.
2726
if (!req.params[0] || req.params[0] === "/") {
28-
const to = normalize(`${req.baseUrl}${req.path}`)
27+
const to = self(req)
2928
return redirect(req, res, "login", {
3029
to: to !== "/" ? to : undefined,
3130
})

src/node/routes/vscode.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { WebsocketRequest } from "../../../typings/pluginapi"
44
import { logError } from "../../common/util"
55
import { toVsCodeArgs } from "../cli"
66
import { isDevMode } from "../constants"
7-
import { ensureAuthenticated, authenticated, redirect } from "../http"
7+
import { authenticated, ensureAuthenticated, redirect, self } from "../http"
88
import { loadAMDModule, readCompilationStats } from "../util"
99
import { Router as WsRouter } from "../wsRouter"
1010
import { errorHandler } from "./errors"
@@ -25,9 +25,9 @@ export class CodeServerRouteWrapper {
2525
const isAuthenticated = await authenticated(req)
2626

2727
if (!isAuthenticated) {
28+
const to = self(req)
2829
return redirect(req, res, "login", {
29-
// req.baseUrl can be blank if already at the root.
30-
to: req.baseUrl && req.baseUrl !== "/" ? req.baseUrl : undefined,
30+
to: to !== "/" ? to : undefined,
3131
})
3232
}
3333

@@ -46,9 +46,8 @@ export class CodeServerRouteWrapper {
4646
(query.folder || query.workspace) &&
4747
!req.args["ignore-last-opened"] // This flag disables this behavior.
4848
) {
49-
// Redirect to the same page but with the query parameters attached
50-
// (preserving the trailing slash if any).
51-
return redirect(req, res, req.baseUrl + (req.originalUrl.endsWith("/") ? "/" : ""), {
49+
const to = self(req)
50+
return redirect(req, res, to, {
5251
folder: query.folder,
5352
workspace: query.workspace,
5453
})

0 commit comments

Comments
 (0)