@@ -351,7 +351,19 @@ private static string DetectBoundary(RebufferableBinaryReader reader)
351351 // Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
352352 // the front as per the protocol and ||||||||||||| is the part we care about.
353353 string boundary = string . Concat ( reader . ReadLine ( ) . Skip ( 2 ) ) ;
354- reader . Buffer ( "--" + boundary + "\n " ) ;
354+
355+ // If the string ends with '--' it means that we found the "end" boundary and we
356+ // need to trim the two dashes to get the actual boundary
357+ if ( boundary . EndsWith ( "--" ) )
358+ {
359+ boundary = boundary . Substring ( 0 , boundary . Length - 2 ) ;
360+ reader . Buffer ( $ "--{ boundary } --\n ") ;
361+ }
362+ else
363+ {
364+ reader . Buffer ( $ "--{ boundary } \n ") ;
365+ }
366+
355367 return boundary ;
356368 }
357369
@@ -374,8 +386,20 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
374386 // Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
375387 // the front as per the protocol and ||||||||||||| is the part we care about.
376388 var line = await reader . ReadLineAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
377- var boundary = string . Concat ( line . Skip ( 2 ) ) ;
378- reader . Buffer ( "--" + boundary + "\n " ) ;
389+ string boundary = string . Concat ( line . Skip ( 2 ) ) ;
390+
391+ // If the string ends with '--' it means that we found the "end" boundary and we
392+ // need to trim the two dashes to get the actual boundary.
393+ if ( boundary . EndsWith ( "--" ) )
394+ {
395+ boundary = boundary . Substring ( 0 , boundary . Length - 2 ) ;
396+ reader . Buffer ( $ "--{ boundary } --\n ") ;
397+ }
398+ else
399+ {
400+ reader . Buffer ( $ "--{ boundary } \n ") ;
401+ }
402+
379403 return boundary ;
380404 }
381405
@@ -470,12 +494,17 @@ private void Parse(RebufferableBinaryReader reader)
470494 while ( true )
471495 {
472496 string line = reader . ReadLine ( ) ;
497+
473498 if ( line == boundary )
474499 {
475500 break ;
476501 }
477-
478- if ( line == null )
502+ else if ( line == endBoundary )
503+ {
504+ readEndBoundary = true ;
505+ break ;
506+ }
507+ else if ( line == null )
479508 {
480509 throw new MultipartParseException ( "Could not find expected boundary" ) ;
481510 }
@@ -518,12 +547,17 @@ private async Task ParseAsync(RebufferableBinaryReader reader, CancellationToken
518547 while ( true )
519548 {
520549 string line = await reader . ReadLineAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
550+
521551 if ( line == boundary )
522552 {
523553 break ;
524554 }
525-
526- if ( line == null )
555+ else if ( line == endBoundary )
556+ {
557+ readEndBoundary = true ;
558+ break ;
559+ }
560+ else if ( line == null )
527561 {
528562 throw new MultipartParseException ( "Could not find expected boundary" ) ;
529563 }
0 commit comments