Skip to content

Commit

Permalink
Fixes #1036 - a bug that causes a crash when requesting public resour…
Browse files Browse the repository at this point in the history
…ces that don't exist
  • Loading branch information
brianhyder committed May 10, 2016
1 parent 0ac1e87 commit e63c50b
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions include/http/request_handler.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e63c50b

Please sign in to comment.