@@ -41,11 +41,30 @@ function IncomingForm(opts) {
41
41
this . _fieldsSize = 0 ;
42
42
this . openedFiles = [ ] ;
43
43
44
+ this . _rxTimeoutId = null ;
45
+ this . rxTimeout = 10 * 60 * 1000 ; // 10 minutes (in ms)
46
+
44
47
return this ;
45
48
}
46
49
util . inherits ( IncomingForm , EventEmitter ) ;
47
50
exports . IncomingForm = IncomingForm ;
48
51
52
+ IncomingForm . prototype . resetRxTimeout = function ( ) {
53
+ if ( this . _rxTimeoutId !== null ) {
54
+ clearTimeout ( this . _rxTimeoutId ) ;
55
+ }
56
+ this . _rxTimeoutId = setTimeout ( function ( ) {
57
+ this . emit ( 'timeout' ) ;
58
+ req . connection . destroy ( ) ;
59
+ } . bind ( this ) , this . rxTimeout ) ;
60
+ } ;
61
+
62
+ IncomingForm . prototype . clearRxTimeout = function ( ) {
63
+ if ( this . _rxTimeoutId !== null ) {
64
+ clearTimeout ( this . _rxTimeoutId ) ;
65
+ }
66
+ } ;
67
+
49
68
IncomingForm . prototype . parse = function ( req , cb ) {
50
69
this . pause = function ( ) {
51
70
try {
@@ -208,6 +227,8 @@ IncomingForm.prototype.handlePart = function(part) {
208
227
hash : self . hash
209
228
} ) ;
210
229
230
+ this . resetRxTimeout ( ) ;
231
+
211
232
this . emit ( 'fileBegin' , part . name , file ) ;
212
233
213
234
file . open ( ) ;
@@ -217,6 +238,7 @@ IncomingForm.prototype.handlePart = function(part) {
217
238
if ( buffer . length == 0 ) {
218
239
return ;
219
240
}
241
+ self . resetRxTimeout ( ) ;
220
242
self . pause ( ) ;
221
243
file . write ( buffer , function ( ) {
222
244
self . resume ( ) ;
@@ -226,6 +248,7 @@ IncomingForm.prototype.handlePart = function(part) {
226
248
part . on ( 'end' , function ( ) {
227
249
file . end ( function ( ) {
228
250
self . _flushing -- ;
251
+ self . clearRxTimeout ( ) ;
229
252
self . emit ( 'file' , part . name , file ) ;
230
253
self . _maybeEnd ( ) ;
231
254
} ) ;
@@ -464,6 +487,8 @@ IncomingForm.prototype._initOctetStream = function() {
464
487
type : mime
465
488
} ) ;
466
489
490
+ this . resetRxTimeout ( ) ;
491
+
467
492
this . emit ( 'fileBegin' , filename , file ) ;
468
493
file . open ( ) ;
469
494
@@ -477,6 +502,7 @@ IncomingForm.prototype._initOctetStream = function() {
477
502
var outstandingWrites = 0 ;
478
503
479
504
self . _parser . on ( 'data' , function ( buffer ) {
505
+ self . resetRxTimeout ( ) ;
480
506
self . pause ( ) ;
481
507
outstandingWrites ++ ;
482
508
@@ -496,6 +522,7 @@ IncomingForm.prototype._initOctetStream = function() {
496
522
497
523
var done = function ( ) {
498
524
file . end ( function ( ) {
525
+ self . clearRxTimeout ( ) ;
499
526
self . emit ( 'file' , 'file' , file ) ;
500
527
self . _maybeEnd ( ) ;
501
528
} ) ;
0 commit comments