1 var fs = require("fs");
2 var http = require("http");
3 var url = require("url");
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...");
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);
18 function upstreamRequest(unify, callback) {
19 // first do a head request
20 console.log("upsteram as ", unify.requestFor);
22 var splpath = unify.requestFor.split("/");
23 var topdir = splpath[1];
25 for(var i=2; i < splpath.length; i++) {
29 toppath += "/" + splpath[i];
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);
40 exports.upstreamRequest = upstreamRequest;
42 // the service file routine .... PLEASE KILL ME!
43 function serviceFile(unify) {
45 // for now, ignore range.
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) {
54 inp.on("end", function(closed) {
59 exports.serviceFile = serviceFile;
61 function serviceDirectory(unify) {
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);
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]);
76 if(files[i].match(/^\..*/) == null) {
77 if(stats.isDirectory()) {
79 res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
81 } else if(stats.isFile()) {
82 var padlength = 80 - (files[i].length) - stats.size.toString().length;
85 padding = new Array(padlength).join(" ");
87 res.write("File: <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
91 console.log("ignoring file, ", files[i]);
95 if(nfiles == 0) res.write("Empty directory....\n");
97 res.write("<hr></pre>");
100 res.write("we have entered bizaro world...\n");
107 exports.serviceDirectory = serviceDirectory;