1 var url = require("url");
2 var fs = require("fs");
4 exports.routeRequest = function(req, res) {
5 // first, strip a /pub/ off the front if it exists
6 var originalurl = url.parse(req.url);
8 thisurl = originalurl.pathname.replace(/^\/pub/, "");
10 console.log("pathname now: ", thisurl);
12 //if(thisurl.pathname == "") thisurl.pathname = "/";
14 var reqpath = global.repoproxy.cacheDir + "/" + thisurl;
16 console.log("request on '%s'", reqpath);
18 // see what we're dealing with
19 fs.stat(reqpath, function(err, stat) {
20 console.log("err is ", err);
21 console.log("stat is ", stat);
22 console.log("fs.stats is ", fs.stats);
25 if(stat.isDirectory()) {
26 if(originalurl.pathname.charAt(originalurl.pathname.length-1) != "/") {
27 // redirect to url + "/"
28 res.writeHead("302", { "Location": originalurl.pathname+"/" });
31 writeDirectoryListing(reqpath, originalurl.pathname, res);
35 fs.readFile(reqpath, "utf8", function(err, data) {
43 res.write("here we need to go upstream");
49 function writeDirectoryListing(reqpath, requesturi, res) {
50 res.write("<html><h1>Directory listing for " + requesturi + "</h1><hr><pre>");
51 if(requesturi != "/") res.write("<a href=\"..\">Parent</a>\n\n");
52 fs.readdir(reqpath, function(err, files) {
53 console.log("doing directory listing on: ", reqpath);
56 // TODO: make this work asynchronously...
57 if(files.length == 0) {
58 res.write("Empty Directory....\b");
60 for(var i=0; i<files.length; i++) {
61 // avoiding statSync is too hard for now, will fix later TODO: fix this sync bit
62 var stats = fs.statSync(reqpath+"/"+files[i]);
64 if(stats.isDirectory()) {
66 res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
67 } else if(stats.isFile()) {
68 var padlength = 80 - (files[i].length) - stats.size.toString().length;
71 padding = new Array(padlength).join(" ");
73 res.write("File: <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
77 res.write("<hr></pre>");
80 res.write("we have entered bizaro world...\n");