Skip to content

Commit 01f13f0

Browse files
committed
fix a bug and update test
1 parent 3fcc677 commit 01f13f0

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

lib/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ var utils = require('./utils');
77

88
function HttpServer(option) {
99
var server = http.createServer(function (req, res) {
10-
var targetPath = path.join(rootPath, req.url);
10+
var requestPath = path.join(rootPath, req.url);
11+
var targetPath;
12+
if(!utils.allowPath(requestPath, rootPath)){
13+
targetPath = rootPath;
14+
req.url = '/';
15+
}else {
16+
targetPath = requestPath;
17+
}
1118
if (fs.existsSync(targetPath)) {
1219
var targetType = fs.lstatSync(targetPath);
1320
if (targetType.isFile()) {

lib/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ exports.assign = function (target) {
7979
} else {
8080
assignObject.apply(null, arguments)
8181
}
82+
}
83+
84+
exports.allowPath = function(requestPath, enablePath){
85+
return requestPath.indexOf(enablePath) === 0;
8286
}

package-lock.json

Lines changed: 31 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ describe('m-server', function() {
7171
var ret = utils.sort('3','3');
7272
done();
7373
assert(ret === 0)
74-
})
74+
});
75+
it('allow enable path', function(done){
76+
assert(utils.allowPath('/test/b/a/c', '/test/b') === true);
77+
done();
78+
});
79+
it('allow disable path', function(done){
80+
assert(utils.allowPath('/c/c', '/test/b') === false);
81+
done();
82+
});
7583

7684
});
7785
});

0 commit comments

Comments
 (0)