Skip to content

Commit

Permalink
sidestep v8 deopt
Browse files Browse the repository at this point in the history
only will deoptimize decode() instead of all of send()
due to the try/catch
  • Loading branch information
aheckmann committed Feb 23, 2012
1 parent f71244e commit 9a20b6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions lib/middleware/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ exports = module.exports = function static(root, options){

exports.mime = mime;

/**
* decodeURIComponent.
*
* Allows V8 to only deoptimize this fn instead of all
* of send().
*
* @param {String} path
* @api private
*/

function decode(path){
try {
return decodeURIComponent(path);
} catch (err) {
return err;
}
}

/**
* Attempt to tranfer the requested file to `res`.
*
Expand Down Expand Up @@ -103,14 +121,10 @@ var send = exports.send = function(req, res, next, options){

// parse url
var url = parse(options.path)
, path
, path = decode(url.pathname)
, type;

try {
path = decodeURIComponent(url.pathname)
} catch (_) {
return next(utils.error(400));
}
if ('URIError: URI malformed' == path) return next(utils.error(400));

// null byte(s)
if (~path.indexOf('\0')) return next(utils.error(400));
Expand Down
2 changes: 1 addition & 1 deletion test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ describe('connect.static()', function(){
// .expect(404, done);
// })
// })
})
})

0 comments on commit 9a20b6c

Please sign in to comment.