Skip to content

Commit

Permalink
fix a bug and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
nunnly committed Mar 21, 2018
1 parent 3fcc677 commit 01f13f0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ var utils = require('./utils');

function HttpServer(option) {
var server = http.createServer(function (req, res) {
var targetPath = path.join(rootPath, req.url);
var requestPath = path.join(rootPath, req.url);
var targetPath;
if(!utils.allowPath(requestPath, rootPath)){
targetPath = rootPath;
req.url = '/';
}else {
targetPath = requestPath;
}
if (fs.existsSync(targetPath)) {
var targetType = fs.lstatSync(targetPath);
if (targetType.isFile()) {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ exports.assign = function (target) {
} else {
assignObject.apply(null, arguments)
}
}

exports.allowPath = function(requestPath, enablePath){
return requestPath.indexOf(enablePath) === 0;
}
73 changes: 31 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ describe('m-server', function() {
var ret = utils.sort('3','3');
done();
assert(ret === 0)
})
});
it('allow enable path', function(done){
assert(utils.allowPath('/test/b/a/c', '/test/b') === true);
done();
});
it('allow disable path', function(done){
assert(utils.allowPath('/c/c', '/test/b') === false);
done();
});

});
});

0 comments on commit 01f13f0

Please sign in to comment.