Skip to content

Commit 744be8c

Browse files
committed
Remove all extensibility from the DefaultHttpContext
- Seal all of the classes - Remove virtual methods - Delete pooled HttpContext code - Removed obsolete AuthenticationManager
1 parent 74f1cca commit 744be8c

18 files changed

+52
-1204
lines changed

src/Http/Http.Abstractions/src/HttpContext.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ public abstract class HttpContext
4040
/// </summary>
4141
public abstract WebSocketManager WebSockets { get; }
4242

43-
/// <summary>
44-
/// This is obsolete and will be removed in a future version.
45-
/// The recommended alternative is to use Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions.
46-
/// See https://go.microsoft.com/fwlink/?linkid=845470.
47-
/// </summary>
48-
[Obsolete("This is obsolete and will be removed in a future version. The recommended alternative is to use Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions. See https://go.microsoft.com/fwlink/?linkid=845470.")]
49-
public abstract AuthenticationManager Authentication { get; }
50-
5143
/// <summary>
5244
/// Gets or sets the user for this request.
5345
/// </summary>

src/Http/Http/src/Authentication/DefaultAuthenticationManager.cs

Lines changed: 0 additions & 184 deletions
This file was deleted.

src/Http/Http/src/DefaultHttpContext.cs

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
using System.Collections.Generic;
66
using System.Security.Claims;
77
using System.Threading;
8-
using Microsoft.AspNetCore.Http.Authentication;
9-
using Microsoft.AspNetCore.Http.Authentication.Internal;
108
using Microsoft.AspNetCore.Http.Features;
119
using Microsoft.AspNetCore.Http.Features.Authentication;
1210
using Microsoft.AspNetCore.Http.Internal;
1311

