From 8d05c54654f41d49c2e9511ffea646308fd403fc Mon Sep 17 00:00:00 2001 From: David Beale Date: Tue, 5 May 2015 10:31:13 +0100 Subject: [PATCH] Stock 404 response --- lib/controllers.js | 48 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/controllers.js b/lib/controllers.js index 04ff9080..a6ad22eb 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -1,8 +1,11 @@ 'use strict'; -module.exports = function (rootDirectory, logger, indexDocument, errorDocument) { + var FileStore = require('./file-store'), - fileStore = new FileStore(rootDirectory), - templateBuilder = require('./xml-template-builder'); + templateBuilder = require('./xml-template-builder'), + path = require('path'); + +module.exports = function (rootDirectory, logger, indexDocument, errorDocument) { + var fileStore = new FileStore(rootDirectory); var buildXmlResponse = function (res, status, template) { res.header('Content-Type', 'application/xml'); @@ -20,13 +23,35 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument) res.header(metaData.key, metaData.value); }); } - res.status(200); + res.status(status); if (req.method === 'HEAD') { return res.end(); } return res.end(data); }; + var errorResponse = function(req, res, keyName) { + logger.error('Object "%s" in bucket "%s" does not exist', keyName, req.bucket.name); + + if (indexDocument) + { + var ErrorDoc = '\n404 - Resource Not Found

404 - Resource Not Found

'; + + return buildResponse(req, res, 404, { + md4: '', + modifiedDate: new Date(), + contentType: 'text/html', + customMetaData: [], + size: ErrorDoc.length + }, ErrorDoc); + } + else + { + var template = templateBuilder.buildKeyNotFound(keyName); + return buildXmlResponse(res, 404, template); + } + }; + /** * The following methods correspond the S3 api. For more information visit: * http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html @@ -75,9 +100,7 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument) fileStore.getObject(req.bucket, result.key, function (err, object, data) { if (err) { - var template = templateBuilder.buildKeyNotFound(keyName); - logger.error('Object "%s" in bucket "%s" does not exist', keyName, req.bucket.name); - return buildXmlResponse(res, 404, template); + return errorResponse(req, res, object.key); } else { @@ -156,12 +179,11 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument) if (indexDocument) { - return fileStore.getObject(req.bucket, keyName + indexDocument, function (err, object, data) { + keyName = path.join(keyName, indexDocument); + return fileStore.getObject(req.bucket, keyName, function (err, object, data) { if (err) { - var template = templateBuilder.buildKeyNotFound(keyName); - logger.error('Object "%s" in bucket "%s" does not exist', keyName, req.bucket.name); - return buildXmlResponse(res, 404, template); + return errorResponse(req, res, keyName); } else { @@ -171,9 +193,7 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument) } else { - var template = templateBuilder.buildKeyNotFound(keyName); - logger.error('Object "%s" in bucket "%s" does not exist', keyName, req.bucket.name); - return buildXmlResponse(res, 404, template); + return errorResponse(req, res, keyName); } }