upstream now appears to work just fine
[nodejs-repoproxy.git] / lib / cache.js
index 18d0045..e065a2a 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,200 @@ exports.startTimer = function() {
        setInterval(maintainCache, cacheTimer);
 }
 
+function upstreamRequest(unify, callback) {
+       // first do a head request
+       console.log("upsteram as ", unify.requestFor);
+       
+       var endData = false;
+       var xpath = "";
+       var filefd = null;
+       if(unify.topPath !=null) if(unify.topPath != "") if(typeof global.repoproxy.repo[unify.topPath] != "undefined") {
+               var uplink = global.repoproxy.repo[unify.topPath].url;
+               xpath = uplink + unify.subPath;
+       }
+       
+       //unify.b.write("would send to '" + xpath + "'");
+       //unify.b.end();
+       
+       console.log("sending off to '%s'", xpath);
+       
+       var headReq = url.parse(xpath);
+       headReq["method"] = "HEAD";
+       
+       getup = http.request(xpath, function(res) {
+               res.setEncoding("utf8");
+               
+               if(!endData) {
+                       console.log("status code is ", typeof res.statusCode);
+                       switch(res.statusCode) {
+                       // TODO: this 301 directory redirect thing needs to work better
+                       case 301:
+                       case 302:
+                               
+                               var loc = res.headers.location.substr(res.headers.location.length-4);
+                               var against_t = xpath + "/";
+                               var against = against_t.substr(against_t.length-4);
+                               
+                               if(loc == against) {
+                                       console.log("got a redirect, upstream for loc => loc/ assuming its a directory");
+                                       makeCacheDir(unify);
+                                       unify.b.writeHead(302, { "Location": unify.originalReq + "/" });
+                               } else {
+                                       console.log("checked '%s' against '%s', was false, sending 404", loc, against);
+                                       unify.b.writeHead(404, {"Content-Type": "text/plain"});
+                                       unify.b.write("404 Not Found\n");
+                               }
+                               unify.b.end();
+                               endData = true;
+                               break;
+                               
+                       case 404:
+                               unify.b.writeHead(404, {"Content-Type": "text/plain"});
+                               unify.b.write("404 Not Found\n");
+                               unify.b.end();
+                               endData = true;
+                               break;
+                       case 200:
+                               makeCacheDir(unify);
+                               if(unify.isDirectoryRequest) {
+                                       serviceDirectory(unify);                                        
+                                       endData = true;
+                               } else {
+                                       // this is where it gets ugly
+                                       console.log("do ugly write: ", unify);
+                                       //unify.b.write(data);
+                                       getAndService(unify, xpath);
+                                       
+                               }
+                               break;
+                       default:
+                               console.log(".... data");
+                               //unify.b.write(data);
+                       }
+               }               
+               //console.log("res is now ", res);
+       });
+       
+       getup.end();
+       
+       //console.log("getup: ", getup);
+}
+
+exports.upstreamRequest = upstreamRequest;
+
+function getAndService(unify, xpath) {
+       
+       if(global.repoproxy.downloads[unify.fullFilePath] == 1) {
+               
+               unify.b.write("trying to service inline");
+               unify.b.end();
+       } else {
+               global.repoproxy.downloads[unify.fullFilePath] = 1;
+       
+               http.get(xpath, function(res) {
+       
+                   var file = fs.createWriteStream(unify.fullFilePath);
+               
+                   console.log("res: ", res);
+               
+                   //res.setEncoding("utf8");
+               
+                   res.on("data", function(data) {
+                           //console.log("chunk");
+                           file.write(data);
+                           unify.b.write(data);
+                   });
+               
+                   res.on("end", function() {
+                           console.log("end...");
+                           unify.b.end();
+                           file.end();
+                           global.repoproxy.downloads[unify.fullFilePath] = 0;
+                   });
+               });
+       }
+}
 
 // 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);
+       });
+       
+       inp.on("end", function(closed) {
+               unify.b.end();
+       });
+}
+
+exports.serviceFile = serviceFile;
+
+function makeCacheDir(path) {
+       console.log("attempting to create... '%s' as '%s'", path.fullPathDirName, path.subPathDirName);
+       
+       var startAt = path.topFullPath;
+       var nextbits = path.subPathDirName.split("/");
+       for(var i=0; i < nextbits.length; i++) {
+               startAt += "/" + nextbits[i];
+               console.log("attempt mkdir on '%s'", startAt);
+               try {
+                       fs.mkdirSync(startAt);
+               } catch(e) {
+                       //console.log("e in mkdir, ", e);
+               }
+       }
+       //process.exit(0);
+}
+
+function serviceDirectory(unify) {
+       var nfiles = 0;
+       var res = unify.b;
        
-       fs.exists(reqpath, function(exists) {
-               if(exists) {
-                               var inp = fs.createReadStream(reqpath);
-                               inp.setEncoding("utf8");
-                               inp.on("data", function(data) {
-                                       res.write(data);
-                               });
+       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]);
+                               }
+                       }
                        
-                       // 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");
+                       if(nfiles == 0) res.write("Empty directory....\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