From 1230b4a3307725c5e0eb4daae991b2db5f4d1c96 Mon Sep 17 00:00:00 2001 From: Bob Zeleznik Date: Mon, 16 Mar 2020 19:42:20 -0400 Subject: fixed pdf uploading. changed routes to allow web pages to be loaded with relative urls. added a 'text' field to web documents with the page text contents. --- src/server/RouteManager.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/server/RouteManager.ts') diff --git a/src/server/RouteManager.ts b/src/server/RouteManager.ts index 63e957cd1..d8265582e 100644 --- a/src/server/RouteManager.ts +++ b/src/server/RouteManager.ts @@ -2,6 +2,7 @@ import RouteSubscriber from "./RouteSubscriber"; import { DashUserModel } from "./authentication/models/user_model"; import { Request, Response, Express } from 'express'; import { cyan, red, green } from 'colors'; +import { Utils } from "../client/northstar/utils/Utils"; export enum Method { GET, @@ -86,6 +87,7 @@ export default class RouteManager { const { method, subscription, secureHandler, publicHandler, errorHandler } = initializer; const isRelease = this._isRelease; + let redirected = ""; const supervised = async (req: Request, res: Response) => { let { user } = req; const { originalUrl: target } = req; @@ -118,13 +120,33 @@ export default class RouteManager { res.redirect("/login"); } } - setTimeout(() => { - if (!res.headersSent) { - console.log(red(`Initiating fallback for ${target}. Please remove dangling promise from route handler`)); + if (!res.headersSent && req.headers.referer?.includes("corsProxy")) { + const url = decodeURIComponent(req.headers.referer!); + const start = url.match(/.*corsProxy\//)![0]; + const original = url.replace(start, ""); + const theurl = original.match(/http[s]?:\/\/[^\/]*/)![0]; + const newdirect = start + encodeURIComponent(theurl + target); + if (newdirect !== redirected) { + redirected = newdirect; + console.log("redirect relative path: " + (theurl + target)); + res.redirect(redirected); + } + } + else { + if (target.startsWith("/doc/")) { + !res.headersSent && setTimeout(() => { + if (!res.headersSent) { + res.redirect("/login"); + console.log(red(`Initiating fallback for ${target}. Please remove dangling promise from route handler`)); + const warning = `request to ${target} fell through - this is a fallback response`; + res.send({ warning }); + } + }, 1000); + } else { const warning = `request to ${target} fell through - this is a fallback response`; res.send({ warning }); } - }, 1000); + } }; const subscribe = (subscriber: RouteSubscriber | string) => { let route: string; @@ -133,7 +155,7 @@ export default class RouteManager { } else { route = subscriber.build; } - if (!/^\/$|^\/[A-Za-z]+(\/\:[A-Za-z?_]+)*$/g.test(route)) { + if (!/^\/$|^\/[A-Za-z\*]+(\/\:[A-Za-z?_\*]+)*$/g.test(route)) { this.failedRegistrations.push({ reason: RegistrationError.Malformed, route -- cgit v1.2.3-70-g09d2 From e9a16afa46af3ecec0bd7b58f9ca13b85d62a860 Mon Sep 17 00:00:00 2001 From: Bob Zeleznik Date: Tue, 17 Mar 2020 01:12:21 -0400 Subject: fixed up route handling for relative paths. changed search to dashsearch for server request. --- src/client/util/SearchUtil.ts | 6 +++--- src/server/ApiManagers/SearchManager.ts | 2 +- src/server/RouteManager.ts | 36 ++++++++++++++++++++++----------- src/server/index.ts | 3 ++- 4 files changed, 30 insertions(+), 17 deletions(-) (limited to 'src/server/RouteManager.ts') diff --git a/src/client/util/SearchUtil.ts b/src/client/util/SearchUtil.ts index 2d9c807dd..b597f1e07 100644 --- a/src/client/util/SearchUtil.ts +++ b/src/client/util/SearchUtil.ts @@ -34,7 +34,7 @@ export namespace SearchUtil { export function Search(query: string, returnDocs: false, options?: SearchParams): Promise; export async function Search(query: string, returnDocs: boolean, options: SearchParams = {}) { query = query || "*"; //If we just have a filter query, search for * as the query - const rpquery = Utils.prepend("/search"); + const rpquery = Utils.prepend("/dashsearch"); const gotten = await rp.get(rpquery, { qs: { ...options, q: query } }); const result: IdSearchResult = gotten.startsWith("<") ? { ids: [], docs: [], numFound: 0, lines: [] } : JSON.parse(gotten); if (!returnDocs) { @@ -52,7 +52,7 @@ export namespace SearchUtil { const newLines: string[][] = []; await Promise.all(fileids.map(async (tr: string, i: number) => { const docQuery = "fileUpload_t:" + tr.substr(0, 7); //If we just have a filter query, search for * as the query - const docResult = JSON.parse(await rp.get(Utils.prepend("/search"), { qs: { ...options, q: docQuery } })); + const docResult = JSON.parse(await rp.get(Utils.prepend("/dashsearch"), { qs: { ...options, q: docQuery } })); newIds.push(...docResult.ids); newLines.push(...docResult.ids.map((dr: any) => txtresult.lines[i])); })); @@ -121,7 +121,7 @@ export namespace SearchUtil { export async function GetAllDocs() { const query = "*"; - const response = await rp.get(Utils.prepend('/search'), { + const response = await rp.get(Utils.prepend('/dashsearch'), { qs: { start: 0, rows: 10000, q: query }, diff --git a/src/server/ApiManagers/SearchManager.ts b/src/server/ApiManagers/SearchManager.ts index be17c3105..5f7d1cf6d 100644 --- a/src/server/ApiManagers/SearchManager.ts +++ b/src/server/ApiManagers/SearchManager.ts @@ -61,7 +61,7 @@ export class SearchManager extends ApiManager { register({ method: Method.GET, - subscription: "/search", + subscription: "/dashsearch", secureHandler: async ({ req, res }) => { const solrQuery: any = {}; ["q", "fq", "start", "rows", "hl", "hl.fl"].forEach(key => solrQuery[key] = req.query[key]); diff --git a/src/server/RouteManager.ts b/src/server/RouteManager.ts index d8265582e..c88f3bb51 100644 --- a/src/server/RouteManager.ts +++ b/src/server/RouteManager.ts @@ -79,6 +79,7 @@ export default class RouteManager { } } + static routes: string[] = []; /** * * @param initializer @@ -86,11 +87,18 @@ export default class RouteManager { addSupervisedRoute = (initializer: RouteInitializer): void => { const { method, subscription, secureHandler, publicHandler, errorHandler } = initializer; + typeof (initializer.subscription) === "string" && RouteManager.routes.push(initializer.subscription); + initializer.subscription instanceof RouteSubscriber && RouteManager.routes.push(initializer.subscription.root); + initializer.subscription instanceof Array && initializer.subscription.map(sub => { + typeof (sub) === "string" && RouteManager.routes.push(sub); + sub instanceof RouteSubscriber && RouteManager.routes.push(sub.root); + }); const isRelease = this._isRelease; let redirected = ""; const supervised = async (req: Request, res: Response) => { let { user } = req; const { originalUrl: target } = req; + console.log("TARGET: " + target); if (process.env.DB === "MEM" && !user) { user = { id: "guest", email: "", userDocumentId: "guestDocId" }; } @@ -126,26 +134,30 @@ export default class RouteManager { const original = url.replace(start, ""); const theurl = original.match(/http[s]?:\/\/[^\/]*/)![0]; const newdirect = start + encodeURIComponent(theurl + target); - if (newdirect !== redirected) { - redirected = newdirect; - console.log("redirect relative path: " + (theurl + target)); - res.redirect(redirected); - } + console.log("REDIRECT: " + (theurl + target)); + res.redirect(newdirect); } - else { - if (target.startsWith("/doc/")) { - !res.headersSent && setTimeout(() => { + else if (!res.headersSent) { + const which2 = RouteManager.routes.findIndex(r => (r !== "/" || r === target) && target.startsWith(r)); + const which = Array.from(registered.keys()).findIndex(r => (r !== "/" || r === target) && target.startsWith(r)); + console.log("WHICH = " + (which === -1 ? "" : Array.from(registered.keys())[which])); + if (which !== -1) { + setTimeout(() => { + console.log("handled:" + target); if (!res.headersSent) { - res.redirect("/login"); console.log(red(`Initiating fallback for ${target}. Please remove dangling promise from route handler`)); const warning = `request to ${target} fell through - this is a fallback response`; res.send({ warning }); } }, 1000); - } else { - const warning = `request to ${target} fell through - this is a fallback response`; - res.send({ warning }); } + else { + console.log("unhandled:" + target); + res.end(); + } + } else { + console.log("pre-sent:" + target); + res.end(); } }; const subscribe = (subscriber: RouteSubscriber | string) => { diff --git a/src/server/index.ts b/src/server/index.ts index c4c05157a..def36e922 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -117,7 +117,8 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: addSupervisedRoute({ method: Method.GET, subscription: "/*", - secureHandler: ({ res }) => { } + secureHandler: ({ res }) => { + } }); logRegistrationOutcome(); -- cgit v1.2.3-70-g09d2 From 1b21c5234a179df337ad51f55c9f82b9d7022c25 Mon Sep 17 00:00:00 2001 From: Bob Zeleznik Date: Tue, 17 Mar 2020 02:54:53 -0400 Subject: cleaned up relative web page paths. --- src/server/RouteManager.ts | 38 ++++++------------------------------- src/server/index.ts | 6 ------ src/server/server_Initialization.ts | 24 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 39 deletions(-) (limited to 'src/server/RouteManager.ts') diff --git a/src/server/RouteManager.ts b/src/server/RouteManager.ts index c88f3bb51..a8680c0c9 100644 --- a/src/server/RouteManager.ts +++ b/src/server/RouteManager.ts @@ -94,11 +94,9 @@ export default class RouteManager { sub instanceof RouteSubscriber && RouteManager.routes.push(sub.root); }); const isRelease = this._isRelease; - let redirected = ""; const supervised = async (req: Request, res: Response) => { let { user } = req; const { originalUrl: target } = req; - console.log("TARGET: " + target); if (process.env.DB === "MEM" && !user) { user = { id: "guest", email: "", userDocumentId: "guestDocId" }; } @@ -128,37 +126,13 @@ export default class RouteManager { res.redirect("/login"); } } - if (!res.headersSent && req.headers.referer?.includes("corsProxy")) { - const url = decodeURIComponent(req.headers.referer!); - const start = url.match(/.*corsProxy\//)![0]; - const original = url.replace(start, ""); - const theurl = original.match(/http[s]?:\/\/[^\/]*/)![0]; - const newdirect = start + encodeURIComponent(theurl + target); - console.log("REDIRECT: " + (theurl + target)); - res.redirect(newdirect); - } - else if (!res.headersSent) { - const which2 = RouteManager.routes.findIndex(r => (r !== "/" || r === target) && target.startsWith(r)); - const which = Array.from(registered.keys()).findIndex(r => (r !== "/" || r === target) && target.startsWith(r)); - console.log("WHICH = " + (which === -1 ? "" : Array.from(registered.keys())[which])); - if (which !== -1) { - setTimeout(() => { - console.log("handled:" + target); - if (!res.headersSent) { - console.log(red(`Initiating fallback for ${target}. Please remove dangling promise from route handler`)); - const warning = `request to ${target} fell through - this is a fallback response`; - res.send({ warning }); - } - }, 1000); - } - else { - console.log("unhandled:" + target); - res.end(); + setTimeout(() => { + if (!res.headersSent) { + console.log(red(`Initiating fallback for ${target}. Please remove dangling promise from route handler`)); + const warning = `request to ${target} fell through - this is a fallback response`; + res.send({ warning }); } - } else { - console.log("pre-sent:" + target); - res.end(); - } + }, 1000); }; const subscribe = (subscriber: RouteSubscriber | string) => { let route: string; diff --git a/src/server/index.ts b/src/server/index.ts index def36e922..10205314a 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -114,12 +114,6 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: } }); - addSupervisedRoute({ - method: Method.GET, - subscription: "/*", - secureHandler: ({ res }) => { - } - }); logRegistrationOutcome(); // initialize the web socket (bidirectional communication: if a user changes diff --git a/src/server/server_Initialization.ts b/src/server/server_Initialization.ts index bb661a124..3ba9cc474 100644 --- a/src/server/server_Initialization.ts +++ b/src/server/server_Initialization.ts @@ -20,7 +20,7 @@ import * as request from 'request'; import RouteSubscriber from './RouteSubscriber'; import { publicDirectory } from '.'; import { logPort, } from './ActionUtilities'; -import { timeMap } from './ApiManagers/UserManager'; +import { Utils } from '../Utils'; import { blue, yellow } from 'colors'; import * as cors from "cors"; @@ -61,9 +61,11 @@ export default async function InitializeServer(routeSetter: RouteSetter) { registerAuthenticationRoutes(app); registerCorsProxy(app); + const isRelease = determineEnvironment(); routeSetter(new RouteManager(app, isRelease)); + registerRelativePath(app); const serverPort = isRelease ? Number(process.env.serverPort) : 1050; const server = app.listen(serverPort, () => { @@ -153,3 +155,23 @@ function registerCorsProxy(server: express.Express) { }).pipe(res); }); } + +function registerRelativePath(server: express.Express) { + server.use("*", (req, res) => { + const target = req.originalUrl; + if (!res.headersSent && req.headers.referer?.includes("corsProxy")) { + const url = decodeURIComponent(req.headers.referer); + const start = url.match(/.*corsProxy\//)![0]; + const original = url.replace(start, ""); + const theurl = original.match(/http[s]?:\/\/[^\/]*/)![0]; + const newdirect = start + encodeURIComponent(theurl + target); + res.redirect(newdirect); + return; + } else if (target.startsWith("/search")) { + const newdirect = req.headers.referer + "corsProxy/" + encodeURIComponent("http://www.google.com" + target); + res.redirect(newdirect); + return; + } + res.end(); + }); +} -- cgit v1.2.3-70-g09d2