@@ -545,7 +545,7 @@ private async Task ParseAsync(RebufferableBinaryReader reader, CancellationToken
545545 /// Parses a section of the stream that is known to be file data.
546546 /// </summary>
547547 /// <param name="parameters">
548- /// The header parameters of this file, expects "name" and "filename" to be valid keys .
548+ /// The header parameters of this file.
549549 /// </param>
550550 /// <param name="reader">
551551 /// The StreamReader to read the data from.
@@ -554,17 +554,17 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
554554 {
555555 int partNumber = 0 ; // begins count parts of file from 0
556556
557- string name = parameters [ "name" ] ;
558- string filename = parameters [ "filename" ] ;
559-
557+ // Read the parameters
558+ parameters . TryGetValue ( "name" , out string name ) ;
559+ parameters . TryGetValue ( "filename" , out string filename ) ;
560560 parameters . TryGetValue ( "content-type" , out string contentType ) ;
561- if ( contentType == null ) contentType = "text/plain" ;
562-
563561 parameters . TryGetValue ( "content-disposition" , out string contentDisposition ) ;
562+
563+ // Default values if expected parameters are missing
564+ if ( contentType == null ) contentType = "text/plain" ;
564565 if ( contentDisposition == null ) contentDisposition = "form-data" ;
565566
566- // We want to create a stream and fill it with the data from the
567- // file.
567+ // We want to create a stream and fill it with the data from the file.
568568 var curBuffer = Utilities . ArrayPool . Rent ( BinaryBufferSize ) ;
569569 var prevBuffer = Utilities . ArrayPool . Rent ( BinaryBufferSize ) ;
570570 var fullBuffer = Utilities . ArrayPool . Rent ( BinaryBufferSize * 2 ) ;
@@ -655,7 +655,7 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
655655 // We also want to chop off the newline that is inserted by the protocl.
656656 // We can do this by reducing endPos by the length of newline in this environment
657657 // and encoding
658- FileHandler ( name , filename , contentType , contentDisposition , fullBuffer , endPos - bufferNewlineLength , partNumber ) ;
658+ FileHandler ( name , filename , contentType , contentDisposition , fullBuffer , endPos - bufferNewlineLength , partNumber ++ ) ;
659659
660660 int writeBackOffset = endPos + endPosLength + boundaryNewlineOffset ;
661661 int writeBackAmount = ( prevLength + curLength ) - writeBackOffset ;
@@ -665,8 +665,7 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
665665 }
666666
667667 // No end, consume the entire previous buffer
668- FileHandler ( name , filename , contentType , contentDisposition , prevBuffer , prevLength , partNumber ) ;
669- partNumber ++ ; // increase part counter
668+ FileHandler ( name , filename , contentType , contentDisposition , prevBuffer , prevLength , partNumber ++ ) ;
670669
671670 // Now we want to swap the two buffers, we don't care
672671 // what happens to the data from prevBuffer so we set
@@ -706,13 +705,14 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
706705 {
707706 int partNumber = 0 ; // begins count parts of file from 0
708707
709- string name = parameters [ "name" ] ;
710- string filename = parameters [ "filename" ] ;
711-
708+ // Read the parameters
709+ parameters . TryGetValue ( "name" , out string name ) ;
710+ parameters . TryGetValue ( "filename" , out string filename ) ;
712711 parameters . TryGetValue ( "content-type" , out string contentType ) ;
713- if ( contentType == null ) contentType = "text/plain" ;
714-
715712 parameters . TryGetValue ( "content-disposition" , out string contentDisposition ) ;
713+
714+ // Default values if expected parameters are missing
715+ if ( contentType == null ) contentType = "text/plain" ;
716716 if ( contentDisposition == null ) contentDisposition = "form-data" ;
717717
718718 // We want to create a stream and fill it with the data from the
@@ -807,7 +807,7 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
807807 // We also want to chop off the newline that is inserted by the protocl.
808808 // We can do this by reducing endPos by the length of newline in this environment
809809 // and encoding
810- FileHandler ( name , filename , contentType , contentDisposition , fullBuffer , endPos - bufferNewlineLength , partNumber ) ;
810+ FileHandler ( name , filename , contentType , contentDisposition , fullBuffer , endPos - bufferNewlineLength , partNumber ++ ) ;
811811
812812 int writeBackOffset = endPos + endPosLength + boundaryNewlineOffset ;
813813 int writeBackAmount = ( prevLength + curLength ) - writeBackOffset ;
@@ -817,8 +817,7 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
817817 }
818818
819819 // No end, consume the entire previous buffer
820- FileHandler ( name , filename , contentType , contentDisposition , prevBuffer , prevLength , partNumber ) ;
821- partNumber ++ ; // increase part counter
820+ FileHandler ( name , filename , contentType , contentDisposition , prevBuffer , prevLength , partNumber ++ ) ;
822821
823822 // Now we want to swap the two buffers, we don't care
824823 // what happens to the data from prevBuffer so we set
@@ -979,16 +978,17 @@ private void ParseSection(RebufferableBinaryReader reader)
979978 throw new MultipartParseException ( "Unexpected end of section" ) ;
980979 }
981980
982- // This line parses the header values into a set of key/value pairs. For example:
983- // Content-Disposition: form-data; name="textdata"
984- // ["content-disposition"] = "form-data"
985- // ["name"] = "textdata"
986- // Content-Disposition: form-data; name="file"; filename="data.txt"
987- // ["content-disposition"] = "form-data"
988- // ["name"] = "file"
989- // ["filename"] = "data.txt"
990- // Content-Type: text/plain
991- // ["content-type"] = "text/plain"
981+ // This line parses the header values into a set of key/value pairs.
982+ // For example:
983+ // Content-Disposition: form-data; name="textdata"
984+ // ["content-disposition"] = "form-data"
985+ // ["name"] = "textdata"
986+ // Content-Disposition: form-data; name="file"; filename="data.txt"
987+ // ["content-disposition"] = "form-data"
988+ // ["name"] = "file"
989+ // ["filename"] = "data.txt"
990+ // Content-Type: text/plain
991+ // ["content-type"] = "text/plain"
992992 Dictionary < string , string > values = SplitBySemicolonIgnoringSemicolonsInQuotes ( line )
993993 . Select ( x => x . Split ( new [ ] { ':' , '=' } , 2 ) )
994994
@@ -1072,16 +1072,17 @@ private async Task ParseSectionAsync(RebufferableBinaryReader reader, Cancellati
10721072 throw new MultipartParseException ( "Unexpected end of section" ) ;
10731073 }
10741074
1075- // This line parses the header values into a set of key/value pairs. For example:
1076- // Content-Disposition: form-data; name="textdata"
1077- // ["content-disposition"] = "form-data"
1078- // ["name"] = "textdata"
1079- // Content-Disposition: form-data; name="file"; filename="data.txt"
1080- // ["content-disposition"] = "form-data"
1081- // ["name"] = "file"
1082- // ["filename"] = "data.txt"
1083- // Content-Type: text/plain
1084- // ["content-type"] = "text/plain"
1075+ // This line parses the header values into a set of key/value pairs.
1076+ // For example:
1077+ // Content-Disposition: form-data; name="textdata"
1078+ // ["content-disposition"] = "form-data"
1079+ // ["name"] = "textdata"
1080+ // Content-Disposition: form-data; name="file"; filename="data.txt"
1081+ // ["content-disposition"] = "form-data"
1082+ // ["name"] = "file"
1083+ // ["filename"] = "data.txt"
1084+ // Content-Type: text/plain
1085+ // ["content-type"] = "text/plain"
10851086 Dictionary < string , string > values = SplitBySemicolonIgnoringSemicolonsInQuotes ( line )
10861087 . Select ( x => x . Split ( new [ ] { ':' , '=' } , 2 ) )
10871088
0 commit comments