Skip to content

Commit 497c8f2

Browse files
committed
Merge branch 'release/4.3.0'
2 parents 8934139 + 30eee9f commit 497c8f2

10 files changed

Lines changed: 61 additions & 52 deletions

Source/HttpMultipartParser.UnitTests/ParserScenarios/EmptyForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace HttpMultipartParser.UnitTests.ParserScenarios
99
public class EmptyForm
1010
{
1111
private static readonly string _testData = TestUtil.TrimAllLines(
12-
@"------WebKitFormBoundaryb4SfPlH9Bv7c2PKS--"
12+
@"------WebKitFormBoundaryb4SfPlH9Bv7c2PKS--"
1313
);
1414

1515
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser.UnitTests/ParserScenarios/FileIsLast.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class FileIsLast
2020
Content-Disposition: form-data; name=""files[]""; filename=""Capture.JPG""
2121
Content-Type: image/jpeg
2222
23-
BinaryData
24-
-----------------------------41952539122868--"
23+
BinaryData
24+
-----------------------------41952539122868--"
2525
);
2626

2727
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser.UnitTests/ParserScenarios/MixedSingleByteAndMultiByteWidth.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class MixedSingleByteAndMultiByteWidth
1717
Content-Disposition: form-data; name=""files[]""; filename=""تست.jpg""
1818
Content-Type: image/jpeg
1919
20-
BinaryData
21-
-----------------------------41952539122868--"
20+
BinaryData
21+
-----------------------------41952539122868--"
2222
);
2323

2424
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser.UnitTests/ParserScenarios/MultipleFilesWithEmptyName.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ public class MultipleFilesWithEmptyName
1414
{
1515
private static readonly string _testData = TestUtil.TrimAllLines(
1616
@"--boundry
17-
Content-Disposition: form-data; name="""";filename=""file1.txt"";
18-
Content-Type: text/plain
17+
Content-Disposition: form-data; name="""";filename=""file1.txt"";
18+
Content-Type: text/plain
1919
20-
THIS IS TEXT FILE 1
21-
--boundry
22-
Content-Disposition: form-data; name="""";filename=""file2.txt"";
23-
Content-Type: text/plain
20+
THIS IS TEXT FILE 1
21+
--boundry
22+
Content-Disposition: form-data; name="""";filename=""file2.txt"";
23+
Content-Type: text/plain
2424
25-
THIS IS TEXT FILE 2 !!!
26-
--boundry
27-
Content-Disposition: form-data; name="""";filename=""file3.txt"";
28-
Content-Type: text/plain
25+
THIS IS TEXT FILE 2 !!!
26+
--boundry
27+
Content-Disposition: form-data; name="""";filename=""file3.txt"";
28+
Content-Type: text/plain
2929
30-
This is text file 3 1234567890
31-
--boundry--"
30+
This is text file 3 1234567890
31+
--boundry--"
3232
);
3333

3434
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser.UnitTests/ParserScenarios/MultipleFilesWithOmittedName.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@ namespace HttpMultipartParser.UnitTests.ParserScenarios
1212
/// </summary>
1313
public class MultipleFilesWithOmittedName
1414
{
15-
private static readonly string _testData = TestUtil.TrimAllLines(@"--boundry
16-
Content-Disposition: form-data; filename=""file1.txt"";
17-
Content-Type: text/plain
15+
private static readonly string _testData = TestUtil.TrimAllLines(
16+
@"--boundry
17+
Content-Disposition: form-data; filename=""file1.txt"";
18+
Content-Type: text/plain
1819
19-
THIS IS TEXT FILE 1
20-
--boundry
21-
Content-Disposition: form-data; filename=""file2.txt"";
22-
Content-Type: text/plain
20+
THIS IS TEXT FILE 1
21+
--boundry
22+
Content-Disposition: form-data; filename=""file2.txt"";
23+
Content-Type: text/plain
2324
24-
THIS IS TEXT FILE 2 !!!
25-
--boundry
26-
Content-Disposition: form-data; filename=""file3.txt"";
27-
Content-Type: text/plain
25+
THIS IS TEXT FILE 2 !!!
26+
--boundry
27+
Content-Disposition: form-data; filename=""file3.txt"";
28+
Content-Type: text/plain
2829
29-
This is text file 3 1234567890
30-
--boundry--"
30+
This is text file 3 1234567890
31+
--boundry--"
3132
);
3233

3334
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser.UnitTests/ParserScenarios/UnclosedBoundary.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public class UnclosedBoundary
1010
{
1111
private static readonly string _testData = TestUtil.TrimAllLines(
1212
@"------51523
13-
Content-Disposition: form-data; name=""value""
13+
Content-Disposition: form-data; name=""value""
1414
15-
my value
16-
------51523"
15+
my value
16+
------51523"
1717
);
1818

1919
private static readonly TestData _testCase = new TestData(

Source/HttpMultipartParser/HttpMultipartParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4141
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.3.3" />
4242
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
43-
<PackageReference Include="System.Buffers" Version="4.5.0" />
43+
<PackageReference Include="System.Buffers" Version="4.5.1" />
4444
</ItemGroup>
4545

4646
<ItemGroup Condition=" !$(TargetFramework.StartsWith('netstandard')) ">

Source/HttpMultipartParser/MultipartStreamPart.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

build.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#tool nuget:?package=GitVersion.CommandLine&version=5.2.4
99
#tool nuget:?package=GitReleaseManager&version=0.11.0
1010
#tool nuget:?package=OpenCover&version=4.7.922
11-
#tool nuget:?package=ReportGenerator&version=4.5.2
11+
#tool nuget:?package=ReportGenerator&version=4.5.6
1212
#tool nuget:?package=coveralls.io&version=1.4.2
1313
#tool nuget:?package=xunit.runner.console&version=2.4.1
1414

0 commit comments

Comments
 (0)