Skip to content

Commit 0fce1c5

Browse files
Add CancellationToken overloads for reading multipart form sections (#41533)
1 parent 5974b48 commit 0fce1c5

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

src/Http/Http/src/Features/FormFeature.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public class FormFeature : IFormFeature
2525
/// <param name="form">The <see cref="IFormCollection"/> to use as the backing store.</param>
2626
public FormFeature(IFormCollection form)
2727
{
28-
if (form == null)
29-
{
30-
throw new ArgumentNullException(nameof(form));
31-
}
28+
ArgumentNullException.ThrowIfNull(form);
3229

3330
Form = form;
3431
_request = default!;
@@ -51,14 +48,8 @@ public FormFeature(HttpRequest request)
5148
/// <param name="options">The <see cref="FormOptions"/>.</param>
5249
public FormFeature(HttpRequest request, FormOptions options)
5350
{
54-
if (request == null)
55-
{
56-
throw new ArgumentNullException(nameof(request));
57-
}
58-
if (options == null)
59-
{
60-
throw new ArgumentNullException(nameof(options));
61-
}
51+
ArgumentNullException.ThrowIfNull(request);
52+
ArgumentNullException.ThrowIfNull(options);
6253

6354
_request = request;
6455
_options = options;
@@ -68,7 +59,7 @@ private MediaTypeHeaderValue? ContentType
6859
{
6960
get
7061
{
71-
MediaTypeHeaderValue.TryParse(_request.ContentType, out var mt);
62+
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out var mt);
7263
return mt;
7364
}
7465
}
@@ -247,7 +238,7 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
247238

248239
// Do not limit the key name length here because the multipart headers length limit is already in effect.
249240
var key = formDataSection.Name;
250-
var value = await formDataSection.GetValueAsync();
241+
var value = await formDataSection.GetValueAsync(cancellationToken);
251242

252243
formAccumulator.Append(key, value);
253244
}

src/Http/WebUtilities/src/FormMultipartSection.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ public FormMultipartSection(MultipartSection section, ContentDispositionHeaderVa
5353
/// Gets the form value
5454
/// </summary>
5555
/// <returns>The form value</returns>
56-
public Task<string> GetValueAsync()
57-
{
58-
return Section.ReadAsStringAsync();
59-
}
56+
public Task<string> GetValueAsync() => Section.ReadAsStringAsync();
57+
58+
/// <summary>
59+
/// Gets the form value
60+
/// </summary>
61+
/// <param name="cancellationToken">The cancellation token.</param>
62+
/// <returns>The form value</returns>
63+
public ValueTask<string> GetValueAsync(CancellationToken cancellationToken)
64+
=> Section.ReadAsStringAsync(cancellationToken);
6065
}

src/Http/WebUtilities/src/MultipartSectionStreamExtensions.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ public static class MultipartSectionStreamExtensions
1616
/// </summary>
1717
/// <param name="section">The section to read from</param>
1818
/// <returns>The body steam as string</returns>
19-
public static async Task<string> ReadAsStringAsync(this MultipartSection section)
19+
public static Task<string> ReadAsStringAsync(this MultipartSection section)
20+
=> section.ReadAsStringAsync(CancellationToken.None).AsTask();
21+
22+
/// <summary>
23+
/// Reads the body of the section as a string
24+
/// </summary>
25+
/// <param name="section">The section to read from</param>
26+
/// <param name="cancellationToken">The cancellationt token.</param>
27+
/// <returns>The body steam as string</returns>
28+
public static async ValueTask<string> ReadAsStringAsync(this MultipartSection section, CancellationToken cancellationToken)
2029
{
21-
if (section == null)
22-
{
23-
throw new ArgumentNullException(nameof(section));
24-
}
30+
ArgumentNullException.ThrowIfNull(section);
2531

2632
if (section.Body is null)
2733
{
2834
throw new ArgumentException("Multipart section must have a body to be read.", nameof(section));
2935
}
3036

31-
MediaTypeHeaderValue.TryParse(section.ContentType, out var sectionMediaType);
37+
_ = MediaTypeHeaderValue.TryParse(section.ContentType, out var sectionMediaType);
3238

3339
var streamEncoding = sectionMediaType?.Encoding;
3440
// https://docs.microsoft.com/en-us/dotnet/core/compatibility/syslib-warnings/syslib0001
@@ -37,14 +43,12 @@ public static async Task<string> ReadAsStringAsync(this MultipartSection section
3743
streamEncoding = Encoding.UTF8;
3844
}
3945

40-
using (var reader = new StreamReader(
46+
using var reader = new StreamReader(
4147
section.Body,
4248
streamEncoding,
4349
detectEncodingFromByteOrderMarks: true,
4450
bufferSize: 1024,
45-
leaveOpen: true))
46-
{
47-
return await reader.ReadToEndAsync();
48-
}
51+
leaveOpen: true);
52+
return await reader.ReadToEndAsync(cancellationToken);
4953
}
5054
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#nullable enable
2+
Microsoft.AspNetCore.WebUtilities.FormMultipartSection.GetValueAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<string!>
23
override Microsoft.AspNetCore.WebUtilities.BufferedReadStream.WriteAsync(System.ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
34
override Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.WriteAsync(System.ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
45
override Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream.ReadAsync(System.Memory<byte> buffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<int>
56
override Microsoft.AspNetCore.WebUtilities.HttpResponseStreamWriter.WriteLineAsync(char value) -> System.Threading.Tasks.Task!
67
override Microsoft.AspNetCore.WebUtilities.HttpResponseStreamWriter.WriteLineAsync(char[]! values, int index, int count) -> System.Threading.Tasks.Task!
78
override Microsoft.AspNetCore.WebUtilities.HttpResponseStreamWriter.WriteLineAsync(string? value) -> System.Threading.Tasks.Task!
9+
static Microsoft.AspNetCore.WebUtilities.MultipartSectionStreamExtensions.ReadAsStringAsync(this Microsoft.AspNetCore.WebUtilities.MultipartSection! section, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<string!>

0 commit comments

Comments
 (0)