Skip to content

Generalize Http2Cat #17438

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 14 commits into from
Dec 4, 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
17 changes: 16 additions & 1 deletion src/Servers/HttpSys/HttpSysServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.xml = version.xml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestClient", "samples\TestClient\TestClient.csproj", "{8B828433-B333-4C19-96AE-00BFFF9D8841}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestClient", "samples\TestClient\TestClient.csproj", "{8B828433-B333-4C19-96AE-00BFFF9D8841}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelfHostServer", "samples\SelfHostServer\SelfHostServer.csproj", "{1236F93A-AC5C-4A77-9477-C88F040151CA}"
EndProject
Expand Down Expand Up @@ -72,6 +72,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Connec
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueueSharing", "samples\QueueSharing\QueueSharing.csproj", "{9B58DF76-DC6D-4728-86B7-40087BDDC897}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets", "..\Kestrel\Transport.Sockets\src\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj", "{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -318,6 +320,18 @@ Global
{9B58DF76-DC6D-4728-86B7-40087BDDC897}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{9B58DF76-DC6D-4728-86B7-40087BDDC897}.Release|x86.ActiveCfg = Release|Any CPU
{9B58DF76-DC6D-4728-86B7-40087BDDC897}.Release|x86.Build.0 = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|x86.ActiveCfg = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Debug|x86.Build.0 = Debug|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|Any CPU.Build.0 = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|x86.ActiveCfg = Release|Any CPU
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -344,6 +358,7 @@ Global
{D93575B3-BFA3-4523-B060-D268D6A0A66B} = {4DA3C456-5050-4AC0-A554-795F6DEC8660}
{00A88B8D-D539-45DD-B071-1E955AF89A4A} = {4DA3C456-5050-4AC0-A554-795F6DEC8660}
{9B58DF76-DC6D-4728-86B7-40087BDDC897} = {3A1E31E3-2794-4CA3-B8E2-253E96BDE514}
{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB} = {4DA3C456-5050-4AC0-A554-795F6DEC8660}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {34B42B42-FA09-41AB-9216-14073990C504}
Expand Down
231 changes: 231 additions & 0 deletions src/Servers/HttpSys/test/FunctionalTests/Http2Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Http2Cat;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Xunit;

namespace Microsoft.AspNetCore.Server.HttpSys.FunctionalTests
{
public class Http2Tests
{
[ConditionalFact(Skip = "https://github.com/aspnet/AspNetCore/issues/17420")]
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
[MaximumOSVersion(OperatingSystems.Windows, "10.0.18362.9999", SkipReason = "This is last version without GoAway support")]
public async Task ConnectionClose_NoOSSupport_NoGoAway()
{
using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
{
httpContext.Response.Headers[HeaderNames.Connection] = "close";
return Task.FromResult(0);
});

await new HostBuilder()
.UseHttp2Cat(address, async h2Connection =>
{
await h2Connection.InitializeConnectionAsync();

h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");

await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);

var headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_HEADERS) != 0);
Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_STREAM) != 0);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

var decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);

// Send and receive a second request to ensure there is no GoAway frame on the wire yet.

await h2Connection.StartStreamAsync(3, Http2Utilities.BrowserRequestHeaders, endStream: true);

headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_HEADERS) != 0);
Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_STREAM) != 0);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

h2Connection.ResetHeaders();
decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);

await h2Connection.StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);

h2Connection.Logger.LogInformation("Connection stopped.");
})
.Build().RunAsync();
}

[ConditionalFact]
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")]
public async Task ConnectionClose_OSSupport_SendsGoAway()
{
using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
{
httpContext.Response.Headers[HeaderNames.Connection] = "close";
return Task.FromResult(0);
});

await new HostBuilder()
.UseHttp2Cat(address, async h2Connection =>
{
await h2Connection.InitializeConnectionAsync();

h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");

await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);

var goAwayFrame = await h2Connection.ReceiveFrameAsync();
h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR);

var headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

var decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);

var dataFrame = await h2Connection.ReceiveFrameAsync();
Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags);
Assert.Equal(0, dataFrame.PayloadLength);

// Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams.

h2Connection.Logger.LogInformation("Connection stopped.");
})
.Build().RunAsync();
}

[ConditionalFact]
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")]
public async Task ConnectionClose_AdditionalRequests_ReceivesSecondGoAway()
{
using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
{
httpContext.Response.Headers[HeaderNames.Connection] = "close";
return Task.FromResult(0);
});

await new HostBuilder()
.UseHttp2Cat(address, async h2Connection =>
{
await h2Connection.InitializeConnectionAsync();

h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");

var streamId = 1;
await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);

var goAwayFrame = await h2Connection.ReceiveFrameAsync();
h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR);

var headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags);
Assert.Equal(streamId, headersFrame.StreamId);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

var decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
h2Connection.ResetHeaders();

