Skip to content

Commit

Permalink
fix: improve link rewrite (#2213)
Browse files Browse the repository at this point in the history
fixes #2212
  • Loading branch information
tripodsan authored Jul 14, 2023
1 parent 1b39601 commit e4a4ce7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 36 deletions.
47 changes: 11 additions & 36 deletions src/server/HelixImportServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ export class HelixImportServer extends BaseServer {
}
}

let buffer;
let buffer = await ret.buffer();
if (contentType.includes('html') || contentType.includes('text')) {
buffer = utils.rewriteUrl(buffer, host);
}

if (!isBodyReq && this._project.cacheDirectory) {
buffer = await ret.buffer();
await utils.writeToCache(
url,
this._project.cacheDirectory,
Expand All @@ -149,40 +152,12 @@ export class HelixImportServer extends BaseServer {
);
}

if (contentType.includes('html') || contentType.includes('text')) {
// make urls relative
let text;
if (buffer) {
text = buffer.toString();
} else {
text = await ret.text();
}
let replacer = new RegExp(`${host}/`, 'gm');
text = text.replace(replacer, '/');
replacer = new RegExp(host, 'gm');
text = text.replace(replacer, '/');
res
.status(ret.status)
.set(respHeaders)
.cookie('hlx-proxyhost', host)
.send(text);
ret.body.pipe(res);
return;
}

if (buffer) {
res
.status(ret.status)
.set(respHeaders)
.cookie('hlx-proxyhost', host)
.send(buffer);
ret.body.pipe(res);
} else {
res
.status(ret.status)
.set(respHeaders);
ret.body.pipe(res);
}
res
.status(ret.status)
.set(respHeaders)
.cookie('hlx-proxyhost', host)
.send(buffer);
ret.body.pipe(res);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,20 @@ window.LiveReloadOptions = {
});
});
},

/**
* Rewrites all absolute urls to the proxy host with relative ones.
* @param {Buffer} html
* @param {string} host
* @returns {Buffer}
*/
rewriteUrl(html, host) {
const hostPattern = host.replaceAll('.', '\\.');
let text = html.toString('utf-8');
const re = new RegExp(`(src|href)\\s*=\\s*(["'])${hostPattern}(/.*?)?(['"])`, 'gm');
text = text.replaceAll(re, (match, arg, q1, value, q2) => (`${arg}=${q1}${value || '/'}${q2}`));
return Buffer.from(text, 'utf-8');
},
};

export default Object.freeze(utils);
39 changes: 39 additions & 0 deletions test/server-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,43 @@ describe('Utils Test', () => {
});
});
});

describe('URL Rewrite', () => {
function test(html, expected) {
const actual = utils.rewriteUrl(Buffer.from(html), 'https://www.example.com').toString('utf-8');
assert.strictEqual(actual, expected);
}

it('replaces host in src', () => {
test('<img src="https://www.example.com/foo.png">', '<img src="/foo.png">');
});

it('replaces host in src (single quotes)', () => {
test("<img src = 'https://www.example.com/foo.png' width='200'>", "<img src='/foo.png' width='200'>");
});

it('replaces host in href', () => {
test('<a href="https://www.example.com">', '<a href="/">');
});

it('replaces host in href with slash', () => {
test('<a href="https://www.example.com/">', '<a href="/">');
});

it('replaces host in href', () => {
test('<a href="https://www.example.com">', '<a href="/">');
});

it('does proper matching', () => {
test('<a href="https://www.example.comm">', '<a href="https://www.example.comm">');
});

it('does not replace host in query params', () => {
test('<a href="https://facebook.com?u=https://www.example.com">', '<a href="https://facebook.com?u=https://www.example.com">');
});

it('does not replace the host in text', () => {
test('<a href="https://www.example.com">https://www.example.com</a>', '<a href="/">https://www.example.com</a>');
});
});
});

0 comments on commit e4a4ce7

Please sign in to comment.