|
| 1 | +// Copyright (c) .NET Foundation. |
| 2 | +// All Rights Reserved |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR |
| 11 | +// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING |
| 12 | +// WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF |
| 13 | +// TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR |
| 14 | +// NON-INFRINGEMENT. |
| 15 | +// See the Apache 2 License for the specific language governing |
| 16 | +// permissions and limitations under the License. |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Collections; |
| 20 | +using System.Collections.Generic; |
| 21 | +using Microsoft.AspNet.Http.Features; |
| 22 | +using Microsoft.AspNet.Http.Features.Authentication; |
| 23 | +using Microsoft.Net.Http.Server; |
| 24 | + |
| 25 | +namespace Microsoft.AspNet.Server.WebListener |
| 26 | +{ |
| 27 | + internal sealed class StandardFeatureCollection : IFeatureCollection |
| 28 | + { |
| 29 | + private static readonly Func<FeatureContext, object> _identityFunc = ReturnIdentity; |
| 30 | + private static readonly Dictionary<Type, Func<FeatureContext, object>> _featureFuncLookup = new Dictionary<Type, Func<FeatureContext, object>>() |
| 31 | + { |
| 32 | + { typeof(IHttpRequestFeature), _identityFunc }, |
| 33 | + { typeof(IHttpConnectionFeature), _identityFunc }, |
| 34 | + { typeof(IHttpResponseFeature), _identityFunc }, |
| 35 | + { typeof(IHttpSendFileFeature), _identityFunc }, |
| 36 | + { typeof(ITlsConnectionFeature), ctx => ctx.GetTlsConnectionFeature() }, |
| 37 | + { typeof(ITlsTokenBindingFeature), ctx => ctx.GetTlsTokenBindingFeature() }, |
| 38 | + { typeof(IHttpBufferingFeature), _identityFunc }, |
| 39 | + { typeof(IHttpRequestLifetimeFeature), _identityFunc }, |
| 40 | + { typeof(IHttpUpgradeFeature), _identityFunc }, |
| 41 | + { typeof(IHttpWebSocketFeature), _identityFunc }, |
| 42 | + { typeof(IHttpAuthenticationFeature), _identityFunc }, |
| 43 | + { typeof(IHttpRequestIdentifierFeature), _identityFunc }, |
| 44 | + { typeof(RequestContext), ctx => ctx.RequestContext }, |
| 45 | + }; |
| 46 | + |
| 47 | + private readonly FeatureContext _featureContext; |
| 48 | + |
| 49 | + public StandardFeatureCollection(FeatureContext featureContext) |
| 50 | + { |
| 51 | + _featureContext = featureContext; |
| 52 | + } |
| 53 | + |
| 54 | + public bool IsReadOnly |
| 55 | + { |
| 56 | + get { return true; } |
| 57 | + } |
| 58 | + |
| 59 | + public int Revision |
| 60 | + { |
| 61 | + get { return 0; } |
| 62 | + } |
| 63 | + |
| 64 | + public object this[Type key] |
| 65 | + { |
| 66 | + get |
| 67 | + { |
| 68 | + Func<FeatureContext, object> lookupFunc; |
| 69 | + _featureFuncLookup.TryGetValue(key, out lookupFunc); |
| 70 | + return lookupFunc?.Invoke(_featureContext); |
| 71 | + } |
| 72 | + set |
| 73 | + { |
| 74 | + throw new InvalidOperationException("The collection is read-only"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static object ReturnIdentity(FeatureContext featureContext) |
| 79 | + { |
| 80 | + return featureContext; |
| 81 | + } |
| 82 | + |
| 83 | + IEnumerator IEnumerable.GetEnumerator() |
| 84 | + { |
| 85 | + return ((IEnumerable<KeyValuePair<Type, object>>)this).GetEnumerator(); |
| 86 | + } |
| 87 | + |
| 88 | + IEnumerator<KeyValuePair<Type, object>> IEnumerable<KeyValuePair<Type, object>>.GetEnumerator() |
| 89 | + { |
| 90 | + foreach (var featureFunc in _featureFuncLookup) |
| 91 | + { |
| 92 | + var feature = featureFunc.Value(_featureContext); |
| 93 | + if (feature != null) |
| 94 | + { |
| 95 | + yield return new KeyValuePair<Type, object>(featureFunc.Key, feature); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + void IDisposable.Dispose() |
| 101 | + { |
| 102 | + // nothing to dispose of |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments