2323using System . Collections . Generic ;
2424using System . IO ;
2525using System . Text ;
26+ using System . Threading ;
2627using System . Threading . Tasks ;
2728
2829namespace HttpMultipartParser
@@ -201,12 +202,15 @@ public static MultipartFormBinaryDataParser Parse(Stream stream, string boundary
201202 /// <param name="ignoreInvalidParts">
202203 /// By default the parser will throw an exception if it encounters an invalid part. Set this to true to ignore invalid parts.
203204 /// </param>
205+ /// <param name="cancellationToken">
206+ /// The cancellation token.
207+ /// </param>
204208 /// <returns>
205209 /// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
206210 /// </returns>
207- public static Task < MultipartFormBinaryDataParser > ParseAsync ( Stream stream , Encoding encoding , int binaryBufferSize = Constants . DefaultBufferSize , string [ ] binaryMimeTypes = null , bool ignoreInvalidParts = false )
211+ public static Task < MultipartFormBinaryDataParser > ParseAsync ( Stream stream , Encoding encoding , int binaryBufferSize = Constants . DefaultBufferSize , string [ ] binaryMimeTypes = null , bool ignoreInvalidParts = false , CancellationToken cancellationToken = default )
208212 {
209- return ParseAsync ( stream , null , encoding , binaryBufferSize , binaryMimeTypes , ignoreInvalidParts ) ;
213+ return ParseAsync ( stream , null , encoding , binaryBufferSize , binaryMimeTypes , ignoreInvalidParts , cancellationToken ) ;
210214 }
211215
212216 /// <summary>
@@ -233,13 +237,16 @@ public static Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, Enco
233237 /// <param name="ignoreInvalidParts">
234238 /// By default the parser will throw an exception if it encounters an invalid part. Set this to true to ignore invalid parts.
235239 /// </param>
240+ /// <param name="cancellationToken">
241+ /// The cancellation token.
242+ /// </param>
236243 /// <returns>
237244 /// A new instance of the <see cref="MultipartFormBinaryDataParser"/> class.
238245 /// </returns>
239- public static async Task < MultipartFormBinaryDataParser > ParseAsync ( Stream stream , string boundary = null , Encoding encoding = null , int binaryBufferSize = Constants . DefaultBufferSize , string [ ] binaryMimeTypes = null , bool ignoreInvalidParts = false )
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 )
240247 {
241248 var parser = new MultipartFormBinaryDataParser ( ) ;
242- await parser . ParseStreamAsync ( stream , boundary , encoding , binaryBufferSize , binaryMimeTypes , ignoreInvalidParts ) . ConfigureAwait ( false ) ;
249+ await parser . ParseStreamAsync ( stream , boundary , encoding , binaryBufferSize , binaryMimeTypes , ignoreInvalidParts , cancellationToken ) . ConfigureAwait ( false ) ;
243250 return parser ;
244251 }
245252
@@ -321,7 +328,10 @@ private void ParseStream(Stream stream, string boundary, Encoding encoding, int
321328 /// <param name="ignoreInvalidParts">
322329 /// By default the parser will throw an exception if it encounters an invalid part. Set this to true to ignore invalid parts.
323330 /// </param>
324- private async Task ParseStreamAsync ( Stream stream , string boundary , Encoding encoding , int binaryBufferSize , string [ ] binaryMimeTypes , bool ignoreInvalidParts )
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 )
325335 {
326336 var desiredEncoding = encoding ?? Constants . DefaultEncoding ;
327337 var streamingParser = new StreamingBinaryMultipartFormDataParser ( stream , boundary , desiredEncoding , binaryBufferSize , binaryMimeTypes , ignoreInvalidParts ) ;
@@ -341,7 +351,7 @@ private async Task ParseStreamAsync(Stream stream, string boundary, Encoding enc
341351 Files [ Files . Count - 1 ] . Data . Write ( buffer , 0 , bytes ) ;
342352 } ;
343353
344- await streamingParser . RunAsync ( ) . ConfigureAwait ( false ) ;
354+ await streamingParser . RunAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
345355
346356 // Reset all the written memory streams so they can be read.
347357 foreach ( var file in Files )
0 commit comments