Created
April 6, 2010 22:52
-
-
Save andrewvc/358229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.deliver = function (webroot, req, res) { | |
var | |
stream, | |
fpRes = exports.filepath(webroot, url.parse(req.url).pathname), | |
fpErr = fpRes[0], | |
filepath = fpRes[1], | |
errorCallback, | |
headerFields = {}, | |
addHeaderCallback, | |
delegate = { | |
error: function (callback) { | |
errorCallback = callback; | |
return delegate; | |
}, | |
//...Setup the other callbacks... | |
}; | |
process.nextTick(function() { | |
//If file is in a directory outside of the webroot, deny the request | |
if (fpErr) { | |
//... omitted for brevity... | |
} | |
else { | |
fs.stat(filepath, function (err, stat) { | |
if( (err || !stat.isFile())) { | |
var exactErr = err || 'File not found'; | |
if (beforeCallback) | |
beforeCallback(); | |
if (otherwiseCallback) | |
otherwiseCallback(exactErr); | |
} else { | |
stream = exports.streamFile(filepath, headerFields, stat, res, req) | |
//... omitted for brevity ... | |
if(errorCallback){ | |
stream.addListener("error", errorCallback); | |
} | |
} | |
}); | |
} | |
}); | |
return delegate; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment