node-http-server

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 29  →  ?path2? @ 30
/src/handler.js
@@ -9,7 +9,7 @@
const fs = require('fs');
const mime = require('mime');
const auth = require("http-auth");
const JSONStream = require('JSONStream');
const stream = require('stream');
 
// Checks whether userPath is a child of rootPath.
function isRooted(userPath, rootPath, separator, callback) {
@@ -24,36 +24,38 @@
// Check if the file is accessible.
fs.access(file, fs.constants.R_OK, (error) => {
if (error) {
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' requesting inaccessible path: ' +
file,
severity: 'warning',
status: 403
});
});
return;
}
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' sent file: ' +
' requesting inaccessible path: ' +
file,
severity: 'info',
status: 200,
data: fs
.createReadStream(file),
type: mime
.lookup(file)
severity: 'warning'
}, {
status: 403,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' sent file: ' +
file,
severity: 'info'
}, {
status: 200,
data: fs
.createReadStream(file),
type: mime
.lookup(file)
});
 
});
}
 
@@ -67,78 +69,99 @@
directory.toUpperCase() === href.toUpperCase())) {
fs.readdir(directory, (error, paths) => {
if (error) {
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' could not access directory: ' +
directory,
severity: 'warning',
status: 500
});
});
return;
}
process.nextTick(() => {
console.log("listing forbidden...");
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' accessed directory listing: ' +
' could not access directory: ' +
directory,
severity: 'warning',
status: 200,
data: JSONStream.parse(paths)
severity: 'warning'
}, {
status: 500,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
console.log("sending listing...");
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' accessed directory listing: ' +
directory,
severity: 'info'
}, {
status: 200,
data: new stream.Readable({
read(size) {
this.push(JSON.stringify(paths));
this.push(null);
}
}),
type: 'application/json'
});
});
return;
}
// Could not access directory index file and directory listing not allowed.
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' no index file found and accessing forbiden index: ' +
href,
severity: 'warning',
status: 400
});
console.log("no dirindex...");
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' no index file found and accessing forbiden index: ' +
href,
severity: 'warning'
}, {
status: 403,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
 
}
 
// Serve the document index.
fs.access(root, fs.constants.R_OK, (error) => {
if (error) {
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' unable to access path: ' +
directory,
severity: 'warning',
status: 403
});
});
return;
}
process.nextTick(() => {
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' sent file: ' +
root,
severity: 'info',
status: 200,
data: fs.createReadStream(root),
type: mime.lookup(root)
' unable to access path: ' +
directory,
severity: 'warning'
}, {
status: 403,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
callback({
message: 'Client: ' +
client.address + ':' +
client.port +
' sent file: ' +
root,
severity: 'info'
}, {
status: 200,
data: fs.createReadStream(root),
type: mime.lookup(root)
});
});
});
@@ -155,8 +178,15 @@
address.port +
' accessing non-existent document: ' +
local,
severity: 'warning',
status: 404
severity: 'warning'
}, {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
@@ -173,16 +203,21 @@
// 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(() => {
callback({
message: 'Client: ' +
address.address + ':' +
address.port +
' requested disallowed file: ' +
file,
severity: 'warning',
status: 404
});
callback({
message: 'Client: ' +
address.address + ':' +
address.port +
' requested disallowed file: ' +
file,
severity: 'warning'
}, {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
@@ -195,120 +230,107 @@
 
module.exports = {
process: (config, request, response, root, callback) => {
process.nextTick(() => {
const requestAddress = request.socket.address();
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
const requestAddress = request.socket.address();
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
);
requestURL.pathname = url.parse(
requestURL
.pathname
.replace(
config.site.rewrite[key], key
),
),
true
)
.pathname;
callback({
message: 'Rewrite path: ' +
originalPath +
' to: ' +
requestURL.path,
severity: 'info'
});
}
});
callback({
message: 'Rewrite path: ' +
originalPath +
' to: ' +
requestURL.path,
severity: 'info'
});
}
});
 
