Example #1
0
        public async Task AppException_BeforeResponseHeaders_500()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                throw new Exception("Application exception");
            });

            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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("500", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #2
0
        public async Task Reset_CompleteAsyncDuringRequestBody_Resets()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, GetPostHeaders("/Reset_CompleteAsyncDuringRequestBody_Resets"), endStream: false);
                await h2Connection.SendDataAsync(1, new byte[10], endStream: false);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();

                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                var resetFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: Http2ErrorCode.NO_ERROR);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #3
0
        public async Task Reset_AfterCompleteAsync_NoReset()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/Reset_AfterCompleteAsync_NoReset"), endStream: true);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);

                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #4
0
    public async Task RequestClose_SendsGoAway()
    {
        await new HostBuilder()
        .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
        {
            await h2Connection.InitializeConnectionAsync();

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

            await h2Connection.StartStreamAsync(1, GetHeaders("/ConnectionRequestClose"), endStream: true);

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

            await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
            {
                // HTTP/2 filters out the connection header
                Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
            });

            var dataFrame = await h2Connection.ReceiveFrameAsync();
            Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

            // 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();
    }
Example #5
0
        public async Task Reset_CompleteAsyncDuringRequestBody_Resets()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, Http2Utilities.PostRequestHeaders, endStream: false);
                await h2Connection.SendDataAsync(1, new byte[10], endStream: false);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                var resetFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: Http2ErrorCode.NO_ERROR);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #6
0
        public async Task Http2_RequestWithDataAndContentLength_Success(string method)
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/Http2_RequestWithDataAndContentLength_Success"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:443"),
                    new KeyValuePair <string, string>(HeaderNames.ContentLength, "11"),
                };

                await h2Connection.StartStreamAsync(1, headers, endStream: false);

                await h2Connection.SendDataAsync(1, Encoding.UTF8.GetBytes("Hello World"), endStream: true);

                // Http.Sys no longer sends a window update here on later versions.
                if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0))
                {
                    var windowUpdate = await h2Connection.ReceiveFrameAsync();
                    Assert.Equal(Http2FrameType.WINDOW_UPDATE, windowUpdate.Type);
                }

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
                Assert.Equal(1, dataFrame.StreamId);

                // Some versions send an empty data frame first.
                if (dataFrame.PayloadLength == 0)
                {
                    Assert.False(dataFrame.DataEndStream);
                    dataFrame = await h2Connection.ReceiveFrameAsync();
                    Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
                    Assert.Equal(1, dataFrame.StreamId);
                }

                Assert.Equal(11, dataFrame.PayloadLength);
                Assert.Equal("Hello World", Encoding.UTF8.GetString(dataFrame.Payload.Span));

                if (!dataFrame.DataEndStream)
                {
                    dataFrame = await h2Connection.ReceiveFrameAsync();
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
                }

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #7
0
        public async Task AppException_AfterHeaders_ResetInternalError()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/AppException_AfterHeaders_ResetInternalError"), endStream: true);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var frame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(frame, 1, endOfStream: false, length: 0);

                    frame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyResetFrame(frame, expectedStreamId: 1, Http2ErrorCode.INTERNAL_ERROR);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #8
0
        public async Task Http2_PostRequestWithoutData_LengthRequired(string method)
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:443"),
                };

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

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("411", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 344);
                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #9
0
        public async Task AppException_BeforeResponseHeaders_500()
        {
            var deploymentParameters = GetHttpsDeploymentParameters();
            var deploymentResult     = await DeployAsync(deploymentParameters);

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

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/AppException_BeforeResponseHeaders_500"), endStream: true);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("500", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #10
0
        public async Task Reset_DuringResponseBody_Resets()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/Reset_DuringResponseBody_Resets"), endStream: true);

                // This is currently flaky, can either receive header or reset at this point
                var headerOrResetFrame = await h2Connection.ReceiveFrameAsync();
                Assert.True(headerOrResetFrame.Type == Http2FrameType.HEADERS || headerOrResetFrame.Type == Http2FrameType.RST_STREAM);

                if (headerOrResetFrame.Type == Http2FrameType.HEADERS)
                {
                    var dataFrame = await h2Connection.ReceiveFrameAsync();
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);

                    var resetFrame = await h2Connection.ReceiveFrameAsync();
                    Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
                }
                else
                {
                    Http2Utilities.VerifyResetFrame(headerOrResetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
                }

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #11
0
        public async Task EmptyResponse_200()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                var feature = httpContext.Features.Get <IHttpUpgradeFeature>();
                Assert.False(feature.IsUpgradableRequest);
                Assert.False(httpContext.Request.CanHaveBody());
                // Default 200
                return(Task.CompletedTask);
            });

            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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #12
0
        public async Task ResponseWithData_Success()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                return(httpContext.Response.WriteAsync("Hello World"));
            });

            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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);

                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #13
