Skip to content

Commit 30eee9f

Browse files
committed
Apply the same logic to determine if a section contains a file in ParseSectionAsync and ParseSection
1 parent 3f456ac commit 30eee9f

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,24 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
405405
return boundary;
406406
}
407407

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+
408426
/// <summary>
409427
/// Finds the next sequence of newlines in the input stream.
410428
/// </summary>
@@ -1057,11 +1075,8 @@ private void ParseSection(RebufferableBinaryReader reader)
10571075
// Now that we've consumed all the parameters we're up to the body. We're going to do
10581076
// different things depending on if we're parsing a, relatively small, form value or a
10591077
// potentially large file.
1060-
if (parameters.ContainsKey("filename"))
1078+
if (IsFilePart(parameters))
10611079
{
1062-
// Right now we assume that if a section contains filename then it is a file.
1063-
// This assumption needs to be checked, it holds true in firefox but is untested for other
1064-
// browsers.
10651080
ParseFilePart(parameters, reader);
10661081
}
10671082
else
@@ -1151,11 +1166,8 @@ private async Task ParseSectionAsync(RebufferableBinaryReader reader, Cancellati
11511166
// Now that we've consumed all the parameters we're up to the body. We're going to do
11521167
// different things depending on if we're parsing a, relatively small, form value or a
11531168
// potentially large file.
1154-
if (parameters.ContainsKey("filename") || !parameters.ContainsKey("name"))
1169+
if (IsFilePart(parameters))
11551170
{
1156-
// Right now we assume that if a section contains filename then it is a file.
1157-
// This assumption needs to be checked, it holds true in firefox but is untested for other
1158-
// browsers.
11591171
await ParseFilePartAsync(parameters, reader, cancellationToken).ConfigureAwait(false);
11601172
}
11611173
else

0 commit comments

Comments
 (0)