all files / express-stormpath/lib/helpers/ get-host.js

100% Statements 7/7
50% Branches 4/8
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                                    96× 96× 96×   96× 30×     96×  
'use strict';
 
/**
 * Get the host header value of a request, which should from from the 'Host'
 * header or the 'X-Forwarded-Host' header, if the appliation has been
 * configured to trust upstream proxies via app.set('trust proxy', true);
 *
 * This function exists because in Express < 5, the req.host value is unreliable
 * beacuse it strips the port, e.g. if the header is `Host: localhost:3000`,
 * then req.host will report `localhost`, which is incorrect and will break URL
 * building.
 *
 * See https://github.com/expressjs/express/issues/2179
 *
 * @param      {object}  req            Request to retrieve host from.
 * @param      {boolean} [stripPort]    Strip port if present in host.
 * @return     {string}                 Host of the provided request.
 */
module.exports = function getHost(req, stripPort) {
  var hostHeader = req.headers.host;
  var xForwardedHostHeader = req.headers['x-forwarded-host'];
  var host = req.app.get('trust proxy') ? (xForwardedHostHeader || hostHeader) : hostHeader;
 
  if (stripPort) {
    host = host.match(/:/g) ? host.slice(0, host.indexOf(':')) : host;
  }
 
  return host;
};