Skip to content

Commit 00d397f

Browse files
committed
Merge branch 'feature/binary_parser' into develop
Resolves #113
2 parents f06d2e6 + ac952e7 commit 00d397f

15 files changed

+1830
-1192
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"cake.tool": {
6-
"version": "2.3.0",
6+
"version": "3.0.0",
77
"commands": [
88
"dotnet-cake"
99
]

Source/HttpMultipartParser.UnitTests/ParserScenarios/SmallData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void SmallDataTest()
6060
{
6161
// The boundry is missing the first two -- in accordance with the multipart
6262
// spec. (A -- is added by the parser, this boundry is what would be sent in the
63-
// requset header)
63+
// request header)
6464
var parser = MultipartFormDataParser.Parse(stream, "---------------------------265001916915724");
6565
Assert.True(_testCase.Validate(parser));
6666
}

Source/HttpMultipartParser/BinaryStreamStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class BinaryStreamStack
5454
/// encoding of UTF8.
5555
/// </summary>
5656
public BinaryStreamStack()
57-
: this(Encoding.UTF8)
57+
: this(Constants.DefaultEncoding)
5858
{
5959
}
6060

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Text;
2+
3+
namespace HttpMultipartParser
4+
{
5+
internal static class Constants
6+
{
7+
/// <summary>
8+
/// The default buffer size.
9+
/// </summary>
10+
/// <remarks>
11+
/// 4096 is the optimal buffer size as it matches the internal buffer of a StreamReader
12+
/// See: http://stackoverflow.com/a/129318/203133
13+
/// See: http://msdn.microsoft.com/en-us/library/9kstw824.aspx (under remarks).
14+
/// </remarks>
15+
internal const int DefaultBufferSize = 4096;
16+
17+
/// <summary>
18+
/// The mimetypes that are considered a file by default.
19+
/// </summary>
20+
internal static readonly string[] DefaultBinaryMimeTypes = { "application/octet-stream" };
21+
22+
/// <summary>
23+
/// The default encoding used by the parser when developer does not specify the encoding.
24+
/// </summary>
25+
internal static readonly Encoding DefaultEncoding = Encoding.UTF8;
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
3+
namespace HttpMultipartParser
4+
{
5+
/// <summary>
6+
/// The FileStreamDelegate defining functions that can handle file stream data from this parser.
7+
///
8+
/// Delegates can assume that the data is sequential i.e. the data received by any delegates will be
9+
/// the data immediately following any previously received data.
10+
/// </summary>
11+
/// <param name="name">The name of the multipart data.</param>
12+
/// <param name="fileName">The name of the file.</param>
13+
/// <param name="contentType">The content type of the multipart data.</param>
14+
/// <param name="contentDisposition">The content disposition of the multipart data.</param>
15+
/// <param name="buffer">Some of the data from the file (not necessarily all of the data).</param>
16+
/// <param name="bytes">The length of data in buffer.</param>
17+
/// <param name="partNumber">Each chunk (or "part") in a given file is sequentially numbered, starting at zero.</param>
18+
/// <param name="additionalProperties">Properties other than the "well known" ones (such as name, filename, content-type, etc.) associated with a file stream.</param>
19+
public delegate void FileStreamDelegate(string name, string fileName, string contentType, string contentDisposition, byte[] buffer, int bytes, int partNumber, IDictionary<string, string> additionalProperties);
20+
21+
/// <summary>
22+
/// The StreamClosedDelegate defining functions that can handle stream being closed.
23+
/// </summary>
24+
public delegate void StreamClosedDelegate();
25+
26+
/// <summary>
27+
/// The ParameterDelegate defining functions that can handle multipart parameter data.
28+
/// </summary>
29+
/// <param name="part">The parsed parameter part.</param>
30+
public delegate void ParameterDelegate(ParameterPart part);
31+
32+
/// <summary>
33+
/// The BinaryParameterDelegate defining functions that can handle multipart parameter data.
34+
/// </summary>
35+
/// <param name="binaryPart">The parsed parameter part.</param>
36+
public delegate void BinaryParameterDelegate(ParameterPartBinary binaryPart);
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
namespace HttpMultipartParser
4+
{
5+
/// <summary>
6+
/// Provides methods to parse a
7+
/// <see href="http://www.ietf.org/rfc/rfc2388.txt">
8+
/// <c>multipart/form-data</c>
9+
/// </see>
10+
/// stream into it's parameters and file data.
11+
/// </summary>
12+
public interface IMultipartFormBinaryDataParser
13+
{
14+
/// <summary>
15+
/// Gets the mapping of parameters parsed files. The name of a given field
16+
/// maps to the parsed file data.
17+
/// </summary>
18+
IReadOnlyList<FilePart> Files { get; }
19+
20+
/// <summary>
21+
/// Gets the parameters. Several ParameterPartBinary may share the same name.
22+
/// </summary>
23+
IReadOnlyList<ParameterPartBinary> Parameters { get; }
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace HttpMultipartParser
5+
{
6+
/// <summary>
7+
/// Provides methods to parse a
8+
/// <see href="http://www.ietf.org/rfc/rfc2388.txt">
9+
/// <c>multipart/form-data</c>
10+
/// </see>
11+
/// stream into it's parameters and file data.
12+
/// </summary>
13+
public interface IStreamingBinaryMultipartFormDataParser
14+
{
15+
/// <summary>
16+
/// Gets or sets the FileHandler. Delegates attached to this property will receive sequential file stream data from this parser.
17+
/// </summary>
18+
FileStreamDelegate FileHandler { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the ParameterHandler. Delegates attached to this property will receive parameter data.
22+
/// </summary>
23+
BinaryParameterDelegate ParameterHandler { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the StreamClosedHandler. Delegates attached to this property will be notified when the source stream is exhausted.
27+
/// </summary>
28+
StreamClosedDelegate StreamClosedHandler { get; set; }
29+
30+
/// <summary>
31+
/// Execute the parser. This should be called after all handlers have been set.
32+
/// </summary>
33+
void Run();
34+
35+
/// <summary>
36+
/// Execute the parser asynchronously. This should be called after all handlers have been set.
37+
/// </summary>
38+
/// <param name="cancellationToken">The cancellation token.</param>
39+
/// <returns>The asynchronous task.</returns>
40+
Task RunAsync(CancellationToken cancellationToken = default);
41+
}
42+
}

Source/HttpMultipartParser/IStreamingMultipartFormDataParser.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1-
using System.Collections.Generic;
21
using System.Threading;
32
using System.Threading.Tasks;
43

54
namespace HttpMultipartParser
65
{
7-
/// <summary>
8-
/// The FileStreamDelegate defining functions that can handle file stream data from this parser.
9-
///
10-
/// Delegates can assume that the data is sequential i.e. the data received by any delegates will be
11-
/// the data immediately following any previously received data.
12-
/// </summary>
13-
/// <param name="name">The name of the multipart data.</param>
14-
/// <param name="fileName">The name of the file.</param>
15-
/// <param name="contentType">The content type of the multipart data.</param>
16-
/// <param name="contentDisposition">The content disposition of the multipart data.</param>
17-
/// <param name="buffer">Some of the data from the file (not necessarily all of the data).</param>
18-
/// <param name="bytes">The length of data in buffer.</param>
19-
/// <param name="partNumber">Each chunk (or "part") in a given file is sequentially numbered, starting at zero.</param>
20-
/// <param name="additionalProperties">Properties other than the "well known" ones (such as name, filename, content-type, etc.) associated with a file stream.</param>
21-
public delegate void FileStreamDelegate(string name, string fileName, string contentType, string contentDisposition, byte[] buffer, int bytes, int partNumber, IDictionary<string, string> additionalProperties);
22-
23-
/// <summary>
24-
/// The StreamClosedDelegate defining functions that can handle stream being closed.
25-
/// </summary>
26-
public delegate void StreamClosedDelegate();
27-
28-
/// <summary>
29-
/// The ParameterDelegate defining functions that can handle multipart parameter data.
30-
/// </summary>
31-
/// <param name="part">The parsed parameter part.</param>
32-
public delegate void ParameterDelegate(ParameterPart part);
33-
346
/// <summary>
357
/// Provides methods to parse a
368
/// <see href="http://www.ietf.org/rfc/rfc2388.txt">

0 commit comments

Comments
 (0)