@@ -51,11 +51,20 @@ internals.state = {
51
51
} ;
52
52
53
53
54
- exports . Dispenser = internals . Dispenser = function ( contentType ) {
54
+ internals . defaults = {
55
+ maxBytes : Infinity
56
+ } ;
57
+
58
+
59
+ exports . Dispenser = internals . Dispenser = function ( options ) {
55
60
56
61
Stream . Writable . call ( this ) ;
57
62
58
- this . _boundary = contentType . boundary ;
63
+ Hoek . assert ( options !== null && typeof options === 'object' ,
64
+ 'options must be an object' ) ;
65
+ const settings = Hoek . applyToDefaults ( internals . defaults , options ) ;
66
+
67
+ this . _boundary = settings . boundary ;
59
68
this . _state = internals . state . preamble ;
60
69
this . _held = '' ;
61
70
@@ -64,8 +73,10 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
64
73
this . _name = '' ;
65
74
this . _pendingHeader = '' ;
66
75
this . _error = null ;
76
+ this . _bytes = 0 ;
77
+ this . _maxBytes = settings . maxBytes ;
67
78
68
- this . _parts = new Nigel . Stream ( new Buffer ( '--' + contentType . boundary ) ) ;
79
+ this . _parts = new Nigel . Stream ( new Buffer ( '--' + settings . boundary ) ) ;
69
80
this . _lines = new Nigel . Stream ( new Buffer ( '\r\n' ) ) ;
70
81
71
82
this . _parts . on ( 'needle' , ( ) => {
@@ -102,6 +113,7 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
102
113
let finish = ( err ) => {
103
114
104
115
if ( piper ) {
116
+ piper . removeListener ( 'data' , onReqData ) ;
105
117
piper . removeListener ( 'error' , finish ) ;
106
118
piper . removeListener ( 'aborted' , onReqAborted ) ;
107
119
}
@@ -143,9 +155,19 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
143
155
finish ( Boom . badRequest ( 'Client request aborted' ) ) ;
144
156
} ;
145
157
158
+ const onReqData = ( data ) => {
159
+
160
+ this . _bytes += Buffer . byteLength ( data ) ;
161
+
162
+ if ( this . _bytes > this . _maxBytes ) {
163
+ finish ( Boom . badRequest ( 'Maximum size exceeded' ) ) ;
164
+ }
165
+ } ;
166
+
146
167
this . once ( 'pipe' , ( req ) => {
147
168
148
169
piper = req ;
170
+ req . on ( 'data' , onReqData ) ;
149
171
req . once ( 'error' , finish ) ;
150
172
req . once ( 'aborted' , onReqAborted ) ;
151
173
} ) ;
0 commit comments