Skip to content

Commit ff31b95

Browse files
committed
#80 - Add CancellationToken to GetClientCertAsyc, GetFormAsync.
1 parent 82f7258 commit ff31b95

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

src/Microsoft.AspNet.Http/HttpRequest.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ public abstract class HttpRequest
6363
public abstract IReadableStringCollection Query { get; }
6464

6565
/// <summary>
66-
/// Gets the query value collection form collection.
66+
/// Gets the form collection.
6767
/// </summary>
6868
/// <returns>The form collection parsed from the request body.</returns>
69-
public abstract Task<IReadableStringCollection> GetFormAsync();
69+
public virtual Task<IReadableStringCollection> GetFormAsync()
70+
{
71+
return GetFormAsync(CancellationToken.None);
72+
}
73+
74+
/// <summary>
75+
/// Gets the form collection.
76+
/// </summary>
77+
/// <returns>The form collection parsed from the request body.</returns>
78+
public abstract Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel);
7079

7180
/// <summary>
7281
/// Gets or set the owin.RequestProtocol.

src/Microsoft.AspNet.HttpFeature/IHttpClientCertificateFeature.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Security.Cryptography.X509Certificates;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.Framework.Runtime;
78

@@ -19,6 +20,6 @@ public interface IHttpClientCertificateFeature
1920
/// Asynchronously retrieves the client certificate, if any.
2021
/// </summary>
2122
/// <returns></returns>
22-
Task<X509Certificate> GetClientCertificateAsync();
23+
Task<X509Certificate> GetClientCertificateAsync(CancellationToken cancel);
2324
}
2425
}

src/Microsoft.AspNet.Owin/OwinEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public OwinEnvironment(HttpContext context)
6969
{
7070
_entries.Add(OwinConstants.CommonKeys.ClientCertificate, new FeatureMap<IHttpClientCertificateFeature>(feature => feature.ClientCertificate,
7171
(feature, value) => feature.ClientCertificate = (X509Certificate)value));
72-
_entries.Add(OwinConstants.CommonKeys.LoadClientCertAsync, new FeatureMap<IHttpClientCertificateFeature>(feature => new Func<Task>(feature.GetClientCertificateAsync)));
72+
_entries.Add(OwinConstants.CommonKeys.LoadClientCertAsync, new FeatureMap<IHttpClientCertificateFeature>(
73+
feature => new Func<Task>(() => feature.GetClientCertificateAsync(CancellationToken.None))));
7374
}
7475

7576
_context.Items[typeof(HttpContext).FullName] = _context; // Store for lookup when we transition back out of OWIN

src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ X509Certificate IHttpClientCertificateFeature.ClientCertificate
203203
set { Prop(OwinConstants.CommonKeys.ClientCertificate, value); }
204204
}
205205

206-
Task<X509Certificate> IHttpClientCertificateFeature.GetClientCertificateAsync()
206+
Task<X509Certificate> IHttpClientCertificateFeature.GetClientCertificateAsync(CancellationToken cancel)
207207
{
208208
throw new NotImplementedException();
209209
}

src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public override IReadableStringCollection Query
129129
get { return QueryFeature.Query; }
130130
}
131131

132-
public override Task<IReadableStringCollection> GetFormAsync()
132+
public override Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel)
133133
{
134-
return FormFeature.GetFormAsync();
134+
return FormFeature.GetFormAsync(cancel);
135135
}
136136

137137
public override string Protocol

src/Microsoft.AspNet.PipelineCore/FormFeature.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
using System.IO;
55
using System.Text;
6+
using System.Threading;
67
using System.Threading.Tasks;
7-
using Microsoft.AspNet.Http;
88
using Microsoft.AspNet.FeatureModel;
9+
using Microsoft.AspNet.Http;
910
using Microsoft.AspNet.HttpFeature;
1011
using Microsoft.AspNet.PipelineCore.Collections;
1112
using Microsoft.AspNet.PipelineCore.Infrastructure;
@@ -24,14 +25,22 @@ public FormFeature(IFeatureCollection features)
2425
_features = features;
2526
}
2627

27-
public async Task<IReadableStringCollection> GetFormAsync()
28+
public async Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel)
2829
{
2930
var body = _request.Fetch(_features).Body;
3031

3132
if (_bodyStream == null || _bodyStream != body)
3233
{
3334
_bodyStream = body;
34-
using (var streamReader = new StreamReader(body, Encoding.UTF8,
35+
if (!_bodyStream.CanSeek)
36+
{
37+
MemoryStream buffer = new MemoryStream();
38+
await _bodyStream.CopyToAsync(buffer, 4096, cancel);
39+
_bodyStream = buffer;
40+
_request.Fetch(_features).Body = _bodyStream;
41+
_bodyStream.Seek(0, SeekOrigin.Begin);
42+
}
43+
using (var streamReader = new StreamReader(_bodyStream, Encoding.UTF8,
3544
detectEncodingFromByteOrderMarks: true,
3645
bufferSize: 1024, leaveOpen: true))
3746
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.AspNet.Http;
67

78
namespace Microsoft.AspNet.PipelineCore
89
{
910
public interface IFormFeature
1011
{
11-
Task<IReadableStringCollection> GetFormAsync();
12+
Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel);
1213
}
1314
}

test/Microsoft.AspNet.PipelineCore.Tests/FormFeatureTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.IO;
55
using System.Text;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.AspNet.FeatureModel;
89
using Microsoft.AspNet.HttpFeature;
@@ -29,7 +30,7 @@ public async Task GetFormAsync_ReturnsParsedFormCollection()
2930
var provider = new FormFeature(features.Object);
3031

3132
// Act
32-
var formCollection = await provider.GetFormAsync();
33+
var formCollection = await provider.GetFormAsync(CancellationToken.None);
3334

3435
// Assert
3536
Assert.Equal("bar", formCollection["foo"]);
@@ -53,16 +54,16 @@ public async Task GetFormAsync_CachesFormCollectionPerBodyStream()
5354
var provider = new FormFeature(features.Object);
5455

5556
// Act - 1
56-
var formCollection = await provider.GetFormAsync();
57+
var formCollection = await provider.GetFormAsync(CancellationToken.None);
5758

5859
// Assert - 1
5960
Assert.Equal("bar", formCollection["foo"]);
6061
Assert.Equal("2", formCollection["baz"]);
61-
Assert.Same(formCollection, await provider.GetFormAsync());
62+
Assert.Same(formCollection, await provider.GetFormAsync(CancellationToken.None));
6263

6364
// Act - 2
6465
request.SetupGet(r => r.Body).Returns(new MemoryStream(formContent2));
65-
formCollection = await provider.GetFormAsync();
66+
formCollection = await provider.GetFormAsync(CancellationToken.None);
6667

6768
// Assert - 2
6869
Assert.Equal("value", formCollection["collection2"]);

0 commit comments

Comments
 (0)