Skip to content

Commit ea31f02

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 edfb2ab commit ea31f02

File tree

8 files changed

+46
-225
lines changed

8 files changed

+46
-225
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/DefaultHttpContext.cs

Lines changed: 33 additions & 67 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;
31-
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;
27+
private DefaultHttpRequest _request;
28+
private DefaultHttpResponse _response;
29+
30+
private DefaultConnectionInfo _connection;
31+
private DefaultWebSocketManager _websockets;
3832

3933
public DefaultHttpContext()
4034
: this(new FeatureCollection())
@@ -48,43 +42,42 @@ public DefaultHttpContext(IFeatureCollection features)
4842
Initialize(features);
4943
}
5044

51-
public virtual void Initialize(IFeatureCollection features)
45+
public void Initialize(IFeatureCollection features)
5246
{
5347
_features = new FeatureReferences<FeatureInterfaces>(features);
54-
_request = InitializeHttpRequest();
55-
_response = InitializeHttpResponse();
56-
}
5748

58-
public virtual void Uninitialize()
59-
{
60-
_features = default(FeatureReferences<FeatureInterfaces>);
61-
if (_request != null)
62-
{
63-
UninitializeHttpRequest(_request);
64-
_request = null;
65-
}
66-
if (_response != null)
49+
if (_request is null)
6750
{
68-
UninitializeHttpResponse(_response);
69-
_response = null;
51+
_request = new DefaultHttpRequest(this);
7052
}
71-
if (_authenticationManager != null)
53+
else
7254
{
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;
55+
_request.Initialize(this);
7756
}
78-
if (_connection != null)
57+
58+
if (_response is null)
7959
{
80-
UninitializeConnectionInfo(_connection);
81-
_connection = null;
60+
_response = new DefaultHttpResponse(this);
8261
}
83-
if (_websockets != null)
62+
else
8463
{
85-
UninitializeWebSocketManager(_websockets);
86-
_websockets = null;
64+
_response.Initialize(this);
8765
}
66+
67+
// Only set the ConnectionInfo if it was already allocated
68+
_connection?.Initialize(features);
69+
}
70+
71+
public void Uninitialize()
72+
{
73+
_features = default;
74+
75+
_request?.Uninitialize();
76+
_response?.Uninitialize();
77+
_connection?.Uninitialize();
78+
_websockets?.Uninitialize();
79+
80+
_websockets = null;
8881
}
8982

9083
private IItemsFeature ItemsFeature =>
@@ -115,17 +108,9 @@ public virtual void Uninitialize()
115108

116109
public override HttpResponse Response => _response;
117110

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());
111+
public override ConnectionInfo Connection => _connection ?? (_connection = new DefaultConnectionInfo(_features.Collection));
127112

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

130115

131116
public override ClaimsPrincipal User
@@ -186,30 +171,11 @@ public override ISession Session
186171
}
187172

188173

189-
190174
public override void Abort()
191175
{
192176
LifetimeFeature.Abort();
193177
}
194178

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-
213179
struct FeatureInterfaces
214180
{
215181
public IItemsFeature Items;

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
}

src/Http/Http/src/Internal/DefaultHttpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.AspNetCore.Http.Internal
1313
{
14-
public class DefaultHttpRequest : HttpRequest
14+
public sealed class DefaultHttpRequest : HttpRequest
1515
{
1616
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
1717
private readonly static Func<IFeatureCollection, IHttpRequestFeature> _nullRequestFeature = f => null;

src/Http/Http/src/Internal/DefaultHttpResponse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Microsoft.AspNetCore.Http.Internal
1111
{
12-
public class DefaultHttpResponse : HttpResponse
12+
public sealed class DefaultHttpResponse : HttpResponse
1313
{
1414
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
1515
private readonly static Func<IFeatureCollection, IHttpResponseFeature> _nullResponseFeature = f => null;
@@ -23,13 +23,13 @@ public DefaultHttpResponse(HttpContext context)
2323
Initialize(context);
2424
}
2525

26-
public virtual void Initialize(HttpContext context)
26+
public void Initialize(HttpContext context)
2727
{
2828
_context = context;
2929
_features = new FeatureReferences<FeatureInterfaces>(context.Features);
3030
}
3131

32-
public virtual void Uninitialize()
32+
public void Uninitialize()
3333
{
3434
_context = null;
3535
_features = default(FeatureReferences<FeatureInterfaces>);

src/Http/Http/src/Internal/DefaultWebSocketManager.cs

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

1111
namespace Microsoft.AspNetCore.Http.Internal
1212
{
13-
public class DefaultWebSocketManager : WebSocketManager
13+
public sealed class DefaultWebSocketManager : WebSocketManager
1414
{
1515
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
1616
private readonly static Func<IFeatureCollection, IHttpRequestFeature> _nullRequestFeature = f => null;
@@ -23,14 +23,14 @@ public DefaultWebSocketManager(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 IHttpRequestFeature HttpRequestFeature =>

src/Http/samples/SampleApp/PooledHttpContext.cs

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

0 commit comments

Comments
 (0)