Skip to content

Commit

Permalink
Follow meta-refresh redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Premi committed Jul 31, 2017
1 parent 5c2477f commit b0d6ccf
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,104 @@ page.viewportSize = {
page.customHeaders = opts.headers || {};
page.zoomFactor = opts.scale;

page.open(opts.url, function (status) {
var previousCount = 0;
var previousPage = null;

function pageOnLoadFinished(status) {
if (status === 'fail') {
console.error('Couldn\'t load url: ' + opts.url);
phantom.exit(1);
return;
}

if (previousPage === page.url) {
if (++ previousCount > 3) {
console.error('Loop detected: ' + page.url);
phantom.exit(1);
return;
}
} else {
previousPage = page.url;
previousCount = 0;
}

var threshold = 10;
var refreshUrl = page.evaluate(function(threshold) {
var patternMetaContent = /^\s*(\d+)(?:\s*;(?:\s*url\s*=)?\s*(.+)?)?$/i;
var parseMetaRefresh = function (content) {
// base code from https://github.com/stevenvachon/http-equiv-refresh
var result = { timeout: null, url: null };

content = patternMetaContent.exec(content);

if (content === null) {
return result;
}

if (content[1] !== undefined) {
result.timeout = parseInt( content[1] );
}

if (content[2] !== undefined) {
var url = (content[2] + '').trim();

if (url.length) {
var firstChar = url[0];
var lastChar = url[url.length-1];

// Remove a single level of encapsulating quotes
if (firstChar==="'" && lastChar==="'" || firstChar==='"' && lastChar==='"') {
if (url.length > 2) {
url = url.substr(1, url.length-2).trim();
}
}
}

if (url.length) {
result.url = url;
}
}

return result;
};

var metas = [];
var tags = document.head.querySelectorAll('[http-equiv="refresh"]');
for (var i = 0, len = tags.length; i < len; ++ i) {
if (tags[i].tagName === 'META') {
metas.push(tags[i].content || tags[i].CONTENT);
}
};

var refreshUrl = false;
var minTime = Number.POSITIVE_INFINITY;
var i = 0;

for (var i = 0, len = metas.length; i < len; ++ i) {
var refresh = parseMetaRefresh(metas[i]);

if (refresh.timeout <= threshold && refresh.timeout < minTime) {
minTime = refresh.refresh;
refreshUrl = refresh.url; // could be null
}
}

return refreshUrl;
}, threshold);

if (false !== refreshUrl) {
page.onLoadFinished = pageOnLoadFinished;

page.evaluate(function (refreshUrl) {
if (null === refreshUrl) {
// window.location.reload();
} else {
window.location.replace(refreshUrl);
}
}, refreshUrl);
return;
}

if (opts.crop) {
page.clipRect = {
top: 0,
Expand Down Expand Up @@ -124,4 +215,7 @@ page.open(opts.url, function (status) {
log.call(console, page.renderBase64(opts.format));
phantom.exit();
}, opts.delay * 1000);
});
}

page.onLoadFinished = pageOnLoadFinished;
page.open(opts.url);

0 comments on commit b0d6ccf

Please sign in to comment.