From e63c50b4ae750073d76fca01c845a04d9c2ca9e5 Mon Sep 17 00:00:00 2001 From: Brian Hyder Date: Tue, 10 May 2016 19:28:44 -0400 Subject: [PATCH] Fixes #1036 - a bug that causes a crash when requesting public resources that don't exist --- include/http/request_handler.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/include/http/request_handler.js b/include/http/request_handler.js index 81b5bcf42..7ba13d6e3 100755 --- a/include/http/request_handler.js +++ b/include/http/request_handler.js @@ -71,6 +71,16 @@ module.exports = function RequestHandlerModule(pb) { 'multipart/form-data': pb.FormBodyParser }; + /** + * Provides the list of directories that are publicly available + * @private + * @static + * @readonly + * @property PUBLIC_ROUTE_PREFIXES + * @type {Array} + */ + var PUBLIC_ROUTE_PREFIXES = ['/js/', '/css/', '/fonts/', '/img/', '/localization/', '/favicon.ico', '/docs/', '/bower_components/']; + /** * The fallback theme (pencilblue) * @static @@ -558,17 +568,19 @@ module.exports = function RequestHandlerModule(pb) { * Derives the locale and localization instance. * @method deriveLocalization * @param {Object} context - * @param {Object} context.session + * @param {Object} [context.session] * @param {String} [context.routeLocalization] */ RequestHandler.prototype.deriveLocalization = function(context) { var opts = {}; var sources = [ - context.routeLocalization, - context.session.locale, - this.req.headers[pb.Localization.ACCEPT_LANG_HEADER] + context.routeLocalization ]; + if (context.session) { + sources.push(context.session.locale); + } + sources.push(this.req.headers[pb.Localization.ACCEPT_LANG_HEADER]); if (this.siteObj) { opts.supported = Object.keys(this.siteObj.supportedLocales); sources.push(this.siteObj.defaultLocale); @@ -596,8 +608,7 @@ module.exports = function RequestHandlerModule(pb) { var self = this; fs.readFile(absolutePath, function(err, content){ if (err) { - self.serve404(); - return; + return self.serve404(); } //build response structure @@ -657,9 +668,8 @@ module.exports = function RequestHandlerModule(pb) { * @return {Boolean} TRUE if mapped to a public resource directory, FALSE if not */ RequestHandler.isPublicRoute = function(path){ - var publicRoutes = ['/js/', '/css/', '/fonts/', '/img/', '/localization/', '/favicon.ico', '/docs/', '/bower_components/']; - for (var i = 0; i < publicRoutes.length; i++) { - if (path.indexOf(publicRoutes[i]) == 0) { + for (var i = 0; i < PUBLIC_ROUTE_PREFIXES.length; i++) { + if (path.indexOf(PUBLIC_ROUTE_PREFIXES[i]) == 0) { return true; } } @@ -714,11 +724,12 @@ module.exports = function RequestHandlerModule(pb) { getActiveTheme(function(error, activeTheme) { //build out params for handlers + self.localizationService = self.localizationService || self.deriveLocalization({}); var params = { mime: self.themeRoute && self.themeRoute.content_type ? self.themeRoute.content_type : 'text/html', error: err, request: self.req, - localization: self.localization, + localization: self.localizationService, activeTheme: activeTheme, reqHandler: self, errorCount: self.errorCount