1 var url = require("url");
2 var fs = require("fs");
3 var cache = require("./cache.js");
4 var path = require("path");
5 var log = require("./log.js");
7 exports.routeRequest = function(req, res) {
8 // first, unify the request
9 log.debug("request: ", req.url);
10 var thisQuery = unifyRequest(req, res, function(unified) {
11 log.debug("unified request is ", unified);
12 if(unified.requestFor == "/favicon.ico") {
13 unified.b.writeHead(404, {"Content-Type": "text/plain"});
14 unified.b.write("404 Not Found\n");
15 } else if(unified.exists) {
16 if(typeof global.repoproxy.downloads[unified.fullFilePath] != "undefined" && global.repoproxy.downloads[unified.fullFilePath] == 1) {
17 cache.upstreamRequest(unified);
18 } else if(unified.isFile) {
19 cache.serviceFile(unified);
20 } else if(unified.isDirectory) {
21 cache.serviceDirectory(unified);
23 log.debug("ERROR: something went majorly wrong with something, ", unified);
26 // it doesnt exist yet, so we send it to the cache service if it matches an upstream service
27 if(typeof global.repoproxy.repo[unified.topPath] != "undefined") {
28 log.debug("file doesnt exist, upstream we go: ", unified);
29 cache.upstreamRequest(unified);
31 unified.b.writeHead(404, {"Content-Type": "text/plain"});
32 unified.b.write("404 Not Found\n");
39 function unifyRequest(req, res, callback) {
40 var unified = new Object();
41 var originalurl = url.parse(req.url);
43 // create the base unified object
47 // create the request url
48 // remove /pub if it exists
49 unified.requestFor = originalurl.pathname.replace(/^\/pub/, "");
50 unified.originalReq = originalurl.pathname;
52 // create the full file path by spanning the cachedir
53 unified.fullFilePath = (global.repoproxy.cacheDir + "/" + originalurl.pathname.replace(/^\/pub/, "")).replace(/\/+/g, "/");
55 // determine the topPath, subpath etc.
56 var spl = unified.requestFor.split("/");
57 unified.topPath = spl[1];
58 unified.topFullPath = (global.repoproxy.cacheDir + "/" + spl[1]).replace(/\/+/g, "/");
61 for(var i=2; i < spl.length; i++) {
62 if(unified.subPath == "") unified.subPath = spl[i];
63 else unified.subPath += "/" + spl[i];
66 unified.subPath = null;
69 // determine if the request is for a directory
70 if(unified.requestFor.match(/\/$/) != null) {
71 unified.isDirectoryRequest = true;
72 unified.fullPathDirName = unified.fullFilePath;
73 unified.subPathDirName = unified.subPath;
75 unified.isDirectoryRequest = false;
76 unified.fullPathDirName = path.dirname(unified.fullFilePath);
77 unified.subPathDirName = path.dirname(unified.subPath);
81 fs.stat(unified.fullFilePath, function(err, stats) {
83 unified.exists = true;
84 if(stats.isDirectory() && !unified.isDirectoryRequest) {
85 //send a 302 and call it a day
86 res.writeHead("302", { 'Location': unified.originalReq+"/" });
90 if(stats.isDirectory()) {
91 unified.isDirectory = true;
92 unified.isFile = false;
93 unified.fileSize = null;
94 } else if(stats.isFile()) {
95 unified.isDirectory = false;
96 unified.isFile = true;
97 unified.fileSize = stats["size"];
99 unified.isDirectory = false;
100 unified.isFile = false;
101 unified.fileSize = null;
104 unified.exists = false;
105 unified.fileSize = null;
114 exports.unifyRequest = unifyRequest;