node-http-server

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 26  →  ?path2? @ 27
/src/handler.js
@@ -23,6 +23,16 @@
// Check if the file is accessible.
fs.access(requestPath, fs.constants.R_OK, (error) => {
if (error) {
process.nextTick(() => {
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' requesting inaccessible path: ' +
requestPath,
module.exports.error.level.WARN
);
});
response.statusCode = 403;
response.end();
return;
@@ -57,9 +67,13 @@
fs.readdir(requestPath, (error, paths) => {
if (error) {
process.nextTick(() => {
callback('Could not list directory: ' +
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' could not access directory: ' +
requestPath,
module.exports.error.level.ERROR
module.exports.error.level.WARN
);
});
response.statusCode = 500;
@@ -67,9 +81,13 @@
return;
}
process.nextTick(() => {
callback('Directory listing requested for: ' +
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' accessed directory listing: ' +
requestPath,
module.exports.error.level.INFO
module.exports.error.level.WARN
);
});
response.statusCode = 200;
@@ -79,7 +97,16 @@
 
return;
}
 
process.nextTick(() => {
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' accessing forbiden index: ' +
requestURL,
module.exports.error.level.WARN
);
});
// Could not access directory index file and directory listing not allowed.
response.statusCode = 404;
response.end();
@@ -91,7 +118,11 @@
fs.access(root, fs.constants.R_OK, (error) => {
if (error) {
process.nextTick(() => {
callback('The server was unable to access the filesystem path: ' +
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' unable to access path: ' +
requestPath,
module.exports.error.level.WARN
);
@@ -143,6 +174,16 @@
// then there is no file to serve.
if (config.site.reject.some((expression) => expression.test(file)) ||
!config.site.accept.some((expression) => expression.test(file))) {
process.nextTick(() => {
const requestAddress = request.socket.address();
callback('Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' requested disallowed file: ' +
file,
module.exports.error.level.WARN
);
});
response.statusCode = 404;
response.end();
return;
@@ -168,6 +209,33 @@
const requestURL = url.parse(
request.url, true
);
// Perform URL re-writes.
Object.keys(config.site.rewrite).forEach((key, index) => {
if(config.site.rewrite[key].test(requestURL.path)) {
const originalPath = requestURL.path;
requestURL.path = requestURL
.path
.replace(
config.site.rewrite[key], key
);
requestURL.pathname = url.parse(
requestURL
.pathname
.replace(
config.site.rewrite[key], key
),
true
)
.pathname;
callback('Rewrite path: ' +
originalPath +
' to: ' +
requestURL.path,
module.exports.error.level.INFO
);
}
});
 
const trimmedPath = requestURL
.pathname