From a15944da75c44bb4f6c81a8f999b2443ab1e6a4e Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Tue, 5 Jul 2016 08:24:03 -0700 Subject: [PATCH] Cleanup for #3178 --- lib/transmit.js | 27 +++++++++++++++++---------- test/transmit.js | 3 ++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/transmit.js b/lib/transmit.js index bf9d1d40d..daaf14629 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -291,9 +291,9 @@ internals.transmit = function (response, callback) { // Write headers - const res = internals.writeHead(response); - if (res) { - return process.nextTick(callback, res); + const error = internals.writeHead(response); + if (error) { + return Hoek.nextTick(callback)(error); } // Write payload @@ -376,28 +376,35 @@ internals.transmit = function (response, callback) { internals.writeHead = function (response) { const res = response.request.raw.res; + const headers = Object.keys(response.headers); + let i = 0; try { - const headers = Object.keys(response.headers); - for (let i = 0; i < headers.length; ++i) { + for (; 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 + for (--i; i >= 0; --i) { + res.setHeader(headers[i], null); // Undo headers + } - res._headers = null; - res._headerNames = {}; + return Boom.wrap(err); + } + try { + res.writeHead(response.statusCode); + } + catch (err) { return Boom.wrap(err); } + + return null; }; diff --git a/test/transmit.js b/test/transmit.js index 0b87bbbd0..17d296700 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2372,13 +2372,14 @@ describe('transmission', () => { const handler = function (request, reply) { - return reply('ok').header('', 'test'); + return reply('ok').header('x', '1').header('', 'test'); }; server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', (res) => { expect(res.statusCode).to.equal(500); + expect(res.headers.x).to.not.exist(); done(); }); });