initial commit
[nodejs-repoproxy.git] / lib / router.js
1 var url = require("url");
2 var fs = require("fs");
3
4 exports.routeRequest = function(req, res) {
5         // first, strip a /pub/ off the front if it exists
6         var originalurl = url.parse(req.url);
7                 
8         thisurl = originalurl.pathname.replace(/^\/pub/, "");
9         
10         console.log("pathname now: ", thisurl);
11         
12         //if(thisurl.pathname == "") thisurl.pathname = "/";
13         
14         var reqpath = global.repoproxy.cacheDir + "/" + thisurl;
15         
16         console.log("request on '%s'", reqpath);
17
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);
23                 
24                 if(err == null) {
25                         if(stat.isDirectory()) {
26                                 if(originalurl.pathname.charAt(originalurl.pathname.length-1) != "/") {
27                                         // redirect to url + "/"
28                                         res.writeHead("302", { "Location": originalurl.pathname+"/" });
29                                         res.end();
30                                 } else {
31                                         writeDirectoryListing(reqpath, originalurl.pathname, res);
32                                 }
33                         } else {
34                                 if(stat.isFile()) {
35                                         fs.readFile(reqpath, "utf8", function(err, data) {
36                                                 res.write(data);
37                                                 res.end();
38                                         });
39                                 }
40                         }
41                 } else {
42                         // go upstream..
43                         res.write("here we need to go upstream");
44                         res.end();
45                 }
46         });     
47 }
48
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);
54                 if(err == null) {
55                         
56                         // TODO: make this work asynchronously...
57                         if(files.length == 0) {
58                                 res.write("Empty Directory....\b");
59                         } else {
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]);
63                                         
64                                         if(stats.isDirectory()) {
65                                                 
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;
69                                                 var padding = "";
70                                                 if(padlength > 0) {
71                                                         padding = new Array(padlength).join(" ");
72                                                 }
73                                                 res.write("File:      <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
74                                         }
75                                 }
76                         }
77                         res.write("<hr></pre>");
78                         res.end();
79                 } else {
80                         res.write("we have entered bizaro world...\n");
81                         res.write("</pre>");
82                         res.end();
83                 }
84         });
85 }