Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: felixge/node-formidable
base: v0.9.4
...
head fork: felixge/node-formidable
compare: v0.9.5
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jul 13, 2010
@felixge Add missing err parameter to docs f92b1db
Commits on Jul 15, 2010
@felixge Feature: IncomingForm.timeout
By default uploads that are not receiving data for longer than 30
seconds will now emit an error.
5e2612c
@felixge Bump version 464fc9d
View
8 Readme.md
@@ -43,7 +43,7 @@ Parse an incoming file upload.
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
- form.parse(req, function(fields, files) {
+ form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(sys.inspect({fields: fields, files: files}));
@@ -74,6 +74,12 @@ Creates a new incoming form.
The encoding to use for incoming form fields.
+#### incomingForm.timeout = 30000
+
+The time in ms after which a stalled upload causes an error. Can be set to
+`false` to disable this feature. The resulting error object will have a
+property `timeout` that is set to `true`.
+
#### incomingForm.uploadDir = '/tmp'
The directory for placing file uploads in. You can later on move them using `fs.rename()`.
View
10 lib/formidable/incoming_form.js
@@ -17,6 +17,7 @@ function IncomingForm() {
this.keepExtensions = false;
this.uploadDir = '/tmp';
this.encoding = 'utf-8';
+ this.timeout = 30000;
this.headers = null;
this.type = null;
@@ -62,6 +63,15 @@ IncomingForm.prototype.parse = function(req, cb) {
this.writeHeaders(req.headers);
var self = this;
+ if (this.timeout) {
+ req.connection.setTimeout(this.timeout);
+ req.connection.addListener('timeout', function() {
+ var err = new Error('timeout, no data received for '+self.timeout+'ms')
+ err.timeout = true;
+ self._error(err);
+ });
+ }
+
req
.addListener('error', function(err) {
self._error(err);
View
2  package.json
@@ -1,5 +1,5 @@
{ "name" : "formidable"
-, "version": "0.9.4"
+, "version": "0.9.5"
, "dependencies": {"gently": ">=0.7.0"}
, "directories" : { "lib" : "./lib/formidable" }
, "main" : "./lib/formidable/index"
View
26 test/simple/test-incoming-form.js
@@ -29,6 +29,7 @@ test(function constructor() {
assert.strictEqual(form.keepExtensions, false);
assert.strictEqual(form.uploadDir, '/tmp');
assert.strictEqual(form.encoding, 'utf-8');
+ assert.strictEqual(form.timeout, 30000);
assert.strictEqual(form.bytesReceived, null);
assert.strictEqual(form.bytesExpected, null);
assert.strictEqual(form._parser, null);
@@ -38,13 +39,23 @@ test(function constructor() {
});
test(function parse() {
- var REQ = {headers: {}}
- , emit = {};
+ var REQ = {headers: {}, connection: {}}
+ , emit = {}
+ , reqEmit = {};
gently.expect(form, 'writeHeaders', function(headers) {
assert.strictEqual(headers, REQ.headers);
});
+ gently.expect(REQ.connection, 'setTimeout', function(timeout) {
+ assert.equal(timeout, form.timeout);
+ });
+
+ gently.expect(REQ.connection, 'addListener', function(event, fn) {
+ assert.equal(event, 'timeout');
+ reqEmit[event] = fn;
+ });
+
var events = ['error', 'data', 'end'];
gently.expect(REQ, 'addListener', events.length, function(event, fn) {
assert.equal(event, events.shift());
@@ -115,6 +126,15 @@ test(function parse() {
assert.strictEqual(form.resume(), false);
})();
+
+ (function testReqEmitTimeout() {
+ gently.expect(form, '_error', function(err) {
+ assert.equal(err.message, 'timeout, no data received for '+form.timeout+'ms');
+ assert.equal(err.timeout, true);
+ });
+
+ reqEmit.timeout();
+ })();
(function testEmitError() {
var ERR = new Error('something bad happened');
@@ -166,6 +186,8 @@ test(function parse() {
, REQ = {headers: {}}
, parseCalled = 0;
+ form.timeout = null;
+
gently.expect(form, 'writeHeaders');
gently.expect(REQ, 'addListener', 3, function() {
return this;

No commit comments for this range

Something went wrong with that request. Please try again.