handle errors (like EISDIR) better
authorMike Shaver <shaver@mozilla.org>
Mon, 07 Feb 2011 18:39:21 +0000
changeset 35 a32498bf2edb
parent 34 7d87f021cc64
child 36 7c4d0cd619e8
push id26
push usershaver@mozilla.com
push date2011-02-07 18:43 +0000
handle errors (like EISDIR) better
server/websocket.js
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -97,28 +97,33 @@ function handleRequest(req, 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) {
+            function send404()
+            {
                 res.writeHead(404, {'Content-type': 'text/plain'});
                 res.write("404 Not found\n");
                 res.end();
+            }
+            
+            if (!exists) {
+                send404();
                 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)).
-                addListener('error', util.log);
+                addListener('error', function (err) { util.log(err); send404(); });
             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(); });