From b38277be266cfd6b6c3cc9e00730898f62a11f6e Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Tue, 18 Sep 2012 18:26:08 +0200 Subject: [PATCH] http: add response.headersSent property Boolean property getter. True if headers was sent, false otherwise. --- doc/api/http.markdown | 4 ++++ lib/http.js | 6 ++++++ test/simple/test-http-header-read.js | 2 ++ 3 files changed, 12 insertions(+) diff --git a/doc/api/http.markdown b/doc/api/http.markdown index c6809067ee5..8d5cd138740 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -398,6 +398,10 @@ or response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]); +### response.headersSent + +Boolean (read-only). True if headers were sent, false otherwise. + ### response.sendDate When true, the Date header will be automatically generated and sent in diff --git a/lib/http.js b/lib/http.js index e4d0dcd8f3e..c4e0a77aadf 100644 --- a/lib/http.js +++ b/lib/http.js @@ -699,6 +699,12 @@ OutgoingMessage.prototype._renderHeaders = function() { }; +Object.defineProperty(OutgoingMessage.prototype, 'headersSent', { + configurable: true, + enumerable: true, + get: function () { return !!this._header; } +}); + OutgoingMessage.prototype.write = function(chunk, encoding) { if (!this._header) { diff --git a/test/simple/test-http-header-read.js b/test/simple/test-http-header-read.js index de20df6044f..e844deafcf2 100644 --- a/test/simple/test-http-header-read.js +++ b/test/simple/test-http-header-read.js @@ -30,7 +30,9 @@ 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 (Issue 752)