From 7febdbaa2052125f764c939b873b415e7ba762e0 Mon Sep 17 00:00:00 2001 From: Chris R Date: Mon, 26 Jun 2017 15:17:35 -0700 Subject: [PATCH] #539 Implement request body size limit --- .../HttpSysOptions.cs | 6 +-- .../RequestProcessing/RequestStream.cs | 4 +- .../Listener/RequestBodyTests.cs | 2 +- .../OpaqueUpgradeTests.cs | 37 +++++++++++++++++++ .../RequestBodyLimitTests.cs | 14 +++---- .../RequestBodyTests.cs | 2 +- 6 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs b/src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs index 1d1e0cc00f..720eee3739 100644 --- a/src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs +++ b/src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs @@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { if (value.HasValue && value < -1) { - throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty); + throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be positive, or -1 for infiniate."); } if (value.HasValue && _urlGroup != null) @@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { if (value <= 0) { - throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty); + throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater than zero."); } if (_requestQueue != null) @@ -125,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { if (value < 0) { - throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty); + throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater or equal to zero."); } _maxRequestBodySize = value; } diff --git a/src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/RequestStream.cs b/src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/RequestStream.cs index f8da3974c3..ce7642dacf 100644 --- a/src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/RequestStream.cs +++ b/src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/RequestStream.cs @@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } if (value.HasValue && value < 0) { - throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty); + throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater or equal to zero."); } _maxSize = value; } @@ -289,7 +289,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys castedAsyncResult.EndCalled = true; // wait & then check for errors // Throws on failure - int dataRead = castedAsyncResult.Task.GetAwaiter().GetResult(); + var dataRead = castedAsyncResult.Task.GetAwaiter().GetResult(); // TODO: Verbose log #dataRead. return dataRead; } diff --git a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/RequestBodyTests.cs b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/RequestBodyTests.cs index d716e2a407..b34cd35ed6 100644 --- a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/RequestBodyTests.cs +++ b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/RequestBodyTests.cs @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys.Listener } [ConditionalFact] - public async Task RequestBody_ReadAync_Success() + public async Task RequestBody_ReadAsync_Success() { string address; using (var server = Utilities.CreateHttpServer(out address)) diff --git a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/OpaqueUpgradeTests.cs index ef2c72e4c1..48421ddcaa 100644 --- a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/OpaqueUpgradeTests.cs @@ -101,6 +101,43 @@ namespace Microsoft.AspNetCore.Server.HttpSys } } + [ConditionalFact] + [OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)] + public async Task OpaqueUpgrade_GetUpgrade_NotAffectedByMaxRequestBodyLimit() + { + ManualResetEvent waitHandle = new ManualResetEvent(false); + bool? upgraded = null; + string address; + using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, async httpContext => + { + var feature = httpContext.Features.Get(); + Assert.NotNull(feature); + Assert.False(feature.IsReadOnly); + Assert.Null(feature.MaxRequestBodySize); // GET/Upgrade requests don't actually have an entity body, so they can't set the limit. + + httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets + var opaqueFeature = httpContext.Features.Get(); + Assert.NotNull(opaqueFeature); + Assert.True(opaqueFeature.IsUpgradableRequest); + var stream = await opaqueFeature.UpgradeAsync(); + Assert.True(feature.IsReadOnly); + Assert.Null(feature.MaxRequestBodySize); + Assert.Throws(() => feature.MaxRequestBodySize = 12); + Assert.Equal(15, stream.Read(new byte[15], 0, 15)); + upgraded = true; + waitHandle.Set(); + })) + { + using (Stream stream = await SendOpaqueRequestAsync("GET", address)) + { + stream.Write(new byte[15], 0, 15); + Assert.True(waitHandle.WaitOne(TimeSpan.FromSeconds(1)), "Timed out"); + Assert.True(upgraded.HasValue, "Upgraded not set"); + Assert.True(upgraded.Value, "Upgrade failed"); + } + } + } + [ConditionalFact] [OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)] public async Task OpaqueUpgrade_WithOnStarting_CallbackCalled() diff --git a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyLimitTests.cs b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyLimitTests.cs index 039e81a1a4..ed835f345f 100644 --- a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyLimitTests.cs +++ b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyLimitTests.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task ContentLengthEqualsLimit_ReadAync_Success() + public async Task ContentLengthEqualsLimit_ReadAsync_Success() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext => @@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task ChunkedEqualsLimit_ReadAync_Success() + public async Task ChunkedEqualsLimit_ReadAsync_Success() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext => @@ -146,7 +146,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmidately() + public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmediately() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext => @@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmidately() + public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmediately() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext => @@ -192,7 +192,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmidately() + public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmediately() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext => @@ -333,7 +333,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task AdjustLimitPerRequest_ContentLength_ReadAync_Success() + public async Task AdjustLimitPerRequest_ContentLength_ReadAsync_Success() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext => @@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task AdjustLimitPerRequest_Chunked_ReadAync_Success() + public async Task AdjustLimitPerRequest_Chunked_ReadAsync_Success() { string address; using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext => diff --git a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyTests.cs b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyTests.cs index 820c999da4..23c7c56be1 100644 --- a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyTests.cs +++ b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyTests.cs @@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys } [ConditionalFact] - public async Task RequestBody_ReadAync_Success() + public async Task RequestBody_ReadAsync_Success() { string address; using (Utilities.CreateHttpServer(out address, async httpContext =>