diff --git a/doc/api/http.markdown b/doc/api/http.markdown index f5811e4a476628..c5f160c9b7accd 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -381,6 +381,15 @@ Example: var contentType = response.getHeader('content-type'); +### response.getAllHeaders() + +Reads out all headers that are already been queued but not yet sent to the +client. This can only be called before headers get implicitly flushed. + +Example: + + var headers = response.getAllHeaders(); + ### response.removeHeader(name) Removes a header that's queued for implicit sending. diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 25157d34a44298..7c1c9503a6eb09 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -337,6 +337,14 @@ OutgoingMessage.prototype.getHeader = function(name) { }; +OutgoingMessage.prototype.getAllHeaders = function() { + if (!this._headers) + return; + else + return util._extend({}, this._headers); +}; + + OutgoingMessage.prototype.removeHeader = function(name) { if (arguments.length < 1) { throw new Error('`name` is required for removeHeader(name).'); diff --git a/test/parallel/test-http-header-get-all.js b/test/parallel/test-http-header-get-all.js new file mode 100644 index 00000000000000..40305a3570f6ec --- /dev/null +++ b/test/parallel/test-http-header-get-all.js @@ -0,0 +1,31 @@ +var common = require('../common'); +var assert = require('assert'); +var http = require('http'); + +var s = http.createServer(function(req, res) { + var contentType = 'content-type'; + var plain = 'text/plain'; + res.setHeader(contentType, plain); + assert.ok(!res.headersSent); + res.writeHead(200); + assert.ok(res.headersSent); + res.end('hello world\n'); + // This checks that after the headers have been sent, getHeader works + // and does not throw an exception (joyent/node Issue 752) + assert.doesNotThrow( + function() { + assert.deepStrictEqual({ 'content-type': 'text/plain' }, res.getAllHeaders()); + } + ); +}); + +s.listen(common.PORT, runTest); + +function runTest() { + http.get({ port: common.PORT }, function(response) { + response.on('end', function() { + s.close(); + }); + response.resume(); + }); +}