mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
serve 404 when not possible key
This commit is contained in:
parent
9ed330bdae
commit
32b4f842b7
3 changed files with 38 additions and 21 deletions
1
TODO
1
TODO
|
@ -1,6 +1,5 @@
|
||||||
cache headers for static assets (and on document GET)
|
cache headers for static assets (and on document GET)
|
||||||
tests
|
tests
|
||||||
add FAVICON (or force 404)
|
|
||||||
add feedback for errors to UI - esp. too long
|
add feedback for errors to UI - esp. too long
|
||||||
add about page
|
add about page
|
||||||
|
|
||||||
|
|
|
@ -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
|
// Generate a random key
|
||||||
DocumentHandler.prototype.randomKey = function() {
|
DocumentHandler.prototype.randomKey = function() {
|
||||||
var text = '';
|
var text = '';
|
||||||
|
|
|
@ -3,6 +3,8 @@ var fs = require('fs');
|
||||||
|
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
|
|
||||||
|
var DocumentHandler = require('./document_handler');
|
||||||
|
|
||||||
// For serving static assets
|
// For serving static assets
|
||||||
|
|
||||||
var StaticHandler = function(path, cacheAssets) {
|
var StaticHandler = function(path, cacheAssets) {
|
||||||
|
@ -33,27 +35,28 @@ StaticHandler.contentTypeFor = function(ext) {
|
||||||
|
|
||||||
// Handle a request, and serve back the asset if it exists
|
// Handle a request, and serve back the asset if it exists
|
||||||
StaticHandler.prototype.handle = function(incPath, response) {
|
StaticHandler.prototype.handle = function(incPath, response) {
|
||||||
// Go to index if not found or /
|
// If this is a potential key, show the index - otherwise
|
||||||
if (!this.availablePaths[incPath]) incPath = this.defaultPath;
|
// bust out a 404
|
||||||
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
|
if (!this.availablePaths[incPath]) {
|
||||||
// And then stream the file back - either from the cache or from source
|
if (incPath === '/' || DocumentHandler.potentialKey(incPath.substring(1))) {
|
||||||
var cached = this.cacheAssets && this.isCached(filePath);
|
incPath = this.defaultPath;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Or break down and cry
|
|
||||||
else {
|
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 });
|
winston.error('unable to read file', { path: filePath, error: error });
|
||||||
response.writeHead(500, { 'content-type': 'application/json' });
|
response.writeHead(500, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
|
response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
|
||||||
|
@ -62,6 +65,16 @@ StaticHandler.prototype.handle = function(incPath, response) {
|
||||||
StaticHandler.cache[filePath] = null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue