public static async Task ProxyRequest(this HttpContext context, HttpMessageInvoker invoker, Uri destinationUri) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (destinationUri == null) { throw new ArgumentNullException(nameof(destinationUri)); } if (context.WebSockets.IsWebSocketRequest) { await context.AcceptProxyWebSocketRequest(destinationUri.ToWebSocketScheme()); } else { using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { using (var responseMessage = await context.SendProxyHttpRequest(invoker, requestMessage)) { await context.CopyProxyHttpResponse(responseMessage); } } } }
/// <summary> /// Forwards current request to the specified destination uri. /// </summary> /// <param name="context"></param> /// <param name="destinationUri">Destination Uri</param> public static async Task ProxyRequest(this HttpContext context, Uri destinationUri) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (destinationUri == null) { throw new ArgumentNullException(nameof(destinationUri)); } if (context.WebSockets.IsWebSocketRequest) { await context.AcceptProxyWebSocketRequest(destinationUri.ToWebSocketScheme()); } else { var proxyService = context.RequestServices.GetRequiredService <ProxyService>(); using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { var prepareRequestHandler = proxyService.Options.PrepareRequest; if (prepareRequestHandler != null) { await prepareRequestHandler(context.Request, requestMessage); } using (var responseMessage = await context.SendProxyHttpRequest(requestMessage)) { await context.CopyProxyHttpResponse(responseMessage); } } } }
/// <summary> /// Forwards current request to the specified destination uri. /// </summary> /// <param name="context"></param> /// <param name="destinationUri">Destination Uri</param> public static async Task ProxyRequest(this HttpContext context, Uri destinationUri) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (destinationUri == null) { throw new ArgumentNullException(nameof(destinationUri)); } if (context.WebSockets.IsWebSocketRequest) { await context.AcceptProxyWebSocketRequest(destinationUri.ToWebSocketScheme()); } else { var proxyService = context.RequestServices.GetRequiredService <ProxyService>(); using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { var prepareRequestHandler = proxyService.Options.PrepareRequest; if (prepareRequestHandler != null) { await prepareRequestHandler(context, context.Request, requestMessage); } using (var responseMessage = await context.SendProxyHttpRequest(requestMessage)) { if (responseMessage.StatusCode == HttpStatusCode.Unauthorized) { var refreshToken = proxyService.Options.RefreshToken; if (refreshToken != null) { var newToken = await refreshToken(context, context.Request, requestMessage); using (var requestMessage2 = context.CreateProxyHttpRequest(destinationUri)) { var prepareRequestHandler2 = proxyService.Options.PrepareRequestWithAccessToken; if (prepareRequestHandler2 != null) { await prepareRequestHandler2(context, context.Request, requestMessage2, newToken.Item2); } using (var responseMessage2 = await context.SendProxyHttpRequest(requestMessage2)) { await context.CopyProxyHttpResponse(responseMessage2); } } } } else { await context.CopyProxyHttpResponse(responseMessage); } } } } }
/// <summary> /// Forwards current request to the specified destination uri. /// </summary> /// <param name="context"></param> /// <param name="destinationUri">Destination Uri</param> public static async Task ProxyRequest(this HttpContext context, Uri destinationUri) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (destinationUri == null) { throw new ArgumentNullException(nameof(destinationUri)); } if (context.WebSockets.IsWebSocketRequest) { await context.AcceptProxyWebSocketRequest(destinationUri.ToWebSocketScheme()); } else { var proxyService = context.RequestServices.GetRequiredService <ProxyService>(); try { using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { var prepareRequestHandler = proxyService.Options.PrepareRequest; if (prepareRequestHandler != null) { await prepareRequestHandler(context.Request, requestMessage); } using (var responseMessage = await context.SendProxyHttpRequest(requestMessage)) { await context.CopyProxyHttpResponse(responseMessage); } } } catch (HttpRequestException ex) when((ex.InnerException as System.Net.Sockets.SocketException)?.ErrorCode == 10061) // Connection actively refused { context.Response.Clear(); context.Response.StatusCode = 503; } catch (TaskCanceledException) { context.Response.Clear(); context.Response.StatusCode = 504; } } }
/// <summary> /// Forwards current request to the specified destination uri. /// </summary> /// <param name="context"></param> /// <param name="destinationUri">Destination Uri</param> public static async Task ProxyForgeRequest(this HttpContext context, Uri destinationUri) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (destinationUri == null) { throw new ArgumentNullException(nameof(destinationUri)); } if (context.WebSockets.IsWebSocketRequest) { await context.AcceptProxyWebSocketRequest(destinationUri.ToWebSocketScheme()); } else { var proxyService = context.RequestServices.GetRequiredService <ForgeProxyService>(); using (var requestMessage = context.CreateProxyHttpRequest(destinationUri)) { //Remove Authorization header created by the Forge Viewer. if (proxyService.Options.UseServerSideAuthorization) { requestMessage.Headers.Remove("Authorization"); var token = proxyService.Token; requestMessage.Headers.Add("Authorization", string.Format("Bearer {0}", token.AccessToken)); } var prepareRequestHandler = proxyService.Options.PrepareRequest; if (prepareRequestHandler != null) { await prepareRequestHandler(context.Request, requestMessage); } using (var responseMessage = await context.SendForgeProxyHttpRequest(requestMessage)) { await context.CopyProxyHttpResponse(responseMessage); } } } }