@@ -342,33 +342,23 @@ private final class HeadersState implements State {
342
342
343
343
/**
344
344
* First checks whether the multipart boundary leading to this state
345
- * was the final boundary, or whether {@link #maxHeadersSize} is
346
- * exceeded. Then looks for the header-body boundary
347
- * ({@code CR LF CR LF}) in the given buffer. If found, convert
348
- * all buffers collected so far into a {@link HttpHeaders} object
345
+ * was the final boundary. Then looks for the header-body boundary
346
+ * ({@code CR LF CR LF}) in the given buffer. If found, checks whether
347
+ * the size of all header buffers does not exceed {@link #maxHeadersSize},
348
+ * converts all buffers collected so far into a {@link HttpHeaders} object
349
349
* and changes to {@link BodyState}, passing the remainder of the
350
- * buffer. If the boundary is not found, the buffer is collected.
350
+ * buffer. If the boundary is not found, the buffer is collected if
351
+ * its size does not exceed {@link #maxHeadersSize}.
351
352
*/
352
353
@ Override
353
354
public void onNext (DataBuffer buf ) {
354
- long prevCount = this .byteCount .get ();
355
- long count = this .byteCount .addAndGet (buf .readableByteCount ());
356
- if (prevCount < 2 && count >= 2 ) {
357
- if (isLastBoundary (buf )) {
358
- if (logger .isTraceEnabled ()) {
359
- logger .trace ("Last boundary found in " + buf );
360
- }
361
-
362
- if (changeState (this , DisposedState .INSTANCE , buf )) {
363
- emitComplete ();
364
- }
365
- return ;
355
+ if (isLastBoundary (buf )) {
356
+ if (logger .isTraceEnabled ()) {
357
+ logger .trace ("Last boundary found in " + buf );
366
358
}
367
- }
368
- else if (count > MultipartParser .this .maxHeadersSize ) {
359
+
369
360
if (changeState (this , DisposedState .INSTANCE , buf )) {
370
- emitError (new DataBufferLimitException ("Part headers exceeded the memory usage limit of " +
371
- MultipartParser .this .maxHeadersSize + " bytes" ));
361
+ emitComplete ();
372
362
}
373
363
return ;
374
364
}
@@ -377,17 +367,23 @@ else if (count > MultipartParser.this.maxHeadersSize) {
377
367
if (logger .isTraceEnabled ()) {
378
368
logger .trace ("End of headers found @" + endIdx + " in " + buf );
379
369
}
380
- DataBuffer headerBuf = MultipartUtils .sliceTo (buf , endIdx );
381
- this .buffers .add (headerBuf );
382
- DataBuffer bodyBuf = MultipartUtils .sliceFrom (buf , endIdx );
383
- DataBufferUtils .release (buf );
384
-
385
- emitHeaders (parseHeaders ());
386
- changeState (this , new BodyState (), bodyBuf );
370
+ long count = this .byteCount .addAndGet (endIdx );
371
+ if (belowMaxHeaderSize (count )) {
372
+ DataBuffer headerBuf = MultipartUtils .sliceTo (buf , endIdx );
373
+ this .buffers .add (headerBuf );
374
+ DataBuffer bodyBuf = MultipartUtils .sliceFrom (buf , endIdx );
375
+ DataBufferUtils .release (buf );
376
+
377
+ emitHeaders (parseHeaders ());
378
+ changeState (this , new BodyState (), bodyBuf );
379
+ }
387
380
}
388
381
else {
389
- this .buffers .add (buf );
390
- requestBuffer ();
382
+ long count = this .byteCount .addAndGet (buf .readableByteCount ());
383
+ if (belowMaxHeaderSize (count )) {
384
+ this .buffers .add (buf );
385
+ requestBuffer ();
386
+ }
391
387
}
392
388
}
393
389
@@ -407,6 +403,21 @@ private boolean isLastBoundary(DataBuffer buf) {
407
403
buf .getByte (0 ) == HYPHEN );
408
404
}
409
405
406
+ /**
407
+ * Checks whether the given {@code count} is below or equal to {@link #maxHeadersSize}
408
+ * and emits a {@link DataBufferLimitException} if not.
409
+ */
410
+ private boolean belowMaxHeaderSize (long count ) {
411
+ if (count <= MultipartParser .this .maxHeadersSize ) {
412
+ return true ;
413
+ }
414
+ else {
415
+ emitError (new DataBufferLimitException ("Part headers exceeded the memory usage limit of " +
416
+ MultipartParser .this .maxHeadersSize + " bytes" ));
417
+ return false ;
418
+ }
419
+ }
420
+
410
421
/**
411
422
* Parses the list of buffers into a {@link HttpHeaders} instance.
412
423
* Converts the joined buffers into a string using ISO=8859-1, and parses
0 commit comments