From b18f014e63e8443550769c685dcfb125035b11ce Mon Sep 17 00:00:00 2001 From: dead-horse Date: Tue, 21 Feb 2017 12:39:10 +0800 Subject: [PATCH] fix: check prefix first to avoid calculate --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8b35b40..63e8bd8 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ module.exports = function staticCache(dir, options, files) { } options = options || {} - options.prefix = (options.prefix || '').replace(/\/$/, '') + path.sep + options.prefix = (options.prefix || '').replace(/\/?$/, '/') files = files || options.files || Object.create(null) dir = dir || options.dir || process.cwd() var enableGzip = !!options.gzip @@ -47,6 +47,8 @@ module.exports = function staticCache(dir, options, files) { return function* staticCache(next) { // only accept HEAD and GET if (this.method !== 'HEAD' && this.method !== 'GET') return yield next + // check prefix first to avoid calculate + if (this.path.indexOf(options.prefix) !== 0) return yield next // decode for `/%E4%B8%AD%E6%96%87` // normalize for `//index` @@ -61,7 +63,7 @@ module.exports = function staticCache(dir, options, files) { if (filename.charAt(0) === path.sep) filename = filename.slice(1) // trim prefix - if (options.prefix !== path.sep) { + if (options.prefix !== '/') { if (filename.indexOf(filePrefix) !== 0) return yield next filename = filename.slice(filePrefix.length) }