96ca84407502f0e99ccf28f9ca6469d9261c6a14
[nodejs-repoproxy.git] / lib / router.js
1 var url = require("url");
2 var fs = require("fs");
3 var cache = require("./cache.js");
4
5 exports.routeRequest = function(req, res) {
6         // first, strip a /pub/ off the front if it exists
7         var originalurl = url.parse(req.url);
8         var range = 0;
9                 
10         thisurl = originalurl.pathname.replace(/^\/pub/, "");
11         
12         console.log("pathname now: ", thisurl);
13         
14         //if(thisurl.pathname == "") thisurl.pathname = "/";
15         
16         var reqpath = global.repoproxy.cacheDir + "/" + thisurl;
17         
18         console.log("request on '%s'", reqpath);
19
20         // see what we're dealing with
21         fs.stat(reqpath, function(err, stat) {
22                 console.log("err is ", err);
23                 console.log("stat is ", stat);
24                 console.log("fs.stats is ", fs.stats);
25                 
26                 if(err == null) {
27                         if(stat.isDirectory()) {
28                                 if(originalurl.pathname.charAt(originalurl.pathname.length-1) != "/") {
29                                         // redirect to url + "/"
30                                         res.writeHead("302", { "Location": originalurl.pathname+"/" });
31                                         res.end();
32                                 } else {
33                                         writeDirectoryListing(reqpath, originalurl.pathname, res);
34                                 }
35                         } else {
36                                 if(stat.isFile()) {
37                                         cache.serviceFile(reqpath, res, range);
38                                 }
39                         }
40                 } else {
41                         // go upstream..
42                         cache.serviceFile(reqpath, res, range);
43                 }
44         });     
45 }
46
47 function writeDirectoryListing(reqpath, requesturi, res) {
48         res.write("<html><h1>Directory listing for " + requesturi + "</h1><hr><pre>");
49         if(requesturi != "/") res.write("<a href=\"..\">Parent</a>\n\n");
50         fs.readdir(reqpath, function(err, files) {
51                 console.log("doing directory listing on: ", reqpath);
52                 if(err == null) {
53                         
54                         // TODO: make this work asynchronously...
55                         if(files.length == 0) {
56                                 res.write("Empty Directory....\b");
57                         } else {
58                                 for(var i=0; i<files.length; i++) {
59                                         // avoiding statSync is too hard for now, will fix later TODO: fix this sync bit
60                                         var stats = fs.statSync(reqpath+"/"+files[i]);
61                                         
62                                         if(stats.isDirectory()) {
63                                                 
64                                                 res.write("Directory: <a href=\""+files[i]+"/\">"+files[i]+"/</a>\n");
65                                         } else if(stats.isFile()) {
66                                                 var padlength = 80 - (files[i].length) - stats.size.toString().length;
67                                                 var padding = "";
68                                                 if(padlength > 0) {
69                                                         padding = new Array(padlength).join(" ");
70                                                 }
71                                                 res.write("File:      <a href=\""+files[i]+"\">"+files[i]+"</a>"+padding+stats.size+" bytes\n");
72                                         }
73                                 }
74                         }
75                         res.write("<hr></pre>");
76                         res.end();
77                 } else {
78                         res.write("we have entered bizaro world...\n");
79                         res.write("</pre>");
80                         res.end();
81                 }
82         });
83 }