Skip to content

Commit e6b6075

Browse files
committed
http: Avoid 'data'/'end' events after pause()
Fixes #1040.
1 parent 48a9a2d commit e6b6075

2 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎lib/http.js‎

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var EventEmitter = require('events').EventEmitter;
2626
var FreeList = require('freelist').FreeList;
2727
var HTTPParser = process.binding('http_parser').HTTPParser;
2828
var assert = require('assert').ok;
29+
var END_OF_FILE = {};
2930

3031

3132
var debug;
@@ -108,11 +109,10 @@ var parsers = new FreeList('parsers', 1000, function() {
108109
parser.onBody = function(b, start, len) {
109110
// TODO body encoding?
110111
var slice = b.slice(start, start + len);
111-
if (parser.incoming._decoder) {
112-
var string = parser.incoming._decoder.write(slice);
113-
if (string.length) parser.incoming.emit('data', string);
112+
if (parser.incoming._paused || parser.incoming._pendings.length) {
113+
parser.incoming._pendings.push(slice);
114114
} else {
115-
parser.incoming.emit('data', slice);
115+
parser.incoming._emitData(slice);
116116
}
117117
};
118118

@@ -133,8 +133,12 @@ var parsers = new FreeList('parsers', 1000, function() {
133133

134134
if (!parser.incoming.upgrade) {
135135
// For upgraded connections, also emit this after parser.execute
136-
parser.incoming.readable = false;
137-
parser.incoming.emit('end');
136+
if (parser.incoming._paused || parser.incoming._pendings.length) {
137+
parser.incoming._pendings.push(END_OF_FILE);
138+
} else {
139+
parser.incoming.readable = false;
140+
parser.incoming.emit('end');
141+
}
138142
}
139143
};
140144

@@ -224,6 +228,9 @@ function IncomingMessage(socket) {
224228

225229
this.readable = true;
226230

231+
this._paused = false;
232+
this._pendings = [];
233+
227234
// request (server) only
228235
this.url = '';
229236

@@ -251,12 +258,44 @@ IncomingMessage.prototype.setEncoding = function(encoding) {
251258

252259

253260
IncomingMessage.prototype.pause = function() {
261+
this._paused = true;
254262
this.socket.pause();
255263
};
256264

257265

258266
IncomingMessage.prototype.resume = function() {
259-
this.socket.resume();
267+
this._paused = false;
268+
if (this.socket) {
269+
this.socket.resume();
270+
}
271+
if (this._pendings.length) {
272+
var self = this;
273+
process.nextTick(function() {
274+
while (!self._paused && self._pendings.length) {
275+
var chunk = self._pendings.shift();
276+
if (chunk !== END_OF_FILE) {
277+
assert(Buffer.isBuffer(chunk));
278+
self._emitData(chunk);
279+
} else {
280+
assert(self._pendings.length === 0);
281+
self.readable = false;
282+
self.emit('end');
283+
}
284+
}
285+
});
286+
}
287+
};
288+
289+
290+
IncomingMessage.prototype._emitData = function(d) {
291+
if (this._decoder) {
292+
var string = this._decoder.write(d);
293+
if (string.length) {
294+
this.emit('data', string);
295+
}
296+
} else {
297+
this.emit('data', d);
298+
}
260299
};
261300

262301

‎test/simple/test-http-pause.js‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
26+
var expectedServer = 'Request Body from Client';
27+
var resultServer = '';
28+
var expectedClient = 'Response Body from Server';
29+
var resultClient = '';
30+
31+
var server = http.createServer(function(req, res) {
32+
common.debug('pause server request');
33+
req.pause();
34+
setTimeout(function() {
35+
common.debug('resume server request');
36+
req.resume();
37+
req.setEncoding('utf8');
38+
req.on('data', function(chunk) {
39+
resultServer += chunk;
40+
});
41+
req.on('end', function() {
42+
common.debug(resultServer);
43+
res.writeHead(200);
44+
res.end(expectedClient);
45+
});
46+
}, 100);
47+
});
48+
49+
server.listen(common.PORT, function() {
50+
var req = http.request({
51+
port: common.PORT,
52+
path: '/',
53+
method: 'POST'
54+
}, function(res) {
55+
common.debug('pause client response');
56+
res.pause();
57+
setTimeout(function() {
58+
common.debug('resume client response');
59+
res.resume();
60+
res.on('data', function(chunk) {
61+
resultClient += chunk;
62+
});
63+
res.on('end', function() {
64+
common.debug(resultClient);
65+
server.close();
66+
});
67+
}, 100);
68+
});
69+
req.end(expectedServer);
70+
});
71+
72+
process.on('exit', function() {
73+
assert.equal(expectedServer, resultServer);
74+
assert.equal(expectedClient, resultClient);
75+
});

0 commit comments

Comments
 (0)