From 716e9e07fd9d936380553d512e8a8abe21caa4f9 Mon Sep 17 00:00:00 2001 From: Yihong Wang Date: Sat, 27 May 2017 17:14:09 -0700 Subject: 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 Reviewed-By: Matteo Collina --- lib/_http_incoming.js | 3 ++ test/disabled/test-http-abort-stream-end.js | 62 ----------------------------- test/parallel/test-http-abort-stream-end.js | 60 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 62 deletions(-) delete mode 100644 test/disabled/test-http-abort-stream-end.js create mode 100644 test/parallel/test-http-abort-stream-end.js diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index 9151836ad8..6e5aff1cc9 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -314,6 +314,9 @@ function _addHeaderLine(field, value, dest) { IncomingMessage.prototype._dump = function _dump() { if (!this._dumped) { this._dumped = true; + // If there is buffered data, it may trigger 'data' events. + // Remove 'data' event listeners explicitly. + this.removeAllListeners('data'); this.resume(); } }; diff --git a/test/disabled/test-http-abort-stream-end.js b/test/disabled/test-http-abort-stream-end.js deleted file mode 100644 index f754e60300..0000000000 --- a/test/disabled/test-http-abort-stream-end.js +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - -'use strict'; -const common = require('../common'); -const assert = require('assert'); - -const http = require('http'); - -var maxSize = 1024; -var size = 0; - -var s = http.createServer(function(req, res) { - this.close(); - - res.writeHead(200, {'Content-Type': 'text/plain'}); - for (var i = 0; i < maxSize; i++) { - res.write('x' + i); - } - res.end(); -}); - -var aborted = false; -s.listen(common.PORT, function() { - var req = http.get('http://localhost:' + common.PORT, function(res) { - res.on('data', function(chunk) { - size += chunk.length; - assert(!aborted, 'got data after abort'); - if (size > maxSize) { - aborted = true; - req.abort(); - size = maxSize; - } - }); - }); - - req.end(); -}); - -process.on('exit', function() { - assert(aborted); - assert.strictEqual(size, maxSize); - console.log('ok'); -}); diff --git a/test/parallel/test-http-abort-stream-end.js b/test/parallel/test-http-abort-stream-end.js new file mode 100644 index 0000000000..8f89aeffff --- /dev/null +++ b/test/parallel/test-http-abort-stream-end.js @@ -0,0 +1,60 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; +require('../common'); +const assert = require('assert'); + +const http = require('http'); + +const maxSize = 1024; +let size = 0; + +const s = http.createServer(function(req, res) { + this.close(); + + res.writeHead(200, {'Content-Type': 'text/plain'}); + for (let i = 0; i < maxSize; i++) { + res.write('x' + i); + } + res.end(); +}); + +let aborted = false; +s.listen(0, function() { + const req = http.get('http://localhost:' + s.address().port, function(res) { + res.on('data', function(chunk) { + size += chunk.length; + assert(!aborted, 'got data after abort'); + if (size > maxSize) { + aborted = true; + req.abort(); + size = maxSize; + } + }); + }); +}); + +process.on('exit', function() { + assert(aborted); + assert.strictEqual(size, maxSize); + console.log('ok'); +}); -- cgit v1.2.3