Skip to content
This repository
  • 3 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
Jul 13, 2010
Felix Geisendörfer Add missing err parameter to docs f92b1db
Jul 15, 2010
Felix Geisendörfer Feature: IncomingForm.timeout
By default uploads that are not receiving data for longer than 30
seconds will now emit an error.
5e2612c
Felix Geisendörfer Bump version 464fc9d
8  Readme.md
Source Rendered
@@ -43,7 +43,7 @@ Parse an incoming file upload.
43 43
       if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
44 44
         // parse a file upload
45 45
         var form = new formidable.IncomingForm();
46  
-        form.parse(req, function(fields, files) {
  46
+        form.parse(req, function(err, fields, files) {
47 47
           res.writeHead(200, {'content-type': 'text/plain'});
48 48
           res.write('received upload:\n\n');
49 49
           res.end(sys.inspect({fields: fields, files: files}));
@@ -74,6 +74,12 @@ Creates a new incoming form.
74 74
 
75 75
 The encoding to use for incoming form fields.
76 76
 
  77
+#### incomingForm.timeout = 30000
  78
+
  79
+The time in ms after which a stalled upload causes an error. Can be set to
  80
+`false` to disable this feature. The resulting error object will have a
  81
+property `timeout` that is set to `true`.
  82
+
77 83
 #### incomingForm.uploadDir = '/tmp'
78 84
 
79 85
 The directory for placing file uploads in. You can later on move them using `fs.rename()`.
10  lib/formidable/incoming_form.js
@@ -17,6 +17,7 @@ function IncomingForm() {
17 17
   this.keepExtensions = false;
18 18
   this.uploadDir = '/tmp';
19 19
   this.encoding = 'utf-8';
  20
+  this.timeout = 30000;
20 21
   this.headers = null;
21 22
   this.type = null;
22 23
 
@@ -62,6 +63,15 @@ IncomingForm.prototype.parse = function(req, cb) {
62 63
   this.writeHeaders(req.headers);
63 64
 
64 65
   var self = this;
  66
+  if (this.timeout) {
  67
+    req.connection.setTimeout(this.timeout);
  68
+    req.connection.addListener('timeout', function() {
  69
+      var err = new Error('timeout, no data received for '+self.timeout+'ms')
  70
+      err.timeout = true;
  71
+      self._error(err);
  72
+    });
  73
+  }
  74
+
65 75
   req
66 76
     .addListener('error', function(err) {
67 77
       self._error(err);
2  package.json
... ...
@@ -1,5 +1,5 @@
1 1
 { "name" : "formidable"
2  
-, "version": "0.9.4"
  2
+, "version": "0.9.5"
3 3
 , "dependencies": {"gently": ">=0.7.0"}
4 4
 , "directories" : { "lib" : "./lib/formidable" }
5 5
 , "main" : "./lib/formidable/index"
26  test/simple/test-incoming-form.js
@@ -29,6 +29,7 @@ test(function constructor() {
29 29
   assert.strictEqual(form.keepExtensions, false);
30 30
   assert.strictEqual(form.uploadDir, '/tmp');
31 31
   assert.strictEqual(form.encoding, 'utf-8');
  32
+  assert.strictEqual(form.timeout, 30000);
32 33
   assert.strictEqual(form.bytesReceived, null);
33 34
   assert.strictEqual(form.bytesExpected, null);
34 35
   assert.strictEqual(form._parser, null);
@@ -38,13 +39,23 @@ test(function constructor() {
38 39
 });
39 40
 
40 41
 test(function parse() {
41  
-  var REQ = {headers: {}}
42  
-    , emit = {};
  42
+  var REQ = {headers: {}, connection: {}}
  43
+    , emit = {}
  44
+    , reqEmit = {};
43 45
 
44 46
   gently.expect(form, 'writeHeaders', function(headers) {
45 47
     assert.strictEqual(headers, REQ.headers);
46 48
   });
47 49
 
  50
+  gently.expect(REQ.connection, 'setTimeout', function(timeout) {
  51
+    assert.equal(timeout, form.timeout);
  52
+  });
  53
+
  54
+  gently.expect(REQ.connection, 'addListener', function(event, fn) {
  55
+    assert.equal(event, 'timeout');
  56
+    reqEmit[event] = fn;
  57
+  });
  58
+
48 59
   var events = ['error', 'data', 'end'];
49 60
   gently.expect(REQ, 'addListener', events.length, function(event, fn) {
50 61
     assert.equal(event, events.shift());
@@ -115,6 +126,15 @@ test(function parse() {
115 126
 
116 127
     assert.strictEqual(form.resume(), false);
117 128
   })();
  129
+
  130
+  (function testReqEmitTimeout() {
  131
+    gently.expect(form, '_error', function(err) {
  132
+      assert.equal(err.message, 'timeout, no data received for '+form.timeout+'ms');
  133
+      assert.equal(err.timeout, true);
  134
+    });
  135
+
  136
+    reqEmit.timeout();
  137
+  })();
118 138
   
119 139
   (function testEmitError() {
120 140
     var ERR = new Error('something bad happened');
@@ -166,6 +186,8 @@ test(function parse() {
166 186
       , REQ = {headers: {}}
167 187
       , parseCalled = 0;
168 188
   
  189
+    form.timeout = null;
  190
+
169 191
     gently.expect(form, 'writeHeaders');
170 192
     gently.expect(REQ, 'addListener', 3, function() {
171 193
       return this;

No commit comments for this range

Something went wrong with that request. Please try again.