var dataFrame = await h2Connection.ReceiveFrameAsync();
Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags);
Assert.Equal(0, dataFrame.PayloadLength);
Assert.Equal(streamId, dataFrame.StreamId);

// Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams.

for (var i = 1; i < 200; i++)
{
streamId = 1 + (i * 2); // Odds.
await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);

headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags);
Assert.Equal(streamId, headersFrame.StreamId);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
h2Connection.ResetHeaders();

dataFrame = await h2Connection.ReceiveFrameAsync();
Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags);
Assert.Equal(0, dataFrame.PayloadLength);
Assert.Equal(streamId, dataFrame.StreamId);
}

streamId = 1 + (200 * 2); // Odds.
await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);

// Final GoAway
goAwayFrame = await h2Connection.ReceiveFrameAsync();
h2Connection.VerifyGoAway(goAwayFrame, streamId, Http2ErrorCode.NO_ERROR);

// Normal response
headersFrame = await h2Connection.ReceiveFrameAsync();

Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type);
Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags);
Assert.Equal(streamId, headersFrame.StreamId);

h2Connection.Logger.LogInformation("Received headers in a single frame.");

decodedHeaders = h2Connection.DecodeHeaders(headersFrame);

// HTTP/2 filters out the connection header
Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
h2Connection.ResetHeaders();

dataFrame = await h2Connection.ReceiveFrameAsync();
Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags);
Assert.Equal(0, dataFrame.PayloadLength);
Assert.Equal(streamId, dataFrame.StreamId);

h2Connection.Logger.LogInformation("Connection stopped.");
})
.Build().RunAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<TestGroupName>HttpSys.FunctionalTests</TestGroupName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SharedSourceRoot)Http2\**\*.cs" LinkBase="Shared\Http2\" />
<Compile Include="$(SharedSourceRoot)Http2cat\**\*.cs" LinkBase="Shared\Http2cat" />
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\**\*.cs" LinkBase="Shared\" />
<Compile Include="$(SharedSourceRoot)TaskToApm.cs" Link="Shared\TaskToApm.cs" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Server.HttpSys" />
<Reference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" />
<Reference Include="Microsoft.Extensions.Hosting" />
<Reference Include="System.Net.Http.WinHttpHandler" />
</ItemGroup>

Expand All @@ -22,4 +31,15 @@
<HelixPreCommand Include="call RunPowershell.cmd UpdateIISExpressCertificate.ps1 || exit /b 1" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="$(SharedSourceRoot)ServerInfrastructure\SharedStrings.resx" Link="Shared\SharedStrings.resx">
<ManifestResourceName>Microsoft.AspNetCore.Server.SharedStrings</ManifestResourceName>
<Generator></Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(SharedSourceRoot)Http2\SR.resx" Link="Shared\Http2\SR.resx">
<ManifestResourceName>System.Net.Http.SR</ManifestResourceName>
<Generator></Generator>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<Compile Include="$(SharedSourceRoot)CertificateGeneration\**\*.cs" />
<Compile Include="$(SharedSourceRoot)ValueTaskExtensions\**\*.cs" />
<Compile Include="$(SharedSourceRoot)UrlDecoder\**\*.cs" />
<Compile Include="$(SharedSourceRoot)Http2\**\*.cs" Link="Shared\Http2\%(Filename)%(Extension)" />
<Compile Include="$(SharedSourceRoot)Http2\**\*.cs" LinkBase="Shared\Http2\" />
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\**\*.cs" LinkBase="Shared\" />
<Compile Include="$(RepoRoot)src\Shared\TaskToApm.cs" Link="Internal\TaskToApm.cs" />
</ItemGroup>

Expand All @@ -35,6 +36,10 @@
<EmbeddedResource Update="CoreStrings.resx">
<Generator></Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(SharedSourceRoot)ServerInfrastructure\SharedStrings.resx" Link="Shared\SharedStrings.resx">
<ManifestResourceName>Microsoft.AspNetCore.Server.SharedStrings</ManifestResourceName>
<Generator></Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(SharedSourceRoot)Http2\SR.resx" Link="Shared\Http2\SR.resx">
<ManifestResourceName>System.Net.Http.SR</ManifestResourceName>
<Generator></Generator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,5 @@ private static X509Certificate2 ConvertToX509Certificate2(X509Certificate certif

return new X509Certificate2(certificate);
}

private class SslDuplexPipe : DuplexPipeStreamAdapter<SslStream>
{
public SslDuplexPipe(IDuplexPipe transport, StreamPipeReaderOptions readerOptions, StreamPipeWriterOptions writerOptions)
: this(transport, readerOptions, writerOptions, s => new SslStream(s))
{

}

public SslDuplexPipe(IDuplexPipe transport, StreamPipeReaderOptions readerOptions, StreamPipeWriterOptions writerOptions, Func<Stream, SslStream> factory) :
base(transport, readerOptions, writerOptions, factory)
{
}
}
}
}
1 change: 0 additions & 1 deletion src/Servers/Kestrel/Core/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Performance, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo("http2cat, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
Loading