Skip to content

Commit 18b6a96

Browse files
committed
Merge branch 'master' into develop
2 parents 7503e7d + 23e6bcf commit 18b6a96

3 files changed

Lines changed: 83 additions & 37 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The parser was built for and tested on NET 4.6.1, NET 4,7,2 and NETSTANDARD 2.0.
8181
// ==== Advanced Parsing ====
8282
var parser = new StreamingMultipartFormDataParser(stream);
8383
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
84-
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber) =>
84+
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
8585
{
8686
// Write the part of the file we've received to a file stream. (Or do something else)
8787
filestream.Write(buffer, 0, bytes);
@@ -154,7 +154,7 @@ The parser was built for and tested on NET 4.6.1, NET 4,7,2 and NETSTANDARD 2.0.
154154
// ==== Advanced Parsing ====
155155
var parser = new StreamingMultipartFormDataParser(stream);
156156
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
157-
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber) =>
157+
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
158158
{
159159
// Write the part of the file we've received to a file stream. (Or do something else)
160160
// Assume that filesreamsByName is a Dictionary<string, FileStream> of all the files
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.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace HttpMultipartParser
6+
{
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+
34+
/// <summary>
35+
/// Provides methods to parse a
36+
/// <see href="http://www.ietf.org/rfc/rfc2388.txt">
37+
/// <c>multipart/form-data</c>
38+
/// </see>
39+
/// stream into it's parameters and file data.
40+
/// </summary>
41+
public interface IStreamingMultipartFormDataParser
42+
{
43+
/// <summary>
44+
/// Gets or sets the FileHandler. Delegates attached to this property will receive sequential file stream data from this parser.
45+
/// </summary>
46+
FileStreamDelegate FileHandler { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the ParameterHandler. Delegates attached to this property will receive parameter data.
50+
/// </summary>
51+
ParameterDelegate ParameterHandler { get; set; }
52+
53+
/// <summary>
54+
/// Gets or sets the StreamClosedHandler. Delegates attached to this property will be notified when the source stream is exhausted.
55+
/// </summary>
56+
StreamClosedDelegate StreamClosedHandler { get; set; }
57+
58+
/// <summary>
59+
/// Execute the parser. This should be called after all handlers have been set.
60+
/// </summary>
61+
void Run();
62+
63+
/// <summary>
64+
/// Execute the parser asynchronously. This should be called after all handlers have been set.
65+
/// </summary>
66+
/// <param name="cancellationToken">The cancellation token.</param>
67+
/// <returns>The asynchronous task.</returns>
68+
Task RunAsync(CancellationToken cancellationToken = default);
69+
}
70+
}

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace HttpMultipartParser
4141
/// parser.Run();
4242
/// </code>
4343
/// </example>
44-
public class StreamingMultipartFormDataParser
44+
public class StreamingMultipartFormDataParser : IStreamingMultipartFormDataParser
4545
{
4646
#region Constants
4747

@@ -161,6 +161,8 @@ public StreamingMultipartFormDataParser(Stream stream, string boundary = null, E
161161

162162
#endregion
163163

164+
#region Public Methods
165+
164166
/// <summary>
165167
/// Begins executing the parser. This should be called after all handlers have been set.
166168
/// </summary>
@@ -229,53 +231,27 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
229231
await ParseAsync(reader, cancellationToken).ConfigureAwait(false);
230232
}
231233

232-
#region Public Properties
233-
234-
/// <summary>
235-
/// The FileStreamDelegate defining functions that can handle file stream data from this parser.
236-
///
237-
/// Delegates can assume that the data is sequential i.e. the data received by any delegates will be
238-
/// the data immediately following any previously received data.
239-
/// </summary>
240-
/// <param name="name">The name of the multipart data.</param>
241-
/// <param name="fileName">The name of the file.</param>
242-
/// <param name="contentType">The content type of the multipart data.</param>
243-
/// <param name="contentDisposition">The content disposition of the multipart data.</param>
244-
/// <param name="buffer">Some of the data from the file (not necessarily all of the data).</param>
245-
/// <param name="bytes">The length of data in buffer.</param>
246-
/// <param name="partNumber">Each chunk (or "part") in a given file is sequentially numbered, starting at zero.</param>
247-
/// <param name="additionalProperties">Properties other than the "well known" ones (such as name, filename, content-type, etc.) associated with a file stream.</param>
248-
public delegate void FileStreamDelegate(
249-
string name, string fileName, string contentType, string contentDisposition, byte[] buffer, int bytes, int partNumber, IDictionary<string, string> additionalProperties);
250-
251-
/// <summary>
252-
/// The StreamClosedDelegate defining functions that can handle stream being closed.
253-
/// </summary>
254-
public delegate void StreamClosedDelegate();
234+
#endregion
255235

256-
/// <summary>
257-
/// The ParameterDelegate defining functions that can handle multipart parameter data.
258-
/// </summary>
259-
/// <param name="part">The parsed parameter part.</param>
260-
public delegate void ParameterDelegate(ParameterPart part);
236+
#region Public Properties
261237

262238
/// <summary>
263-
/// Gets or sets the binary buffer size.
239+
/// Gets the binary buffer size.
264240
/// </summary>
265-
public int BinaryBufferSize { get; set; }
241+
public int BinaryBufferSize { get; private set; }
266242

267243
/// <summary>
268-
/// Gets the encoding.
244+
/// Gets the encoding.
269245
/// </summary>
270246
public Encoding Encoding { get; private set; }
271247

272248
/// <summary>
273-
/// Gets or sets the FileHandler. Delegates attached to this property will recieve sequential file stream data from this parser.
249+
/// Gets or sets the FileHandler. Delegates attached to this property will receive sequential file stream data from this parser.
274250
/// </summary>
275251
public FileStreamDelegate FileHandler { get; set; }
276252

277253
/// <summary>
278-
/// Gets or sets the ParameterHandler. Delegates attached to this property will recieve parameter data.
254+
/// Gets or sets the ParameterHandler. Delegates attached to this property will receive parameter data.
279255
/// </summary>
280256
public ParameterDelegate ParameterHandler { get; set; }
281257

@@ -286,7 +262,7 @@ public delegate void FileStreamDelegate(
286262

287263
#endregion
288264

289-
#region Methods
265+
#region Private Methods
290266

291267
/// <summary>
292268
/// Detects the boundary from the input stream. Assumes that the

0 commit comments

Comments
 (0)