walnut.js/lib/no-www.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
module.exports.scrubTheDub = function (req, res, redirectives) {
2015-11-14 04:25:12 +00:00
// hack for bricked app-cache
// Also 301 redirects will not work for appcache (must issue html)
if (require('./unbrick-appcache').unbrick(req, res)) {
return true;
2015-11-14 04:25:12 +00:00
}
// TODO port number for non-443
var escapeHtml = require('escape-html');
var newLocation;
var safeLocation;
// TODO req.hostname
var hostname = (req.headers.host||'').split(':')[0];
if (redirectives) {
newLocation = require('./hostname-redirects').redirectTo(hostname, redirectives);
if (!newLocation) {
return false;
}
} else {
2015-11-23 09:31:54 +00:00
newLocation = 'https://' + hostname.replace(/^www\./, '');
}
2015-11-23 09:31:54 +00:00
newLocation += req.url;
safeLocation = escapeHtml(newLocation);
2015-11-14 04:25:12 +00:00
var metaRedirect = ''
+ '<html>\n'
+ '<head>\n'
+ ' <style>* { background-color: white; color: white; text-decoration: none; }</style>\n'
+ ' <META http-equiv="refresh" content="0;URL=' + safeLocation + '">\n'
+ '</head>\n'
+ '<body style="display: none;">\n'
+ ' <p>You requested an old resource. Please use this instead: \n'
+ ' <a href="' + safeLocation + '">' + safeLocation + '</a></p>\n'
+ '</body>\n'
+ '</html>\n'
;
res.end(metaRedirect);
return true;
2015-11-14 04:25:12 +00:00
};