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

Handle thrown error from res.setHeader() and res.writeHead() #3178

Merged
merged 2 commits into from
Jul 5, 2016
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
58 changes: 45 additions & 13 deletions lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ exports.send = function (request, callback) {
return internals.fail(request, err, callback);
}

return internals.transmit(response, callback);
return internals.transmit(response, (err) => {

if (err) {
request._setResponse(err);
return internals.fail(request, err, callback);
}

return callback();
});
});
};

Expand Down Expand Up @@ -274,23 +282,19 @@ internals.transmit = function (response, callback) {
response.headers.etag = response.headers.etag.slice(0, -1) + '-' + (response.headers['content-encoding'] || encoding) + '"';
}

// Write headers

const headers = Object.keys(response.headers);
for (let i = 0; i < headers.length; ++i) {
const header = headers[i];
const value = response.headers[header];
if (value !== undefined) {
request.raw.res.setHeader(header, value);
}
}
// Connection: close

const isInjection = Shot.isInjection(request.raw.req);
if (!isInjection && !request.connection._started) {
request.raw.res.setHeader('connection', 'close');
response._header('connection', 'close');
}

request.raw.res.writeHead(response.statusCode);
// Write headers

const res = internals.writeHead(response);
if (res) {
return process.nextTick(callback, res);
}

// Write payload

Expand Down Expand Up @@ -369,6 +373,34 @@ internals.transmit = function (response, callback) {
};


internals.writeHead = function (response) {

const res = response.request.raw.res;

try {
const headers = Object.keys(response.headers);
for (let i = 0; i < headers.length; ++i) {
const header = headers[i];
const value = response.headers[header];
if (value !== undefined) {
res.setHeader(header, value);
}
}

res.writeHead(response.statusCode);
}
catch (err) {

// Reset headers and return err

res._headers = null;
res._headerNames = {};

return Boom.wrap(err);
}
};


internals.Empty = function () {

Stream.Readable.call(this);
Expand Down
49 changes: 49 additions & 0 deletions test/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,55 @@ describe('transmission', () => {
done();
});
});

it('returns 500 when node rejects a header', (done) => {

const server = new Hapi.Server();
server.connection();

const handler = function (request, reply) {

return reply('ok').header('', 'test');
};

server.route({ method: 'GET', path: '/', handler: handler });
server.inject('/', (res) => {

expect(res.statusCode).to.equal(500);
done();
});
});

it('returns 500 for out of range status code', (done) => {

const server = new Hapi.Server();
server.connection();

const handler = function (request, reply) {

// Patch writeHead to always fail on out of range headers

const origWriteHead = request.raw.res.writeHead;
request.raw.res.writeHead = function (statusCode) {

statusCode |= 0;
if (statusCode < 100 || statusCode > 999) {
throw new RangeError(`Invalid status code: ${statusCode}`);
}

return origWriteHead.apply(this, arguments);
};

return reply('ok').code(1);
};

server.route({ method: 'GET', path: '/', handler: handler });
server.inject('/', (res) => {

expect(res.statusCode).to.equal(500);
done();
});
});
});

describe('cache()', () => {
Expand Down