|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Security.Claims; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | +using Microsoft.AspNetCore.Http.Authentication; |
| 7 | +using Microsoft.AspNetCore.Http.Features; |
| 8 | +using Microsoft.AspNetCore.Http.Features.Authentication; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Http.Internal |
| 11 | +{ |
| 12 | + public sealed class ReusableHttpContext : HttpContext |
| 13 | + { |
| 14 | + // Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 |
| 15 | + private readonly static Func<IFeatureCollection, IItemsFeature> _newItemsFeature = f => new ItemsFeature(); |
| 16 | + private readonly static Func<IFeatureCollection, IServiceProvidersFeature> _newServiceProvidersFeature = f => new ServiceProvidersFeature(); |
| 17 | + private readonly static Func<IFeatureCollection, IHttpAuthenticationFeature> _newHttpAuthenticationFeature = f => new HttpAuthenticationFeature(); |
| 18 | + private readonly static Func<IFeatureCollection, IHttpRequestLifetimeFeature> _newHttpRequestLifetimeFeature = f => new HttpRequestLifetimeFeature(); |
| 19 | + private readonly static Func<IFeatureCollection, ISessionFeature> _newSessionFeature = f => new DefaultSessionFeature(); |
| 20 | + private readonly static Func<IFeatureCollection, ISessionFeature> _nullSessionFeature = f => null; |
| 21 | + private readonly static Func<IFeatureCollection, IHttpRequestIdentifierFeature> _newHttpRequestIdentifierFeature = f => new HttpRequestIdentifierFeature(); |
| 22 | + |
| 23 | + private FeatureReferences<FeatureInterfaces> _features; |
| 24 | + |
| 25 | + private ReusableHttpRequest _request; |
| 26 | + private ReusableHttpResponse _response; |
| 27 | + |
| 28 | + private ReusableConnectionInfo _connection; |
| 29 | + private ReusableWebSocketManager _websockets; |
| 30 | + |
| 31 | + public ReusableHttpContext(IFeatureCollection features) |
| 32 | + { |
| 33 | + _features = new FeatureReferences<FeatureInterfaces>(features); |
| 34 | + _request = new ReusableHttpRequest(this); |
| 35 | + _response = new ReusableHttpResponse(this); |
| 36 | + } |
| 37 | + |
| 38 | + public void Initialize(IFeatureCollection features) |
| 39 | + { |
| 40 | + _features = new FeatureReferences<FeatureInterfaces>(features); |
| 41 | + _request.Initialize(this); |
| 42 | + _response.Initialize(this); |
| 43 | + _connection?.Initialize(features); |
| 44 | + _websockets?.Initialize(features); |
| 45 | + } |
| 46 | + |
| 47 | + public void Uninitialize() |
| 48 | + { |
| 49 | + _features = default; |
| 50 | + |
| 51 | + _request.Uninitialize(); |
| 52 | + _response.Uninitialize(); |
| 53 | + _connection?.Uninitialize(); |
| 54 | + _websockets?.Uninitialize(); |
| 55 | + } |
| 56 | + |
| 57 | + private IItemsFeature ItemsFeature => |
| 58 | + _features.Fetch(ref _features.Cache.Items, _newItemsFeature); |
| 59 | + |
| 60 | + private IServiceProvidersFeature ServiceProvidersFeature => |
| 61 | + _features.Fetch(ref _features.Cache.ServiceProviders, _newServiceProvidersFeature); |
| 62 | + |
| 63 | + private IHttpAuthenticationFeature HttpAuthenticationFeature => |
| 64 | + _features.Fetch(ref _features.Cache.Authentication, _newHttpAuthenticationFeature); |
| 65 | + |
| 66 | + private IHttpRequestLifetimeFeature LifetimeFeature => |
| 67 | + _features.Fetch(ref _features.Cache.Lifetime, _newHttpRequestLifetimeFeature); |
| 68 | + |
| 69 | + private ISessionFeature SessionFeature => |
| 70 | + _features.Fetch(ref _features.Cache.Session, _newSessionFeature); |
| 71 | + |
| 72 | + private ISessionFeature SessionFeatureOrNull => |
| 73 | + _features.Fetch(ref _features.Cache.Session, _nullSessionFeature); |
| 74 | + |
| 75 | + |
| 76 | + private IHttpRequestIdentifierFeature RequestIdentifierFeature => |
| 77 | + _features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature); |
| 78 | + |
| 79 | + public override IFeatureCollection Features => _features.Collection; |
| 80 | + |
| 81 | + public override HttpRequest Request => _request; |
| 82 | + |
| 83 | + public override HttpResponse Response => _response; |
| 84 | + |
| 85 | + public override ConnectionInfo Connection => _connection ?? (_connection = new ReusableConnectionInfo(_features.Collection)); |
| 86 | + |
| 87 | + [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.")] |
| 88 | + public override AuthenticationManager Authentication => throw new NotSupportedException(); |
| 89 | + |
| 90 | + public override WebSocketManager WebSockets => _websockets ?? (_websockets = new ReusableWebSocketManager(_features.Collection)); |
| 91 | + |
| 92 | + |
| 93 | + public override ClaimsPrincipal User |
| 94 | + { |
| 95 | + get |
| 96 | + { |
| 97 | + var user = HttpAuthenticationFeature.User; |
| 98 | + if (user == null) |
| 99 | + { |
| 100 | + user = new ClaimsPrincipal(new ClaimsIdentity()); |
| 101 | + HttpAuthenticationFeature.User = user; |
| 102 | + } |
| 103 | + return user; |
| 104 | + } |
| 105 | + set { HttpAuthenticationFeature.User = value; } |
| 106 | + } |
| 107 | + |
| 108 | + public override IDictionary<object, object> Items |
| 109 | + { |
| 110 | + get { return ItemsFeature.Items; } |
| 111 | + set { ItemsFeature.Items = value; } |
| 112 | + } |
| 113 | + |
| 114 | + public override IServiceProvider RequestServices |
| 115 | + { |
| 116 | + get { return ServiceProvidersFeature.RequestServices; } |
| 117 | + set { ServiceProvidersFeature.RequestServices = value; } |
| 118 | + } |
| 119 | + |
| 120 | + public override CancellationToken RequestAborted |
| 121 | + { |
| 122 | + get { return LifetimeFeature.RequestAborted; } |
| 123 | + set { LifetimeFeature.RequestAborted = value; } |
| 124 | + } |
| 125 | + |
| 126 | + public override string TraceIdentifier |
| 127 | + { |
| 128 | + get { return RequestIdentifierFeature.TraceIdentifier; } |
| 129 | + set { RequestIdentifierFeature.TraceIdentifier = value; } |
| 130 | + } |
| 131 | + |
| 132 | + public override ISession Session |
| 133 | + { |
| 134 | + get |
| 135 | + { |
| 136 | + var feature = SessionFeatureOrNull; |
| 137 | + if (feature == null) |
| 138 | + { |
| 139 | + throw new InvalidOperationException("Session has not been configured for this application " + |
| 140 | + "or request."); |
| 141 | + } |
| 142 | + return feature.Session; |
| 143 | + } |
| 144 | + set |
| 145 | + { |
| 146 | + SessionFeature.Session = value; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public override void Abort() |
| 151 | + { |
| 152 | + LifetimeFeature.Abort(); |
| 153 | + } |
| 154 | + |
| 155 | + struct FeatureInterfaces |
| 156 | + { |
| 157 | + public IItemsFeature Items; |
| 158 | + public IServiceProvidersFeature ServiceProviders; |
| 159 | + public IHttpAuthenticationFeature Authentication; |
| 160 | + public IHttpRequestLifetimeFeature Lifetime; |
| 161 | + public ISessionFeature Session; |
| 162 | + public IHttpRequestIdentifierFeature RequestIdentifier; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments