forwarded-host
Version:
Determine the host of the originating request
42 lines (32 loc) • 878 B
JavaScript
;
const debug = require('debug');
const info = debug('forwarded-host');
module.exports = function parse(req) {
if (!req) {
throw new TypeError('argument req is required')
}
const headers = req.headers;
info (headers);
const xForwardedHost = headers['x-forwarded-host'];
const xForwardedProto = headers['x-forwarded-proto'];
const xForwardedPort = headers['x-forwarded-port'];
const headerPort = headers['host'];
var proto = xForwardedProto || req.protocol;
var host, url;
if (xForwardedHost) {
var port = xForwardedPort;
if (port) port = +port;
host = xForwardedHost;
if (port && port !== 80 && port !== 443) host += ':' + port;
if (!proto) {
if (port === 443) proto = 'https';
else proto = 'http';
}
} else {
host = headers['host'];
if (!proto) proto = 'http';
}
url = proto + '://' + host;
info(url);
return url;
}