0
        public async Task Reset_AfterResponseHeaders_Resets()
        {
            await new HostBuilder()
            .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
            {
                await h2Connection.InitializeConnectionAsync();

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/Reset_AfterResponseHeaders_Resets"), endStream: true);

                // Any app errors?
                var client   = CreateClient();
                var response = await client.GetAsync(Fixture.Client.BaseAddress + "/Reset_AfterResponseHeaders_Resets_Complete");
                Assert.True(response.IsSuccessStatusCode);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, expectedStreamId: 1, endOfStream: false, length: 0);

                var resetFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #14
0
        public async Task Reset_CompleteAsyncDurringRequestBody_Resets()
        {
            var appResult = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
            {
                try
                {
                    Assert.Equal("HTTP/2", httpContext.Request.Protocol);
                    var feature = httpContext.Features.Get <IHttpResetFeature>();
                    Assert.NotNull(feature);

                    var read = await httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
                    Assert.Equal(10, read);

                    var readTask = httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
                    await httpContext.Response.CompleteAsync();
                    feature.Reset((int)Http2ErrorCode.NO_ERROR); // GRPC does this
                    await Assert.ThrowsAsync <IOException>(() => readTask);

                    appResult.SetResult(0);
                }
                catch (Exception ex)
                {
                    appResult.SetException(ex);
                }
            });

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

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

                await h2Connection.StartStreamAsync(1, Http2Utilities.PostRequestHeaders, endStream: false);
                await h2Connection.SendDataAsync(1, new byte[10], endStream: false);

                // Any app errors?
                Assert.Equal(0, await appResult.Task.DefaultTimeout());

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                var resetFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: Http2ErrorCode.NO_ERROR);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #15
0
        public async Task RequestWithDataAndNoContentLength_Success(string method)
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
                Assert.True(httpContext.Request.CanHaveBody());
                Assert.Null(httpContext.Request.ContentLength);
                // The client didn't send this header, Http.Sys added it for back compat with HTTP/1.1.
                Assert.Equal("chunked", httpContext.Request.Headers.TransferEncoding);
                return(httpContext.Request.Body.CopyToAsync(httpContext.Response.Body));
            });

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

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:80"),
                };

                await h2Connection.StartStreamAsync(1, headers, endStream: false);

                await h2Connection.SendDataAsync(1, Encoding.UTF8.GetBytes("Hello World"), endStream: true);

                // Http.Sys no longer sends a window update here on later versions.
                if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0))
                {
                    var windowUpdate = await h2Connection.ReceiveFrameAsync();
                    Assert.Equal(Http2FrameType.WINDOW_UPDATE, windowUpdate.Type);
                }

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
                Assert.Equal("Hello World", Encoding.UTF8.GetString(dataFrame.Payload.Span));

                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #16
0
        public async Task Reset_AfterCompleteAsync_NoReset()
        {
            var appResult = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
            {
                try
                {
                    Assert.Equal("HTTP/2", httpContext.Request.Protocol);
                    var feature = httpContext.Features.Get <IHttpResetFeature>();
                    Assert.NotNull(feature);
                    await httpContext.Response.WriteAsync("Hello World");
                    await httpContext.Response.CompleteAsync();
                    // The request and response are fully complete, the reset doesn't get sent.
                    feature.Reset(1111);
                    appResult.SetResult(0);
                }
                catch (Exception ex)
                {
                    appResult.SetException(ex);
                }
            });

            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);

                // Any app errors?
                Assert.Equal(0, await appResult.Task.DefaultTimeout());

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);

                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #17
0
        public async Task RequestWithoutData_Success(string method)
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
                Assert.False(httpContext.Request.CanHaveBody());
                Assert.Null(httpContext.Request.ContentLength);
                Assert.False(httpContext.Request.Headers.ContainsKey(HeaderNames.TransferEncoding));
                return(Task.CompletedTask);
            });

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

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:80"),
                };

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

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #18
0
        public async Task Http2_ResponseWithData_Success()
        {
            var deploymentParameters = GetHttpsDeploymentParameters();
            var deploymentResult     = await DeployAsync(deploymentParameters);

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

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

                await h2Connection.StartStreamAsync(1, GetHeaders("/Http2_ResponseWithData_Success"), endStream: true);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
                Assert.Equal(1, dataFrame.StreamId);

                // Some versions send an empty data frame first.
                if (dataFrame.PayloadLength == 0)
                {
                    Assert.False(dataFrame.DataEndStream);
                    dataFrame = await h2Connection.ReceiveFrameAsync();
                    Assert.Equal(Http2FrameType.DATA, dataFrame.Type);
                    Assert.Equal(1, dataFrame.StreamId);
                }

                Assert.Equal(11, dataFrame.PayloadLength);
                Assert.Equal("Hello World", Encoding.UTF8.GetString(dataFrame.Payload.Span));

                if (!dataFrame.DataEndStream)
                {
                    dataFrame = await h2Connection.ReceiveFrameAsync();
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
                }

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #19
0
        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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    // HTTP/2 filters out the connection header
                    Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                // 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();
        }
