Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
machine:
node:
version: v4.5.0
dependencies:
# https://discuss.circleci.com/t/testing-multiple-versions-of-node/542
pre:
- case $CIRCLE_NODE_INDEX in 0) NODE_VERSION=0.12 ;; 1) NODE_VERSION=4 ;; esac; nvm install $NODE_VERSION && nvm alias default $NODE_VERSION
version: v4.8.4
npm:
version: v2.15.11
71 changes: 37 additions & 34 deletions lib/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,58 @@ var moment = require('moment');

var utils = require('./utils.js');

var Redirect = function( redirect_data, options ){

var Redirect = function(redirect_data, options) {
options = options || {};

var redirect = this;
var server = options.server;
var router = server.router;
var router = options.server.router;
var status = redirect_data.permanent ? PERMANENT_STATUS : TEMPORARY_STATUS;
var start = redirect_data.start;
var end = redirect_data.end;
var from = redirect_data.from;
var to = redirect_data.to;
var expired = ( moment() > moment( end ) );

// don't create redirect route at all if expired
// don't check prematurity yet since it could become valid later
if( !expired ){

if (from instanceof RegExp) {
this.route = from;
} else {
this.route = path.normalize( from ).replace( /\\/g, '/' );
this.route = utils.formatRouteForExpress(this.route);
var from = _.isObject(redirect_data.from) && !_.isRegExp(redirect_data.from) ? redirect_data.from : {path: redirect_data.from};
var to = _.isObject(redirect_data.to) && !_.isFunction(redirect_data.to) ? redirect_data.to : {url: redirect_data.to};
var expired = moment() > moment(end);

// Don't create redirect route at all if expired
// Don't check prematurity yet since it could become valid later
if (!expired) {
redirect.route = from.path || '*';
if (_.isString(redirect.route)) {
redirect.route = path.normalize(redirect.route).replace(/\\/g, '/');
redirect.route = utils.formatRouteForExpress(redirect.route);
}

router.get( this.route, function( req, res, next ){
var expired = ( moment() > moment( end ) );
var premature = ( moment() < moment( start ) );
// if redirect is expired or premature skip it
if( !expired && !premature ){
var url = typeof(to) == 'function' ? to(req.params) : to;
res.redirect( status, utils.expandVariables(url, req.params) );
}
else {
next();
router.get(redirect.route, function(req, res, next) {
// If redirect is expired or premature skip it
var expired = moment() > moment(end);
var premature = moment() < moment(start);
if (expired || premature) return next();

// If the protocol or host don't match, skip redirect
if (from.protocol && from.protocol !== req.protocol) return next();
if (from.host && from.host !== req.host) return next();

// Compute to
var newTo = to;
if (_.isFunction(newTo.url)) {
newTo = newTo.url(req.params);
if (!_.isObject(newTo)) newTo = Object.assign({}, to, {url: newTo});
}
});

// removes the redirect route
this.destroy = function(){
// Compute location
var location = newTo.protocol || newTo.host ? ((newTo.protocol || req.protocol) + '://' + (newTo.host || req.host)) : '';
location += newTo.url ? utils.expandVariables(newTo.url, req.params) : req.url;

router.routes.get = _( router.routes.get ).reject( function( current_route ){
res.redirect(status, location);
});

// Removes the redirect route
this.destroy = function() {
router.routes.get = _(router.routes.get).reject(function(current_route) {
return redirect.route === current_route.path;
});

};

}

};

module.exports = Redirect;
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var SolidusServer = function( options ){
router.engine( DEFAULT_VIEW_EXTENSION, handlebars.engine );
router.set( 'view engine', DEFAULT_VIEW_EXTENSION );
router.set( 'views', paths.views );
router.set( 'trust proxy', true ); // Use the X-Forwarded-* headers: https://expressjs.com/en/guide/behind-proxies.html
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required by Fastly, or any other CDN?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, the HTTPS protocol doesn't seem to be detected by Solidus on Heroku, so HTTPS requests end up in redirect loops.

router.use( express.compress() );
router.use(function(req, res, next) {
res.set({'X-Powered-By': 'Solidus/' + VERSION});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"commander": "1.1.x",
"continuation-local-storage": "~3.1.1",
"express": "3.1.x",
"express-expose": "SparkartGroupInc/express-expose#escape-script-ending-tag",
"express-expose": "sparkartgroup/express-expose#escape-script-ending-tag",
"express-handlebars": "1.2.2",
"glob": "~3.2.6",
"handlebars": "^1.3.0",
Expand Down
36 changes: 35 additions & 1 deletion test/fixtures/site 1/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,38 @@ module.exports = [
to: function(params) {
return "/new/{1}/{0}/" + (1000 + parseInt(params['2']));
}
}];
}, {
from: {
protocol: 'http',
host: 'solidusjs.com',
path: '/match-http-root'
},
to: '/new/match-http-root'
}, {
from: {
protocol: 'https',
host: 'solidusjs.com',
path: '/match-https-root'
},
to: '/new/match-https-root'
}, {
from: {
host: 'no-path.com'
},
to: {
host: 'www.no-path.com'
}
}, {
from: '/to-https-www',
to: {
protocol: 'https',
host: 'www.solidusjs.com'
}
}, {
from: '/to-https-www-url/{dynamic}',
to: {
protocol: 'https',
host: 'www.solidusjs.com',
url: '/new/url/{dynamic}'
}
}];
20 changes: 20 additions & 0 deletions test/solidus.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,26 @@ describe( 'Solidus', function(){
function( callback ){
s_request.get('/redirect9/12-34-56-78').expect( 'location', '/new/56/12/1078', callback );
},
function( callback ){
s_request.get('/match-http-root?with=params').set('Host', 'solidusjs.com').expect( 'location', '/new/match-http-root', callback );
},
function( callback ){
// Bad protocol
s_request.get('/match-https-root?with=params').set('Host', 'solidusjs.com').expect( 404, callback );
},
function( callback ){
// Bad host
s_request.get('/match-http-root?with=params').set('Host', 'www.solidusjs.com').expect( 404, callback );
},
function( callback ){
s_request.get('/some/path?with=params').set('Host', 'no-path.com').expect( 'location', 'http://www.no-path.com/some/path?with=params', callback );
},
function( callback ){
s_request.get('/to-https-www?with=params').set('Host', 'solidusjs.com').expect( 'location', 'https://www.solidusjs.com/to-https-www?with=params', callback );
},
function( callback ){
s_request.get('/to-https-www-url/old-path?with=params').set('Host', 'solidusjs.com').expect( 'location', 'https://www.solidusjs.com/new/url/old-path', callback );
},
function( callback ){
s_request.get('/past-redirect').expect( 404, callback );
},
Expand Down