Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
action: function(req, res, next) {
var hostname = req.handler.hostname;
var siteObj = RequestHandler2.sites[hostname];
var redirectHost = RequestHandler2.redirectHosts[hostname];
// If we need to redirect to a different host
if (!siteObj && redirectHost && RequestHandler2.sites[redirectHost]) {
return req.handler.doRedirect(pb.SiteService.getHostWithProtocol(redirectHost), HttpStatus.MOVED_PERMANENTLY);
}
req.handler.siteObj = req.siteObj = siteObj;
//derive the localization. We do it here so that if the site isn't
//available we can still have one available when we error out
req.handler.localizationService = req.localizationService = req.handler.deriveLocalization({ session: req.session });
//make sure we have a site
if (!siteObj) {
return next(new Error("The host (" + hostname + ") has not been registered with a site. In single site mode, you must use your site root (" + pb.config.siteRoot + ")."));
}
req.handler.site = req.site = req.handler.siteObj.uid;
req.handler.siteName = req.siteName = req.handler.siteObj.displayName;
next();
static deriveSite (req, res, next) {
var hostname = req.handler.hostname;
var siteObj = RequestHandler.sites[hostname];
var redirectHost = RequestHandler.redirectHosts[hostname];
// If we need to redirect to a different host
if (!siteObj && redirectHost && RequestHandler.sites[redirectHost]) {
return req.router.redirect(pb.SiteService.getHostWithProtocol(redirectHost), HttpStatus.MOVED_PERMANENTLY);
}
req.handler.siteObj = req.siteObj = siteObj;
//derive the localization. We do it here so that if the site isn't
//available we can still have one available when we error out
req.handler.localizationService = req.localizationService = req.handler.deriveLocalization({session: req.session});
//make sure we have a site
if (!siteObj) {
return next(new Error("The host (" + hostname + ") has not been registered with a site. In single site mode, you must use your site root (" + pb.config.siteRoot + ")."));
}
req.handler.site = req.site = req.handler.siteObj.uid;
req.handler.siteName = req.siteName = req.handler.siteObj.displayName;
next();
"use strict";
const HttpStatusCodes = require("http-status-codes");
module.exports = {
// Status codes where we want to redirect the user
redirect: {
[HttpStatusCodes.MOVED_PERMANENTLY]: true,
[HttpStatusCodes.MOVED_TEMPORARILY]: true,
[HttpStatusCodes.PERMANENT_REDIRECT]: true,
[HttpStatusCodes.TEMPORARY_REDIRECT]: true
}
};
app.use((request, response, next) => {
// redirect to omit trailing slashes
if (request.path.substr(-1) == '/' && request.path.length > 1) {
const query = request.url.slice(request.path.length);
response.redirect(
HttpStatus.MOVED_PERMANENTLY,
request.path.slice(0, -1) + query
);
} else {
next();
}
});
it('should do a redirect when the controller result contains a string typed redirect property', function(done) {
req.controllerResult = { redirect: '/', code: HttpStatusCodes.MOVED_PERMANENTLY };
sandbox.stub(req.handler, 'doRedirect').withArgs(req.controllerResult.redirect, req.controllerResult.code);
this.pb.Middleware.writeResponse(req, res, function(err) {
should(err).eql(undefined);
req.didRedirect.should.eql(true);
req.handler.doRedirect.calledOnce.should.eql(true);
done();
});
});
it('should redirect when an outdated hostname is used for a site', function() {
this.pb.RequestHandler.sites = {
'test2.localhost:8080': {}
};
this.pb.RequestHandler.redirectHosts = {
'test1.localhost:8080': 'test2.localhost:8080'
};
req.router = {
redirect: function(){}
};
sandbox.stub(req.router, 'redirect').withArgs(sinon.match.string, HttpStatusCodes.MOVED_PERMANENTLY);
this.pb.Middleware.deriveSite(req, res, function() {});
req.router.redirect.calledOnce.should.eql(true);
});
mole.statsd.increment('api_response');
mole.statsd.timing('api_response_time', response.timer.end());
if (error) {
mole.logger.error('Error in API request: %s', error.stack);
mole.statsd.increment('api_error');
return handleError(error, request, response);
}
if (apiResponse.redirect && !apiResponse.target) {
mole.logger.info(
'Redirecting request %s to %s (%d)',
request.url,
apiResponse.redirect,
(apiResponse.redirect_type || httpStatus.MOVED_PERMANENTLY)
);
response.writeHead(
(apiResponse.redirect_type || httpStatus.MOVED_PERMANENTLY),
apiRedirectHeaders(apiResponse)
);
return response.end();
}
proxyUserRequest(mole, request, response, apiResponse);
});
}
api.get(request, options.routes, function (error, apiResponse) {
mole.statsd.increment('api_response');
mole.statsd.timing('api_response_time', response.timer.end());
if (error) {
mole.logger.error('Error in API request: %s', error.stack);
mole.statsd.increment('api_error');
return handleError(error, request, response);
}
if (apiResponse.redirect && !apiResponse.target) {
mole.logger.info(
'Redirecting request %s to %s (%d)',
request.url,
apiResponse.redirect,
(apiResponse.redirect_type || httpStatus.MOVED_PERMANENTLY)
);
response.writeHead(
(apiResponse.redirect_type || httpStatus.MOVED_PERMANENTLY),
apiRedirectHeaders(apiResponse)
);
return response.end();
}
proxyUserRequest(mole, request, response, apiResponse);
});
}
export function movedPermanently(arg1: T | string | Wrapper, arg2?: string): T {
return answer(HTTP.MOVED_PERMANENTLY, arg1, arg2);
}