lots of work on the request pipeline
[nodejs-repoproxy.git] / lib / cache.js
1 var fs = require("fs");
2 var http = require("http");
3 var url = require("url");
4
5 function maintainCache() {
6         // TODO i should check that im already running here and exit if i am
7         console.log("Cache maintainence routine starting...");
8         console.log("Cache maintainence routine ended...");
9 }
10
11 exports.startTimer = function() {
12         // our once-a-day cache maintainer
13         var cacheTimer = global.repoproxy.scancache*3600*1000;
14         //var cacheTimer = global.repoproxy.scancache*100;
15         setInterval(maintainCache, cacheTimer);
16 }
17
18 function upstreamRequest(unify, callback) {
19         // first do a head request
20         console.log("upsteram as ", unify.requestFor);
21         
22         var splpath = unify.requestFor.split("/");
23         var topdir = splpath[1];
24         var toppath = "";
25         for(var i=2; i < splpath.length; i++) {
26                 if(toppath == "") {
27                         toppath = splpath[i];
28                 } else {
29                         toppath += "/" + splpath[i];
30                 }
31         }
32         console.log("uppath = '%s' and '%s'", topdir, toppath);
33         if(typeof global.repoproxy.repo[topdir] != "undefined") {
34                 console.log("which is ", global.repoproxy.repo[topdir]);
35                 console.log("so upstream is, ", global.repoproxy.repo[topdir].url + toppath);
36         }
37         
38 }
39
40 exports.upstreamRequest = upstreamRequest;
41
42 // the service file routine .... PLEASE KILL ME!
43 function serviceFile(unify) {
44         
45         // for now, ignore range.
46
47         // file should already exist, so we just poop it out
48         var inp = fs.createReadStream(unify.fullFilePath);
49         inp.setEncoding("utf8");
50         inp.on("data", function(data) {
51                 unify.b.write(data);
52         });
53         
54         inp.on("end", function(closed) {
55                 unify.b.end();
56         });
57 }
58
59 exports.serviceFile = serviceFile;
60
61 function serviceDirectory(unify) {
62         var nfiles = 0;
63         var res = unify.b;
64         
65         res.write("<html><h1>Directory listing for " + unify.originalReq + "</h1><hr><pre>");
66         if(unify.fullFilePath != "/") res.write("<a href=\"..\">Parent</a>\n\n");
67         fs.readdir(unify.fullFilePath, function(err, files) {
68                 console.log("doing directory listing on: ", unify.fullFilePath);
69                 if(err == null) {
70                         
71                         // TODO: make this work asynchronously...
72                         for(var i=0; i<files.length; i++) {
73                                 // avoiding statSync is too hard for now, will fix later TODO: fix this sync bit
74                                 var stats = fs.statSync(unify.fullFilePath+"/"+files[i]);
75                                 
76                                 if(files[i].match(/^\..*/) == null) {
77                                         if(stats.isDirectory()) {
78                                                 
79                                                 res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
80                                                 nfiles++;
81                                         } else if(stats.isFile()) {
82                                                 var padlength = 80 - (files[i].length) - stats.size.toString().length;
83                                                 var padding = "";
84                                                 if(padlength > 0) {
85                                                         padding = new Array(padlength).join(" ");
86                                                 }
87                                                 res.write("File:      <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
88                                                 nfiles++;
89                                         }
90                                 } else {
91                                         console.log("ignoring file, ", files[i]);
92                                 }
93                         }
94                         
95                         if(nfiles == 0) res.write("Empty directory....\n");
96                         
97                         res.write("<hr></pre>");
98                         res.end();
99                 } else {
100                         res.write("we have entered bizaro world...\n");
101                         res.write("</pre>");
102                         res.end();
103                 }
104         });
105 }
106
107 exports.serviceDirectory = serviceDirectory;