Ensure a trailing slash on subpath proxy

This commit is contained in:
Asher 2020-03-31 12:59:07 -05:00
parent e68d72c4d6
commit 561b6343c8
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 14 additions and 5 deletions

View File

@ -83,7 +83,7 @@ Now you can browse to `<port>.coder.com`. Note that this uses the host header so
ensure your reverse proxy forwards that information if you are using one.
### Sub-paths
Just browse to `/proxy/<port>`.
Just browse to `/proxy/<port>/`.
## x86 releases?

View File

@ -30,15 +30,24 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
request: http.IncomingMessage,
response: http.ServerResponse,
): Promise<HttpResponse> {
const isRoot = !route.requestPath || route.requestPath === "/index.html"
if (!this.authenticated(request)) {
// Only redirect from the root. Other requests get an unauthorized error.
if (route.requestPath && route.requestPath !== "/index.html") {
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
if (isRoot) {
return { redirect: "/login", query: { to: route.fullPath } }
}
return { redirect: "/login", query: { to: route.fullPath } }
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
}
const payload = this.doProxy(route.requestPath, request, response, route.base.replace(/^\//, ""))
// Ensure there is a trailing slash so relative paths work correctly.
const base = route.base.replace(/^\//, "")
if (isRoot && !route.originalPath.endsWith("/")) {
return {
redirect: `/proxy/${base}/`,
}
}
const payload = this.doProxy(route.requestPath, request, response, base)
if (payload) {
return payload
}