aspnet / BasicMiddleware

[Archived] Basic middleware components for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
169 stars 84 forks source link

Question: How to make IIS respect an AbortAction in rewrite #203

Closed davidpeden3 closed 7 years ago

davidpeden3 commented 7 years ago

I have created a catch-all rule to abort any/all requests not matched by earlier rewrite rules:

    /// <summary>
    ///     <rule name="AbortAllRequestsRule" patternSyntax="ECMAScript" stopProcessing="true">
    ///         <match url=".*" />
    ///         <action type="AbortRequest" />
    ///     </rule>
    /// </summary>
    public class AbortAllRequestsRule : IRule
    {
        private readonly IISUrlRewriteRule _rule;

        public AbortAllRequestsRule()
        {
            // create rule builder
            var ruleBuilder = new UrlRewriteRuleBuilder
            {
                Name = nameof(AbortAllRequestsRule),
                Enabled = true
            };

            // add url match
            ruleBuilder.AddUrlMatch(
                ".*",
                ignoreCase: true,
                negate: false,
                patternSyntax: PatternSyntax.ECMAScript);

            // add action
            var action = new AbortAction();
            ruleBuilder.AddUrlAction(action);

            // create rule
            _rule = ruleBuilder.Build(true);
        }

        public void ApplyRule(RewriteContext context)
        {
            _rule.ApplyRule(context);
        }
    }

In Kestrel, this properly aborts the request. However, IIS sees an aborted request, and without additional configuration, throws a very detailed 502 response (HTML page).

Adding the following snippet to web.config:

<httpErrors errorMode="Custom">
    <clear/>
</httpErrors>

Pares the verbose 502 response down to a more reasonable one:

HTTP/1.1 502 Bad Gateway
Content-Type: text/html
Server: Microsoft-IIS/8.5
Date: Fri, 03 Feb 2017 19:53:07 GMT
Content-Length: 89

The specified CGI application encountered an error and the server terminated the process.

But, is it possible to have IIS properly proxy the lack of a response from Kestrel and, itself, abort the response rather than returning a 502?

Tratcher commented 7 years ago

This issue was moved to aspnet/AspNetCoreModule#66