const trimmedPath = requestURL
.pathname
.split('/')
.filter(Boolean)
.join('/');
const requestPath = trimmedPath === '/' ?
path.join(root, trimmedPath) :
path.resolve(root, trimmedPath);
const trimmedPath = requestURL
.pathname
.split('/')
.filter(Boolean)
.join('/');
const requestPath = trimmedPath === '/' ?
path.join(root, trimmedPath) :
path.resolve(root, trimmedPath);
 
fs.realpath(requestPath, (error, resolvedPath) => {
// If the path does not exist, then return early.
if (error) {
process.nextTick(() => {
callback({
message: 'Unknown path requested: ' +
requestAddress.address + ':' +
requestAddress.port +
' requesting: ' +
requestURL.pathname,
severity: 'warning',
status: 404
});
fs.realpath(requestPath, (error, resolvedPath) => {
// If the path does not exist, then return early.
if (error) {
callback({
message: 'Unknown path requested: ' +
requestAddress.address + ':' +
requestAddress.port +
' requesting: ' +
requestURL.pathname,
severity: 'warning'
}, {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
// Check for path traversals early on and bail if the requested path does not
// lie within the specified document root.
isRooted(resolvedPath, root, path.sep, (rooted) => {
if (!rooted) {
callback({
message: 'Attempted path traversal: ' +
requestAddress.address + ':' +
requestAddress.port +
' requesting: ' +
requestURL.pathname,
severity: 'warning'
}, {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
// Check for path traversals early on and bail if the requested path does not
// lie within the specified document root.
isRooted(resolvedPath, root, path.sep, (rooted) => {
if (!rooted) {
process.nextTick(() => {
callback({
message: 'Attempted path traversal: ' +
requestAddress.address + ':' +
requestAddress.port +
' requesting: ' +
requestURL.pathname,
severity: 'warning',
status: 404
});
});
return;
}
 
// If authentication is required for this path then perform authentication.
if (config.auth.locations.some(
(authPath) => authPath.toUpperCase() === requestURL.pathname.toUpperCase())) {
// Create digest authentication.
const authentication = auth.digest({
realm: config.auth.realm,
file: path.resolve(
path.dirname(require.main.filename),
config.auth.digest
)
});
// Requested location requires authentication.
authentication.check(request, response, (request, response) => {
process.nextTick(() => {
callback({
message: 'Authenticated client: ' +
requestAddress.address + ':' +
requestAddress.port +
' accessing: ' +
requestURL.pathname,
severity: 'info'
});
});
serve(config,
requestPath,
requestURL.pathname,
requestAddress,
callback
);
});
return;
}
 
// If no authentication is required then serve the request.
process.nextTick(() => {
// If authentication is required for this path then perform authentication.
if (config.auth.locations.some(
(authPath) => authPath.toUpperCase() === requestURL.pathname.toUpperCase())) {
// Create digest authentication.
const authentication = auth.digest({
realm: config.auth.realm,
file: path.resolve(
path.dirname(require.main.filename),
config.auth.digest
)
});
// Requested location requires authentication.
authentication.check(request, response, (request, response) => {
callback({
message: 'Client: ' +
message: 'Authenticated client: ' +
requestAddress.address + ':' +
requestAddress.port +
' accessing: ' +
@@ -315,14 +337,31 @@
requestURL.pathname,
severity: 'info'
});
serve(config,
requestPath,
requestURL.pathname,
requestAddress,
callback
);
});
serve(config,
requestPath,
return;
}
 
// If no authentication is required then serve the request.
callback({
message: 'Client: ' +
requestAddress.address + ':' +
requestAddress.port +
' accessing: ' +
requestURL.pathname,
requestAddress,
callback
);
severity: 'info'
});
serve(config,
requestPath,
requestURL.pathname,
requestAddress,
callback
);
});
});
}