Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Add HttpContext.Features, move Get/SetFeature. #383

Merged
merged 1 commit into from
Aug 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Microsoft.AspNet.Http.Abstractions/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Microsoft.AspNet.Http
{
public abstract class HttpContext : IDisposable
{
public abstract IFeatureCollection Features { get; }

public abstract HttpRequest Request { get; }

public abstract HttpResponse Response { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class SendFileResponseExtensions
/// <returns>True if sendfile feature exists in the response.</returns>
public static bool SupportsSendFile([NotNull] this HttpResponse response)
{
return response.HttpContext.GetFeature<IHttpSendFileFeature>() != null;
return response.HttpContext.Features.Get<IHttpSendFileFeature>() != null;
}

/// <summary>
Expand All @@ -47,7 +47,7 @@ public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull]
/// <returns></returns>
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName, long offset, long? count, CancellationToken cancellationToken)
{
var sendFile = response.HttpContext.GetFeature<IHttpSendFileFeature>();
var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
if (sendFile == null)
{
throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
Expand Down
30 changes: 30 additions & 0 deletions src/Microsoft.AspNet.Http.Features/FeatureCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNet.Http.Features
{
public static class FeatureCollectionExtensions
{
/// <summary>
/// Retrieves the requested feature from the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <param name="features">The collection.</param>
/// <returns>The requested feature, or null if it is not present.</returns>
public static TFeature Get<TFeature>(this IFeatureCollection features)
{
return (TFeature)features[typeof(TFeature)];
}

/// <summary>
/// Sets the given feature in the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <param name="features">The collection.</param>
/// <param name="instance">The feature value.</param>
public static void Set<TFeature>(this IFeatureCollection features, TFeature instance)
{
features[typeof(TFeature)] = instance;
}
}
}
6 changes: 4 additions & 2 deletions src/Microsoft.AspNet.Http/DefaultHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class DefaultHttpContext : HttpContext
public DefaultHttpContext()
: this(new FeatureCollection())
{
SetFeature<IHttpRequestFeature>(new HttpRequestFeature());
SetFeature<IHttpResponseFeature>(new HttpResponseFeature());
_features.Set<IHttpRequestFeature>(new HttpRequestFeature());
_features.Set<IHttpResponseFeature>(new HttpResponseFeature());
}

public DefaultHttpContext(IFeatureCollection features)
Expand Down Expand Up @@ -76,6 +76,8 @@ private ISessionFeature SessionFeature
get { return _session.Fetch(_features); }
}

public override IFeatureCollection Features { get { return _features; } }

public override HttpRequest Request { get { return _request; } }

public override HttpResponse Response { get { return _response; } }
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.AspNet.Owin/OwinEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public class OwinEnvironment : IDictionary<string, object>

public OwinEnvironment(HttpContext context)
{
if (context.GetFeature<IHttpRequestFeature>() == null)
if (context.Features.Get<IHttpRequestFeature>() == null)
{
throw new ArgumentException("Missing required feature: " + nameof(IHttpRequestFeature) + ".", nameof(context));
}
if (context.GetFeature<IHttpResponseFeature>() == null)
if (context.Features.Get<IHttpResponseFeature>() == null)
{
throw new ArgumentException("Missing required feature: " + nameof(IHttpResponseFeature) + ".", nameof(context));
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public OwinEnvironment(HttpContext context)
};

// owin.CallCancelled is required but the feature may not be present.
if (context.GetFeature<IHttpRequestLifetimeFeature>() != null)
if (context.Features.Get<IHttpRequestLifetimeFeature>() != null)
{
_entries[OwinConstants.CallCancelled] = new FeatureMap<IHttpRequestLifetimeFeature>(feature => feature.RequestAborted);
}
Expand Down Expand Up @@ -334,7 +334,7 @@ public bool CanSet

internal bool TryGet(HttpContext context, out object value)
{
object featureInstance = context.GetFeature(FeatureInterface);
object featureInstance = context.Features[FeatureInterface];
if (featureInstance == null)
{
value = null;
Expand All @@ -350,7 +350,7 @@ internal bool TryGet(HttpContext context, out object value)

internal void Set(HttpContext context, object value)
{
var feature = context.GetFeature(FeatureInterface);
var feature = context.Features[FeatureInterface];
if (feature == null)
{
if (FeatureFactory == null)
Expand All @@ -360,7 +360,7 @@ internal void Set(HttpContext context, object value)
else
{
feature = FeatureFactory();
context.SetFeature(FeatureInterface, feature);
context.Features[FeatureInterface] = feature;
}
}
Setter(feature, value);
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Owin/OwinExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static AddMiddleware UseOwin(this IApplicationBuilder builder)
{
// Use the existing OWIN env if there is one.
IDictionary<string, object> env;
var owinEnvFeature = httpContext.GetFeature<IOwinEnvironmentFeature>();
var owinEnvFeature = httpContext.Features.Get<IOwinEnvironmentFeature>();
if (owinEnvFeature != null)
{
env = owinEnvFeature.Environment;
Expand Down Expand Up @@ -87,7 +87,7 @@ private static CreateMiddleware CreateMiddlewareFactory(Func<RequestDelegate, Re
{
var app = middleware(httpContext =>
{
return next(httpContext.GetFeature<IOwinEnvironmentFeature>().Environment);
return next(httpContext.Features.Get<IOwinEnvironmentFeature>().Environment);
});

return env =>
Expand All @@ -98,7 +98,7 @@ private static CreateMiddleware CreateMiddlewareFactory(Func<RequestDelegate, Re
if (env.TryGetValue(typeof(HttpContext).FullName, out obj))
{
context = (HttpContext)obj;
context.SetFeature<IOwinEnvironmentFeature>(new OwinEnvironmentFeature() { Environment = env });
context.Features.Set<IOwinEnvironmentFeature>(new OwinEnvironmentFeature() { Environment = env });
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void SendFileSupport()
var context = new DefaultHttpContext();
var response = context.Response;
Assert.False(response.SupportsSendFile());
context.SetFeature<IHttpSendFileFeature>(new FakeSendFileFeature());
context.Features.Set<IHttpSendFileFeature>(new FakeSendFileFeature());
Assert.True(response.SupportsSendFile());
}

Expand All @@ -34,7 +34,7 @@ public async Task SendFileWorks()
var context = new DefaultHttpContext();
var response = context.Response;
var fakeFeature = new FakeSendFileFeature();
context.SetFeature<IHttpSendFileFeature>(fakeFeature);
context.Features.Set<IHttpSendFileFeature>(fakeFeature);

await response.SendFileAsync("bob", 1, 3, CancellationToken.None);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Authentication;
using Microsoft.AspNet.Http.Features.Authentication.Internal;
using Microsoft.AspNet.Http.Internal;
Expand Down Expand Up @@ -50,7 +51,7 @@ public async Task SignInOutIn()
{
var context = CreateContext();
var handler = new AuthHandler();
context.SetFeature<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = handler });
context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = handler });
var user = new ClaimsPrincipal();
await context.Authentication.SignInAsync("ignored", user);
Assert.True(handler.SignedIn);
Expand Down
12 changes: 6 additions & 6 deletions test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void GetOnSessionProperty_ReturnsAvailableSession()
session.Set("key2", null);
var feature = new BlahSessionFeature();
feature.Session = session;
context.SetFeature<ISessionFeature>(feature);
context.Features.Set<ISessionFeature>(feature);

// Act & Assert
Assert.Same(session, context.Session);
Expand Down Expand Up @@ -67,7 +67,7 @@ public void SettingSession_OverridesAvailableSession()
session.Set("key2", null);
var feature = new BlahSessionFeature();
feature.Session = session;
context.SetFeature<ISessionFeature>(feature);
context.Features.Set<ISessionFeature>(feature);

// Act
context.Session = new TestSession();
Expand Down Expand Up @@ -109,9 +109,9 @@ public void EmptyUserIsNeverNull()
public void GetItems_DefaultCollectionProvided()
{
var context = new DefaultHttpContext(new FeatureCollection());
Assert.Null(context.GetFeature<IItemsFeature>());
Assert.Null(context.Features.Get<IItemsFeature>());
var items = context.Items;
Assert.NotNull(context.GetFeature<IItemsFeature>());
Assert.NotNull(context.Features.Get<IItemsFeature>());
Assert.NotNull(items);
Assert.Same(items, context.Items);
var item = new object();
Expand All @@ -123,10 +123,10 @@ public void GetItems_DefaultCollectionProvided()
public void SetItems_NewCollectionUsed()
{
var context = new DefaultHttpContext(new FeatureCollection());
Assert.Null(context.GetFeature<IItemsFeature>());
Assert.Null(context.Features.Get<IItemsFeature>());
var items = new Dictionary<object, object>();
context.Items = items;
Assert.NotNull(context.GetFeature<IItemsFeature>());
Assert.NotNull(context.Features.Get<IItemsFeature>());
Assert.Same(items, context.Items);
var item = new object();
items["foo"] = item;
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.AspNet.Http.Tests/DefaultHttpRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void IsHttps_CorrectlyReflectsScheme()
public void Query_GetAndSet()
{
var request = new DefaultHttpContext().Request;
var requestFeature = request.HttpContext.GetFeature<IHttpRequestFeature>();
var requestFeature = request.HttpContext.Features.Get<IHttpRequestFeature>();
Assert.Equal(string.Empty, requestFeature.QueryString);
Assert.Equal(QueryString.Empty, request.QueryString);
var query0 = request.Query;
Expand Down Expand Up @@ -191,7 +191,7 @@ public void Cookies_GetAndSet()
private static HttpRequest CreateRequest(IDictionary<string, StringValues> headers)
{
var context = new DefaultHttpContext();
context.GetFeature<IHttpRequestFeature>().Headers = headers;
context.Features.Get<IHttpRequestFeature>().Headers = headers;
return context.Request;
}

Expand Down
20 changes: 10 additions & 10 deletions test/Microsoft.AspNet.Http.Tests/FormFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection()
context.Request.Body = new MemoryStream(formContent);

// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
var formFeature = context.Features.Get<IFormFeature>();
Assert.Null(formFeature);

// Act
Expand All @@ -34,7 +34,7 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection()
Assert.Equal("2", formCollection["baz"]);

// Cached
formFeature = context.GetFeature<IFormFeature>();
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
Expand Down Expand Up @@ -132,15 +132,15 @@ public async Task ReadForm_EmptyMultipart_ReturnsParsedFormCollection()
context.Request.Body = new MemoryStream(formContent);

// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
var formFeature = context.Features.Get<IFormFeature>();
Assert.Null(formFeature);

var formCollection = context.Request.Form;

Assert.NotNull(formCollection);

// Cached
formFeature = context.GetFeature<IFormFeature>();
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formCollection, formFeature.Form);
Expand All @@ -161,15 +161,15 @@ public async Task ReadForm_MultipartWithField_ReturnsParsedFormCollection()
context.Request.Body = new MemoryStream(formContent);

// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
var formFeature = context.Features.Get<IFormFeature>();
Assert.Null(formFeature);

var formCollection = context.Request.Form;

Assert.NotNull(formCollection);

// Cached
formFeature = context.GetFeature<IFormFeature>();
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formCollection, formFeature.Form);
Expand All @@ -192,15 +192,15 @@ public async Task ReadFormAsync_MultipartWithFile_ReturnsParsedFormCollection()
context.Request.Body = new MemoryStream(formContent);

// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
var formFeature = context.Features.Get<IFormFeature>();
Assert.Null(formFeature);

var formCollection = await context.Request.ReadFormAsync();

Assert.NotNull(formCollection);

// Cached
formFeature = context.GetFeature<IFormFeature>();
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
Expand Down Expand Up @@ -232,15 +232,15 @@ public async Task ReadFormAsync_MultipartWithFieldAndFile_ReturnsParsedFormColle
context.Request.Body = new MemoryStream(formContent);

// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
var formFeature = context.Features.Get<IFormFeature>();
Assert.Null(formFeature);

var formCollection = await context.Request.ReadFormAsync();

Assert.NotNull(formCollection);

// Cached
formFeature = context.GetFeature<IFormFeature>();
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
Expand Down