diff options
author | Alexander Borisov <alexander.borisov@nginx.com> | 2018-12-19 15:55:54 +0300 |
---|---|---|
committer | Alexander Borisov <alexander.borisov@nginx.com> | 2018-12-19 15:55:54 +0300 |
commit | de3c062c6e3e869d726b93a1ffe617059df7611a (patch) | |
tree | 2c2cba0e96befd1dffe7df0b1b565f87e414d7d8 /src/nodejs/unit-http | |
parent | aeb026c8ab41b907f2e19a9f2fa978717d034830 (diff) | |
download | unit-de3c062c6e3e869d726b93a1ffe617059df7611a.tar.gz unit-de3c062c6e3e869d726b93a1ffe617059df7611a.tar.bz2 |
Node.js: buffering HTTP headers before writing the body.
Diffstat (limited to '')
-rwxr-xr-x | src/nodejs/unit-http/http_server.js | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 57163c0b..47851c98 100755 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -178,21 +178,16 @@ function writeHead(statusCode, reason, obj) { } } } - - unit_lib.unit_response_headers(this, statusCode, this.headers, this.headers_count, this.headers_len); - - this.headersSent = true; }; ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { var contentLength = 0; if (!this.headersSent) { - this.writeHead(this.statusCode); - } + unit_lib.unit_response_headers(this, this.statusCode, this.headers, + this.headers_count, this.headers_len); - if (this.finished) { - return this; + this.headersSent = true; } if (typeof chunk === 'function') { @@ -225,15 +220,23 @@ ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { }; ServerResponse.prototype.write = function write(chunk, encoding, callback) { + if (this.finished) { + throw new Error("Write after end"); + } + this._writeBody(chunk, encoding, callback); return true; }; ServerResponse.prototype.end = function end(chunk, encoding, callback) { - this._writeBody(chunk, encoding, callback); + if (!this.finished) { + this._writeBody(chunk, encoding, callback); - this.finished = true; + unit_lib.unit_response_end(this); + + this.finished = true; + } return this; }; |