Skip to content

Move BadHttpRequestException to Http.Abstractions #20339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public partial interface ICorsMetadata
}
namespace Microsoft.AspNetCore.Http
{
public partial class BadHttpRequestException : System.IO.IOException
{
public BadHttpRequestException(string message) { }
public BadHttpRequestException(string message, System.Exception innerException) { }
public BadHttpRequestException(string message, int statusCode) { }
public BadHttpRequestException(string message, int statusCode, System.Exception innerException) { }
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
}
public abstract partial class ConnectionInfo
{
protected ConnectionInfo() { }
Expand Down
60 changes: 60 additions & 0 deletions src/Http/Http.Abstractions/src/BadHttpRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.IO;

namespace Microsoft.AspNetCore.Http
{
/// <summary>
/// Represents an HTTP request error
/// </summary>
public class BadHttpRequestException : IOException
{
/// <summary>
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class.
/// </summary>
/// <param name="message">The message to associate with this exception.</param>
/// <param name="statusCode">The HTTP status code to associate with this exception.</param>
public BadHttpRequestException(string message, int statusCode)
: base(message)
{
StatusCode = statusCode;
}

/// <summary>
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class with the <see cref="StatusCode"/> set to 400 Bad Request.
/// </summary>
/// <param name="message">The message to associate with this exception</param>
public BadHttpRequestException(string message)
: base(message)
{
StatusCode = StatusCodes.Status400BadRequest;
}

/// <summary>
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class.
/// </summary>
/// <param name="message">The message to associate with this exception.</param>
/// <param name="statusCode">The HTTP status code to associate with this exception.</param>
/// <param name="innerException">The inner exception to associate with this exception</param>
public BadHttpRequestException(string message, int statusCode, Exception innerException)
: base(message, innerException)
{
StatusCode = statusCode;
}

/// <summary>
/// Initializes a new instance of the <see cref="BadHttpRequestException"/> class with the <see cref="StatusCode"/> set to 400 Bad Request.
/// </summary>
/// <param name="message">The message to associate with this exception</param>
/// <param name="innerException">The inner exception to associate with this exception</param>
public BadHttpRequestException(string message, Exception innerException)
: base(message, innerException)
{
StatusCode = StatusCodes.Status400BadRequest;
}

/// <summary>
/// Gets the HTTP status code for this exception.
/// </summary>
public int StatusCode { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public static partial class WebHostBuilderIISExtensions
}
namespace Microsoft.AspNetCore.Server.IIS
{
public sealed partial class BadHttpRequestException : System.IO.IOException
[System.ObsoleteAttribute("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
public sealed partial class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
{
internal BadHttpRequestException() { }
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
internal BadHttpRequestException() : base (default(string), default(int)) { }
public new int StatusCode { get { throw null; } }
}
public static partial class HttpContextExtensions
{
Expand Down
32 changes: 6 additions & 26 deletions src/Servers/IIS/IIS/src/BadHttpRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Server.IIS
{
public sealed class BadHttpRequestException : IOException
[Obsolete("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
public sealed class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
{
private BadHttpRequestException(string message, int statusCode, RequestRejectionReason reason)
: base(message)
internal BadHttpRequestException(string message, int statusCode, RequestRejectionReason reason)
: base(message, statusCode)
{
StatusCode = statusCode;
Reason = reason;
}

public int StatusCode { get; }
public new int StatusCode { get => base.StatusCode; }

internal RequestRejectionReason Reason { get; }

internal static void Throw(RequestRejectionReason reason)
{
throw GetException(reason);
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static BadHttpRequestException GetException(RequestRejectionReason reason)
{
BadHttpRequestException ex;
switch (reason)
{
case RequestRejectionReason.RequestBodyTooLarge:
ex = new BadHttpRequestException(CoreStrings.BadRequest_RequestBodyTooLarge, StatusCodes.Status413PayloadTooLarge, reason);
break;
default:
ex = new BadHttpRequestException(CoreStrings.BadRequest, StatusCodes.Status400BadRequest, reason);
break;
}
return ex;
}
}
}
2 changes: 1 addition & 1 deletion src/Servers/IIS/IIS/src/Core/IISHttpContext.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private async Task ReadBody()

if (_consumedBytes > MaxRequestBodySize)
{
BadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
}

var result = await _bodyInputPipe.Writer.FlushAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Servers/IIS/IIS/src/Core/IISHttpContext.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void UnexpectedError(ILogger logger, string className, Exception e
_unexpectedError(logger, className, methodName, ex);
}

public static void ConnectionBadRequest(ILogger logger, string connectionId, BadHttpRequestException ex)
public static void ConnectionBadRequest(ILogger logger, string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex)
{
_connectionBadRequest(logger, connectionId, ex.Message, ex);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Servers/IIS/IIS/src/Core/IISHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace Microsoft.AspNetCore.Server.IIS.Core
{
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;

internal abstract partial class IISHttpContext : NativeRequestContext, IThreadPoolWorkItem, IDisposable
{
private const int MinAllocBufferSize = 2048;
Expand Down Expand Up @@ -293,7 +295,7 @@ private void InitializeRequestIO()

if (RequestHeaders.ContentLength > MaxRequestBodySize)
{
BadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
}

HasStartedConsumingRequestBody = true;
Expand Down
2 changes: 2 additions & 0 deletions src/Servers/IIS/IIS/src/Core/IISHttpContextOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Microsoft.AspNetCore.Server.IIS.Core
{
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;

internal class IISHttpContextOfT<TContext> : IISHttpContext
{
private readonly IHttpApplication<TContext> _application;
Expand Down
36 changes: 36 additions & 0 deletions src/Servers/IIS/IIS/src/IISBadHttpRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Server.IIS
{
internal static class IISBadHttpRequestException
{
internal static void Throw(RequestRejectionReason reason)
{
throw GetException(reason);
}

[MethodImpl(MethodImplOptions.NoInlining)]
#pragma warning disable CS0618 // Type or member is obsolete
internal static BadHttpRequestException GetException(RequestRejectionReason reason)
{
BadHttpRequestException ex;
switch (reason)
{
case RequestRejectionReason.RequestBodyTooLarge:
ex = new BadHttpRequestException(CoreStrings.BadRequest_RequestBodyTooLarge, StatusCodes.Status413PayloadTooLarge, reason);
break;
default:
ex = new BadHttpRequestException(CoreStrings.BadRequest, StatusCodes.Status400BadRequest, reason);
break;
}
return ex;
}
#pragma warning restore CS0618 // Type or member is obsolete
}
}
10 changes: 8 additions & 2 deletions src/Servers/IIS/IIS/test/IIS.Tests/MaxRequestBodySizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;

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

BadHttpRequestException exception = null;

using (var testServer = await TestServer.Create(
async ctx =>
{
Expand Down Expand Up @@ -60,6 +62,7 @@ public async Task RequestBodyTooLargeContentLengthExceedingPerRequestLimit()
var perRequestMaxRequestBodySize = 0x100;

BadHttpRequestException exception = null;

using (var testServer = await TestServer.Create(
async ctx =>
{
Expand All @@ -71,6 +74,7 @@ public async Task RequestBodyTooLargeContentLengthExceedingPerRequestLimit()

await ctx.Request.Body.ReadAsync(new byte[2000]);
}

catch (BadHttpRequestException ex)
{
exception = ex;
Expand Down Expand Up @@ -266,6 +270,7 @@ public async Task RequestBodyTooLargeChunked()
var maxRequestSize = 0x1000;

BadHttpRequestException exception = null;

using (var testServer = await TestServer.Create(
async ctx =>
{
Expand Down Expand Up @@ -307,13 +312,14 @@ public async Task EveryReadFailsWhenContentLengthHeaderExceedsGlobalLimit()
{
BadHttpRequestException requestRejectedEx1 = null;
BadHttpRequestException requestRejectedEx2 = null;

using (var testServer = await TestServer.Create(
async ctx =>
{
var buffer = new byte[1];
requestRejectedEx1 = await Assert.ThrowsAsync<BadHttpRequestException>(
requestRejectedEx1 = await Assert.ThrowsAnyAsync<BadHttpRequestException>(
async () => await ctx.Request.Body.ReadAsync(buffer, 0, 1));
requestRejectedEx2 = await Assert.ThrowsAsync<BadHttpRequestException>(
requestRejectedEx2 = await Assert.ThrowsAnyAsync<BadHttpRequestException>(
async () => await ctx.Request.Body.ReadAsync(buffer, 0, 1));
throw requestRejectedEx2;
}, LoggerFactory, new IISServerOptions { MaxRequestBodySize = 0 }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public void Load() { }
}
namespace Microsoft.AspNetCore.Server.Kestrel.Core
{
public sealed partial class BadHttpRequestException : System.IO.IOException
[System.ObsoleteAttribute("Moved to Microsoft.AspNetCore.Http.BadHttpRequestException")]
public sealed partial class BadHttpRequestException : Microsoft.AspNetCore.Http.BadHttpRequestException
{
internal BadHttpRequestException() { }
public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
internal BadHttpRequestException() : base (default(string), default(int)) { }
public new int StatusCode { get { throw null; } }
}
public partial class Http2Limits
{
Expand Down
Loading