From 7948a76f745069d9843fde03cd25bad9ca5b7c37 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Mon, 31 Oct 2016 14:19:11 -0700 Subject: [PATCH] Added support for the AbortRequest action --- .../RewriteMiddlewareLoggingExtensions.cs | 11 ++++++++++ .../IISUrlRewrite/UrlRewriteRuleBuilder.cs | 3 ++- .../Internal/UrlActions/AbortAction.cs | 17 ++++++++++++++ .../Internal/UrlActions/RewriteAction.cs | 2 +- .../UrlActions/AbortActionTests.cs | 22 +++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Rewrite/Internal/UrlActions/AbortAction.cs create mode 100644 test/Microsoft.AspNetCore.Rewrite.Tests/UrlActions/AbortActionTests.cs 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); + } + } +}