2020-03-23 18:47:01 +00:00
|
|
|
import * as http from "http"
|
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-04-02 18:09:09 +00:00
|
|
|
import { HttpProvider, HttpResponse, Route, WsResponse } from "../http"
|
2020-03-31 19:56:01 +00:00
|
|
|
|
2020-03-23 18:47:01 +00:00
|
|
|
/**
|
|
|
|
* Proxy HTTP provider.
|
|
|
|
*/
|
2020-04-02 18:09:09 +00:00
|
|
|
export class ProxyHttpProvider extends HttpProvider {
|
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
2020-03-23 19:26:47 +00:00
|
|
|
if (!this.authenticated(request)) {
|
2020-04-01 16:28:09 +00:00
|
|
|
if (this.isRoot(route)) {
|
2020-03-31 17:59:07 +00:00
|
|
|
return { redirect: "/login", query: { to: route.fullPath } }
|
2020-03-23 19:26:47 +00:00
|
|
|
}
|
2020-03-31 17:59:07 +00:00
|
|
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
2020-03-23 18:47:01 +00:00
|
|
|
}
|
2020-03-23 19:26:47 +00:00
|
|
|
|
2020-03-31 17:59:07 +00:00
|
|
|
// Ensure there is a trailing slash so relative paths work correctly.
|
2020-04-01 16:28:09 +00:00
|
|
|
if (this.isRoot(route) && !route.fullPath.endsWith("/")) {
|
2020-03-31 17:59:07 +00:00
|
|
|
return {
|
2020-04-02 16:32:19 +00:00
|
|
|
redirect: `${route.fullPath}/`,
|
2020-03-31 17:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:32:19 +00:00
|
|
|
const port = route.base.replace(/^\//, "")
|
2020-04-02 18:09:09 +00:00
|
|
|
return {
|
|
|
|
proxy: {
|
|
|
|
base: `${this.options.base}/${port}`,
|
|
|
|
port,
|
|
|
|
},
|
2020-03-23 18:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:09:09 +00:00
|
|
|
public async handleWebSocket(route: Route, request: http.IncomingMessage): Promise<WsResponse> {
|
2020-03-23 23:02:31 +00:00
|
|
|
this.ensureAuthenticated(request)
|
2020-03-31 19:56:01 +00:00
|
|
|
const port = route.base.replace(/^\//, "")
|
2020-04-02 18:09:09 +00:00
|
|
|
return {
|
|
|
|
proxy: {
|
|
|
|
base: `${this.options.base}/${port}`,
|
|
|
|
port,
|
|
|
|
},
|
2020-03-23 23:02:31 +00:00
|
|
|
}
|
2020-03-23 18:47:01 +00:00
|
|
|
}
|
|
|
|
}
|