Skip to content

Commit 42059df

Browse files
committed
fix(rewrites): also rewrite protocol-relative URLs - fixes #991
1 parent 54ae01b commit 42059df

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/server/proxy-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports.rewriteLinks = function (userServer) {
1313
}
1414

1515
return {
16-
match: new RegExp("https?:\\\\/\\\\/" + string + "|https?://" + string + "(\/)?|('|\")(https?://|/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
16+
match: new RegExp("https?:\\\\/\\\\/" + string + "|('|\")\\\/\\\/" + string + "|https?://" + string + "(\/)?|('|\")(https?://|/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
17+
//match: new RegExp("https?:\\\\/\\\\/" + string + "|https?://" + string + "(\/)?|('|\")(https?://|/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
1718
//match: new RegExp("https?:\\\\?/\\\\?/" + string + "(\/)?|('|\")(https?://|\\\\?/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
1819
//match: new RegExp('https?://' + string + '(\/)?|(\'|")(https?://|/|\\.)?' + string + '(\/)?(.*?)(?=[ ,\'"\\s])', 'g'),
1920
//match: new RegExp("https?:\\\\/\\\\/" + string, "g"),

test/specs/resp-mod/rewrite-links.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ describe("Rewriting Domains", function() {
167167
var actual = input.replace(rewrite.match, bound);
168168
assert.equal(actual, input);
169169
});
170+
it("should use the regex to replace links that have protocol relative URLS", function() {
171+
var input = '<script type="text/javascript" src="//test.dev/file.js">';
172+
var expected = '<script type="text/javascript" src="//' + proxyUrl + '/file.js">';
173+
var rewrite = utils.rewriteLinks({hostname: "test.dev"}, proxyUrl);
174+
var bound = rewrite.fn.bind(null, {headers: {host: proxyUrl}}, {});
175+
var actual = input.replace(rewrite.match, bound);
176+
assert.equal(actual, expected);
177+
});
170178
it("should not replace when host + subdomain ", function() {
171179
var input = '<a href="http://assets.cdn.example.com:1234/foo">Link 1</a>';
172180
var rewrite = utils.rewriteLinks({hostname: "example.com", port: 1234}, proxyUrl);

0 commit comments

Comments
 (0)