tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

IFlurlClientBuilder.AddMiddleware cuts the chain of delegating handlers #821

Open andreikarkkanen opened 7 months ago

andreikarkkanen commented 7 months ago

Flurl.Http 4.0.2

The passing delegating handler, is typically a chain of handlers. However, AddMiddleware assigns the outer handler to the very first element in the chain of handlers, like this:

foreach (var middleware in Enumerable.Reverse(_addMiddleware).Select(create => create())) 
{
    middleware.InnerHandler = outerHandler;
    outerHandler = middleware;
}

This behavior disrupts the intended sequence of handlers in the chain. A better approach would be for AddMiddleware to follow the chain to its end before making any assignments.

while (middleware.InnerHandler is DelegatingHandler delegatingHandler)
{
    middleware = delegatingHandler;
}
middleware.InnerHandler = outerHandler;
tmenier commented 6 months ago

Thanks for reporting, appears to be legit.