async Task <Stream> IHttpUpgradeFeature.UpgradeAsync()
        {
            if (!((IHttpUpgradeFeature)this).IsUpgradableRequest)
            {
                throw new InvalidOperationException(CoreStrings.CannotUpgradeNonUpgradableRequest);
            }

            if (IsUpgraded)
            {
                throw new InvalidOperationException(CoreStrings.UpgradeCannotBeCalledMultipleTimes);
            }

            if (!ServiceContext.ConnectionManager.UpgradedConnectionCount.TryLockOne())
            {
                throw new InvalidOperationException(CoreStrings.UpgradedConnectionLimitReached);
            }

            IsUpgraded = true;

            ConnectionFeatures.Get <IDecrementConcurrentConnectionCountFeature>()?.ReleaseConnection();

            StatusCode   = StatusCodes.Status101SwitchingProtocols;
            ReasonPhrase = "Switching Protocols";
            ResponseHeaders["Connection"] = "Upgrade";

            await FlushAsync(default(CancellationToken));

            return(_streams.Upgrade());
        }
Ejemplo n.º 2
0
        TFeature?IFeatureCollection.Get <TFeature>() where TFeature : default
        {
            // Using Unsafe.As for the cast due to https://github.com/dotnet/runtime/issues/49614
            // The type of TFeature is confirmed by the typeof() check and the As cast only accepts
            // that type; however the Jit does not eliminate a regular cast in a shared generic.

            TFeature?feature = default;

            if (typeof(TFeature) == typeof(IHttpRequestFeature))
            {
                feature = Unsafe.As <IHttpRequestFeature?, TFeature?>(ref _currentIHttpRequestFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpResponseFeature))
            {
                feature = Unsafe.As <IHttpResponseFeature?, TFeature?>(ref _currentIHttpResponseFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpResponseBodyFeature))
            {
                feature = Unsafe.As <IHttpResponseBodyFeature?, TFeature?>(ref _currentIHttpResponseBodyFeature);
            }
            else if (typeof(TFeature) == typeof(IRouteValuesFeature))
            {
                feature = Unsafe.As <IRouteValuesFeature?, TFeature?>(ref _currentIRouteValuesFeature);
            }
            else if (typeof(TFeature) == typeof(IEndpointFeature))
            {
                feature = Unsafe.As <IEndpointFeature?, TFeature?>(ref _currentIEndpointFeature);
            }
            else if (typeof(TFeature) == typeof(IServiceProvidersFeature))
            {
                feature = Unsafe.As <IServiceProvidersFeature?, TFeature?>(ref _currentIServiceProvidersFeature);
            }
            else if (typeof(TFeature) == typeof(IItemsFeature))
            {
                feature = Unsafe.As <IItemsFeature?, TFeature?>(ref _currentIItemsFeature);
            }
            else if (typeof(TFeature) == typeof(IQueryFeature))
            {
                feature = Unsafe.As <IQueryFeature?, TFeature?>(ref _currentIQueryFeature);
            }
            else if (typeof(TFeature) == typeof(IRequestBodyPipeFeature))
            {
                feature = Unsafe.As <IRequestBodyPipeFeature?, TFeature?>(ref _currentIRequestBodyPipeFeature);
            }
            else if (typeof(TFeature) == typeof(IFormFeature))
            {
                feature = Unsafe.As <IFormFeature?, TFeature?>(ref _currentIFormFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpAuthenticationFeature))
            {
                feature = Unsafe.As <IHttpAuthenticationFeature?, TFeature?>(ref _currentIHttpAuthenticationFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpRequestIdentifierFeature))
            {
                feature = Unsafe.As <IHttpRequestIdentifierFeature?, TFeature?>(ref _currentIHttpRequestIdentifierFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpConnectionFeature))
            {
                feature = Unsafe.As <IHttpConnectionFeature?, TFeature?>(ref _currentIHttpConnectionFeature);
            }
            else if (typeof(TFeature) == typeof(ISessionFeature))
            {
                feature = Unsafe.As <ISessionFeature?, TFeature?>(ref _currentISessionFeature);
            }
            else if (typeof(TFeature) == typeof(IResponseCookiesFeature))
            {
                feature = Unsafe.As <IResponseCookiesFeature?, TFeature?>(ref _currentIResponseCookiesFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpRequestTrailersFeature))
            {
                feature = Unsafe.As <IHttpRequestTrailersFeature?, TFeature?>(ref _currentIHttpRequestTrailersFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpResponseTrailersFeature))
            {
                feature = Unsafe.As <IHttpResponseTrailersFeature?, TFeature?>(ref _currentIHttpResponseTrailersFeature);
            }
            else if (typeof(TFeature) == typeof(ITlsConnectionFeature))
            {
                feature = Unsafe.As <ITlsConnectionFeature?, TFeature?>(ref _currentITlsConnectionFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpUpgradeFeature))
            {
                feature = Unsafe.As <IHttpUpgradeFeature?, TFeature?>(ref _currentIHttpUpgradeFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpWebSocketFeature))
            {
                feature = Unsafe.As <IHttpWebSocketFeature?, TFeature?>(ref _currentIHttpWebSocketFeature);
            }
            else if (typeof(TFeature) == typeof(IHttp2StreamIdFeature))
            {
                feature = Unsafe.As <IHttp2StreamIdFeature?, TFeature?>(ref _currentIHttp2StreamIdFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpRequestLifetimeFeature))
            {
                feature = Unsafe.As <IHttpRequestLifetimeFeature?, TFeature?>(ref _currentIHttpRequestLifetimeFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpMaxRequestBodySizeFeature))
            {
                feature = Unsafe.As <IHttpMaxRequestBodySizeFeature?, TFeature?>(ref _currentIHttpMaxRequestBodySizeFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpMinRequestBodyDataRateFeature))
            {
                feature = Unsafe.As <IHttpMinRequestBodyDataRateFeature?, TFeature?>(ref _currentIHttpMinRequestBodyDataRateFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpMinResponseDataRateFeature))
            {
                feature = Unsafe.As <IHttpMinResponseDataRateFeature?, TFeature?>(ref _currentIHttpMinResponseDataRateFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpBodyControlFeature))
            {
                feature = Unsafe.As <IHttpBodyControlFeature?, TFeature?>(ref _currentIHttpBodyControlFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpRequestBodyDetectionFeature))
            {
                feature = Unsafe.As <IHttpRequestBodyDetectionFeature?, TFeature?>(ref _currentIHttpRequestBodyDetectionFeature);
            }
            else if (typeof(TFeature) == typeof(IHttpResetFeature))
            {
                feature = Unsafe.As <IHttpResetFeature?, TFeature?>(ref _currentIHttpResetFeature);
            }
            else if (MaybeExtra != null)
            {
                feature = (TFeature?)(ExtraFeatureGet(typeof(TFeature)));
            }

            if (feature == null)
            {
                feature = ConnectionFeatures.Get <TFeature>();
            }

            return(feature);
        }
        TFeature IFeatureCollection.Get <TFeature>()
        {
            TFeature feature = default;

            if (typeof(TFeature) == typeof(IHttpRequestFeature))
            {
                feature = (TFeature)_currentIHttpRequestFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpResponseFeature))
            {
                feature = (TFeature)_currentIHttpResponseFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpRequestIdentifierFeature))
            {
                feature = (TFeature)_currentIHttpRequestIdentifierFeature;
            }
            else if (typeof(TFeature) == typeof(IServiceProvidersFeature))
            {
                feature = (TFeature)_currentIServiceProvidersFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpRequestLifetimeFeature))
            {
                feature = (TFeature)_currentIHttpRequestLifetimeFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpConnectionFeature))
            {
                feature = (TFeature)_currentIHttpConnectionFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpAuthenticationFeature))
            {
                feature = (TFeature)_currentIHttpAuthenticationFeature;
            }
            else if (typeof(TFeature) == typeof(IQueryFeature))
            {
                feature = (TFeature)_currentIQueryFeature;
            }
            else if (typeof(TFeature) == typeof(IFormFeature))
            {
                feature = (TFeature)_currentIFormFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpUpgradeFeature))
            {
                feature = (TFeature)_currentIHttpUpgradeFeature;
            }
            else if (typeof(TFeature) == typeof(IHttp2StreamIdFeature))
            {
                feature = (TFeature)_currentIHttp2StreamIdFeature;
            }
            else if (typeof(TFeature) == typeof(IResponseCookiesFeature))
            {
                feature = (TFeature)_currentIResponseCookiesFeature;
            }
            else if (typeof(TFeature) == typeof(IItemsFeature))
            {
                feature = (TFeature)_currentIItemsFeature;
            }
            else if (typeof(TFeature) == typeof(ITlsConnectionFeature))
            {
                feature = (TFeature)_currentITlsConnectionFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpWebSocketFeature))
            {
                feature = (TFeature)_currentIHttpWebSocketFeature;
            }
            else if (typeof(TFeature) == typeof(ISessionFeature))
            {
                feature = (TFeature)_currentISessionFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpMaxRequestBodySizeFeature))
            {
                feature = (TFeature)_currentIHttpMaxRequestBodySizeFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpMinRequestBodyDataRateFeature))
            {
                feature = (TFeature)_currentIHttpMinRequestBodyDataRateFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpMinResponseDataRateFeature))
            {
                feature = (TFeature)_currentIHttpMinResponseDataRateFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpBodyControlFeature))
            {
                feature = (TFeature)_currentIHttpBodyControlFeature;
            }
            else if (typeof(TFeature) == typeof(IHttpSendFileFeature))
            {
                feature = (TFeature)_currentIHttpSendFileFeature;
            }
            else if (MaybeExtra != null)
            {
                feature = (TFeature)(ExtraFeatureGet(typeof(TFeature)));
            }

            if (feature == null)
            {
                feature = ConnectionFeatures.Get <TFeature>();
            }

            return(feature);
        }