Add headerRedirect option #1

Слито
coolaj86 слито 1 коммит(ов) из :master в master 2018-10-02 22:59:42 +00:00
2 изменённых файлов: 8 добавлений и 2 удалений

Просмотреть файл

@ -1,6 +1,6 @@
# redirect-https # redirect-https
Redirect from HTTP to HTTPS using meta redirects Redirect from HTTP to HTTPS
See <https://coolaj86.com/articles/secure-your-redirects/> See <https://coolaj86.com/articles/secure-your-redirects/>
@ -58,7 +58,7 @@ server.listen(insecurePort, function () {
}); });
``` ```
# Why meta redirects? # Meta redirect by default, but why?
When something is broken (i.e. insecure), you don't want it to kinda work, you want developers to notice. When something is broken (i.e. insecure), you don't want it to kinda work, you want developers to notice.

Просмотреть файл

@ -33,6 +33,7 @@ module.exports = function (opts) {
var newLocation = 'https://' var newLocation = 'https://'
+ host.replace(/:\d+/, ':' + opts.port) + url + host.replace(/:\d+/, ':' + opts.port) + url
; ;
//var encodedLocation = encodeURI(newLocation); //var encodedLocation = encodeURI(newLocation);
var escapedLocation = escapeHtml(newLocation); var escapedLocation = escapeHtml(newLocation);
var decodedLocation; var decodedLocation;
@ -41,6 +42,7 @@ module.exports = function (opts) {
} catch(e) { } catch(e) {
decodedLocation = newLocation; // "#/error/?error_message=" + e.toString(); decodedLocation = newLocation; // "#/error/?error_message=" + e.toString();
} }
var body = opts.body var body = opts.body
.replace(/{{\s*HTML_URL\s*}}/ig, escapeHtml(decodedLocation)) .replace(/{{\s*HTML_URL\s*}}/ig, escapeHtml(decodedLocation))
.replace(/{{\s*URL\s*}}/ig, escapedLocation) .replace(/{{\s*URL\s*}}/ig, escapedLocation)
@ -57,6 +59,10 @@ module.exports = function (opts) {
+ '</html>\n' + '</html>\n'
; ;
if (opts.headerRedirect) {
res.statusCode = opts.headerRedirect.responseCode || 302;
res.setHeader('Location', newLocation);
}
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(metaRedirect); res.end(metaRedirect);
}; };