@@ -48,6 +48,11 @@ public class StreamingMultipartFormDataParser
4848 /// <summary>
4949 /// The default buffer size.
5050 /// </summary>
51+ /// <remarks>
52+ /// 4096 is the optimal buffer size as it matches the internal buffer of a StreamReader
53+ /// See: http://stackoverflow.com/a/129318/203133
54+ /// See: http://msdn.microsoft.com/en-us/library/9kstw824.aspx (under remarks).
55+ /// </remarks>
5156 private const int DefaultBufferSize = 4096 ;
5257
5358 #endregion
@@ -153,9 +158,6 @@ public StreamingMultipartFormDataParser(Stream stream, Encoding encoding)
153158 public StreamingMultipartFormDataParser ( Stream stream , string boundary , Encoding encoding )
154159 : this ( stream , boundary , encoding , DefaultBufferSize )
155160 {
156- // 4096 is the optimal buffer size as it matches the internal buffer of a StreamReader
157- // See: http://stackoverflow.com/a/129318/203133
158- // See: http://msdn.microsoft.com/en-us/library/9kstw824.aspx (under remarks)
159161 }
160162
161163 /// <summary>
@@ -403,6 +405,24 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
403405 return boundary ;
404406 }
405407
408+ /// <summary>
409+ /// Use a few assumptions to determine if a section contains a file or a "data" parameter.
410+ /// </summary>
411+ /// <param name="parameters">The section parameters.</param>
412+ /// <returns>true if the section contains a file, false otherwise.</returns>
413+ private static bool IsFilePart ( IDictionary < string , string > parameters )
414+ {
415+ // If a section contains filename, then it's a file.
416+ if ( parameters . ContainsKey ( "filename" ) ) return true ;
417+
418+ // If the section is missing the filename and the name, then it's a file.
419+ // For example, images in an mjpeg stream have neither a name nor a filename.
420+ else if ( ! parameters . ContainsKey ( "name" ) ) return true ;
421+
422+ // In all other cases, we assume it's a "data" parameter.
423+ return false ;
424+ }
425+
406426 /// <summary>
407427 /// Finds the next sequence of newlines in the input stream.
408428 /// </summary>
@@ -1055,11 +1075,8 @@ private void ParseSection(RebufferableBinaryReader reader)
10551075 // Now that we've consumed all the parameters we're up to the body. We're going to do
10561076 // different things depending on if we're parsing a, relatively small, form value or a
10571077 // potentially large file.
1058- if ( parameters . ContainsKey ( "filename" ) )
1078+ if ( IsFilePart ( parameters ) )
10591079 {
1060- // Right now we assume that if a section contains filename then it is a file.
1061- // This assumption needs to be checked, it holds true in firefox but is untested for other
1062- // browsers.
10631080 ParseFilePart ( parameters , reader ) ;
10641081 }
10651082 else
@@ -1149,11 +1166,8 @@ private async Task ParseSectionAsync(RebufferableBinaryReader reader, Cancellati
11491166 // Now that we've consumed all the parameters we're up to the body. We're going to do
11501167 // different things depending on if we're parsing a, relatively small, form value or a
11511168 // potentially large file.
1152- if ( parameters . ContainsKey ( "filename" ) )
1169+ if ( IsFilePart ( parameters ) )
11531170 {
1154- // Right now we assume that if a section contains filename then it is a file.
1155- // This assumption needs to be checked, it holds true in firefox but is untested for other
1156- // browsers.
11571171 await ParseFilePartAsync ( parameters , reader , cancellationToken ) . ConfigureAwait ( false ) ;
11581172 }
11591173 else
0 commit comments