#80 - Add CancellationToken to GetClientCertAsyc, GetFormAsync.

This commit is contained in:
Chris Ross 2014-06-24 12:06:55 -07:00
parent 82f72581c9
commit ff31b958fe
8 changed files with 37 additions and 15 deletions

View File

@ -63,10 +63,19 @@ namespace Microsoft.AspNet.Http
public abstract IReadableStringCollection Query { get; } public abstract IReadableStringCollection Query { get; }
/// <summary> /// <summary>
/// Gets the query value collection form collection. /// Gets the form collection.
/// </summary> /// </summary>
/// <returns>The form collection parsed from the request body.</returns> /// <returns>The form collection parsed from the request body.</returns>
public abstract Task<IReadableStringCollection> GetFormAsync(); public virtual Task<IReadableStringCollection> GetFormAsync()
{
return GetFormAsync(CancellationToken.None);
}
/// <summary>
/// Gets the form collection.
/// </summary>
/// <returns>The form collection parsed from the request body.</returns>
public abstract Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel);
/// <summary> /// <summary>
/// Gets or set the owin.RequestProtocol. /// Gets or set the owin.RequestProtocol.

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Framework.Runtime; using Microsoft.Framework.Runtime;
@ -19,6 +20,6 @@ namespace Microsoft.AspNet.HttpFeature
/// Asynchronously retrieves the client certificate, if any. /// Asynchronously retrieves the client certificate, if any.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Task<X509Certificate> GetClientCertificateAsync(); Task<X509Certificate> GetClientCertificateAsync(CancellationToken cancel);
} }
} }

View File

@ -69,7 +69,8 @@ namespace Microsoft.AspNet.Owin
{ {
_entries.Add(OwinConstants.CommonKeys.ClientCertificate, new FeatureMap<IHttpClientCertificateFeature>(feature => feature.ClientCertificate, _entries.Add(OwinConstants.CommonKeys.ClientCertificate, new FeatureMap<IHttpClientCertificateFeature>(feature => feature.ClientCertificate,
(feature, value) => feature.ClientCertificate = (X509Certificate)value)); (feature, value) => feature.ClientCertificate = (X509Certificate)value));
_entries.Add(OwinConstants.CommonKeys.LoadClientCertAsync, new FeatureMap<IHttpClientCertificateFeature>(feature => new Func<Task>(feature.GetClientCertificateAsync))); _entries.Add(OwinConstants.CommonKeys.LoadClientCertAsync, new FeatureMap<IHttpClientCertificateFeature>(
feature => new Func<Task>(() => feature.GetClientCertificateAsync(CancellationToken.None))));
} }
_context.Items[typeof(HttpContext).FullName] = _context; // Store for lookup when we transition back out of OWIN _context.Items[typeof(HttpContext).FullName] = _context; // Store for lookup when we transition back out of OWIN

View File

@ -203,7 +203,7 @@ namespace Microsoft.AspNet.Owin
set { Prop(OwinConstants.CommonKeys.ClientCertificate, value); } set { Prop(OwinConstants.CommonKeys.ClientCertificate, value); }
} }
Task<X509Certificate> IHttpClientCertificateFeature.GetClientCertificateAsync() Task<X509Certificate> IHttpClientCertificateFeature.GetClientCertificateAsync(CancellationToken cancel)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -129,9 +129,9 @@ namespace Microsoft.AspNet.PipelineCore
get { return QueryFeature.Query; } get { return QueryFeature.Query; }
} }
public override Task<IReadableStringCollection> GetFormAsync() public override Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel)
{ {
return FormFeature.GetFormAsync(); return FormFeature.GetFormAsync(cancel);
} }
public override string Protocol public override string Protocol

View File

@ -3,9 +3,10 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.HttpFeature;
using Microsoft.AspNet.PipelineCore.Collections; using Microsoft.AspNet.PipelineCore.Collections;
using Microsoft.AspNet.PipelineCore.Infrastructure; using Microsoft.AspNet.PipelineCore.Infrastructure;
@ -24,14 +25,22 @@ namespace Microsoft.AspNet.PipelineCore
_features = features; _features = features;
} }
public async Task<IReadableStringCollection> GetFormAsync() public async Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel)
{ {
var body = _request.Fetch(_features).Body; var body = _request.Fetch(_features).Body;
if (_bodyStream == null || _bodyStream != body) if (_bodyStream == null || _bodyStream != body)
{ {
_bodyStream = body; _bodyStream = body;
using (var streamReader = new StreamReader(body, Encoding.UTF8, if (!_bodyStream.CanSeek)
{
MemoryStream buffer = new MemoryStream();
await _bodyStream.CopyToAsync(buffer, 4096, cancel);
_bodyStream = buffer;
_request.Fetch(_features).Body = _bodyStream;
_bodyStream.Seek(0, SeekOrigin.Begin);
}
using (var streamReader = new StreamReader(_bodyStream, Encoding.UTF8,
detectEncodingFromByteOrderMarks: true, detectEncodingFromByteOrderMarks: true,
bufferSize: 1024, leaveOpen: true)) bufferSize: 1024, leaveOpen: true))
{ {

View File

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
@ -8,6 +9,6 @@ namespace Microsoft.AspNet.PipelineCore
{ {
public interface IFormFeature public interface IFormFeature
{ {
Task<IReadableStringCollection> GetFormAsync(); Task<IReadableStringCollection> GetFormAsync(CancellationToken cancel);
} }
} }

View File

@ -3,6 +3,7 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.HttpFeature;
@ -29,7 +30,7 @@ namespace Microsoft.AspNet.PipelineCore.Tests
var provider = new FormFeature(features.Object); var provider = new FormFeature(features.Object);
// Act // Act
var formCollection = await provider.GetFormAsync(); var formCollection = await provider.GetFormAsync(CancellationToken.None);
// Assert // Assert
Assert.Equal("bar", formCollection["foo"]); Assert.Equal("bar", formCollection["foo"]);
@ -53,16 +54,16 @@ namespace Microsoft.AspNet.PipelineCore.Tests
var provider = new FormFeature(features.Object); var provider = new FormFeature(features.Object);
// Act - 1 // Act - 1
var formCollection = await provider.GetFormAsync(); var formCollection = await provider.GetFormAsync(CancellationToken.None);
// Assert - 1 // Assert - 1
Assert.Equal("bar", formCollection["foo"]); Assert.Equal("bar", formCollection["foo"]);
Assert.Equal("2", formCollection["baz"]); Assert.Equal("2", formCollection["baz"]);
Assert.Same(formCollection, await provider.GetFormAsync()); Assert.Same(formCollection, await provider.GetFormAsync(CancellationToken.None));
// Act - 2 // Act - 2
request.SetupGet(r => r.Body).Returns(new MemoryStream(formContent2)); request.SetupGet(r => r.Body).Returns(new MemoryStream(formContent2));
formCollection = await provider.GetFormAsync(); formCollection = await provider.GetFormAsync(CancellationToken.None);
// Assert - 2 // Assert - 2
Assert.Equal("value", formCollection["collection2"]); Assert.Equal("value", formCollection["collection2"]);