From 38e14d3eec79e40317bfd64388dd9bad97a24aff Mon Sep 17 00:00:00 2001 From: bobzel Date: Fri, 29 Apr 2022 23:40:38 -0400 Subject: fixes to webpage proxying. fixes for images (and clippings) to use appropriate sampled image. --- src/server/server_Initialization.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/server/server_Initialization.ts') diff --git a/src/server/server_Initialization.ts b/src/server/server_Initialization.ts index 81ed0d2a1..5bfd0213c 100644 --- a/src/server/server_Initialization.ts +++ b/src/server/server_Initialization.ts @@ -193,16 +193,11 @@ function proxyServe(req: any, requrl: string, response: any) { return `href="${resolvedServerUrl + "/corsProxy/http" + href}"`; }; const zipToStringDecoder = new (require('string_decoder').StringDecoder)('utf8'); - const htmlText = zipToStringDecoder.write(zlib.gunzipSync(htmlBodyMemoryStream.read()).toString('utf8') - .replace('', ' ') - .replace(/href="http([^"]*)"/g, replacer) - .replace(/target="_blank"/g, "")); - rewrittenHtmlBody = zlib.gzipSync(htmlText); const bodyStream = htmlBodyMemoryStream.read(); if (bodyStream) { const htmlText = zipToStringDecoder.write(zlib.gunzipSync(bodyStream).toString('utf8') .replace('', ' ') - // .replace(/href="http([^"]*)"/g, replacer) + .replace(/href="https?([^"]*)"/g, replacer) .replace(/target="_blank"/g, "")); rewrittenHtmlBody = zlib.gzipSync(htmlText); } else { @@ -216,7 +211,7 @@ function proxyServe(req: any, requrl: string, response: any) { }) .on('data', (e: any) => { try { - if (!response.connection.writable) { + if (response.connection?.writable) { rewrittenHtmlBody && response.send(rewrittenHtmlBody); } } catch (e) { @@ -237,12 +232,17 @@ function registerEmbeddedBrowseRelativePathHandler(server: express.Express) { const relativeUrl = req.originalUrl; if (!req.user) res.redirect("/home"); // When no user is logged in, we interpret a relative URL as being a reference to something they don't have access to and redirect to /home else if (!res.headersSent && req.headers.referer?.includes("corsProxy")) { // a request for something by a proxied referrer means it must be a relative reference. So construct a proxied absolute reference here. - const proxiedRefererUrl = decodeURIComponent(req.headers.referer); // (e.g., http://localhost:/corsProxy/https://en.wikipedia.org/wiki/Engelbart) - const dashServerUrl = proxiedRefererUrl.match(/.*corsProxy\//)![0]; // the dash server url (e.g.: http://localhost:/corsProxy/ ) - const actualReferUrl = proxiedRefererUrl.replace(dashServerUrl, ""); // the url of the referer without the proxy (e.g., : http:s//en.wikipedia.org/wiki/Engelbart) - const absoluteTargetBaseUrl = actualReferUrl.match(/http[s]?:\/\/[^\/]*/)![0]; // the base of the original url (e.g., https://en.wikipedia.org) - const redirectedProxiedUrl = dashServerUrl + encodeURIComponent(absoluteTargetBaseUrl + relativeUrl); // the new proxied full url (e..g, http://localhost:/corsProxy/https://en.wikipedia.org/) - res.redirect(redirectedProxiedUrl); + try { + const proxiedRefererUrl = decodeURIComponent(req.headers.referer); // (e.g., http://localhost:/corsProxy/https://en.wikipedia.org/wiki/Engelbart) + const dashServerUrl = proxiedRefererUrl.match(/.*corsProxy\//)![0]; // the dash server url (e.g.: http://localhost:/corsProxy/ ) + const actualReferUrl = proxiedRefererUrl.replace(dashServerUrl, ""); // the url of the referer without the proxy (e.g., : http:s//en.wikipedia.org/wiki/Engelbart) + const absoluteTargetBaseUrl = actualReferUrl.match(/https?:\/\/[^\/]*/)![0]; // the base of the original url (e.g., https://en.wikipedia.org) + const redirectedProxiedUrl = dashServerUrl + encodeURIComponent(absoluteTargetBaseUrl + relativeUrl); // the new proxied full url (e..g, http://localhost:/corsProxy/https://en.wikipedia.org/) + if (relativeUrl.startsWith("//")) res.redirect("http:" + relativeUrl); + else res.redirect(redirectedProxiedUrl); + } catch (e) { + console.log("Error embed: ", e); + } } else if (relativeUrl.startsWith("/search") && !req.headers.referer?.includes("corsProxy")) { // detect search query and use default search engine res.redirect(req.headers.referer + "corsProxy/" + encodeURIComponent("http://www.google.com" + relativeUrl)); } else { -- cgit v1.2.3-70-g09d2