/* Copyright (c) 2014 IOnU Security Inc. All rights reserved Created April 2014 by Kendrick Webster K2Proxy/router.js - calls appropriate request handler based on path, or shows 404 response for unmapped paths */ "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); } function show404(response) { setTimeout(function () { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not found"); response.end(); }, 5000); // delay response to increase difficulty of DOS attacks } function route(handle, pathname, response, request) { var path1, path2; // get 1st part of path, i.e. if pathname is "/foo/bar" or "/foo/", path1 = "/foo" // assumption: pathname always starts with '/' path1 = '/' + pathname.substr(1).replace(/\/.*$/, ""); path2 = pathname.substr(path1.length); trace('About to route a request for "' + path1 + '", extra path = "' + path2 + '"'); if (typeof handle[path1] === 'function') { handle[path1](response, request, path2); } else { trace_info("No request handler found for " + path1); show404(response); } } exports.show404 = show404; exports.route = route;