Create helper for determining if route is the root
This commit is contained in:
parent
74a0bacdcf
commit
411c61fb02
|
@ -43,8 +43,7 @@ export class ApiHttpProvider extends HttpProvider {
|
||||||
|
|
||||||
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
||||||
this.ensureAuthenticated(request)
|
this.ensureAuthenticated(request)
|
||||||
// Only serve root pages.
|
if (!this.isRoot(route)) {
|
||||||
if (route.requestPath && route.requestPath !== "/index.html") {
|
|
||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,7 @@ export class DashboardHttpProvider extends HttpProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
||||||
// Only serve root pages.
|
if (!this.isRoot(route)) {
|
||||||
if (route.requestPath && route.requestPath !== "/index.html") {
|
|
||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,7 @@ interface LoginPayload {
|
||||||
*/
|
*/
|
||||||
export class LoginHttpProvider extends HttpProvider {
|
export class LoginHttpProvider extends HttpProvider {
|
||||||
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
||||||
// Only serve root pages and only if password authentication is enabled.
|
if (this.options.auth !== AuthType.Password || !this.isRoot(route)) {
|
||||||
if (this.options.auth !== AuthType.Password || (route.requestPath && route.requestPath !== "/index.html")) {
|
|
||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
}
|
}
|
||||||
switch (route.base) {
|
switch (route.base) {
|
||||||
|
|
|
@ -41,10 +41,8 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
|
||||||
request: http.IncomingMessage,
|
request: http.IncomingMessage,
|
||||||
response: http.ServerResponse,
|
response: http.ServerResponse,
|
||||||
): Promise<HttpResponse> {
|
): Promise<HttpResponse> {
|
||||||
const isRoot = !route.requestPath || route.requestPath === "/index.html"
|
|
||||||
if (!this.authenticated(request)) {
|
if (!this.authenticated(request)) {
|
||||||
// Only redirect from the root. Other requests get an unauthorized error.
|
if (this.isRoot(route)) {
|
||||||
if (isRoot) {
|
|
||||||
return { redirect: "/login", query: { to: route.fullPath } }
|
return { redirect: "/login", query: { to: route.fullPath } }
|
||||||
}
|
}
|
||||||
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
||||||
|
@ -53,7 +51,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
|
||||||
// Ensure there is a trailing slash so relative paths work correctly.
|
// Ensure there is a trailing slash so relative paths work correctly.
|
||||||
const port = route.base.replace(/^\//, "")
|
const port = route.base.replace(/^\//, "")
|
||||||
const base = `${this.options.base}/${port}`
|
const base = `${this.options.base}/${port}`
|
||||||
if (isRoot && !route.fullPath.endsWith("/")) {
|
if (this.isRoot(route) && !route.fullPath.endsWith("/")) {
|
||||||
return {
|
return {
|
||||||
redirect: `${base}/`,
|
redirect: `${base}/`,
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,8 +61,7 @@ export class UpdateHttpProvider extends HttpProvider {
|
||||||
this.ensureAuthenticated(request)
|
this.ensureAuthenticated(request)
|
||||||
this.ensureMethod(request)
|
this.ensureMethod(request)
|
||||||
|
|
||||||
// Only serve root pages.
|
if (!this.isRoot(route)) {
|
||||||
if (route.requestPath && route.requestPath !== "/index.html") {
|
|
||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,8 +128,7 @@ export class VscodeHttpProvider extends HttpProvider {
|
||||||
|
|
||||||
switch (route.base) {
|
switch (route.base) {
|
||||||
case "/":
|
case "/":
|
||||||
// Only serve this at the root.
|
if (!this.isRoot(route)) {
|
||||||
if (route.requestPath && route.requestPath !== "/index.html") {
|
|
||||||
throw new HttpError("Not found", HttpCode.NotFound)
|
throw new HttpError("Not found", HttpCode.NotFound)
|
||||||
} else if (!this.authenticated(request)) {
|
} else if (!this.authenticated(request)) {
|
||||||
return { redirect: "/login", query: { to: this.options.base } }
|
return { redirect: "/login", query: { to: this.options.base } }
|
||||||
|
|
|
@ -359,6 +359,14 @@ export abstract class HttpProvider {
|
||||||
}
|
}
|
||||||
return cookies as T
|
return cookies as T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the route is for the root page. For example /base, /base/,
|
||||||
|
* or /base/index.html but not /base/path or /base/file.js.
|
||||||
|
*/
|
||||||
|
protected isRoot(route: Route): boolean {
|
||||||
|
return !route.requestPath || route.requestPath === "/index.html"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue