Skip to content

IISHttpContext.FeatureCollection.Protocol recognizes HTTP/2 #14412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/Servers/HttpSys/src/FeatureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,7 @@ string IHttpRequestFeature.Protocol
{
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;
Expand Down
29 changes: 4 additions & 25 deletions src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -37,7 +38,7 @@ internal partial class IISHttpContext : IFeatureCollection,
// 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<KeyValuePair<Type, object>> MaybeExtra;
Expand Down Expand Up @@ -86,30 +87,8 @@ private void ExtraFeatureSet(Type key, object value)

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
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/HttpSys/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal static class Constants
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);
}
}
18 changes: 18 additions & 0 deletions src/Shared/HttpSys/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh, fancy new C# 8.0 features 😜

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 it's a better showcase than the one from the docs 😃

{
{ Major: 2, Minor: 0 } => "HTTP/2",
{ Major: 1, Minor: 1 } => "HTTP/1.1",
{ Major: 1, Minor: 0 } => "HTTP/1.0",
_ => "HTTP/" + version.ToString(2)
};
}
}