Skip to content

Commit 59f0f94

Browse files
committed
Add ParserOptions
1 parent e199922 commit 59f0f94

5 files changed

Lines changed: 301 additions & 171 deletions

File tree

Source/HttpMultipartParser/MultipartFormBinaryDataParser.cs

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// <author>Jake Woods</author>
2121
// --------------------------------------------------------------------------------------------------------------------
2222

23+
using System;
2324
using System.Collections.Generic;
2425
using System.IO;
2526
using System.Text;
@@ -143,9 +144,17 @@ private MultipartFormBinaryDataParser()
143144
/// <returns>
144145
/// A new instance of the <see cref="MultipartFormDataParser"/> class.
145146
/// </returns>
147+
[Obsolete("Please use Parse(Stream, ParserOptions)")]
146148
public static MultipartFormBinaryDataParser Parse(Stream stream, Encoding encoding, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false)
147149
{
148-
return Parse(stream, null, encoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts);
150+
var options = new ParserOptions
151+
{
152+
BinaryBufferSize = binaryBufferSize,
153+
BinaryMimeTypes = binaryMimeTypes,
154+
Encoding = encoding,
155+
IgnoreInvalidParts = ignoreInvalidParts
156+
};
157+
return Parse(stream, options);
149158
}
150159

151160
/// <summary>
@@ -175,10 +184,37 @@ public static MultipartFormBinaryDataParser Parse(Stream stream, Encoding encodi
175184
/// <returns>
176185
/// A new instance of the <see cref="MultipartFormDataParser"/> class.
177186
/// </returns>
187+
[Obsolete("Please use Parse(Stream, ParserOptions)")]
178188
public static MultipartFormBinaryDataParser Parse(Stream stream, string boundary = null, Encoding encoding = null, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false)
189+
{
190+
var options = new ParserOptions
191+
{
192+
BinaryBufferSize = binaryBufferSize,
193+
BinaryMimeTypes = binaryMimeTypes,
194+
Boundary = boundary,
195+
Encoding = encoding,
196+
IgnoreInvalidParts = ignoreInvalidParts
197+
};
198+
return Parse(stream, options);
199+
}
200+
201+
/// <summary>
202+
/// Parse the stream into a new instance of the <see cref="MultipartFormBinaryDataParser" /> class.
203+
/// with the boundary, input encoding and buffer size.
204+
/// </summary>
205+
/// <param name="stream">
206+
/// The stream containing the multipart data.
207+
/// </param>
208+
/// <param name="options">
209+
/// The options that configure the parser.
210+
/// </param>
211+
/// <returns>
212+
/// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
213+
/// </returns>
214+
public static MultipartFormBinaryDataParser Parse(Stream stream, ParserOptions options)
179215
{
180216
var parser = new MultipartFormBinaryDataParser();
181-
parser.ParseStream(stream, boundary, encoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts);
217+
parser.ParseStream(stream, options);
182218
return parser;
183219
}
184220

@@ -208,9 +244,17 @@ public static MultipartFormBinaryDataParser Parse(Stream stream, string boundary
208244
/// <returns>
209245
/// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
210246
/// </returns>
247+
[Obsolete("Please use ParseAsync(Stream, ParserOptions, CancellationToken)")]
211248
public static Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, Encoding encoding, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false, CancellationToken cancellationToken = default)
212249
{
213-
return ParseAsync(stream, null, encoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts, cancellationToken);
250+
var options = new ParserOptions
251+
{
252+
BinaryBufferSize = binaryBufferSize,
253+
BinaryMimeTypes = binaryMimeTypes,
254+
Encoding = encoding,
255+
IgnoreInvalidParts = ignoreInvalidParts
256+
};
257+
return ParseAsync(stream, options, cancellationToken);
214258
}
215259

216260
/// <summary>
@@ -243,43 +287,50 @@ public static Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, Enco
243287
/// <returns>
244288
/// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
245289
/// </returns>
246-
public static async Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, string boundary = null, Encoding encoding = null, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false, CancellationToken cancellationToken = default)
290+
[Obsolete("Please use ParseAsync(Stream, ParserOptions, CancellationToken)")]
291+
public static Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, string boundary = null, Encoding encoding = null, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false, CancellationToken cancellationToken = default)
247292
{
248-
var parser = new MultipartFormBinaryDataParser();
249-
await parser.ParseStreamAsync(stream, boundary, encoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts, cancellationToken).ConfigureAwait(false);
250-
return parser;
293+
var options = new ParserOptions
294+
{
295+
BinaryBufferSize = binaryBufferSize,
296+
BinaryMimeTypes = binaryMimeTypes,
297+
Boundary = boundary,
298+
Encoding = encoding,
299+
IgnoreInvalidParts = ignoreInvalidParts
300+
};
301+
return ParseAsync(stream, options, cancellationToken);
251302
}
252303

