Sleds/K2Proxy/server.js

72 lines
2.1 KiB
JavaScript

/*
Copyright (c) 2014 IOnU Security Inc. All rights reserved
Created April 2014 by Kendrick Webster
K2Proxy/server.js - HTTP server event dispatcher
*/
"use strict";
function trace(m) { // comment or un-comment as needed for debugging:
// console.log(m);
}
function trace_info(m) { // comment or un-comment as needed for debugging:
console.log(m);
}
var http = require("http"),
url = require("url"),
config = require("./config");
/*
To do: we could avoid using the formidable library if we parse the HTTP POST
data here. With knowledge of maximum packet sizes, we could reject any POST
requests that exceed a size limit. This would eliminate the susceptibility
to one type of DOS attack (sending huge amounts of POST data to exhaust
server resources).
Another option is to use a different form parsing library (Multiparty):
http://stackoverflow.com/questions/20553575/how-to-cancel-user-upload-in-formidable-node-js
*/
/*
function start(route, handle) {
function onRequest(request, response) {
var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '"+
postDataChunk + "'.");
});
request.addListener("end", function() {
route(handle, pathname, response, postData);
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
*/
/*
Start the server.
<route> is the router function
<handle> is an associative array (object) of request handlers
*/
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
trace("Request for " + pathname + " received.");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(config.listenPort);
trace_info("Server has started.");
}
exports.start = start;