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) {
19 // first do a head request
20 console.log("upsteram as ", unify.requestFor);
25 if(unify.topPath !=null) if(unify.topPath != "") if(typeof global.repoproxy.repo[unify.topPath] != "undefined") {
26 var uplink = global.repoproxy.repo[unify.topPath].url;
27 xpath = uplink + unify.subPath;
30 //unify.b.write("would send to '" + xpath + "'");
33 console.log("sending off to '%s'", xpath);
35 var headReq = url.parse(xpath);
36 headReq["method"] = "HEAD";
38 getup = http.request(xpath, function(res) {
39 res.setEncoding("utf8");
42 console.log("status code is ", typeof res.statusCode);
43 switch(res.statusCode) {
44 // TODO: this 301 directory redirect thing needs to work better
48 var loc = res.headers.location.substr(res.headers.location.length-4);
49 var against_t = xpath + "/";
50 var against = against_t.substr(against_t.length-4);
53 console.log("got a redirect, upstream for loc => loc/ assuming its a directory");
55 unify.b.writeHead(302, { "Location": unify.originalReq + "/" });
57 console.log("checked '%s' against '%s', was false, sending 404", loc, against);
58 unify.b.writeHead(404, {"Content-Type": "text/plain"});
59 unify.b.write("404 Not Found\n");
66 unify.b.writeHead(404, {"Content-Type": "text/plain"});
67 unify.b.write("404 Not Found\n");
73 if(unify.isDirectoryRequest) {
74 serviceDirectory(unify);
77 // this is where it gets ugly
78 console.log("do ugly write: ", unify);
79 //unify.b.write(data);
80 getAndService(unify, xpath);
85 console.log(".... data");
86 //unify.b.write(data);
89 //console.log("res is now ", res);
94 //console.log("getup: ", getup);
97 exports.upstreamRequest = upstreamRequest;
99 function getAndService(unify, xpath) {
101 if(typeof global.repoproxy.downloads[unify.fullFilePath] != "undefined" && global.repoproxy.downloads[unify.fullFilePath] == 1) {
103 console.log("service inline");
104 unify.b.write("trying to service inline");
107 global.repoproxy.downloads[unify.fullFilePath] = 1;
109 http.get(xpath, function(res) {
111 var file = fs.createWriteStream(unify.fullFilePath);
113 console.log("res: ", res);
115 //res.setEncoding("utf8");
117 res.on("data", function(data) {
118 //console.log("chunk");
123 res.on("end", function() {
124 console.log("end...");
127 global.repoproxy.downloads[unify.fullFilePath] = 0;
130 res.on("error", function(err) {
131 console.log("res threw error... ", err);
137 // the service file routine .... PLEASE KILL ME!
138 function serviceFile(unify) {
140 // for now, ignore range.
142 // file should already exist, so we just poop it out
143 var inp = fs.createReadStream(unify.fullFilePath);
144 //inp.setEncoding("utf8");
145 inp.on("data", function(data) {
149 inp.on("end", function(closed) {
154 exports.serviceFile = serviceFile;
156 function makeCacheDir(path) {
157 console.log("attempting to create... '%s' as '%s'", path.fullPathDirName, path.subPathDirName);
159 var startAt = path.topFullPath;
160 var nextbits = path.subPathDirName.split("/");
161 for(var i=0; i < nextbits.length; i++) {
162 startAt += "/" + nextbits[i];
163 console.log("attempt mkdir on '%s'", startAt);
165 fs.mkdirSync(startAt);
167 //console.log("e in mkdir, ", e);
173 function serviceDirectory(unify) {
177 res.write("<html><h1>Directory listing for " + unify.originalReq + "</h1><hr><pre>");
178 if(unify.originalReq != "/") res.write("<a href=\"..\">Parent</a>\n\n");
179 fs.readdir(unify.fullFilePath, function(err, files) {
180 console.log("doing directory listing on: ", unify.fullFilePath);
183 // TODO: make this work asynchronously...
184 for(var i=0; i<files.length; i++) {
185 // avoiding statSync is too hard for now, will fix later TODO: fix this sync bit
186 var stats = fs.statSync(unify.fullFilePath+"/"+files[i]);
188 if(files[i].match(/^\..*/) == null) {
189 if(stats.isDirectory()) {
191 res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
193 } else if(stats.isFile()) {
194 var padlength = 80 - (files[i].length) - stats.size.toString().length;
197 padding = new Array(padlength).join(" ");
199 res.write("File: <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
203 console.log("ignoring file, ", files[i]);
207 if(nfiles == 0) res.write("Empty directory....\n");
209 res.write("<hr></pre>");
212 res.write("we have entered bizaro world...\n");
219 exports.serviceDirectory = serviceDirectory;