1412
namespace Microsoft.AspNetCore.Http
1513
{
16-
public class DefaultHttpContext : HttpContext
14+
public sealed class DefaultHttpContext : HttpContext
1715
{
1816
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
1917
private readonly static Func<IFeatureCollection, IItemsFeature> _newItemsFeature = f => new ItemsFeature();
@@ -26,15 +24,11 @@ public class DefaultHttpContext : HttpContext
2624

2725
private FeatureReferences<FeatureInterfaces> _features;
2826

29-
private HttpRequest _request;
30-
private HttpResponse _response;
27+
private readonly DefaultHttpRequest _request;
28+
private readonly DefaultHttpResponse _response;
3129

32-
#pragma warning disable CS0618 // Type or member is obsolete
33-
private AuthenticationManager _authenticationManager;
34-
#pragma warning restore CS0618 // Type or member is obsolete
35-
36-
private ConnectionInfo _connection;
37-
private WebSocketManager _websockets;
30+
private DefaultConnectionInfo _connection;
31+
private DefaultWebSocketManager _websockets;
3832

3933
public DefaultHttpContext()
4034
: this(new FeatureCollection())
@@ -45,46 +39,27 @@ public DefaultHttpContext()
4539

4640
public DefaultHttpContext(IFeatureCollection features)
4741
{
48-
Initialize(features);
42+
_features = new FeatureReferences<FeatureInterfaces>(features);
43+
_request = new DefaultHttpRequest(this);
44+
_response = new DefaultHttpResponse(this);
4945
}
5046

51-
public virtual void Initialize(IFeatureCollection features)
47+
public void Initialize(IFeatureCollection features)
5248
{
5349
_features = new FeatureReferences<FeatureInterfaces>(features);
54-
_request = InitializeHttpRequest();
55-
_response = InitializeHttpResponse();
50+
_request.Initialize();
51+
_response.Initialize();
52+
_connection?.Initialize(features);
53+
_websockets?.Initialize(features);
5654
}
5755

58-
public virtual void Uninitialize()
56+
public void Uninitialize()
5957
{
60-
_features = default(FeatureReferences<FeatureInterfaces>);
61-
if (_request != null)
62-
{
63-
UninitializeHttpRequest(_request);
64-
_request = null;
65-
}
66-
if (_response != null)
67-
{
68-
UninitializeHttpResponse(_response);
69-
_response = null;
70-
}
71-
if (_authenticationManager != null)
72-
{
73-
#pragma warning disable CS0618 // Type or member is obsolete
74-
UninitializeAuthenticationManager(_authenticationManager);
75-
#pragma warning restore CS0618 // Type or member is obsolete
76-
_authenticationManager = null;
77-
}
78-
if (_connection != null)
79-
{
80-
UninitializeConnectionInfo(_connection);
81-
_connection = null;
82-
}
83-
if (_websockets != null)
84-
{
85-
UninitializeWebSocketManager(_websockets);
86-
_websockets = null;
87-
}
58+
_features = default;
59+
_request.Uninitialize();
60+
_response.Uninitialize();
61+
_connection?.Uninitialize();
62+
_websockets?.Uninitialize();
8863
}
8964

9065
private IItemsFeature ItemsFeature =>
@@ -115,17 +90,9 @@ public virtual void Uninitialize()
11590

11691
public override HttpResponse Response => _response;
11792

118-
public override ConnectionInfo Connection => _connection ?? (_connection = InitializeConnectionInfo());
119-
120-
/// <summary>
121-
/// This is obsolete and will be removed in a future version.
122-
/// The recommended alternative is to use Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions.
123-
/// See https://go.microsoft.com/fwlink/?linkid=845470.
124-
/// </summary>
125-
[Obsolete("This is obsolete and will be removed in a future version. The recommended alternative is to use Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions. See https://go.microsoft.com/fwlink/?linkid=845470.")]
126-
public override AuthenticationManager Authentication => _authenticationManager ?? (_authenticationManager = InitializeAuthenticationManager());
93+
public override ConnectionInfo Connection => _connection ?? (_connection = new DefaultConnectionInfo(_features.Collection));
12794

128-
public override WebSocketManager WebSockets => _websockets ?? (_websockets = InitializeWebSocketManager());
95+
public override WebSocketManager WebSockets => _websockets ?? (_websockets = new DefaultWebSocketManager(_features.Collection));
12996

13097

13198
public override ClaimsPrincipal User
@@ -186,30 +153,11 @@ public override ISession Session
186153
}
187154

188155

189-
190156
public override void Abort()
191157
{
192158
LifetimeFeature.Abort();
193159
}
194160

195-
196-
protected virtual HttpRequest InitializeHttpRequest() => new DefaultHttpRequest(this);
197-
protected virtual void UninitializeHttpRequest(HttpRequest instance) { }
198-
199-
protected virtual HttpResponse InitializeHttpResponse() => new DefaultHttpResponse(this);
200-
protected virtual void UninitializeHttpResponse(HttpResponse instance) { }
201-
202-
protected virtual ConnectionInfo InitializeConnectionInfo() => new DefaultConnectionInfo(Features);
203-
protected virtual void UninitializeConnectionInfo(ConnectionInfo instance) { }
204-
205-
[Obsolete("This is obsolete and will be removed in a future version. See https://go.microsoft.com/fwlink/?linkid=845470.")]
206-
protected virtual AuthenticationManager InitializeAuthenticationManager() => new DefaultAuthenticationManager(this);
207-
[Obsolete("This is obsolete and will be removed in a future version. See https://go.microsoft.com/fwlink/?linkid=845470.")]
208-
protected virtual void UninitializeAuthenticationManager(AuthenticationManager instance) { }
209-
210-
protected virtual WebSocketManager InitializeWebSocketManager() => new DefaultWebSocketManager(Features);
211-
protected virtual void UninitializeWebSocketManager(WebSocketManager instance) { }
212-
213161
struct FeatureInterfaces
214162
{
215163
public IItemsFeature Items;

src/Http/Http/src/HttpContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static HttpContext CreateHttpContext(IFeatureCollection featureCollectio
5555
return container.HttpContext;
5656
}
5757

58-
return new ReusableHttpContext(featureCollection);
58+
return new DefaultHttpContext(featureCollection);
5959
}
6060

6161
public void Dispose(HttpContext httpContext)

src/Http/Http/src/Internal/DefaultConnectionInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.AspNetCore.Http.Internal
1212
{
13-
public class DefaultConnectionInfo : ConnectionInfo
13+
public sealed class DefaultConnectionInfo : ConnectionInfo
1414
{
1515
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
1616
private readonly static Func<IFeatureCollection, IHttpConnectionFeature> _newHttpConnectionFeature = f => new HttpConnectionFeature();
@@ -23,14 +23,14 @@ public DefaultConnectionInfo(IFeatureCollection features)
2323
Initialize(features);
2424
}
2525

26-
public virtual void Initialize( IFeatureCollection features)
26+
public void Initialize( IFeatureCollection features)
2727
{
2828
_features = new FeatureReferences<FeatureInterfaces>(features);
2929
}
3030

31-
public virtual void Uninitialize()
31+
public void Uninitialize()
3232
{
33-
_features = default(FeatureReferences<FeatureInterfaces>);
33+
_features = default;
3434
}
3535

3636
private IHttpConnectionFeature HttpConnectionFeature =>
@@ -76,7 +76,7 @@ public override X509Certificate2 ClientCertificate
7676
set { TlsConnectionFeature.ClientCertificate = value; }
7777
}
7878

79-
public override Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken = new CancellationToken())
79+
public override Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken = default)
8080
{
8181
return TlsConnectionFeature.GetClientCertificateAsync(cancellationToken);
8282
}

0 commit comments

Comments
 (0)