Example #20
0
        public async Task Http2_MethodsRequestWithoutData_Success(string method)
        {
            var deploymentParameters = GetHttpsDeploymentParameters();
            var deploymentResult     = await DeployAsync(deploymentParameters);

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

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/Http2_MethodsRequestWithoutData_Success"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:443"),
                };

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

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #21
0
    public async Task RequestWithoutData_Success(string method)
    {
        using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
        {
            Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
            Assert.False(httpContext.Request.CanHaveBody());
            Assert.Null(httpContext.Request.ContentLength);
            Assert.False(httpContext.Request.Headers.ContainsKey(HeaderNames.TransferEncoding));
            return(Task.CompletedTask);
        });

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

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

            var headers = new[]
            {
                new KeyValuePair <string, string>(InternalHeaderNames.Method, method),
                new KeyValuePair <string, string>(InternalHeaderNames.Path, "/"),
                new KeyValuePair <string, string>(InternalHeaderNames.Scheme, "https"),
                new KeyValuePair <string, string>(InternalHeaderNames.Authority, "localhost:80"),
            };

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

            await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
            {
                Assert.Equal("200", decodedHeaders[InternalHeaderNames.Status]);
            });

            var dataFrame = await h2Connection.ReceiveFrameAsync();
            Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

            h2Connection.Logger.LogInformation("Connection stopped.");
        })
        .Build().RunAsync();
    }
Example #22
0
        public async Task EmptyResponse_200()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                // Default 200
                Assert.False(httpContext.Request.CanHaveBody());
                return(Task.CompletedTask);
            });

            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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 0);

                    dataFrame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #23
0
        public async Task RequestWithoutData_LengthRequired_Rejected(string method)
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                throw new NotImplementedException();
            });

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

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

                var headers = new[]
                {
                    new KeyValuePair <string, string>(HeaderNames.Method, method),
                    new KeyValuePair <string, string>(HeaderNames.Path, "/"),
                    new KeyValuePair <string, string>(HeaderNames.Scheme, "https"),
                    new KeyValuePair <string, string>(HeaderNames.Authority, "localhost:80"),
                };

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

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("411", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 344);
                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #24
0
    public async Task AppException_BeforeResponseHeaders_500()
    {
        await new HostBuilder()
        .UseHttp2Cat(Fixture.Client.BaseAddress.AbsoluteUri, async h2Connection =>
        {
            await h2Connection.InitializeConnectionAsync();

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

            await h2Connection.StartStreamAsync(1, GetHeaders("/AppException_BeforeResponseHeaders_500"), endStream: true);

            await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
            {
                Assert.Equal("500", decodedHeaders[InternalHeaderNames.Status]);
            });

            var dataFrame = await h2Connection.ReceiveFrameAsync();
            Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

            h2Connection.Logger.LogInformation("Connection stopped.");
        })
        .Build().RunAsync();
    }
Example #25
0
        public async Task AppException_AfterHeaders_ResetInternalError()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
            {
                await httpContext.Response.Body.FlushAsync();
                throw new Exception("Application exception");
            });

            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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var frame = await h2Connection.ReceiveFrameAsync();
                if (Environment.OSVersion.Version >= Win10_Regressed_DataFrame)
                {
                    // TODO: Remove when the regression is fixed.
                    // https://github.com/dotnet/aspnetcore/issues/23164#issuecomment-652646163
                    Http2Utilities.VerifyDataFrame(frame, 1, endOfStream: false, length: 0);

                    frame = await h2Connection.ReceiveFrameAsync();
                }
                Http2Utilities.VerifyResetFrame(frame, expectedStreamId: 1, Http2ErrorCode.INTERNAL_ERROR);

                h2Connection.Logger.LogInformation("Connection stopped.");
            })
            .Build().RunAsync();
        }
Example #26
0
        public async Task ConnectionRequestClose_OSSupport_SendsGoAway()
        {
            using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
            {
                httpContext.Connection.RequestClose();
                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);

                await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
                {
                    // HTTP/2 filters out the connection header
                    Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                    Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
                });

                var dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);

                // 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();
        }
Example #27
0
    public async Task ConnectionClose_AdditionalRequests_ReceivesSecondGoAway()
    {
        using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
        {
            httpContext.Response.Headers.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();
            Http2Utilities.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR);

            await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
            {
                // HTTP/2 filters out the connection header
                Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                Assert.Equal("200", decodedHeaders[InternalHeaderNames.Status]);
            });

            var dataFrame = await h2Connection.ReceiveFrameAsync();
            Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);

            // 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);

                await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
                {
                    // HTTP/2 filters out the connection header
                    Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                    Assert.Equal("200", decodedHeaders[InternalHeaderNames.Status]);
                });

                dataFrame = await h2Connection.ReceiveFrameAsync();
                Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);
            }

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

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

            // Normal response
            await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
            {
                // HTTP/2 filters out the connection header
                Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
                Assert.Equal("200", decodedHeaders[InternalHeaderNames.Status]);
            });

            dataFrame = await h2Connection.ReceiveFrameAsync();
            Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);

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