Skip to content

Commit

Permalink
Stock 404 response
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBeale committed May 5, 2015
1 parent 4635b18 commit 8d05c54
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions lib/controllers.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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 = '<!DOCTYPE html>\n<html><head><title>404 - Resource Not Found</title></head><body><h1>404 - Resource Not Found</h1></body></html>';

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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 8d05c54

Please sign in to comment.