diff --git a/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs b/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs index 296a38fc83..eaa1b0e345 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Extensions/RewriteMiddlewareLoggingExtensions.cs @@ -18,6 +18,7 @@ namespace Microsoft.AspNetCore.Rewrite.Logging private static readonly Action _redirectedToHttps; private static readonly Action _redirectSummary; private static readonly Action _rewriteSummary; + private static readonly Action _abortedRequest; static RewriteMiddlewareLoggingExtensions() { @@ -70,6 +71,11 @@ namespace Microsoft.AspNetCore.Rewrite.Logging LogLevel.Information, 10, "Request was rewritten to {rewrittenUrl}"); + + _abortedRequest = LoggerMessage.Define( + LogLevel.Debug, + 11, + "Request to {requestedUrl} was aborted"); } public static void RewriteMiddlewareRequestContinueResults(this ILogger logger, string currentUrl) @@ -121,5 +127,10 @@ namespace Microsoft.AspNetCore.Rewrite.Logging { _rewriteSummary(logger, rewrittenUrl, null); } + + public static void AbortedRequest(this ILogger logger, string requestedUrl) + { + _abortedRequest(logger, requestedUrl, null); + } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs index 172c5ea457..311a503cca 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs @@ -52,7 +52,8 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite _action = new RedirectAction(statusCode, url, appendQueryString); break; case ActionType.AbortRequest: - throw new NotImplementedException("Abort Requests are not implemented"); + _action = new AbortAction(); + break; case ActionType.CustomResponse: throw new NotImplementedException("Custom Responses are not implemented"); } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/AbortAction.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/AbortAction.cs new file mode 100644 index 0000000000..e0ac36396c --- /dev/null +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/AbortAction.cs @@ -0,0 +1,17 @@ +// 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 Microsoft.AspNetCore.Rewrite.Logging; + +namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions +{ + public class AbortAction : UrlAction + { + public override void ApplyAction(RewriteContext context, MatchResults ruleMatch, MatchResults condMatch) + { + context.HttpContext.Abort(); + context.Result = RuleResult.EndResponse; + context.Logger?.AbortedRequest(context.HttpContext.Request.Path + context.HttpContext.Request.QueryString); + } + } +} diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/RewriteAction.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/RewriteAction.cs index f6637747d5..bd9a7fd63b 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/RewriteAction.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/RewriteAction.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions bool queryStringDelete, bool escapeBackReferences) { - // For the replacement, we must have at least + // For the replacement, we must have at least // one segment (cannot have an empty replacement) Result = result; Url = pattern; diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/UrlActions/AbortActionTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlActions/AbortActionTests.cs new file mode 100644 index 0000000000..e39e58fb84 --- /dev/null +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/UrlActions/AbortActionTests.cs @@ -0,0 +1,22 @@ +// 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 Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite.Internal.UrlActions; +using Xunit; + +namespace Microsoft.AspNetCore.Rewrite.Tests.UrlActions +{ + public class AbortActionTests + { + public void AbortAction_VerifyEndResponseResult() + { + var context = new RewriteContext { HttpContext = new DefaultHttpContext() }; + var action = new AbortAction(); + + action.ApplyAction(context, null, null); + + Assert.Equal(RuleResult.EndResponse, context.Result); + } + } +}