internal static async Task ProxyRequest( this HttpContext context, Uri destinationUri, ProxyOptions proxyOptions, IHttpClientFactory httpClientFactory) { using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { if (proxyOptions.PrepareRequest != null) { var prepareRequestContext = new PrepareRequestContext(context.Request, context.Connection, requestMessage); proxyOptions.PrepareRequest(prepareRequestContext); } var httpClient = httpClientFactory.CreateClient("ProxyKit"); using (var responseMessage = await httpClient.SendAsync( requestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted)) { await context.CopyProxyHttpResponse(responseMessage); } } }
public static void ApplyForwardedHeader(this PrepareRequestContext prepareRequest) { var headers = prepareRequest.DestinationRequestMessage.Headers; var protocol = prepareRequest.IncomingRequest.Protocol; var @for = prepareRequest.ConnectionInfo.RemoteIpAddress; var host = prepareRequest.IncomingRequest.Host; var pathBase = prepareRequest.IncomingRequest.PathBase.Value; // TODO should be escaped? headers.ApplyForwardedHeaders(@for, host, protocol, pathBase); }
internal static async Task ProxyRequest( this HttpContext context, Uri destinationUri, ProxyOptions proxyOptions, IHttpClientFactory httpClientFactory) { using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { if (proxyOptions.PrepareRequest != null) { var prepareRequestContext = new PrepareRequestContext(context.Request, context.Connection, requestMessage); proxyOptions.PrepareRequest(prepareRequestContext); } var httpClient = httpClientFactory.CreateClient("ProxyKit"); try { using (var responseMessage = await httpClient.SendAsync( requestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted)) { await context.CopyProxyHttpResponse(responseMessage); } } catch (TaskCanceledException ex) { if (RequestHasTimedOut(ex)) { context.Response.StatusCode = 504; return; } throw; } catch (HttpRequestException ex) { if (UpstreamIsUnavailable(ex)) { context.Response.StatusCode = 503; return; } throw; } } }