253-
#endregion
254-
255-
#region Private Methods
256-
257304
/// <summary>
258-
/// Parse the stream with the boundary, input encoding and buffer size.
305+
/// Asynchronously parse the stream into a new instance of the <see cref="MultipartFormBinaryDataParser" /> class
306+
/// with the boundary, input encoding and buffer size.
259307
/// </summary>
260308
/// <param name="stream">
261309
/// The stream containing the multipart data.
262310
/// </param>
263-
/// <param name="boundary">
264-
/// The multipart/form-data boundary. This should be the value
265-
/// returned by the request header.
266-
/// </param>
267-
/// <param name="encoding">
268-
/// The encoding of the multipart data.
269-
/// </param>
270-
/// <param name="binaryBufferSize">
271-
/// The size of the buffer to use for parsing the multipart form data. This must be larger
272-
/// then (size of boundary + 4 + # bytes in newline).
273-
/// </param>
274-
/// <param name="binaryMimeTypes">
275-
/// List of mimetypes that should be detected as file.
311+
/// <param name="options">
312+
/// The options that configure the parser.
276313
/// </param>
277-
/// <param name="ignoreInvalidParts">
278-
/// By default the parser will throw an exception if it encounters an invalid part. Set this to true to ignore invalid parts.
314+
/// <param name="cancellationToken">
315+
/// The cancellation token.
279316
/// </param>
280-
private void ParseStream(Stream stream, string boundary, Encoding encoding, int binaryBufferSize, string[] binaryMimeTypes, bool ignoreInvalidParts)
317+
/// <returns>
318+
/// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
319+
/// </returns>
320+
public static async Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, ParserOptions options, CancellationToken cancellationToken = default)
281321
{
282-
var streamingParser = new StreamingBinaryMultipartFormDataParser(stream, boundary, encoding ?? Constants.DefaultEncoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts);
322+
var parser = new MultipartFormBinaryDataParser();
323+
await parser.ParseStreamAsync(stream, options, cancellationToken).ConfigureAwait(false);
324+
return parser;
325+
}
326+
327+
#endregion
328+
329+
#region Private Methods
330+
331+
private void ParseStream(Stream stream, ParserOptions options)
332+
{
333+
var streamingParser = new StreamingBinaryMultipartFormDataParser(stream, options);
283334
streamingParser.ParameterHandler += binaryParameterPart =>
284335
{
285336
_parameters.Add(binaryParameterPart);
@@ -305,36 +356,9 @@ private void ParseStream(Stream stream, string boundary, Encoding encoding, int
305356
}
306357
}
307358

308-
/// <summary>
309-
/// Parse the stream with the boundary, input encoding and buffer size.
310-
/// </summary>
311-
/// <param name="stream">
312-
/// The stream containing the multipart data.
313-
/// </param>
314-
/// <param name="boundary">
315-
/// The multipart/form-data boundary. This should be the value
316-
/// returned by the request header.
317-
/// </param>
318-
/// <param name="encoding">
319-
/// The encoding of the multipart data.
320-
/// </param>
321-
/// <param name="binaryBufferSize">
322-
/// The size of the buffer to use for parsing the multipart form data. This must be larger
323-
/// then (size of boundary + 4 + # bytes in newline).
324-
/// </param>
325-
/// <param name="binaryMimeTypes">
326-
/// List of mimetypes that should be detected as file.
327-
/// </param>
328-
/// <param name="ignoreInvalidParts">
329-
/// By default the parser will throw an exception if it encounters an invalid part. Set this to true to ignore invalid parts.
330-
/// </param>
331-
/// <param name="cancellationToken">
332-
/// The cancellation token.
333-
/// </param>
334-
private async Task ParseStreamAsync(Stream stream, string boundary, Encoding encoding, int binaryBufferSize, string[] binaryMimeTypes, bool ignoreInvalidParts, CancellationToken cancellationToken)
359+
private async Task ParseStreamAsync(Stream stream, ParserOptions options, CancellationToken cancellationToken)
335360
{
336-
var desiredEncoding = encoding ?? Constants.DefaultEncoding;
337-
var streamingParser = new StreamingBinaryMultipartFormDataParser(stream, boundary, desiredEncoding, binaryBufferSize, binaryMimeTypes, ignoreInvalidParts);
361+
var streamingParser = new StreamingBinaryMultipartFormDataParser(stream, options);
338362
streamingParser.ParameterHandler += binaryParameterPart =>
339363
{
340364
_parameters.Add(binaryParameterPart);

0 commit comments

Comments
 (0)