コード例 #1
0
        /// <summary>
        /// Proxies a request inside of a controller's method body from the request on the controller's route.
        /// </summary>
        /// <param name="controller">The ASP.NET <see cref="ControllerBase"/>.</param>
        /// <param name="httpEndpoint">The HTTP endpoint to use.</param>
        /// <param name="wsEndpoint">The WS endpoint to use.</param>
        /// <param name="httpProxyOptions">The HTTP options.</param>
        /// <param name="wsProxyOptions">The WS options.</param>
        /// <returns>A <see cref="Task"/> which completes when the request has been successfully proxied and written to the response.</returns>
        public static Task ProxyAsync(this ControllerBase controller, string httpEndpoint, string wsEndpoint, HttpProxyOptions httpProxyOptions = null, WsProxyOptions wsProxyOptions = null)
        {
            var httpProxy = new HttpProxy((c, a) => new ValueTask <string>(httpEndpoint), httpProxyOptions);
            var wsProxy   = new WsProxy((c, a) => new ValueTask <string>(wsEndpoint), wsProxyOptions);
            var proxy     = new Builders.Proxy(null, httpProxy, wsProxy);

            return(controller.HttpContext.ExecuteProxyOperationAsync(proxy));
        }
コード例 #2
0
        internal static async Task ExecuteProxyOperationAsync(this HttpContext context, Builders.Proxy proxy)
        {
            var isWebSocket = context.WebSockets.IsWebSocketRequest;

            if (isWebSocket && proxy.WsProxy != null)
            {
                await context.ExecuteWsProxyOperationAsync(proxy.WsProxy).ConfigureAwait(false);

                return;
            }

            if (!isWebSocket && proxy.HttpProxy != null)
            {
                await context.ExecuteHttpProxyOperationAsync(proxy.HttpProxy).ConfigureAwait(false);

                return;
            }

            var requestType = isWebSocket ? "WebSocket" : "HTTP(S)";

            // If the failures are not caught, then write a generic response.
            context.Response.StatusCode = 502 /* BAD GATEWAY */;
            await context.Response.WriteAsync($"Request could not be proxied.\n\nThe {requestType} request cannot be proxied because the underlying proxy definition does not have a definition of that type.").ConfigureAwait(false);
        }