Change ApplicationProtocol to ReadOnlyMemory<byte> #2182

This commit is contained in:
Chris Ross (ASP.NET) 2017-12-29 12:33:34 -08:00
parent 1fa001e7db
commit 2d3a01d48d
7 changed files with 15 additions and 22 deletions

View File

@ -15,7 +15,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="../../test/shared/TestCertificates/testCert.pfx" CopyToOutputDirectory="PreserveNewest" /> <Content Include="testCert.pfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
</Project> </Project>

Binary file not shown.

View File

@ -1,11 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Features namespace Microsoft.AspNetCore.Server.Kestrel.Core.Features
{ {
// TODO: this should be merged with ITlsConnectionFeature
public interface ITlsApplicationProtocolFeature public interface ITlsApplicationProtocolFeature
{ {
string ApplicationProtocol { get; } ReadOnlyMemory<byte> ApplicationProtocol { get; }
} }
} }

View File

@ -23,6 +23,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
{ {
public class HttpConnection : ITimeoutControl, IConnectionTimeoutFeature, IRequestProcessor public class HttpConnection : ITimeoutControl, IConnectionTimeoutFeature, IRequestProcessor
{ {
private static readonly ReadOnlyMemory<byte> Http2Id = new ReadOnlyMemory<byte>(new[] { (byte)'h', (byte)'2' });
private readonly HttpConnectionContext _context; private readonly HttpConnectionContext _context;
private readonly TaskCompletionSource<object> _socketClosedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); private readonly TaskCompletionSource<object> _socketClosedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
@ -303,7 +305,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
private HttpProtocols SelectProtocol() private HttpProtocols SelectProtocol()
{ {
var hasTls = _context.ConnectionFeatures.Get<ITlsConnectionFeature>() != null; var hasTls = _context.ConnectionFeatures.Get<ITlsConnectionFeature>() != null;
var applicationProtocol = _context.ConnectionFeatures.Get<ITlsApplicationProtocolFeature>()?.ApplicationProtocol; var applicationProtocol = _context.ConnectionFeatures.Get<ITlsApplicationProtocolFeature>()?.ApplicationProtocol
?? new ReadOnlyMemory<byte>();
var http1Enabled = (_context.Protocols & HttpProtocols.Http1) == HttpProtocols.Http1; var http1Enabled = (_context.Protocols & HttpProtocols.Http1) == HttpProtocols.Http1;
var http2Enabled = (_context.Protocols & HttpProtocols.Http2) == HttpProtocols.Http2; var http2Enabled = (_context.Protocols & HttpProtocols.Http2) == HttpProtocols.Http2;
@ -319,7 +322,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
error = CoreStrings.EndPointRequiresTlsForHttp1AndHttp2; error = CoreStrings.EndPointRequiresTlsForHttp1AndHttp2;
} }
if (!http1Enabled && http2Enabled && hasTls && applicationProtocol != "h2") if (!http1Enabled && http2Enabled && hasTls && !Http2Id.SequenceEqual(applicationProtocol))
{ {
error = CoreStrings.EndPointHttp2NotNegotiated; error = CoreStrings.EndPointHttp2NotNegotiated;
} }
@ -330,7 +333,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
return HttpProtocols.None; return HttpProtocols.None;
} }
return http2Enabled && (!hasTls || applicationProtocol == "h2") ? HttpProtocols.Http2 : HttpProtocols.Http1; return http2Enabled && (!hasTls || Http2Id.SequenceEqual(applicationProtocol)) ? HttpProtocols.Http2 : HttpProtocols.Http1;
} }
public void Tick(DateTimeOffset now) public void Tick(DateTimeOffset now)

View File

@ -160,20 +160,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
} }
#if NETCOREAPP2_1 #if NETCOREAPP2_1
// Don't allocate in the common case, see https://github.com/dotnet/corefx/issues/25432 feature.ApplicationProtocol = sslStream.NegotiatedApplicationProtocol.Protocol;
if (sslStream.NegotiatedApplicationProtocol == SslApplicationProtocol.Http11)
{
feature.ApplicationProtocol = "http/1.1";
}
else if (sslStream.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2)
{
feature.ApplicationProtocol = "h2";
}
else
{
feature.ApplicationProtocol = sslStream.NegotiatedApplicationProtocol.ToString();
}
context.Features.Set<ITlsApplicationProtocolFeature>(feature); context.Features.Set<ITlsApplicationProtocolFeature>(feature);
#endif #endif
feature.ClientCertificate = ConvertToX509Certificate2(sslStream.RemoteCertificate); feature.ClientCertificate = ConvertToX509Certificate2(sslStream.RemoteCertificate);

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
{ {
public X509Certificate2 ClientCertificate { get; set; } public X509Certificate2 ClientCertificate { get; set; }
public string ApplicationProtocol { get; set; } public ReadOnlyMemory<byte> ApplicationProtocol { get; set; }
public Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken) public Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken)
{ {

View File

@ -4,7 +4,7 @@
<AssemblyName>Microsoft.AspNetCore.Server.Kestrel.Core</AssemblyName> <AssemblyName>Microsoft.AspNetCore.Server.Kestrel.Core</AssemblyName>
<RootNamespace>Microsoft.AspNetCore.Server.Kestrel.Core</RootNamespace> <RootNamespace>Microsoft.AspNetCore.Server.Kestrel.Core</RootNamespace>
<Description>Core components of ASP.NET Core Kestrel cross-platform web server.</Description> <Description>Core components of ASP.NET Core Kestrel cross-platform web server.</Description>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;kestrel</PackageTags> <PackageTags>aspnetcore;kestrel</PackageTags>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>