From 32b4f842b7c5ce9ed34e8fb4a74af65f15593778 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Mon, 21 Nov 2011 23:13:46 -0500 Subject: [PATCH] serve 404 when not possible key --- TODO | 1 - lib/document_handler.js | 5 ++++ lib/static_handler.js | 53 +++++++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index a9608eb..648d12e 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ cache headers for static assets (and on document GET) tests -add FAVICON (or force 404) add feedback for errors to UI - esp. too long add about page diff --git a/lib/document_handler.js b/lib/document_handler.js index d92297c..ee78c50 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -78,6 +78,11 @@ DocumentHandler.prototype.chooseKey = function(callback) { }); }; +// Return a boolean indicating whether or not something can be a key +DocumentHandler.potentialKey = function(key) { + return key.match(/^[a-zA-Z0-9]+$/); +}; + // Generate a random key DocumentHandler.prototype.randomKey = function() { var text = ''; diff --git a/lib/static_handler.js b/lib/static_handler.js index 53a34f7..a8af060 100644 --- a/lib/static_handler.js +++ b/lib/static_handler.js @@ -3,6 +3,8 @@ var fs = require('fs'); var winston = require('winston'); +var DocumentHandler = require('./document_handler'); + // For serving static assets var StaticHandler = function(path, cacheAssets) { @@ -33,27 +35,28 @@ StaticHandler.contentTypeFor = function(ext) { // Handle a request, and serve back the asset if it exists StaticHandler.prototype.handle = function(incPath, response) { - // Go to index if not found or / - if (!this.availablePaths[incPath]) incPath = this.defaultPath; - var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); - // And then stream the file back - either from the cache or from source - var cached = this.cacheAssets && this.isCached(filePath); - var method = cached ? this.serveCached : this.retrieve; - // Run! - var _this = this; - method(filePath, function(error, content) { - // Get the content - if (content) { - var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); - response.writeHead(200, { 'content-type': contentType }); - response.end(content, 'utf-8'); - // Stick it in the cache if its not in there - if (!cached && _this.cacheAssets) { - StaticHandler.cache[filePath] = content; - } + // If this is a potential key, show the index - otherwise + // bust out a 404 + if (!this.availablePaths[incPath]) { + if (incPath === '/' || DocumentHandler.potentialKey(incPath.substring(1))) { + incPath = this.defaultPath; } - // Or break down and cry else { + winston.warn('failed to find static asset', { path: incPath }); + response.writeHead(404, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ message: 'no such file' })); + return; + } + } + // And then stream the file back - either from the cache or from source + var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); + var cached = this.cacheAssets && this.isCached(filePath); + // Go get'er + var _this = this; + var method = cached ? this.serveCached : this.retrieve; + method(filePath, function(error, content) { + // detect errors + if (error) { winston.error('unable to read file', { path: filePath, error: error }); response.writeHead(500, { 'content-type': 'application/json' }); response.end(JSON.stringify({ message: 'IO: Unable to read file' })); @@ -62,6 +65,16 @@ StaticHandler.prototype.handle = function(incPath, response) { StaticHandler.cache[filePath] = null; } } + // Get the content + else { + var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); + response.writeHead(200, { 'content-type': contentType }); + response.end(content, 'utf-8'); + // Stick it in the cache if its not in there + if (!cached && _this.cacheAssets) { + StaticHandler.cache[filePath] = content; + } + } }); }; @@ -71,7 +84,7 @@ StaticHandler.prototype.retrieve = function(filePath, callback) { winston.verbose('loading static asset', { path: filePath }); fs.readFile(filePath, function(error, content) { callback(error, content); - }); + }); }; // Retrieve from memory cache