UNPKG

flowstate

Version:

Per-request state management middleware.

51 lines (43 loc) 1.6 kB
var uri = require('url'); /** * Reconstructs the original URL of the request. * * This function builds a URL that corresponds the original URL requested by the * client, including the protocol (http or https) and host. * * If the request passed through any proxies that terminate SSL, the * `X-Forwarded-Proto` header is used to detect if the request was encrypted to * the proxy, assuming that the proxy has been flagged as trusted. * * @param {http.IncomingMessage} req * @param {Object} [options] * @return {String} * @api private */ exports.originalURL = function(req, options) { options = options || {}; var app = req.app; if (app && app.get && app.get('trust proxy')) { options.proxy = true; } var trustProxy = options.proxy; var proto = (req.headers['x-forwarded-proto'] || '').toLowerCase() , tls = req.connection.encrypted || (trustProxy && 'https' == proto.split(/\s*,\s*/)[0]) , host = (trustProxy && req.headers['x-forwarded-host']) || req.headers.host , protocol = tls ? 'https' : 'http' , path = req.originalUrl || req.url || ''; return protocol + '://' + host + path; }; exports.originalURLWithoutQuery = function(req, options) { var loc = uri.parse(exports.originalURL(req, options), false); delete loc.search; delete loc.query; return uri.format(loc); }; exports.originalURLWithoutState = function(req, options) { var loc = uri.parse(exports.originalURL(req, options), false); delete loc.search; delete loc.query; // TODO: test case for preserving query params, but not state return uri.format(loc); };