X-Git-Url: http://git.pjr.cc/?p=nodejs-repoproxy.git;a=blobdiff_plain;f=lib%2Frouter.js;h=8a7dca251eb82398749e9e10a27e69b27afc5cb2;hp=ae8408c919f3c29943a77b5c9e2407d2606e9330;hb=68fbf923faf99f86cc36dc5776451f89925df8bd;hpb=48d657a4697bd9239f7ce9e4d7a479e08e1cb04e diff --git a/lib/router.js b/lib/router.js index ae8408c..8a7dca2 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,85 +1,107 @@ var url = require("url"); var fs = require("fs"); +var cache = require("./cache.js"); +var path = require("path"); exports.routeRequest = function(req, res) { - // first, strip a /pub/ off the front if it exists - var originalurl = url.parse(req.url); - - thisurl = originalurl.pathname.replace(/^\/pub/, ""); - - console.log("pathname now: ", thisurl); - - //if(thisurl.pathname == "") thisurl.pathname = "/"; - - var reqpath = global.repoproxy.cacheDir + "/" + thisurl; - - console.log("request on '%s'", reqpath); - - // see what we're dealing with - fs.stat(reqpath, function(err, stat) { - console.log("err is ", err); - console.log("stat is ", stat); - console.log("fs.stats is ", fs.stats); - - if(err == null) { - if(stat.isDirectory()) { - if(originalurl.pathname.charAt(originalurl.pathname.length-1) != "/") { - // redirect to url + "/" - res.writeHead("302", { "Location": originalurl.pathname+"/" }); - res.end(); - } else { - writeDirectoryListing(reqpath, originalurl.pathname, res); - } + // first, unify the request + var thisQuery = unifyRequest(req, res, function(unified) { + if(unified.requestFor == "/favicon.ico") { + unified.b.writeHead(404, {"Content-Type": "text/plain"}); + unified.b.write("404 Not Found\n"); + } else if(unified.exists) { + if(unified.isFile) { + cache.serviceFile(unified); + } else if(unified.isDirectory) { + cache.serviceDirectory(unified); } else { - if(stat.isFile()) { - fs.readFile(reqpath, "utf8", function(err, data) { - res.write(data); - res.end(); - }); - } + console.log("ERROR: something went majorly wrong with something, ", unified); } } else { - // go upstream.. - res.write("here we need to go upstream"); - res.end(); + // it doesnt exist yet, so we send it to the cache service + console.log("file doesnt exist, upstream we go: ", unified); + cache.upstreamRequest(unified, function(err) { + if(err == null) { + cache.watchAndService(unfied); + } // if upstream sends anything other then a 200, cache.upstreamRequest will handle it (i.e. 302, 404, etc) + }); } - }); + }); } -function writeDirectoryListing(reqpath, requesturi, res) { - res.write("

Directory listing for " + requesturi + "


");
-	if(requesturi != "/") res.write("Parent\n\n");
-	fs.readdir(reqpath, function(err, files) {
-		console.log("doing directory listing on: ", reqpath);
+function unifyRequest(req, res, callback, testing) {
+	var unified = new Object();
+	var originalurl = url.parse(req.url);
+	
+	// create the base unified object
+	unified.a = req;
+	unified.b = res;
+
+	// create the request url
+	// remove /pub if it exists
+	unified.requestFor = originalurl.pathname.replace(/^\/pub/, "");
+	unified.originalReq = originalurl.pathname;
+	
+	// create the full file path by spanning the cachedir
+	unified.fullFilePath = (global.repoproxy.cacheDir + "/" + originalurl.pathname.replace(/^\/pub/, "")).replace(/\/+/g, "/");
+	
+	// determine the topPath, subpath etc.
+	var spl = unified.requestFor.split("/");
+	unified.topPath = spl[1];
+	unified.topFullPath = (global.repoproxy.cacheDir + "/" + spl[1]).replace(/\/+/g, "/");
+	unified.subPath = "";
+	if(spl.length > 2) {
+		for(var i=2; i < spl.length; i++) {
+			if(unified.subPath == "") unified.subPath = spl[i];
+			else unified.subPath += "/" + spl[i];
+		}
+	} else {
+		unified.subPath = null;
+	}
+	
+	// determine if the request is for a directory
+	if(unified.requestFor.match(/\/$/) != null) {
+		unified.isDirectoryRequest = true;
+		unified.fullPathDirName = unified.fullFilePath;
+		unified.subPathDirName = unified.subPath;
+	} else {
+		unified.isDirectoryRequest = false;
+		unified.fullPathDirName = path.dirname(unified.fullFilePath);
+		unified.subPathDirName = path.dirname(unified.subPath);
+	}
+	
+	
+	fs.stat(unified.fullFilePath, function(err, stats) {
 		if(err == null) {
+			unified.exists = true;
+			if(stats.isDirectory() && !unified.isDirectoryRequest) {
+				//send a 302 and call it a day
+				res.writeHead("302", { 'Location': unified.originalReq+"/" });
+				res.end();
+				
+				// TODO: remove this after testing
+				if(testing) callback(null);
+				return 302;
+			}
 			
-			// TODO: make this work asynchronously...
-			if(files.length == 0) {
-				res.write("Empty Directory....\b");
+			if(stats.isDirectory()) {
+				unified.isDirectory = true;
+				unified.isFile = false;
+			} else if(stats.isFile()) {
+				unified.isDirectory = false;
+				unified.isFile = true;				
 			} else {
-				for(var i=0; i"+files[i]+"/\n");
-					} else if(stats.isFile()) {
-						var padlength = 80 - (files[i].length) - stats.size.toString().length;
-						var padding = "";
-						if(padlength > 0) {
-							padding = new Array(padlength).join(" ");
-						}
-						res.write("File:      "+files[i]+""+padding+stats.size+" bytes\n");
-					}
-				}
+				unified.isDirectory = false;
+				unified.isFile = false;
 			}
-			res.write("
"); - res.end(); } else { - res.write("we have entered bizaro world...\n"); - res.write(""); - res.end(); + unified.exists = false; } + + callback(unified); }); -} \ No newline at end of file + + return 0; +} + +exports.unifyRequest = unifyRequest; \ No newline at end of file