Exemplo n.º 1
0
        private async Task ProcessRemoteResponse(HttpContext context, HttpWebResponse remoteResponse, HostMappingOptions mapping)
        {
            if (remoteResponse == null)
            {
                _logger.LogError($"Unable to get response from {context.Request.Scheme}{context.Request.Host.ToString().ToLower()}{context.Request.Path}{context.Request.QueryString}");

                await context.Response.WriteAsync("Unable to process the request");

                return;
            }

            await _next.Invoke(context);

            LogRemoteResponse(context, remoteResponse);

            context.Response.StatusCode = (int)remoteResponse.StatusCode;

            _responseBuilder.CopyHeaders(context, remoteResponse, mapping);

            if (remoteResponse.StatusCode == HttpStatusCode.NoContent || remoteResponse.StatusCode == HttpStatusCode.Found)
            {
                return;
            }

            context.Response.Body.Write(new byte[0], 0, 0);

            using (var remoteResponseStream = remoteResponse.GetResponseStream())
            {
                remoteResponseStream?.CopyTo(context.Response.Body);
            }
        }