Skip to content

Commit 265cda9

Browse files
committed
Fix zero length buffer bug for http res.end()
Reported by Kadir Pekel <kadirpekel@gmail.com>
1 parent 1b24fc6 commit 265cda9

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ OutgoingMessage.prototype.end = function (data, encoding) {
560560
if (!hot) {
561561
if (this.chunkedEncoding) {
562562
ret = this._send('0\r\n' + this._trailer + '\r\n'); // Last chunk.
563-
} else if (!data) {
563+
} else {
564564
// Force a flush, HACK.
565565
ret = this._send('');
566566
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Serving up a zero-length buffer should work.
2+
3+
var common = require("../common");
4+
var assert = common.assert;
5+
var http = require('http');
6+
7+
var server = http.createServer(function (req, res) {
8+
var buffer = new Buffer(0);
9+
res.writeHead(200, {'Content-Type': 'text/html',
10+
'Content-Length': buffer.length});
11+
res.end(buffer);
12+
});
13+
14+
var gotResponse = false;
15+
var resBodySize = 0;
16+
17+
server.listen(common.PORT, function () {
18+
var client = http.createClient(common.PORT);
19+
20+
var req = client.request('GET', '/');
21+
req.end();
22+
23+
req.on('response', function (res) {
24+
gotResponse = true;
25+
26+
res.on('data', function (d) {
27+
resBodySize += d.length;
28+
});
29+
30+
res.on('end', function (d) {
31+
server.close();
32+
});
33+
});
34+
});
35+
36+
process.on('exit', function () {
37+
assert.ok(gotResponse);
38+
assert.equal(0, resBodySize);
39+
});
40+
41+

0 commit comments

Comments
 (0)