Skip to content

Commit 0aa11c6

Browse files
committed
Merge branch 'feature/allow_files_with_omitted_name' into develop
Resolves #58
2 parents 826114d + 2c944cc commit 0aa11c6

4 files changed

Lines changed: 88 additions & 17 deletions

File tree

Source/HttpMultipartParser.UnitTests/ParserScenarios/MultipleFilesWithNoName.cs renamed to Source/HttpMultipartParser.UnitTests/ParserScenarios/MultipleFilesWithEmptyName.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace HttpMultipartParser.UnitTests.ParserScenarios
1010
/// <summary>
1111
/// Test case for multiple files with no name.
1212
/// </summary>
13-
public class MultipleFilesWithNoName
13+
public class MultipleFilesWithEmptyName
1414
{
1515
private static readonly string MultipleFilesSameName_testData = TestUtil.TrimAllLines(@"--boundry
1616
Content-Disposition: form-data; name="""";filename=""file1.txt"";
@@ -39,7 +39,7 @@ This is text file 3 1234567890
3939
}
4040
);
4141

42-
public MultipleFilesWithNoName()
42+
public MultipleFilesWithEmptyName()
4343
{
4444
}
4545

@@ -50,7 +50,7 @@ public MultipleFilesWithNoName()
5050
[Fact]
5151
public void MultipleFilesWithNoNameTest()
5252
{
53-
using (Stream stream = TestUtil.StringToStream(MultipleFilesSameName_testData, Encoding.UTF8))
53+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
5454
{
5555
var parser = MultipartFormDataParser.Parse(stream, "boundry", Encoding.UTF8, 16);
5656
Assert.True(_testCase.Validate(parser));
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace HttpMultipartParser.UnitTests.ParserScenarios
9+
{
10+
/// <summary>
11+
/// Test case for multiple files with omitted name.
12+
/// </summary>
13+
public class MultipleFilesWithOmittedName
14+
{
15+
private static readonly string MultipleFilesWithOmittedName_testData = TestUtil.TrimAllLines(@"--boundry
16+
Content-Disposition: form-data; filename=""file1.txt"";
17+
Content-Type: text/plain
18+
19+
THIS IS TEXT FILE 1
20+
--boundry
21+
Content-Disposition: form-data; filename=""file2.txt"";
22+
Content-Type: text/plain
23+
24+
THIS IS TEXT FILE 2 !!!
25+
--boundry
26+
Content-Disposition: form-data; filename=""file3.txt"";
27+
Content-Type: text/plain
28+
29+
This is text file 3 1234567890
30+
--boundry--");
31+
32+
private static readonly TestData _testCase = new TestData(
33+
MultipleFilesWithOmittedName_testData,
34+
Enumerable.Empty<ParameterPart>().ToList(),
35+
new List<FilePart> {
36+
new FilePart( null, "file1.txt", TestUtil.StringToStreamNoBom("THIS IS TEXT FILE 1")),
37+
new FilePart( null, "file2.txt", TestUtil.StringToStreamNoBom("THIS IS TEXT FILE 2 !!!")),
38+
new FilePart( null, "file3.txt", TestUtil.StringToStreamNoBom("This is text file 3 1234567890"))
39+
}
40+
);
41+
42+
public MultipleFilesWithOmittedName()
43+
{
44+
}
45+
46+
/// <summary>
47+
/// Checks that multiple files don't get in the way of parsing each other
48+
/// and that everything parses correctly.
49+
/// </summary>
50+
[Fact]
51+
public void MultipleFilesWithOmittedNameTest()
52+
{
53+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
54+
{
55+
var parser = MultipartFormDataParser.Parse(stream, "boundry", Encoding.UTF8, 16);
56+
Assert.True(_testCase.Validate(parser));
57+
}
58+
}
59+
60+
[Fact]
61+
public async Task MultipleFilesWithOmittedNameAsync()
62+
{
63+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
64+
{
65+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8, 16).ConfigureAwait(false);
66+
Assert.True(_testCase.Validate(parser));
67+
}
68+
}
69+
}
70+
}

Source/HttpMultipartParser.UnitTests/ParserScenarios/MultipleFilesWithSameName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MultipleFilesWithSameName()
5050
[Fact]
5151
public void MultipleFilesWithSameNameTest()
5252
{
53-
using (Stream stream = TestUtil.StringToStream(MultipleFilesSameName_testData, Encoding.UTF8))
53+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
5454
{
5555
var parser = MultipartFormDataParser.Parse(stream, "boundry", Encoding.UTF8, 16);
5656
Assert.True(_testCase.Validate(parser));

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
@@ -705,13 +705,14 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
705705
{
706706
int partNumber = 0; // begins count parts of file from 0
707707

708-
string name = parameters["name"];
709-
string filename = parameters["filename"];
710-
708+
// Read the parameters
709+
parameters.TryGetValue("name", out string name);
710+
parameters.TryGetValue("filename", out string filename);
711711
parameters.TryGetValue("content-type", out string contentType);
712-
if (contentType == null) contentType = "text/plain";
713-
714712
parameters.TryGetValue("content-disposition", out string contentDisposition);
713+
714+
// Default values if expected parameters are missing
715+
if (contentType == null) contentType = "text/plain";
715716
if (contentDisposition == null) contentDisposition = "form-data";
716717

717718
// We want to create a stream and fill it with the data from the

0 commit comments

Comments
 (0)