Last active
March 1, 2016 03:42
-
-
Save CemraJC/ec25e5887ff4ca50d4a8 to your computer and use it in GitHub Desktop.
A very simple server for serving 'wget'ed web pages and other static websites - Now includes RegEx Path Munging, for persnickety GET requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Super Simple Node Server | |
var http = require('http'); | |
var fs = require('fs'); | |
var wd = process.cwd(); | |
var serve; | |
console.log('Current directory: ' + process.cwd()); | |
// Colors! ^.^ | |
var white_c = "\033[1;39m"; | |
var red_c = "\033[1;31m"; | |
var green_c = "\033[0;32m"; | |
// File Finding Handler | |
// Path is always relative to the root path | |
function searchHandler(path){ | |
var stat, tpath = path, i = 0; | |
console.log("\n Original request:", path); | |
// Adjustments to serve wget files with GET parameters in them and protocols | |
path = path.replace(/\?/, '@'); | |
path = path.replace(/https?:\/\//, '/'); // Removes http(s) start | |
// These are path permutations that "could" work | |
var attempt = [ | |
path, | |
path.replace(/@.*$/,path.substr(path.indexOf("@")).replace(/\//g, '%2F')), | |
path.replace(/^\/[a-z0-9.]+\//, '/'), | |
path.replace(/(\?|@)[a-zA-Z=;?&]*$/, ''), | |
path.replace(/(\?|@)[a-zA-Z=;?&]*$/, 'index.html'), | |
path.concat('@hl=en') | |
]; | |
// Try to serve it | |
var res = attemptServe(tpath); | |
if (serve) { | |
return res; | |
} | |
while (!res) { | |
// If it doesn't work, try a permutation | |
if (i >= attempt.length) { | |
console.log(" All attempts used...\n"); | |
return false; | |
} else { | |
tpath = attempt[i]; | |
console.log(" Attempting: ", tpath); | |
i++; | |
} | |
// Try again. Loop until run out of permutations | |
// Then return false. | |
res = attemptServe(tpath); | |
if (serve) { | |
return res; | |
} | |
} | |
try{ | |
return fileHandler(tpath); | |
} catch(e) { | |
throw(e); | |
} | |
} | |
function attemptServe(path){ | |
var stat, result; | |
try { | |
stat = fs.statSync(fs.realpathSync(wd + path)); | |
} catch(e) { | |
return false; | |
} | |
if (stat.isDirectory()) { | |
result = searchHandler(path.replace(/(\/|\\)?$/, "/index.html")); | |
if (serve) { | |
console.log(" Serving!"); | |
return result; | |
} | |
} | |
return stat.isFile() ? true : false; | |
} | |
// File Getting Handler | |
function fileHandler(path){ | |
try{ | |
console.log(" Reading", path, "..."); | |
var file = fs.readFileSync(wd + path); | |
serve = true; | |
return file; | |
}catch(e){ | |
throw(e); | |
} | |
} | |
var server = http.createServer(function(req, res) { | |
if(req.url.match(/\$\$\$killserver/)){ | |
console.log("Killcode Received. Exiting..."); | |
process.exit(0); | |
} | |
var orig = wd + req.url; | |
try{ | |
var file = searchHandler(req.url); | |
if(!file){ | |
throw(new Error("Not Found.")); | |
} | |
res.writeHead(200); | |
res.end(file); | |
serve = false; // File served, reset the system | |
console.log(green_c, "Found:", white_c, wd + req.url, "\n"); | |
}catch(e){ | |
console.log(red_c, e.message, white_c, "\n"); | |
res.writeHead(404); | |
res.end("Not Found.\n"); | |
} | |
}).listen(80); | |
console.log('Listening on port 80\n'); | |
// Quit the server if the code changes (Should never happen, except in development) | |
try{ | |
fs.watch(process.argv[1], function(event, filename){ | |
console.log("Something Changed in Server Code - Exiting"); | |
process.exit(0); | |
}); | |
} catch(e) { | |
console.log(red_c, e.message, white_c, "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment