diff --git a/src/Servers/HttpSys/src/FeatureContext.cs b/src/Servers/HttpSys/src/FeatureContext.cs index a689230ab8..d2e2626874 100644 --- a/src/Servers/HttpSys/src/FeatureContext.cs +++ b/src/Servers/HttpSys/src/FeatureContext.cs @@ -174,23 +174,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { if (IsNotInitialized(Fields.Protocol)) { - var protocol = Request.ProtocolVersion; - if (protocol == Constants.V2) - { - _httpProtocolVersion = "HTTP/2"; - } - else if (protocol == Constants.V1_1) - { - _httpProtocolVersion = "HTTP/1.1"; - } - else if (protocol == Constants.V1_0) - { - _httpProtocolVersion = "HTTP/1.0"; - } - else - { - _httpProtocolVersion = "HTTP/" + protocol.ToString(2); - } + _httpProtocolVersion = Request.ProtocolVersion.GetHttpProtocolVersion(); SetInitialized(Fields.Protocol); } return _httpProtocolVersion; diff --git a/src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs b/src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs index cab956dd0a..6635385e7f 100644 --- a/src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs +++ b/src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs @@ -15,6 +15,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features.Authentication; +using Microsoft.AspNetCore.HttpSys.Internal; using Microsoft.AspNetCore.Server.IIS.Core.IO; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; @@ -37,7 +38,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core // then the list of `implementedFeatures` in the generated code project MUST also be updated. private int _featureRevision; - private string _httpProtocolVersion = null; + private string _httpProtocolVersion; private X509Certificate2 _certificate; private List> MaybeExtra; @@ -86,30 +87,8 @@ namespace Microsoft.AspNetCore.Server.IIS.Core string IHttpRequestFeature.Protocol { - get - { - if (_httpProtocolVersion == null) - { - var protocol = HttpVersion; - if (protocol.Major == 1 && protocol.Minor == 1) - { - _httpProtocolVersion = "HTTP/1.1"; - } - else if (protocol.Major == 1 && protocol.Minor == 0) - { - _httpProtocolVersion = "HTTP/1.0"; - } - else - { - _httpProtocolVersion = "HTTP/" + protocol.ToString(2); - } - } - return _httpProtocolVersion; - } - set - { - _httpProtocolVersion = value; - } + get => _httpProtocolVersion ??= HttpVersion.GetHttpProtocolVersion(); + set => _httpProtocolVersion = value; } string IHttpRequestFeature.Scheme diff --git a/src/Shared/HttpSys/Constants.cs b/src/Shared/HttpSys/Constants.cs index 6f861c239f..4d0576c477 100644 --- a/src/Shared/HttpSys/Constants.cs +++ b/src/Shared/HttpSys/Constants.cs @@ -15,8 +15,8 @@ namespace Microsoft.AspNetCore.HttpSys.Internal internal const string SchemeDelimiter = "://"; internal const string DefaultServerAddress = "http://localhost:5000"; - internal static Version V1_0 = new Version(1, 0); - internal static Version V1_1 = new Version(1, 1); - internal static Version V2 = new Version(2, 0); + internal static readonly Version V1_0 = new Version(1, 0); + internal static readonly Version V1_1 = new Version(1, 1); + internal static readonly Version V2 = new Version(2, 0); } } diff --git a/src/Shared/HttpSys/Extensions.cs b/src/Shared/HttpSys/Extensions.cs new file mode 100644 index 0000000000..c813991357 --- /dev/null +++ b/src/Shared/HttpSys/Extensions.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNetCore.HttpSys.Internal +{ + internal static class Extensions + { + public static string GetHttpProtocolVersion(this Version version) => version switch + { + { Major: 2, Minor: 0 } => "HTTP/2", + { Major: 1, Minor: 1 } => "HTTP/1.1", + { Major: 1, Minor: 0 } => "HTTP/1.0", + _ => "HTTP/" + version.ToString(2) + }; + } +}