Skip to content
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

[Bug Report]: Web UI is trying to handle resources that do not belong to it #150

Open
1 task done
luispfgarces opened this issue Jul 21, 2023 · 1 comment
Open
1 task done
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@luispfgarces
Copy link
Contributor

Prerequisites

  • I have searched issues to ensure it has not already been reported

Description

When performing an HTTP request to another middleware (in this case, it happened requesting to a resource owned by the KafkaFlow middleware), the Rules.Framework Web UI tries to handle it and fails with an exception.

Steps to reproduce

  1. Register Web UI middleware.
  2. Register other middlewares after.
  3. Run the app.
  4. Perform a request that is supposed to be handled by other middleware.

Expected behavior

The Web UI should not try to handle resources that are not its' responsibility.

Actual behavior

When trying to access another resource that does not belong to Rules.Framework Web UI (from KafkaFlow UI in this case), an exception is thrown:

    System.InvalidOperationException: StatusCode cannot be set because the response has already started.
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowResponseAlreadyStartedException(String value)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.set_StatusCode(Int32 value)
       at KafkaFlow.Admin.Dashboard.ApplicationBuilderExtensions.<>c__DisplayClass1_2.<<UseKafkaFlowDashboard>b__11>d.MoveNext()
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
       at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.InvokeCore(HttpContext context, String matchedPath, String remainingPath)
       at Rules.Framework.WebUI.WebUIMiddleware.InvokeAsync(HttpContext httpContext)
       at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
       at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
       at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
       at Farfetch.Framework.PingPong.AspNetCore.PingPongMiddleware.Invoke(HttpContext httpContext)
       at Farfetch.Framework.PingPong.AspNetCore.PingPongMiddleware.Invoke(HttpContext httpContext)
       at Farfetch.Framework.PingPong.AspNetCore.PingPongMiddleware.Invoke(HttpContext httpContext)
       at Prometheus.HttpMetrics.HttpInProgressMiddleware.Invoke(HttpContext context)
       at Farfetch.Framework.Monitoring.WebMetrics.AspNetCore.Middlewares.HttpRequestDurationMiddleware.Invoke(HttpContext context)
       at Farfetch.Framework.Monitoring.WebMetrics.AspNetCore.Middlewares.HttpRequestCountMiddleware.Invoke(HttpContext context)
       at NewRelic.Providers.Wrapper.AspNetCore.WrapPipelineMiddleware.Invoke(HttpContext context)

Rules Framework version

2.0.2

@luispfgarces luispfgarces added the bug Something isn't working label Jul 21, 2023
@luispfgarces
Copy link
Contributor Author

Proposed solution:

  1. Move the handling of the next middleware to InvokeAsync.
  2. Add an additional filter resource segment to StaticFileMiddleware configuration.
private async Task<bool> ExecuteHandlersAsync(HttpContext httpContext)
{
    var results = this.httpRequestHandlers.Select(d => d
          .HandleAsync(httpContext.Request, httpContext.Response, this.next));

    var handle = await Task.WhenAll(results).ConfigureAwait(false);
    return handle.All(d => !d);
}
public async Task InvokeAsync(HttpContext httpContext)
{
    var anyHandlerExecuted = await this.ExecuteHandlersAsync(httpContext).ConfigureAwait(false);
    if (!anyHandlerExecuted)
    {
        await this.next(httpContext).ConfigureAwait(false);
        if (!httpContext.Response.HasStarted)
        {
            await this.ExecuteStaticFileMiddlewareAsync(httpContext).ConfigureAwait(true);
        }
    }
}

We can change the StaticFileMiddleware configuration like this:

var staticFileOptions = new StaticFileOptions
{
    RequestPath = string.IsNullOrEmpty(options.RoutePrefix) ? "/rules-static" : $"/{options.RoutePrefix}/rules-static",
    FileProvider = provider,
    ServeUnknownFileTypes = true
};

But we have to check other files in the Web UI that are using static files and include the /rules-static resource segment.

@jdromeiro jdromeiro added the help wanted Extra attention is needed label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants