Skip to content

Commit

Permalink
fix: check prefix first to avoid calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Feb 21, 2017
1 parent aedf1bd commit b18f014
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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)
}
Expand Down

0 comments on commit b18f014

Please sign in to comment.