Skip to content

Commit b463e04

Browse files
authored
Move BadHttpRequestException to Http.Abstractions (#20339)
1 parent 1f76cce commit b463e04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+494
-248
lines changed

src/Http/Http.Abstractions/ref/Microsoft.AspNetCore.Http.Abstractions.netcoreapp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public partial interface ICorsMetadata
9797
}
9898
namespace Microsoft.AspNetCore.Http
9999
{
100+
public partial class BadHttpRequestException : System.IO.IOException
101+
{
102+
public BadHttpRequestException(string message) { }
103+
public BadHttpRequestException(string message, System.Exception innerException) { }
104+
public BadHttpRequestException(string message, int statusCode) { }
105+
public BadHttpRequestException(string message, int statusCode, System.Exception innerException) { }
106+
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
107+
}
100108
public abstract partial class ConnectionInfo
101109
{
102110
protected ConnectionInfo() { }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Microsoft.AspNetCore.Http
5+
{
6+
/// <summary>
7+
/// Represents an HTTP request error
8+
/// </summary>
9+
public class BadHttpRequestException : IOException
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class.
13+
/// </summary>
14+
/// <param name="message">The message to associate with this exception.</param>
15+
/// <param name="statusCode">The HTTP status code to associate with this exception.</param>
16+
public BadHttpRequestException(string message, int statusCode)
17+
: base(message)
18+
{
19+
StatusCode = statusCode;
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class with the <see cref="StatusCode"/> set to 400 Bad Request.
24+
/// </summary>
25+
/// <param name="message">The message to associate with this exception</param>
26+
public BadHttpRequestException(string message)
27+
: base(message)
28+
{
29+
StatusCode = StatusCodes.Status400BadRequest;
30+
}
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class.
34+
/// </summary>
35+
/// <param name="message">The message to associate with this exception.</param>
36+
/// <param name="statusCode">The HTTP status code to associate with this exception.</param>
37+
/// <param name="innerException">The inner exception to associate with this exception</param>
38+
public BadHttpRequestException(string message, int statusCode, Exception innerException)
39+
: base(message, innerException)
40+
{
41+
StatusCode = statusCode;
42+
}
43+
44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class with the <see cref="StatusCode"/> set to 400 Bad Request.
46+
/// </summary>
47+
/// <param name="message">The message to associate with this exception</param>
48+
/// <param name="innerException">The inner exception to associate with this exception</param>
49+
public BadHttpRequestException(string message, Exception innerException)
50+
: base(message, innerException)
51+
{
52+
StatusCode = StatusCodes.Status400BadRequest;
53+
}
54+
55+
/// <summary>
56+
/// Gets the HTTP status code for this exception.
57+
/// </summary>
58+
public int StatusCode { get; }
59+
}
60+
}

src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public static partial class WebHostBuilderIISExtensions
2121
}
2222
namespace Microsoft.AspNetCore.Server.IIS
2323
{
24-
public sealed partial class BadHttpRequestException : System.IO.IOException
24+
[System.ObsoleteAttribute("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
25+
public sealed partial class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
2526
{
26-
internal BadHttpRequestException() { }
27-
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
27+
internal BadHttpRequestException() : base (default(string), default(int)) { }
28+
public new int StatusCode { get { throw null; } }
2829
}
2930
public static partial class HttpContextExtensions
3031
{
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Runtime.CompilerServices;
67
using Microsoft.AspNetCore.Http;
78

89
namespace Microsoft.AspNetCore.Server.IIS
910
{
10-
public sealed class BadHttpRequestException : IOException
11+
[Obsolete("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
12+
public sealed class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
1113
{
12-
private BadHttpRequestException(string message, int statusCode, RequestRejectionReason reason)
13-
: base(message)
14+
internal BadHttpRequestException(string message, int statusCode, RequestRejectionReason reason)
15+
: base(message, statusCode)
1416
{
15-
StatusCode = statusCode;
1617
Reason = reason;
1718
}
1819

19-
public int StatusCode { get; }
20+
public new int StatusCode { get => base.StatusCode; }
2021

2122
internal RequestRejectionReason Reason { get; }
22-
23-
internal static void Throw(RequestRejectionReason reason)
24-
{
25-
throw GetException(reason);
26-
}
27-
28-
[MethodImpl(MethodImplOptions.NoInlining)]
29-
internal static BadHttpRequestException GetException(RequestRejectionReason reason)
30-
{
31-
BadHttpRequestException ex;
32-
switch (reason)
33-
{
34-
case RequestRejectionReason.RequestBodyTooLarge:
35-
ex = new BadHttpRequestException(CoreStrings.BadRequest_RequestBodyTooLarge, StatusCodes.Status413PayloadTooLarge, reason);
36-
break;
37-
default:
38-
ex = new BadHttpRequestException(CoreStrings.BadRequest, StatusCodes.Status400BadRequest, reason);
39-
break;
40-
}
41-
return ex;
42-
}
4323
}
4424
}

src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private async Task ReadBody()
122122

123123
if (_consumedBytes > MaxRequestBodySize)
124124
{
125-
BadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
125+
IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
126126
}
127127

128128
var result = await _bodyInputPipe.Writer.FlushAsync();

src/Servers/IIS/IIS/src/Core/IISHttpContext.Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void UnexpectedError(ILogger logger, string className, Exception e
3838
_unexpectedError(logger, className, methodName, ex);
3939
}
4040

41-
public static void ConnectionBadRequest(ILogger logger, string connectionId, BadHttpRequestException ex)
41+
public static void ConnectionBadRequest(ILogger logger, string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex)
4242
{
4343
_connectionBadRequest(logger, connectionId, ex.Message, ex);
4444
}

src/Servers/IIS/IIS/src/Core/IISHttpContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
namespace Microsoft.AspNetCore.Server.IIS.Core
2828
{
29+
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;
30+
2931
internal abstract partial class IISHttpContext : NativeRequestContext, IThreadPoolWorkItem, IDisposable
3032
{
3133
private const int MinAllocBufferSize = 2048;
@@ -293,7 +295,7 @@ private void InitializeRequestIO()
293295

294296
if (RequestHeaders.ContentLength > MaxRequestBodySize)
295297
{
296-
BadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
298+
IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
297299
}
298300

299301
HasStartedConsumingRequestBody = true;

src/Servers/IIS/IIS/src/Core/IISHttpContextOfT.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Microsoft.AspNetCore.Server.IIS.Core
1313
{
14+
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;
15+
1416
internal class IISHttpContextOfT<TContext> : IISHttpContext
1517
{
1618
private readonly IHttpApplication<TContext> _application;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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;
5+
using System.IO;
6+
using System.Runtime.CompilerServices;
7+
using Microsoft.AspNetCore.Http;
8+
9+
namespace Microsoft.AspNetCore.Server.IIS
10+
{
11+
internal static class IISBadHttpRequestException
12+
{
13+
internal static void Throw(RequestRejectionReason reason)
14+
{
15+
throw GetException(reason);
16+
}
17+
18+
[MethodImpl(MethodImplOptions.NoInlining)]
19+
#pragma warning disable CS0618 // Type or member is obsolete
20+
internal static BadHttpRequestException GetException(RequestRejectionReason reason)
21+
{
22+
BadHttpRequestException ex;
23+
switch (reason)
24+
{
25+
case RequestRejectionReason.RequestBodyTooLarge:
26+
ex = new BadHttpRequestException(CoreStrings.BadRequest_RequestBodyTooLarge, StatusCodes.Status413PayloadTooLarge, reason);
27+
break;
28+
default:
29+
ex = new BadHttpRequestException(CoreStrings.BadRequest, StatusCodes.Status400BadRequest, reason);
30+
break;
31+
}
32+
return ex;
33+
}
34+
#pragma warning restore CS0618 // Type or member is obsolete
35+
}
36+
}

src/Servers/IIS/IIS/test/IIS.Tests/MaxRequestBodySizeTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Testing;
1212
using Microsoft.Extensions.Logging.Testing;
1313
using Xunit;
14+
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;
1415

1516
namespace IIS.Tests
1617
{
@@ -24,6 +25,7 @@ public async Task RequestBodyTooLargeContentLengthExceedsGlobalLimit()
2425
var globalMaxRequestBodySize = 0x100000000;
2526

2627
BadHttpRequestException exception = null;
28+
2729
using (var testServer = await TestServer.Create(
2830
async ctx =>
2931
{
@@ -60,6 +62,7 @@ public async Task RequestBodyTooLargeContentLengthExceedingPerRequestLimit()
6062
var perRequestMaxRequestBodySize = 0x100;
6163

6264
BadHttpRequestException exception = null;
65+
6366
using (var testServer = await TestServer.Create(
6467
async ctx =>
6568
{
@@ -71,6 +74,7 @@ public async Task RequestBodyTooLargeContentLengthExceedingPerRequestLimit()
7174

7275
await ctx.Request.Body.ReadAsync(new byte[2000]);
7376
}
77+
7478
catch (BadHttpRequestException ex)
7579
{
7680
exception = ex;
@@ -266,6 +270,7 @@ public async Task RequestBodyTooLargeChunked()
266270
var maxRequestSize = 0x1000;
267271

268272
BadHttpRequestException exception = null;
273+
269274
using (var testServer = await TestServer.Create(
270275
async ctx =>
271276
{
@@ -307,13 +312,14 @@ public async Task EveryReadFailsWhenContentLengthHeaderExceedsGlobalLimit()
307312
{
308313
BadHttpRequestException requestRejectedEx1 = null;
309314
BadHttpRequestException requestRejectedEx2 = null;
315+
310316
using (var testServer = await TestServer.Create(
311317
async ctx =>
312318
{
313319
var buffer = new byte[1];
314-
requestRejectedEx1 = await Assert.ThrowsAsync<BadHttpRequestException>(
320+
requestRejectedEx1 = await Assert.ThrowsAnyAsync<BadHttpRequestException>(
315321
async () => await ctx.Request.Body.ReadAsync(buffer, 0, 1));
316-
requestRejectedEx2 = await Assert.ThrowsAsync<BadHttpRequestException>(
322+
requestRejectedEx2 = await Assert.ThrowsAnyAsync<BadHttpRequestException>(
317323
async () => await ctx.Request.Body.ReadAsync(buffer, 0, 1));
318324
throw requestRejectedEx2;
319325
}, LoggerFactory, new IISServerOptions { MaxRequestBodySize = 0 }))

src/Servers/Kestrel/Core/ref/Microsoft.AspNetCore.Server.Kestrel.Core.netcoreapp.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ public void Load() { }
6262
}
6363
namespace Microsoft.AspNetCore.Server.Kestrel.Core
6464
{
65-
public sealed partial class BadHttpRequestException : System.IO.IOException
65+
[System.ObsoleteAttribute("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
66+
public sealed partial class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
6667
{
67-
internal BadHttpRequestException() { }
68-
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
68+
internal BadHttpRequestException() : base (default(string), default(int)) { }
69+
public new int StatusCode { get { throw null; } }
6970
}
7071
public partial class Http2Limits
7172
{

0 commit comments

Comments
 (0)