Skip to content

Commit 752d99c

Browse files
gfoidlTratcher
authored andcommitted
IISHttpContext.FeatureCollection.Protocol recognizes HTTP/2 (#14412)
1 parent 0d412c3 commit 752d99c

File tree

4 files changed

+26
-45
lines changed

4 files changed

+26
-45
lines changed

src/Servers/HttpSys/src/FeatureContext.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,7 @@ string IHttpRequestFeature.Protocol
174174
{
175175
if (IsNotInitialized(Fields.Protocol))
176176
{
177-
var protocol = Request.ProtocolVersion;
178-
if (protocol == Constants.V2)
179-
{
180-
_httpProtocolVersion = "HTTP/2";
181-
}
182-
else if (protocol == Constants.V1_1)
183-
{
184-
_httpProtocolVersion = "HTTP/1.1";
185-
}
186-
else if (protocol == Constants.V1_0)
187-
{
188-
_httpProtocolVersion = "HTTP/1.0";
189-
}
190-
else
191-
{
192-
_httpProtocolVersion = "HTTP/" + protocol.ToString(2);
193-
}
177+
_httpProtocolVersion = Request.ProtocolVersion.GetHttpProtocolVersion();
194178
SetInitialized(Fields.Protocol);
195179
}
196180
return _httpProtocolVersion;

src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.AspNetCore.Http;
1616
using Microsoft.AspNetCore.Http.Features;
1717
using Microsoft.AspNetCore.Http.Features.Authentication;
18+
using Microsoft.AspNetCore.HttpSys.Internal;
1819
using Microsoft.AspNetCore.Server.IIS.Core.IO;
1920
using Microsoft.AspNetCore.WebUtilities;
2021
using Microsoft.Extensions.Logging;
@@ -37,7 +38,7 @@ internal partial class IISHttpContext : IFeatureCollection,
3738
// then the list of `implementedFeatures` in the generated code project MUST also be updated.
3839

3940
private int _featureRevision;
40-
private string _httpProtocolVersion = null;
41+
private string _httpProtocolVersion;
4142
private X509Certificate2 _certificate;
4243

4344
private List<KeyValuePair<Type, object>> MaybeExtra;
@@ -86,30 +87,8 @@ private void ExtraFeatureSet(Type key, object value)
8687

8788
string IHttpRequestFeature.Protocol
8889
{
89-
get
90-
{
91-
if (_httpProtocolVersion == null)
92-
{
93-
var protocol = HttpVersion;
94-
if (protocol.Major == 1 && protocol.Minor == 1)
95-
{
96-
_httpProtocolVersion = "HTTP/1.1";
97-
}
98-
else if (protocol.Major == 1 && protocol.Minor == 0)
99-
{
100-
_httpProtocolVersion = "HTTP/1.0";
101-
}
102-
else
103-
{
104-
_httpProtocolVersion = "HTTP/" + protocol.ToString(2);
105-
}
106-
}
107-
return _httpProtocolVersion;
108-
}
109-
set
110-
{
111-
_httpProtocolVersion = value;
112-
}
90+
get => _httpProtocolVersion ??= HttpVersion.GetHttpProtocolVersion();
91+
set => _httpProtocolVersion = value;
11392
}
11493

11594
string IHttpRequestFeature.Scheme

src/Shared/HttpSys/Constants.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal static class Constants
1515
internal const string SchemeDelimiter = "://";
1616
internal const string DefaultServerAddress = "http://localhost:5000";
1717

18-
internal static Version V1_0 = new Version(1, 0);
19-
internal static Version V1_1 = new Version(1, 1);
20-
internal static Version V2 = new Version(2, 0);
18+
internal static readonly Version V1_0 = new Version(1, 0);
19+
internal static readonly Version V1_1 = new Version(1, 1);
20+
internal static readonly Version V2 = new Version(2, 0);
2121
}
2222
}

src/Shared/HttpSys/Extensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNetCore.HttpSys.Internal
7+
{
8+
internal static class Extensions
9+
{
10+
public static string GetHttpProtocolVersion(this Version version) => version switch
11+
{
12+
{ Major: 2, Minor: 0 } => "HTTP/2",
13+
{ Major: 1, Minor: 1 } => "HTTP/1.1",
14+
{ Major: 1, Minor: 0 } => "HTTP/1.0",
15+
_ => "HTTP/" + version.ToString(2)
16+
};
17+
}
18+
}

0 commit comments

Comments
 (0)