lots of work on the request pipeline
[nodejs-repoproxy.git] / lib / cache.js
index 18d0045..bf1a3b0 100644 (file)
@@ -1,5 +1,6 @@
 var fs = require("fs");
-
+var http = require("http");
+var url = require("url");
 
 function maintainCache() {
        // TODO i should check that im already running here and exit if i am
@@ -14,31 +15,93 @@ exports.startTimer = function() {
        setInterval(maintainCache, cacheTimer);
 }
 
+function upstreamRequest(unify, callback) {
+       // first do a head request
+       console.log("upsteram as ", unify.requestFor);
+       
+       var splpath = unify.requestFor.split("/");
+       var topdir = splpath[1];
+       var toppath = "";
+       for(var i=2; i < splpath.length; i++) {
+               if(toppath == "") {
+                       toppath = splpath[i];
+               } else {
+                       toppath += "/" + splpath[i];
+               }
+       }
+       console.log("uppath = '%s' and '%s'", topdir, toppath);
+       if(typeof global.repoproxy.repo[topdir] != "undefined") {
+               console.log("which is ", global.repoproxy.repo[topdir]);
+               console.log("so upstream is, ", global.repoproxy.repo[topdir].url + toppath);
+       }
+       
+}
+
+exports.upstreamRequest = upstreamRequest;
 
 // the service file routine .... PLEASE KILL ME!
-function serviceFile(reqpath, res, range) {
+function serviceFile(unify) {
        
        // for now, ignore range.
+
+       // file should already exist, so we just poop it out
+       var inp = fs.createReadStream(unify.fullFilePath);
+       inp.setEncoding("utf8");
+       inp.on("data", function(data) {
+               unify.b.write(data);
+       });
        
-       fs.exists(reqpath, function(exists) {
-               if(exists) {
-                               var inp = fs.createReadStream(reqpath);
-                               inp.setEncoding("utf8");
-                               inp.on("data", function(data) {
-                                       res.write(data);
-                               });
+       inp.on("end", function(closed) {
+               unify.b.end();
+       });
+}
+
+exports.serviceFile = serviceFile;
+
+function serviceDirectory(unify) {
+       var nfiles = 0;
+       var res = unify.b;
+       
+       res.write("<html><h1>Directory listing for " + unify.originalReq + "</h1><hr><pre>");
+       if(unify.fullFilePath != "/") res.write("<a href=\"..\">Parent</a>\n\n");
+       fs.readdir(unify.fullFilePath, function(err, files) {
+               console.log("doing directory listing on: ", unify.fullFilePath);
+               if(err == null) {
+                       
+                       // TODO: make this work asynchronously...
+                       for(var i=0; i<files.length; i++) {
+                               // avoiding statSync is too hard for now, will fix later TODO: fix this sync bit
+                               var stats = fs.statSync(unify.fullFilePath+"/"+files[i]);
                                
-                               inp.on("end", function(closed) {
-                                       res.end();
-                               });
-               } else {
+                               if(files[i].match(/^\..*/) == null) {
+                                       if(stats.isDirectory()) {
+                                               
+                                               res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
+                                               nfiles++;
+                                       } 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:      <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
+                                               nfiles++;
+                                       }
+                               } else {
+                                       console.log("ignoring file, ", files[i]);
+                               }
+                       }
+                       
+                       if(nfiles == 0) res.write("Empty directory....\n");
                        
-                       // TODO, we need to send this upstream, if its upstream we go up.
-                       res.writeHead(404, {"Content-Type": "text/plain"});
-                       res.write("404 Not Found\n");
+                       res.write("<hr></pre>");
+                       res.end();
+               } else {
+                       res.write("we have entered bizaro world...\n");
+                       res.write("</pre>");
                        res.end();
                }
        });
 }
 
-exports.serviceFile = serviceFile;
+exports.serviceDirectory = serviceDirectory;
\ No newline at end of file