|
| 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.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Http2Cat; |
| 6 | +using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2; |
| 7 | +using Microsoft.AspNetCore.Testing; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Net.Http.Headers; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.Server.HttpSys.FunctionalTests |
| 14 | +{ |
| 15 | + public class Http2Tests |
| 16 | + { |
| 17 | + [ConditionalFact(Skip = "https://github.com/aspnet/AspNetCore/issues/17420")] |
| 18 | + [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")] |
| 19 | + [MaximumOSVersion(OperatingSystems.Windows, "10.0.18362.9999", SkipReason = "This is last version without GoAway support")] |
| 20 | + public async Task ConnectionClose_NoOSSupport_NoGoAway() |
| 21 | + { |
| 22 | + using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext => |
| 23 | + { |
| 24 | + httpContext.Response.Headers[HeaderNames.Connection] = "close"; |
| 25 | + return Task.FromResult(0); |
| 26 | + }); |
| 27 | + |
| 28 | + await new HostBuilder() |
| 29 | + .UseHttp2Cat(address, async h2Connection => |
| 30 | + { |
| 31 | + await h2Connection.InitializeConnectionAsync(); |
| 32 | + |
| 33 | + h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1."); |
| 34 | + |
| 35 | + await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 36 | + |
| 37 | + var headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 38 | + |
| 39 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 40 | + Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_HEADERS) != 0); |
| 41 | + Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_STREAM) != 0); |
| 42 | + |
| 43 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 44 | + |
| 45 | + var decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 46 | + |
| 47 | + // HTTP/2 filters out the connection header |
| 48 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 49 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 50 | + |
| 51 | + // Send and receive a second request to ensure there is no GoAway frame on the wire yet. |
| 52 | + |
| 53 | + await h2Connection.StartStreamAsync(3, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 54 | + |
| 55 | + headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 56 | + |
| 57 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 58 | + Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_HEADERS) != 0); |
| 59 | + Assert.True((headersFrame.Flags & (byte)Http2HeadersFrameFlags.END_STREAM) != 0); |
| 60 | + |
| 61 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 62 | + |
| 63 | + h2Connection.ResetHeaders(); |
| 64 | + decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 65 | + |
| 66 | + // HTTP/2 filters out the connection header |
| 67 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 68 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 69 | + |
| 70 | + await h2Connection.StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false); |
| 71 | + |
| 72 | + h2Connection.Logger.LogInformation("Connection stopped."); |
| 73 | + }) |
| 74 | + .Build().RunAsync(); |
| 75 | + } |
| 76 | + |
| 77 | + [ConditionalFact] |
| 78 | + [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")] |
| 79 | + public async Task ConnectionClose_OSSupport_SendsGoAway() |
| 80 | + { |
| 81 | + using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext => |
| 82 | + { |
| 83 | + httpContext.Response.Headers[HeaderNames.Connection] = "close"; |
| 84 | + return Task.FromResult(0); |
| 85 | + }); |
| 86 | + |
| 87 | + await new HostBuilder() |
| 88 | + .UseHttp2Cat(address, async h2Connection => |
| 89 | + { |
| 90 | + await h2Connection.InitializeConnectionAsync(); |
| 91 | + |
| 92 | + h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1."); |
| 93 | + |
| 94 | + await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 95 | + |
| 96 | + var goAwayFrame = await h2Connection.ReceiveFrameAsync(); |
| 97 | + h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR); |
| 98 | + |
| 99 | + var headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 100 | + |
| 101 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 102 | + Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags); |
| 103 | + |
| 104 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 105 | + |
| 106 | + var decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 107 | + |
| 108 | + // HTTP/2 filters out the connection header |
| 109 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 110 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 111 | + |
| 112 | + var dataFrame = await h2Connection.ReceiveFrameAsync(); |
| 113 | + Assert.Equal(Http2FrameType.DATA, dataFrame.Type); |
| 114 | + Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags); |
| 115 | + Assert.Equal(0, dataFrame.PayloadLength); |
| 116 | + |
| 117 | + // Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams. |
| 118 | + |
| 119 | + h2Connection.Logger.LogInformation("Connection stopped."); |
| 120 | + }) |
| 121 | + .Build().RunAsync(); |
| 122 | + } |
| 123 | + |
| 124 | + [ConditionalFact] |
| 125 | + [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")] |
| 126 | + public async Task ConnectionClose_AdditionalRequests_ReceivesSecondGoAway() |
| 127 | + { |
| 128 | + using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext => |
| 129 | + { |
| 130 | + httpContext.Response.Headers[HeaderNames.Connection] = "close"; |
| 131 | + return Task.FromResult(0); |
| 132 | + }); |
| 133 | + |
| 134 | + await new HostBuilder() |
| 135 | + .UseHttp2Cat(address, async h2Connection => |
| 136 | + { |
| 137 | + await h2Connection.InitializeConnectionAsync(); |
| 138 | + |
| 139 | + h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1."); |
| 140 | + |
| 141 | + var streamId = 1; |
| 142 | + await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 143 | + |
| 144 | + var goAwayFrame = await h2Connection.ReceiveFrameAsync(); |
| 145 | + h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR); |
| 146 | + |
| 147 | + var headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 148 | + |
| 149 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 150 | + Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags); |
| 151 | + Assert.Equal(streamId, headersFrame.StreamId); |
| 152 | + |
| 153 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 154 | + |
| 155 | + var decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 156 | + |
| 157 | + // HTTP/2 filters out the connection header |
| 158 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 159 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 160 | + h2Connection.ResetHeaders(); |
| 161 | + |
| 162 | + var dataFrame = await h2Connection.ReceiveFrameAsync(); |
| 163 | + Assert.Equal(Http2FrameType.DATA, dataFrame.Type); |
| 164 | + Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags); |
| 165 | + Assert.Equal(0, dataFrame.PayloadLength); |
| 166 | + Assert.Equal(streamId, dataFrame.StreamId); |
| 167 | + |
| 168 | + // Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams. |
| 169 | + |
| 170 | + for (var i = 1; i < 200; i++) |
| 171 | + { |
| 172 | + streamId = 1 + (i * 2); // Odds. |
| 173 | + await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 174 | + |
| 175 | + headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 176 | + |
| 177 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 178 | + Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags); |
| 179 | + Assert.Equal(streamId, headersFrame.StreamId); |
| 180 | + |
| 181 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 182 | + |
| 183 | + decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 184 | + |
| 185 | + // HTTP/2 filters out the connection header |
| 186 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 187 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 188 | + h2Connection.ResetHeaders(); |
| 189 | + |
| 190 | + dataFrame = await h2Connection.ReceiveFrameAsync(); |
| 191 | + Assert.Equal(Http2FrameType.DATA, dataFrame.Type); |
| 192 | + Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags); |
| 193 | + Assert.Equal(0, dataFrame.PayloadLength); |
| 194 | + Assert.Equal(streamId, dataFrame.StreamId); |
| 195 | + } |
| 196 | + |
| 197 | + streamId = 1 + (200 * 2); // Odds. |
| 198 | + await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true); |
| 199 | + |
| 200 | + // Final GoAway |
| 201 | + goAwayFrame = await h2Connection.ReceiveFrameAsync(); |
| 202 | + h2Connection.VerifyGoAway(goAwayFrame, streamId, Http2ErrorCode.NO_ERROR); |
| 203 | + |
| 204 | + // Normal response |
| 205 | + headersFrame = await h2Connection.ReceiveFrameAsync(); |
| 206 | + |
| 207 | + Assert.Equal(Http2FrameType.HEADERS, headersFrame.Type); |
| 208 | + Assert.Equal(Http2HeadersFrameFlags.END_HEADERS, headersFrame.HeadersFlags); |
| 209 | + Assert.Equal(streamId, headersFrame.StreamId); |
| 210 | + |
| 211 | + h2Connection.Logger.LogInformation("Received headers in a single frame."); |
| 212 | + |
| 213 | + decodedHeaders = h2Connection.DecodeHeaders(headersFrame); |
| 214 | + |
| 215 | + // HTTP/2 filters out the connection header |
| 216 | + Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection)); |
| 217 | + Assert.Equal("200", decodedHeaders[HeaderNames.Status]); |
| 218 | + h2Connection.ResetHeaders(); |
| 219 | + |
| 220 | + dataFrame = await h2Connection.ReceiveFrameAsync(); |
| 221 | + Assert.Equal(Http2FrameType.DATA, dataFrame.Type); |
| 222 | + Assert.Equal(Http2DataFrameFlags.END_STREAM, dataFrame.DataFlags); |
| 223 | + Assert.Equal(0, dataFrame.PayloadLength); |
| 224 | + Assert.Equal(streamId, dataFrame.StreamId); |
| 225 | + |
| 226 | + h2Connection.Logger.LogInformation("Connection stopped."); |
| 227 | + }) |
| 228 | + .Build().RunAsync(); |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments