Skip to content

AspNetCore.Proxy 4.0.1

Compare
Choose a tag to compare
@twitchax twitchax released this 16 Mar 18:45
· 64 commits to master since this release

Available on NuGet.

Breaking Changes

This release makes a few breaking changes.

Static Method Attributes Removed

You can no longer proxy using the static method attributes. The feature was not widely used, and using the extension methods on a controller makes this same pattern much easier.

[ProxyRoute("api/posts/{arg1}/{arg2}")]
public static async Task<string> GetProxy(string arg1, string arg2)

Builder Pattern

The builder pattern used throughout this library now more closely matches ASP.NET Core builder patterns. For example, UseProxies now looks like this.

app.UseProxies(proxies =>
{
    // Bare string.
    proxies.Map("echo/post", proxy => proxy.UseHttp("https://postman-echo.com/post"));

    // Computed to task.
    proxies.Map("api/comments/task/{postId}", proxy => proxy.UseHttp((_, args) => new ValueTask<string>($"https://jsonplaceholder.typicode.com/comments/{args["postId"]}")));

    // Computed to string.
    proxies.Map("api/comments/string/{postId}", proxy => proxy.UseHttp((_, args) => $"https://jsonplaceholder.typicode.com/comments/{args["postId"]}"));
});

In addition, option builders can call Build to build the concrete types.

private HttpProxyOptions _httpOptions = HttpProxyOptionsBuilder.Instance
        .WithShouldAddForwardedHeaders(false)
        .Build();

Features

This release adds a few features.

WebSocket Options

The WebSocket proxies now support options.

private WsProxyOptions _wsOptions = WsProxyOptionsBuilder.Instance
        .WithBufferSize(8192)
        .WithIntercept(context => new ValueTask<bool>(context.WebSockets.WebSocketRequestedProtocols.Contains("interceptedProtocol")))
        .WithBeforeConnect((context, wso) =>
        {
            wso.AddSubProtocol("myRandomProto");
            return Task.CompletedTask;
        })
        .WithHandleFailure(async (context, e) =>
        {
            context.Response.StatusCode = 599;
            await context.Response.WriteAsync("Failure handled.");
        }).Build();