Skip to content

Commit

Permalink
Double content-type pass. Closes #2979
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 11, 2016
1 parent 0c8109e commit e033f1e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
9 changes: 9 additions & 0 deletions lib/response.js
Expand Up @@ -24,6 +24,7 @@ exports = module.exports = internals.Response = function (source, request, optio
this.statusCode = null;
this.headers = {}; // Incomplete as some headers are stored in flags
this.variety = null;
this.source = null;
this.app = {};
this.plugins = {};
this.send = null; // Set by reply()
Expand All @@ -41,6 +42,7 @@ exports = module.exports = internals.Response = function (source, request, optio
this._payload = null; // Readable stream
this._takeover = false;
this._contentEncoding = null; // Set during transmit
this._contentType = null; // Used if no explicit content-type is set and type is known
this._error = null; // The boom object when created from an error

this._processors = {
Expand Down Expand Up @@ -77,6 +79,7 @@ internals.Response.prototype._setSource = function (source, variety) {
}
else if (Buffer.isBuffer(source)) {
this.variety = 'buffer';
this._contentType = 'application/octet-stream';
}
else if (source instanceof Stream) {
this.variety = 'stream';
Expand All @@ -88,6 +91,12 @@ internals.Response.prototype._setSource = function (source, variety) {
}

this.source = source;

if (this.variety === 'plain' &&
this.source !== null) {

this._contentType = (typeof this.source === 'string' ? 'text/html' : 'application/json');
}
};


Expand Down
23 changes: 8 additions & 15 deletions lib/transmit.js
Expand Up @@ -44,7 +44,7 @@ internals.marshal = function (request, next) {
const response = request.response;

Cors.headers(response);
internals.content(response);
internals.content(response, false);
internals.security(response);

if (response.statusCode !== 304 &&
Expand Down Expand Up @@ -141,6 +141,7 @@ internals.marshal = function (request, next) {
response._header('content-length', response._payload.size(), { override: false });
}

internals.content(response, true);
return Auth.response(request, next); // Must be last in case requires access to headers
});
});
Expand Down Expand Up @@ -427,25 +428,17 @@ internals.security = function (response) {
};


internals.content = function (response) {
internals.content = function (response, postMarshal) {

const type = response.headers['content-type'];
if (!type) {
const charset = (response.settings.charset ? '; charset=' + response.settings.charset : '');

if (typeof response.source === 'string') {
response.type('text/html' + charset);
}
else if (Buffer.isBuffer(response.source)) {
response.type('application/octet-stream');
}
else if (response.variety === 'plain' &&
response.source !== null) {

response.type('application/json' + charset);
if (response._contentType) {
const charset = (response.settings.charset && response._contentType !== 'application/octet-stream' ? '; charset=' + response.settings.charset : '');
response.type(response._contentType + charset);
}
}
else if (response.settings.charset &&
else if ((!response._contentType || !postMarshal) &&
response.settings.charset &&
type.match(/^(?:text\/)|(?:application\/(?:json)|(?:javascript))/)) {

const hasParams = (type.indexOf(';') !== -1);
Expand Down
2 changes: 1 addition & 1 deletion test/connection.js
Expand Up @@ -1566,7 +1566,7 @@ describe('Connection', () => {
server.inject({ method: 'HEAD', url: '/' }, (res) => {

expect(res.statusCode).to.equal(205);
expect(res.headers['content-type']).to.contain('text/html');
expect(res.headers['content-type']).to.equal('text/html; charset=utf-8');
expect(res.headers['content-length']).to.not.exist();
expect(res.headers.etag).to.equal('"test"');
expect(res.result).to.not.exist();
Expand Down
4 changes: 2 additions & 2 deletions test/response.js
Expand Up @@ -880,10 +880,10 @@ describe('Response', () => {
response.type('text/html');
}

return callback(null, response.source);
return callback(null, response.source.value);
};

return reply(request.generateResponse('text', { variety: 'test', marshal }));
return reply(request.generateResponse({ value: 'text' }, { variety: 'test', marshal }));
};

const onPreResponse = function (request, reply) {
Expand Down

0 comments on commit e033f1e

Please sign in to comment.