コード例 #1
0
        private async Task ProxyTheRequest(IOwinContext context, HttpRequestMessage proxyRequest, ProxyRule proxyRule)
        {
            using (var responseMessage = await _httpClient.SendAsync(proxyRequest,
                                                                     HttpCompletionOption.ResponseHeadersRead,
                                                                     context.Request.CallCancelled
                                                                     ))
            {
                if (proxyRule.PreProcessResponse || proxyRule.ResponseModifier == null)
                {
                    context.Response.StatusCode  = (int)responseMessage.StatusCode;
                    context.Response.ContentType = responseMessage.Content?.Headers.ContentType?.MediaType;

                    foreach (var header in responseMessage.Headers)
                    {
                        context.Response.Headers.SetValues(header.Key, header.Value.ToArray());
                    }

                    // SendAsync removes chunking from the response.
                    // This removes the header so it doesn't expect a chunked response.
                    context.Response.Headers.Remove("transfer-encoding");

                    if (responseMessage.Content != null)
                    {
                        foreach (var contentHeader in responseMessage.Content.Headers)
                        {
                            context.Response.Headers.SetValues(contentHeader.Key, contentHeader.Value.ToArray());
                        }

                        await responseMessage.Content.CopyToAsync(context.Response.Body);
                    }
                }

                if (proxyRule.ResponseModifier != null)
                {
                    await proxyRule.ResponseModifier.Invoke(responseMessage, context);
                }
            }
        }
コード例 #2
0
 public void AddProxyRule(ProxyRule rule)
 {
     ProxyRules.Add(rule);
 }