Skip to content

Commit 7febdba

Browse files
committed
#539 Implement request body size limit
1 parent 7b15720 commit 7febdba

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

src/Microsoft.AspNetCore.Server.HttpSys/HttpSysOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public long? MaxConnections
7272
{
7373
if (value.HasValue && value < -1)
7474
{
75-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty);
75+
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be positive, or -1 for infiniate.");
7676
}
7777

7878
if (value.HasValue && _urlGroup != null)
@@ -97,7 +97,7 @@ public long RequestQueueLimit
9797
{
9898
if (value <= 0)
9999
{
100-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty);
100+
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater than zero.");
101101
}
102102

103103
if (_requestQueue != null)
@@ -125,7 +125,7 @@ public long? MaxRequestBodySize
125125
{
126126
if (value < 0)
127127
{
128-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty);
128+
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater or equal to zero.");
129129
}
130130
_maxRequestBodySize = value;
131131
}

src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/RequestStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public long? MaxSize
5151
}
5252
if (value.HasValue && value < 0)
5353
{
54-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Empty);
54+
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater or equal to zero.");
5555
}
5656
_maxSize = value;
5757
}
@@ -289,7 +289,7 @@ public override int EndRead(IAsyncResult asyncResult)
289289
castedAsyncResult.EndCalled = true;
290290
// wait & then check for errors
291291
// Throws on failure
292-
int dataRead = castedAsyncResult.Task.GetAwaiter().GetResult();
292+
var dataRead = castedAsyncResult.Task.GetAwaiter().GetResult();
293293
// TODO: Verbose log #dataRead.
294294
return dataRead;
295295
}

test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/RequestBodyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task RequestBody_ReadSync_Success()
3636
}
3737

3838
[ConditionalFact]
39-
public async Task RequestBody_ReadAync_Success()
39+
public async Task RequestBody_ReadAsync_Success()
4040
{
4141
string address;
4242
using (var server = Utilities.CreateHttpServer(out address))

test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/OpaqueUpgradeTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,43 @@ public async Task OpaqueUpgrade_GetUpgrade_Success()
101101
}
102102
}
103103

104+
[ConditionalFact]
105+
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)]
106+
public async Task OpaqueUpgrade_GetUpgrade_NotAffectedByMaxRequestBodyLimit()
107+
{
108+
ManualResetEvent waitHandle = new ManualResetEvent(false);
109+
bool? upgraded = null;
110+
string address;
111+
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, async httpContext =>
112+
{
113+
var feature = httpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
114+
Assert.NotNull(feature);
115+
Assert.False(feature.IsReadOnly);
116+
Assert.Null(feature.MaxRequestBodySize); // GET/Upgrade requests don't actually have an entity body, so they can't set the limit.
117+
118+
httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets
119+
var opaqueFeature = httpContext.Features.Get<IHttpUpgradeFeature>();
120+
Assert.NotNull(opaqueFeature);
121+
Assert.True(opaqueFeature.IsUpgradableRequest);
122+
var stream = await opaqueFeature.UpgradeAsync();
123+
Assert.True(feature.IsReadOnly);
124+
Assert.Null(feature.MaxRequestBodySize);
125+
Assert.Throws<InvalidOperationException>(() => feature.MaxRequestBodySize = 12);
126+
Assert.Equal(15, stream.Read(new byte[15], 0, 15));
127+
upgraded = true;
128+
waitHandle.Set();
129+
}))
130+
{
131+
using (Stream stream = await SendOpaqueRequestAsync("GET", address))
132+
{
133+
stream.Write(new byte[15], 0, 15);
134+
Assert.True(waitHandle.WaitOne(TimeSpan.FromSeconds(1)), "Timed out");
135+
Assert.True(upgraded.HasValue, "Upgraded not set");
136+
Assert.True(upgraded.Value, "Upgrade failed");
137+
}
138+
}
139+
}
140+
104141
[ConditionalFact]
105142
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)]
106143
public async Task OpaqueUpgrade_WithOnStarting_CallbackCalled()

test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyLimitTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task ContentLengthEqualsLimit_ReadSync_Success()
3838
}
3939

4040
[ConditionalFact]
41-
public async Task ContentLengthEqualsLimit_ReadAync_Success()
41+
public async Task ContentLengthEqualsLimit_ReadAsync_Success()
4242
{
4343
string address;
4444
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
@@ -103,7 +103,7 @@ public async Task ChunkedEqualsLimit_ReadSync_Success()
103103
}
104104

105105
[ConditionalFact]
106-
public async Task ChunkedEqualsLimit_ReadAync_Success()
106+
public async Task ChunkedEqualsLimit_ReadAsync_Success()
107107
{
108108
string address;
109109
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
@@ -146,7 +146,7 @@ public async Task ChunkedEqualsLimit_ReadBeginEnd_Success()
146146
}
147147

148148
[ConditionalFact]
149-
public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmidately()
149+
public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmediately()
150150
{
151151
string address;
152152
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
@@ -169,7 +169,7 @@ public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmidately()
169169
}
170170

171171
[ConditionalFact]
172-
public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmidately()
172+
public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmediately()
173173
{
174174
string address;
175175
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
@@ -192,7 +192,7 @@ public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmidately()
192192
}
193193

194194
[ConditionalFact]
195-
public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmidately()
195+
public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmediately()
196196
{
197197
string address;
198198
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 10, httpContext =>
@@ -333,7 +333,7 @@ public async Task Chunked_ReadAsyncPartialBodyUnderLimit_ThrowsAfterLimit()
333333
}
334334

335335
[ConditionalFact]
336-
public async Task AdjustLimitPerRequest_ContentLength_ReadAync_Success()
336+
public async Task AdjustLimitPerRequest_ContentLength_ReadAsync_Success()
337337
{
338338
string address;
339339
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>
@@ -357,7 +357,7 @@ public async Task AdjustLimitPerRequest_ContentLength_ReadAync_Success()
357357
}
358358

359359
[ConditionalFact]
360-
public async Task AdjustLimitPerRequest_Chunked_ReadAync_Success()
360+
public async Task AdjustLimitPerRequest_Chunked_ReadAsync_Success()
361361
{
362362
string address;
363363
using (Utilities.CreateHttpServer(out address, options => options.MaxRequestBodySize = 11, async httpContext =>

test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/RequestBodyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task RequestBody_ReadSync_Success()
3535
}
3636

3737
[ConditionalFact]
38-
public async Task RequestBody_ReadAync_Success()
38+
public async Task RequestBody_ReadAsync_Success()
3939
{
4040
string address;
4141
using (Utilities.CreateHttpServer(out address, async httpContext =>

0 commit comments

Comments
 (0)