Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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;
                }
            }
        }