X-Git-Url: http://git.pjr.cc/?p=nodejs-repoproxy.git;a=blobdiff_plain;f=lib%2Frouter.js;h=71fddb18dae178c24a280a0360fa4e709da64ddf;hp=96ca84407502f0e99ccf28f9ca6469d9261c6a14;hb=d9c7eb8248208029df200a897d680914cd0f337f;hpb=c3e6676d533e875b020d231075aac04e4b885677 diff --git a/lib/router.js b/lib/router.js index 96ca844..71fddb1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,83 +1,101 @@ 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); - var range = 0; - - 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.exists) { + if(unified.isFile) { + cache.serviceFile(unified); + } else if(unified.isDirectory) { + cache.serviceDirectory(unified); } else { - if(stat.isFile()) { - cache.serviceFile(reqpath, res, range); - } + console.log("ERROR: something went majorly wrong with something, ", unified); } } else { - // go upstream.. - cache.serviceFile(reqpath, res, range); + // 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 if the request is for a directory
+	if(unified.requestFor.match(/\/$/) != null) {
+		unified.isDirectoryRequest = true;
+		unified.fullPathDirName = unified.fullFilePath;
+	} else {
+		unified.isDirectoryRequest = false;
+		unified.fullPathDirName = path.dirname(unified.fullFilePath);
+	}
+	
+	// determine the topPath, subpath etc.
+	var spl = unified.requestFor.split("/");
+	unified.topPath = spl[1];
+	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;
+	}
+	
+	
+	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