http: suppress data event if req aborted

Re-enable test-http-abort-stream-end and put it into parallel
category. Use system random port when calling server.listen()
and fix eslint errors.

After calling request.abort(), in order to avoid the buffered
data to trigger the 'data' event, explicitly remove 'data' event
listeners.

PR-URL: https://github.com/nodejs/node/pull/13260
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Yihong Wang 2017-05-27 17:14:09 -07:00 committed by Matteo Collina
parent fd54b10500
commit 716e9e07fd
2 changed files with 11 additions and 10 deletions

View File

@ -314,6 +314,9 @@ function _addHeaderLine(field, value, dest) {
IncomingMessage.prototype._dump = function _dump() { IncomingMessage.prototype._dump = function _dump() {
if (!this._dumped) { if (!this._dumped) {
this._dumped = true; this._dumped = true;
// If there is buffered data, it may trigger 'data' events.
// Remove 'data' event listeners explicitly.
this.removeAllListeners('data');
this.resume(); this.resume();
} }
}; };

View File

@ -20,27 +20,27 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const http = require('http'); const http = require('http');
var maxSize = 1024; const maxSize = 1024;
var size = 0; let size = 0;
var s = http.createServer(function(req, res) { const s = http.createServer(function(req, res) {
this.close(); this.close();
res.writeHead(200, {'Content-Type': 'text/plain'}); res.writeHead(200, {'Content-Type': 'text/plain'});
for (var i = 0; i < maxSize; i++) { for (let i = 0; i < maxSize; i++) {
res.write('x' + i); res.write('x' + i);
} }
res.end(); res.end();
}); });
var aborted = false; let aborted = false;
s.listen(common.PORT, function() { s.listen(0, function() {
var req = http.get('http://localhost:' + common.PORT, function(res) { const req = http.get('http://localhost:' + s.address().port, function(res) {
res.on('data', function(chunk) { res.on('data', function(chunk) {
size += chunk.length; size += chunk.length;
assert(!aborted, 'got data after abort'); assert(!aborted, 'got data after abort');
@ -51,8 +51,6 @@ s.listen(common.PORT, function() {
} }
}); });
}); });
req.end();
}); });
process.on('exit', function() { process.on('exit', function() {