/src/handler.js |
@@ -8,8 +8,9 @@ |
const path = require('path'); |
const fs = require('fs'); |
const mime = require('mime'); |
const auth = require("http-auth"); |
|
// Checks whether userPath is a child of rootPath |
// Checks whether userPath is a child of rootPath. |
function isRooted(userPath, rootPath, separator) { |
userPath = userPath.split(separator).filter(Boolean); |
rootPath = rootPath.split(separator).filter(Boolean); |
@@ -17,71 +18,47 @@ |
rootPath.every((e, i) => e === userPath[i]); |
} |
|
module.exports = { |
error: { |
level: { |
INFO: 1, |
WARN: 2, |
ERROR: 3 |
} |
}, |
process: (config, request, response, root, callback) => { |
// Serves files. |
function files(config, request, response, resource, callback) { |
// Check if the file is accessible. |
process.nextTick(() => { |
const requestAddress = request.socket.address(); |
const requestedURL = url.parse(request.url, true); |
|
process.nextTick(() => { |
callback('Client: ' + |
requestAddress.address + ':' + |
requestAddress.port + |
' accessing: ' + |
requestedURL.pathname, |
module.exports.error.level.INFO |
); |
}); |
|
const trimmedPath = requestedURL |
.pathname |
.split('/') |
.filter(Boolean) |
.join('/'); |
const filesystemPath = trimmedPath === '/' ? |
path.join(root, trimmedPath) : |
path.resolve(root, trimmedPath); |
|
if (!isRooted(filesystemPath, root, path.sep)) { |
process.nextTick(() => { |
callback('Attempted path traversal: ' + |
requestAddress.address + ':' + |
requestAddress.port + |
' requesting: ' + |
requestedURL.pathname, |
module.exports.error.level.WARN |
); |
}); |
fs.access(resource, fs.constants.R_OK, (error) => { |
if (error) { |
response.statusCode = 403; |
response.end(); |
return; |
} |
|
fs.stat(filesystemPath, (error, stats) => { |
// Document does not exist. |
if (error) { |
response.statusCode = 404; |
response.setHeader( |
'Content-Type', |
mime.lookup(resource) |
); |
|
var readStream = fs.createReadStream(resource) |
.on('open', () => { |
response.statusCode = 200; |
readStream.pipe(response); |
}) |
.on('error', () => { |
response.statusCode = 500; |
response.end(); |
return; |
}); |
|
}); |
}); |
} |
|
switch (stats.isDirectory()) { |
case true: // Directory is requested so provide directory indexes. |
const root = path.resolve(filesystemPath, config.site.index); |
// Serves a directory index. |
function index(config, request, response, resource, callback) { |
process.nextTick(() => { |
const root = path.resolve(resource, config.site.index); |
fs.stat(root, (error, stats) => { |
if (error) { |
fs.readdir(filesystemPath, (error, paths) => { |
fs.readdir(resource, (error, paths) => { |
if (error) { |
process.nextTick(() => { |
callback('Could not list directory: ' + |
filesystemPath, |
resource, |
module.exports.error.level.ERROR |
); |
}); |
@@ -91,7 +68,7 @@ |
} |
process.nextTick(() => { |
callback('Directory listing requested for: ' + |
filesystemPath, |
resource, |
module.exports.error.level.INFO |
); |
}); |
@@ -103,11 +80,11 @@ |
return; |
} |
|
fs.access(filesystemPath, fs.constants.R_OK, (error) => { |
fs.access(resource, fs.constants.R_OK, (error) => { |
if (error) { |
process.nextTick(() => { |
callback('The server was unable to access the filesystem path: ' + |
filesystemPath, |
resource, |
module.exports.error.level.WARN |
); |
}); |
@@ -117,7 +94,10 @@ |
} |
|
// Set MIME content type. |
response.setHeader('Content-Type', mime.lookup(root)); |
response.setHeader( |
'Content-Type', |
mime.lookup(root) |
); |
|
var readStream = fs.createReadStream(root) |
.on('open', () => { |
@@ -132,32 +112,110 @@ |
}); |
|
}); |
break; |
default: // Browser requesting file. |
// Check if the file is accessible. |
fs.access(filesystemPath, fs.constants.R_OK, (error) => { |
}); |
} |
|
// Determines whether the requested resource is a directory or a file. |
function serve(config, request, response, resource, callback) { |
process.nextTick(() => { |
fs.stat(resource, (error, stats) => { |
// Document does not exist. |
if (error) { |
response.statusCode = 403; |
response.statusCode = 404; |
response.end(); |
return; |
} |
|
response.setHeader('Content-Type', mime.lookup(filesystemPath)); |
switch (stats.isDirectory()) { |
case true: // Directory is requested so provide directory indexes. |
index(config, request, response, resource, callback) |
break; |
default: // Browser requesting file. |
files(config, request, response, resource, callback); |
break; |
} |
}); |
}); |
} |
|
var readStream = fs.createReadStream(filesystemPath) |
.on('open', () => { |
response.statusCode = 200; |
readStream.pipe(response); |
}) |
.on('error', () => { |
response.statusCode = 500; |
response.end(); |
module.exports = { |
error: { |
level: { |
INFO: 1, |
WARN: 2, |
ERROR: 3 |
} |
}, |
process: (config, request, response, root, callback) => { |
process.nextTick(() => { |
// Create digest authentication. |
const authentication = auth.digest({ |
realm: config.auth.realm, |
file: path.resolve( |
path.dirname(require.main.filename), |
config.auth.digest |
) |
}); |
|
const requestAddress = request.socket.address(); |
const requestedURL = url.parse( |
request.url, true |
); |
|
const trimmedPath = requestedURL |
.pathname |
.split('/') |
.filter(Boolean) |
.join('/'); |
const resource = trimmedPath === '/' ? |
path.join(root, trimmedPath) : |
path.resolve(root, trimmedPath); |
|
if (!isRooted(resource, root, path.sep)) { |
process.nextTick(() => { |
callback('Attempted path traversal: ' + |
requestAddress.address + ':' + |
requestAddress.port + |
' requesting: ' + |
requestedURL.pathname, |
module.exports.error.level.WARN |
); |
}); |
response.statusCode = 404; |
response.end(); |
return; |
} |
|
switch (config.auth.locations.some( |
(authPath) => authPath.toUpperCase() === requestedURL.pathname.toUpperCase())) { |
case true: |
// Requested location requires authentication. |
authentication.check(request, response, (request, response) => { |
process.nextTick(() => { |
callback('Authenticated client: ' + |
requestAddress.address + ':' + |
requestAddress.port + |
' accessing: ' + |
requestedURL.pathname, |
module.exports.error.level.INFO |
); |
}); |
serve(config, request, response, resource, callback); |
}); |
break; |
default: |
process.nextTick(() => { |
callback('Client: ' + |
requestAddress.address + ':' + |
requestAddress.port + |
' accessing: ' + |
requestedURL.pathname, |
module.exports.error.level.INFO |
); |
}); |
serve(config, request, response, resource, callback); |
break; |
} |
}) |
}); |
} |
}; |