public async Task CatchAllAsync() { await SyncContext.Clear; var host = await GetHostAsync(); var error = await forwarder.SendAsync(HttpContext, $"{config.Backend.Scheme}://{host}:{config.Backend.Port}", httpClient, forwarderRequestConfig, transformer); if (error != ForwarderError.None) { var errorFeature = HttpContext.GetForwarderErrorFeature(); var exception = errorFeature.Exception; LogError("CatchAll", exception); } }
private void ConfigureReverseProxy(IApplicationBuilder app, IHttpForwarder forwarder) { var mappings = _options.GetReverseProxyMappings(); if (mappings?.Any() == true) { var httpClient = new HttpMessageInvoker(new SocketsHttpHandler() { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false }); var requestOptions = new ForwarderRequestConfig { ActivityTimeout = TimeSpan.FromSeconds(100) }; app.UseRouting(); app.UseEndpoints(endpoints => { foreach (var mapping in mappings) { endpoints.Map(mapping.Key, async httpContext => { await forwarder.SendAsync(httpContext, mapping.Value, httpClient, requestOptions); }); } }); } }
/// <summary> /// Forwards the incoming request to the destination server, and the response back to the client. /// </summary> /// <param name="context">The HttpContext to forward.</param> /// <param name="destinationPrefix">The url prefix for where to forward the request to.</param> /// <param name="httpClient">The HTTP client used to forward the request.</param> /// <param name="requestConfig">Config for the outgoing request.</param> /// <returns>The status of a forwarding operation.</returns> public static ValueTask <ForwarderError> SendAsync(this IHttpForwarder forwarder, HttpContext context, string destinationPrefix, HttpMessageInvoker httpClient, ForwarderRequestConfig requestConfig) { if (forwarder is null) { throw new ArgumentNullException(nameof(forwarder)); } return(forwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig, HttpTransformer.Default)); }
public async Task CatchAllAsync() { var error = await forwarder.SendAsync(HttpContext, $"http://{KubeService.Dex}:5556", httpClient, new ForwarderRequestConfig(), transformer); if (error != ForwarderError.None) { var errorFeature = HttpContext.GetForwarderErrorFeature(); var exception = errorFeature.Exception; LogError("CatchAll", exception); } }
/// <summary> /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// </summary> public void Configure(IApplicationBuilder app, IHttpForwarder httpProxy) { var httpClient = new HttpMessageInvoker(new SocketsHttpHandler() { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false }); var transformBuilder = app.ApplicationServices.GetRequiredService <ITransformBuilder>(); var transformer = transformBuilder.Create(context => { context.AddQueryRemoveKey("param1"); context.AddQueryValue("area", "xx2", false); context.AddOriginalHost(false); }); // or var transformer = new CustomTransformer(); // or var transformer = HttpTransformer.Default; var requestOptions = new ForwarderRequestConfig { Timeout = TimeSpan.FromSeconds(100) }; app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.Map("/{**catch-all}", async httpContext => { await httpProxy.SendAsync(httpContext, "https://example.com", httpClient, requestOptions, transformer); var errorFeature = httpContext.GetForwarderErrorFeature(); if (errorFeature != null) { var error = errorFeature.Error; var exception = errorFeature.Exception; } }); }); }
/// <summary> /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// </summary> public void Configure(IApplicationBuilder app, IHttpForwarder forwarder) { // Configure our own HttpMessageInvoker for outbound calls for proxy operations var httpClient = new HttpMessageInvoker(new SocketsHttpHandler() { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false }); // Setup our own request transform class var transformer = new CustomTransformer(); // or HttpTransformer.Default; var requestOptions = new ForwarderRequestConfig { Timeout = TimeSpan.FromSeconds(100) }; app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.Map("/test/{**catch-all}", async httpContext => { var error = await forwarder.SendAsync(httpContext, "https://example.com", httpClient, requestOptions, (context, proxyRequest) => { // Customize the query string: var queryContext = new QueryTransformContext(context.Request); queryContext.Collection.Remove("param1"); queryContext.Collection["area"] = "xx2"; // Assign the custom uri. Be careful about extra slashes when concatenating here. proxyRequest.RequestUri = new Uri("https://example.com" + context.Request.Path + queryContext.QueryString); // Suppress the original request header, use the one from the destination Uri. proxyRequest.Headers.Host = null; return(default); }); // Check if the proxy operation was successful if (error != ForwarderError.None) { var errorFeature = httpContext.Features.Get <IForwarderErrorFeature>(); var exception = errorFeature.Exception; } }); // When using IHttpForwarder for direct forwarding you are responsible for routing, destination discovery, load balancing, affinity, etc.. // For an alternate example that includes those features see BasicYarpSample. endpoints.Map("/{**catch-all}", async httpContext => { var error = await forwarder.SendAsync(httpContext, "https://example.com", httpClient, requestOptions, transformer); // Check if the proxy operation was successful if (error != ForwarderError.None) { var errorFeature = httpContext.Features.Get <IForwarderErrorFeature>(); var exception = errorFeature.Exception; } }); });
/// <summary> /// Forwards the incoming request to the destination server, and the response back to the client. /// </summary> /// <param name="context">The HttpContext to forward.</param> /// <param name="destinationPrefix">The url prefix for where to forward the request to.</param> /// <param name="httpClient">The HTTP client used to forward the request.</param> /// <param name="requestConfig">Config for the outgoing request.</param> /// <param name="requestTransform">Transform function to apply to the forwarded request.</param> /// <returns>The status of a forwarding operation.</returns> public static ValueTask <ForwarderError> SendAsync(this IHttpForwarder forwarder, HttpContext context, string destinationPrefix, HttpMessageInvoker httpClient, ForwarderRequestConfig requestConfig, Func <HttpContext, HttpRequestMessage, ValueTask> requestTransform) { return(forwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig, new RequestTransformer(requestTransform))); }