From b7da6101408efa34cb3385279f953a6d32600430 Mon Sep 17 00:00:00 2001 From: Eugene Girshov Date: Sun, 17 Feb 2013 00:55:11 +0200 Subject: [PATCH] emit error on aborted connection Emit 'error' if connection is closed before request received fully. Adjust test-fixtures to postpone socket.close(). add standalone test case on connection aborted --- lib/incoming_form.js | 3 ++- test/integration/test-fixtures.js | 7 +++++- test/legacy/simple/test-incoming-form.js | 1 + test/standalone/test-connection-aborted.js | 27 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/standalone/test-connection-aborted.js diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 47dafe37..fc41894c 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -80,6 +80,7 @@ IncomingForm.prototype.parse = function(req, cb) { }) .on('aborted', function() { self.emit('aborted'); + self._error(new Error('Request aborted')); }) .on('data', function(buffer) { self.write(buffer); @@ -257,7 +258,7 @@ IncomingForm.prototype._parseContentType = function() { }; IncomingForm.prototype._error = function(err) { - if (this.error) { + if (this.error || this.ended) { return; } diff --git a/test/integration/test-fixtures.js b/test/integration/test-fixtures.js index 66ad259e..eb32fd8c 100644 --- a/test/integration/test-fixtures.js +++ b/test/integration/test-fixtures.js @@ -78,6 +78,7 @@ function uploadFixture(name, cb) { parts.push({type: 'field', name: name, value: value}); }) .on('end', function() { + res.end('OK'); callback(null, parts); }); }); @@ -85,5 +86,9 @@ function uploadFixture(name, cb) { var socket = net.createConnection(common.port); var file = fs.createReadStream(common.dir.fixture + '/http/' + name); - file.pipe(socket); + file.pipe(socket, {end: false}); + socket.on('data', function () { + socket.end(); + }); + } diff --git a/test/legacy/simple/test-incoming-form.js b/test/legacy/simple/test-incoming-form.js index f7a0bc4b..d79a8e20 100644 --- a/test/legacy/simple/test-incoming-form.js +++ b/test/legacy/simple/test-incoming-form.js @@ -149,6 +149,7 @@ test(function parse() { gently.expect(form, 'emit',function(event) { assert.equal(event, 'aborted'); }); + gently.expect(form, '_error'); emit.aborted(); })(); diff --git a/test/standalone/test-connection-aborted.js b/test/standalone/test-connection-aborted.js new file mode 100644 index 00000000..4ea4431a --- /dev/null +++ b/test/standalone/test-connection-aborted.js @@ -0,0 +1,27 @@ +var assert = require('assert'); +var http = require('http'); +var net = require('net'); +var formidable = require('../../lib/index'); + +var server = http.createServer(function (req, res) { + var form = new formidable.IncomingForm(); + var aborted_received = false; + form.on('aborted', function () { + aborted_received = true; + }); + form.on('error', function () { + assert(aborted_received, 'Error event should follow aborted'); + server.close(); + }); + form.on('end', function () { + throw new Error('Unexpected "end" event'); + }); + form.parse(req); +}).listen(0, 'localhost', function () { + var client = net.connect(server.address().port); + client.write( + "POST / HTTP/1.1\r\n" + + "Content-Length: 70\r\n" + + "Content-Type: multipart/form-data; boundary=foo\r\n\r\n"); + client.end(); +});