get ready for more static fileses
authorMike Shaver <shaver@mozilla.org>
Thu, 03 Feb 2011 08:40:05 +0000
changeset 32 33b9573c5757
parent 31 ad67b2adaa87
child 33 a7b6a81983f7
push id25
push usershaver@mozilla.com
push dateSun, 06 Feb 2011 05:21:47 +0000
get ready for more static fileses
lib/indexer.js
server/entry.html
server/static/index.html
server/websocket.js
--- a/lib/indexer.js
+++ b/lib/indexer.js
@@ -1,15 +1,14 @@
 var stemmer = require("./stemmer");
 
 const MIN_WORD_LENGTH = 3;
 const FORCED_HASH = {js: true, ff: true, "2000": true, "98": true};
 const STOP_HASH = {bug: true, the: true, for: true, but: true, and: true };
 
-
 const PUNCTUATION_RE = /[:<>|\]/_@\[\"]/g;
 const PUNCTUATION_START_RE = /^[\.:<>|\]/\',\?!()@\[\"]/;
 const PUNCTUATION_END_RE = /[\.:<>|\]/\',\?!()@\[\"]+$/;
 
 exports.processKeywords = function(words)
 {
     words = words.filter(function (v) {
 			     return (v.length >= MIN_WORD_LENGTH || FORCED_HASH[v.toLowerCase()] === true) &&
rename from server/entry.html
rename to server/static/index.html
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -73,34 +73,63 @@ function runSearch(message, cb)
         return undefined;
     }
 
     return undefined;
 }
 
 const ENTRYHTML_PATH = path.normalize(path.join(__dirname, "entry.html"));
 
+function typeForPathname(path)
+{
+    var suffix = path.replace(/.*(\.[-.]*)/, '$1');
+    switch (suffix) {
+    case '.html': return 'text/html';
+    case '.css': return 'text/css';
+    case '.js': return 'application/javascript';
+    default: return 'text/plain';
+    }
+}
+
 function handleRequest(req, res)
 {
     var urlx = url.parse(req.url);
     var pathname = urlx.pathname;
     
-    if (req.method == "GET" && (pathname == "/" || pathname == "/entry.html")) {
-        res.writeHead(200, {'Content-Type': 'text/html'});
-        fs.createReadStream(ENTRYHTML_PATH, { flags: 'r', bufferSize: 4*1024 }).
-            addListener('data', function(chunk) { res.write(chunk); }).
-            addListener('end', res.end.bind(res));
+    if (pathname == "/")
+        pathname = "/static/index.html";
+    
+    pathname.replace(/\.\.\//g, ''); // naughty, naughty
+
+    if (req.method == "GET" && pathname.substr(0, 7) == "/static") {
+        var filename = path.join(process.cwd(), pathname);
+        path.exists(filename, function (exists) {
+            if (!exists) {
+                res.writeHead(404, {'Content-type': 'text/plain'});
+                res.write("404 Not found\n");
+                res.end();
+                return;
+            }
+            
+            res.writeHead(200, {'Content-Type': typeForPathname(pathname)});
+            fs.createReadStream(pathname.substr(1), { flags: 'r', bufferSize: 4*1024 }).
+                addListener('data', function(chunk) { res.write(chunk); }).
+                addListener('end', res.end.bind(res));
+        });
+        return;
     }
     
     if (req.method == "GET" && pathname == "/json") {
         terms = decodeURIComponent(urlx.query);
         res.writeHead(200, {'Content-Type': 'text/plain'});
         runSearch(terms, function (data) { res.write(data); res.end(); });
+        return;
     }
-           
+    
+    // fall through to socket.io's handling
 }
 
 var httpServer = http.createServer(handleRequest);
 
 var clientCount = 0;
 
 var socket = io.listen(httpServer);
 socket.on("clientConnect",