#539 Implement request body size limit
This commit is contained in:
parent
7b15720a05
commit
7febdbaa20
|
|
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
{
|
{
|
||||||
if (value.HasValue && value < -1)
|
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)
|
if (value.HasValue && _urlGroup != null)
|
||||||
|
|
@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
{
|
{
|
||||||
if (value <= 0)
|
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)
|
if (_requestQueue != null)
|
||||||
|
|
@ -125,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
{
|
{
|
||||||
if (value < 0)
|
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;
|
_maxRequestBodySize = value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
if (value.HasValue && value < 0)
|
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;
|
_maxSize = value;
|
||||||
}
|
}
|
||||||
|
|
@ -289,7 +289,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
castedAsyncResult.EndCalled = true;
|
castedAsyncResult.EndCalled = true;
|
||||||
// wait & then check for errors
|
// wait & then check for errors
|
||||||
// Throws on failure
|
// Throws on failure
|
||||||
int dataRead = castedAsyncResult.Task.GetAwaiter().GetResult();
|
var dataRead = castedAsyncResult.Task.GetAwaiter().GetResult();
|
||||||
// TODO: Verbose log #dataRead.
|
// TODO: Verbose log #dataRead.
|
||||||
return dataRead;
|
return dataRead;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task RequestBody_ReadAync_Success()
|
public async Task RequestBody_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (var server = Utilities.CreateHttpServer(out address))
|
using (var server = Utilities.CreateHttpServer(out address))
|
||||||
|
|
|
||||||
|
|
@ -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<IHttpMaxRequestBodySizeFeature>();
|
||||||
|
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<IHttpUpgradeFeature>();
|
||||||
|
Assert.NotNull(opaqueFeature);
|
||||||
|
Assert.True(opaqueFeature.IsUpgradableRequest);
|
||||||
|
var stream = await opaqueFeature.UpgradeAsync();
|
||||||
|
Assert.True(feature.IsReadOnly);
|
||||||
|
Assert.Null(feature.MaxRequestBodySize);
|
||||||
|
Assert.Throws<InvalidOperationException>(() => 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]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)]
|
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)]
|
||||||
public async Task OpaqueUpgrade_WithOnStarting_CallbackCalled()
|
public async Task OpaqueUpgrade_WithOnStarting_CallbackCalled()
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task ContentLengthEqualsLimit_ReadAync_Success()
|
public async Task ContentLengthEqualsLimit_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
||||||
|
|
@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task ChunkedEqualsLimit_ReadAync_Success()
|
public async Task ChunkedEqualsLimit_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
||||||
|
|
@ -146,7 +146,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmidately()
|
public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmediately()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
||||||
|
|
@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmidately()
|
public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmediately()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
||||||
|
|
@ -192,7 +192,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmidately()
|
public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmediately()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
|
||||||
|
|
@ -333,7 +333,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task AdjustLimitPerRequest_ContentLength_ReadAync_Success()
|
public async Task AdjustLimitPerRequest_ContentLength_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
||||||
|
|
@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task AdjustLimitPerRequest_Chunked_ReadAync_Success()
|
public async Task AdjustLimitPerRequest_Chunked_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact]
|
[ConditionalFact]
|
||||||
public async Task RequestBody_ReadAync_Success()
|
public async Task RequestBody_ReadAsync_Success()
|
||||||
{
|
{
|
||||||
string address;
|
string address;
|
||||||
using (Utilities.CreateHttpServer(out address, async httpContext =>
|
using (Utilities.CreateHttpServer(out address, async httpContext =>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue