Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding file mode option for inline, attachment or no content-disposition header #704

Merged
merged 2 commits into from
Mar 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ http.route({ method: 'GET', path: '/{path}', handler: { file: filePath } });
http.start();
```

File handlers also support specifying if the _'Content-Disposition'_ should be sent on the response. The default is to not send this header, however if it needs to be sent there is an optional _'mode'_ property that can be set to _'attachment'_, _'inline'_, or false. Below is an example setting the mode to _'attachment'_.

```javascript
// Create Hapi server
var http = new Hapi.Server('0.0.0.0', 8080, { files: { relativeTo: 'cwd' } });

// Serve index.html file up a directory in the public folder
http.route({ method: 'GET', path: '/', handler: { file: { path: './public/index.html', mode: 'attachment } } });

http.start();
```

Please note, that when setting _'file'_ to an object, the file path is assigned to the _'path'_ property.

#### Directory

Expand Down
8 changes: 5 additions & 3 deletions lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ var Response = require('./response');
var internals = {};


exports.fileHandler = function (route, filePath) {
exports.fileHandler = function (route, options) {

var filePath = (typeof options === 'object') ? options.path : options;
var mode = (typeof options === 'object') ? options.mode : false;
Utils.assert(filePath && (typeof filePath === 'function' || typeof filePath === 'string'), 'Invalid file path');
Utils.assert(typeof filePath !== 'string' || route.params.length === 0, 'Route path with static file path cannot contain a parameter');
Utils.assert(typeof filePath !== 'string' || filePath[filePath.length - 1] !== '/', 'File path cannot end with a \'/\'');
Expand Down Expand Up @@ -44,8 +46,8 @@ exports.fileHandler = function (route, filePath) {
path = staticPath;
}

return request.reply(new Response.File(path));
}
return request.reply(new Response.File(path, { mode: mode }));
};

return handler;
};
Expand Down
8 changes: 6 additions & 2 deletions lib/response/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ var internals = {

// File response (Base -> Generic -> Stream -> File)

exports = module.exports = internals.File = function (filePath) {
exports = module.exports = internals.File = function (filePath, options) {

Utils.assert(this.constructor === internals.File, 'File must be instantiated using new');
Utils.assert(!options || !options.mode || ['attachment', 'inline'].indexOf(options.mode) !== -1, 'options.mode must be either false, attachment, or inline');

Stream.call(this, null);
this.variety = 'file';
this.varieties.file = true;

this._filePath = Path.normalize(filePath);
this._mode = options ? options.mode : false;

return this;
};
Expand Down Expand Up @@ -83,7 +85,9 @@ internals.File.prototype._prepare = function (request, callback) {
});
}

self._headers['Content-Disposition'] = 'inline; filename=' + encodeURIComponent(fileName);
if (self._mode) {
self._headers['Content-Disposition'] = self._mode + '; filename=' + encodeURIComponent(fileName);
}

return Stream.prototype._prepare.call(self, request, callback);
});
Expand Down
109 changes: 107 additions & 2 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,13 @@ describe('Response', function () {
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.equal('inline; filename=package.json');
expect(res.headers['content-disposition']).to.not.exist;
done();
});
});
});

it('returns a file in the response with the correct headers using cwd relative paths', function (done) {
it('returns a file in the response with the correct headers using cwd relative paths without content-disposition header', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'cwd' } });
server.route({ method: 'GET', path: '/', handler: { file: './package.json' } });
Expand All @@ -486,6 +486,111 @@ describe('Response', function () {

Request.get(server.settings.uri, function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.not.exist;
done();
});
});
});

it('returns a file in the response with the inline content-disposition header when using route config', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'cwd' } });
server.route({ method: 'GET', path: '/', handler: { file: { path: './package.json', mode: 'inline' } }});

server.start(function () {

Request.get(server.settings.uri, function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.equal('inline; filename=package.json');
done();
});
});
});

it('returns a file in the response with the attachment content-disposition header when using route config', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'cwd' } });
server.route({ method: 'GET', path: '/', handler: { file: { path: './package.json', mode: 'attachment' } }});

server.start(function () {

Request.get(server.settings.uri, function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.equal('attachment; filename=package.json');
done();
});
});
});

it('returns a file in the response without the content-disposition header when using route config mode false', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'cwd' } });
server.route({ method: 'GET', path: '/', handler: { file: { path: './package.json', mode: false } }});

server.start(function () {

Request.get(server.settings.uri, function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.not.exist;
done();
});
});
});

it('returns a file with correct headers when using attachment mode', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'routes' } });
var handler = function (request) {

request.reply(new Hapi.Response.File(__dirname + '/../../package.json', { mode: 'attachment' }));
};

server.route({ method: 'GET', path: '/file', handler: handler });

server.start(function () {

Request.get(server.settings.uri + '/file', function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
expect(res.headers['content-length']).to.exist;
expect(res.headers['content-disposition']).to.equal('attachment; filename=package.json');
done();
});
});
});

it('returns a file with correct headers when using inline mode', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'routes' } });
var handler = function (request) {

request.reply(new Hapi.Response.File(__dirname + '/../../package.json', { mode: 'inline' }));
};

server.route({ method: 'GET', path: '/file', handler: handler });

server.start(function () {

Request.get(server.settings.uri + '/file', function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
expect(res.headers['content-type']).to.equal('application/json');
Expand Down