Skip to content

Commit f509bf5

Browse files
committed
support for RX timeout
Don't hang indefinitely when the client stops sending data to us. Close the connection when timeout is reached.
1 parent 53bdc35 commit f509bf5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/incoming_form.js

+27
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function IncomingForm(opts) {
4141
this._fieldsSize = 0;
4242
this.openedFiles = [];
4343

44+
this._rxTimeoutId = null;
45+
this.rxTimeout = 10 * 60 * 1000; // 10 minutes (in ms)
46+
4447
return this;
4548
}
4649
util.inherits(IncomingForm, EventEmitter);
@@ -76,6 +79,22 @@ IncomingForm.prototype.parse = function(req, cb) {
7679
return true;
7780
};
7881

82+
this.resetRxTimeout = function() {
83+
if (this._rxTimeoutId !== null) {
84+
clearTimeout(this._rxTimeoutId);
85+
}
86+
this._rxTimeoutId = setTimeout(function() {
87+
this.emit('timeout');
88+
req.connection.destroy();
89+
}.bind(this), this.rxTimeout);
90+
};
91+
92+
this.clearRxTimeout = function() {
93+
if (this._rxTimeoutId !== null) {
94+
clearTimeout(this._rxTimeoutId);
95+
}
96+
};
97+
7998
// Setup callback first, so we don't miss anything from data events emitted
8099
// immediately.
81100
if (cb) {
@@ -208,6 +227,8 @@ IncomingForm.prototype.handlePart = function(part) {
208227
hash: self.hash
209228
});
210229

230+
this.resetRxTimeout();
231+
211232
this.emit('fileBegin', part.name, file);
212233

213234
file.open();
@@ -217,6 +238,7 @@ IncomingForm.prototype.handlePart = function(part) {
217238
if (buffer.length == 0) {
218239
return;
219240
}
241+
self.resetRxTimeout();
220242
self.pause();
221243
file.write(buffer, function() {
222244
self.resume();
@@ -226,6 +248,7 @@ IncomingForm.prototype.handlePart = function(part) {
226248
part.on('end', function() {
227249
file.end(function() {
228250
self._flushing--;
251+
self.clearRxTimeout();
229252
self.emit('file', part.name, file);
230253
self._maybeEnd();
231254
});
@@ -464,6 +487,8 @@ IncomingForm.prototype._initOctetStream = function() {
464487
type: mime
465488
});
466489

490+
this.resetRxTimeout();
491+
467492
this.emit('fileBegin', filename, file);
468493
file.open();
469494

@@ -477,6 +502,7 @@ IncomingForm.prototype._initOctetStream = function() {
477502
var outstandingWrites = 0;
478503

479504
self._parser.on('data', function(buffer){
505+
self.resetRxTimeout();
480506
self.pause();
481507
outstandingWrites++;
482508

@@ -496,6 +522,7 @@ IncomingForm.prototype._initOctetStream = function() {
496522

497523
var done = function(){
498524
file.end(function() {
525+
self.clearRxTimeout();
499526
self.emit('file', 'file', file);
500527
self._maybeEnd();
501528
});

0 commit comments

Comments
 (0)