Skip to content

Commit 895f89d

Browse files
srhry
authored andcommitted
Avoided sending empty chunkedEncoding chunks in the middle of http responses
1 parent 15ec99e commit 895f89d

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

lib/http.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,13 @@ OutgoingMessage.prototype.write = function (chunk, encoding) {
392392

393393
encoding = encoding || "ascii";
394394
if (this.chunkedEncoding) {
395-
if (typeof chunk == 'string') {
396-
this._send(process._byteLength(chunk, encoding).toString(16));
397-
} else {
398-
this._send(chunk.length.toString(16));
395+
var chunkLength = (typeof chunk == 'string' ? process._byteLength(chunk, encoding) : chunk.length);
396+
if (chunkLength > 0) {
397+
this._send(chunkLength.toString(16));
398+
this._send(CRLF);
399+
this._send(chunk, encoding);
400+
this._send(CRLF);
399401
}
400-
this._send(CRLF);
401-
this._send(chunk, encoding);
402-
this._send(CRLF);
403402
} else {
404403
this._send(chunk, encoding);
405404
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
PORT = 8000;
2+
sys = require('sys');
3+
http = require('http');
4+
assert = require('assert');
5+
6+
server = http.createServer(function (request, response) {
7+
sys.puts('responding to ' + request.url);
8+
9+
response.writeHead(200, {'Content-Type': 'text/plain'});
10+
response.write('1\n');
11+
response.write('');
12+
response.write('2\n');
13+
response.write('');
14+
response.end('3\n');
15+
16+
this.close();
17+
})
18+
server.listen(PORT);
19+
20+
var response="";
21+
22+
process.addListener('exit', function () {
23+
assert.equal('1\n2\n3\n', response);
24+
});
25+
26+
27+
server.addListener('listening', function () {
28+
var client = http.createClient(PORT);
29+
var req = client.request("/");
30+
req.end();
31+
req.addListener('response', function (res) {
32+
assert.equal(200, res.statusCode);
33+
res.setEncoding("ascii");
34+
res.addListener('data', function (chunk) {
35+
response += chunk;
36+
});
37+
sys.error("Got /hello response");
38+
});
39+
});
40+

0 commit comments

Comments
 (0)