コード例 #1
0
        /// <summary>
        /// Invoked when this handler is determined to be the best suited to handle the supplied connection.
        /// </summary>
        /// <param name="context">
        /// The HTTP context.
        /// </param>
        /// <returns>
        /// The handling task.
        /// </returns>
        public override async Task Handle(HttpContext context)
        {
            Diagnostics.DiagnosticsWebSession diagSession = new Diagnostics.DiagnosticsWebSession();

            if (Diagnostics.Collector.IsDiagnosticsEnabled)
            {
                diagSession.DateStarted = DateTime.Now;
            }

            try
            {
                // Use helper to get the full, proper URL for the request.
                //var fullUrl = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(context.Request);
                var fullUrl = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(context.Request);

                // Next we need to try and parse the URL as a URI, because the websocket client
                // requires this for connecting upstream.

                if (!Uri.TryCreate(fullUrl, UriKind.RelativeOrAbsolute, out Uri reqUrl))
                {
                    LoggerProxy.Default.Error("Failed to parse HTTP URL.");
                    return;
                }

                if (context.Connection.ClientCertificate != null)
                {
                    // TODO - Handle client certificates.
                }

                bool requestHasZeroContentLength = false;

                foreach (var hdr in context.Request.Headers)
                {
                    try
                    {
                        if (hdr.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase) && hdr.Value.ToString().Equals("0"))
                        {
                            requestHasZeroContentLength = true;
                        }
                    }
                    catch { }
                }

                HttpRequestMessage requestMsg;
                diagSession.ClientRequestUri = fullUrl;

                // Let's do our first call to message begin for the request side.
                var requestMessageNfo = new HttpMessageInfo
                {
                    Url             = reqUrl,
                    Method          = new HttpMethod(context.Request.Method),
                    IsEncrypted     = context.Request.IsHttps,
                    Headers         = context.Request.Headers.ToNameValueCollection(),
                    MessageProtocol = MessageProtocol.Http,
                    MessageType     = MessageType.Request,
                    RemoteAddress   = context.Connection.RemoteIpAddress,
                    RemotePort      = (ushort)context.Connection.RemotePort,
                    LocalAddress    = context.Connection.LocalIpAddress,
                    LocalPort       = (ushort)context.Connection.LocalPort
                };

                _newMessageCb?.Invoke(requestMessageNfo);

                if (Diagnostics.Collector.IsDiagnosticsEnabled)
                {
                    diagSession.ClientRequestHeaders = context.Request.Headers.ToString();
                }

                if (requestMessageNfo.ProxyNextAction == ProxyNextAction.DropConnection)
                {
                    // Apply whatever the user did here and then quit.
                    context.Response.ClearAllHeaders();
                    await context.Response.ApplyMessageInfo(requestMessageNfo, context.RequestAborted);

                    return;
                }

                // Create the message AFTER we give the user a chance to alter things.
                requestMsg = new HttpRequestMessage(requestMessageNfo.Method, requestMessageNfo.Url);
                var initialFailedHeaders = requestMsg.PopulateHeaders(requestMessageNfo.Headers);

                // Check if we have a request body.
                if (context.Request.Body != null)
                {
                    // Now check what the user wanted to do.
                    switch (requestMessageNfo.ProxyNextAction)
                    {
                    // We have a body and the user previously instructed us to give them the
                    // content, if any, for inspection.
                    case ProxyNextAction.AllowButRequestContentInspection:
                    {
                        // Get the request body into memory.
                        using (var ms = new MemoryStream())
                        {
                            await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(context.Request.Body, ms, s_maxInMemoryData, context.RequestAborted);

                            var requestBody = ms.ToArray();

                            // If we don't have a body, there's no sense in calling the message end callback.
                            if (requestBody.Length > 0)
                            {
                                diagSession.ClientRequestBody = requestBody;

                                // We'll now call the message end function for the request side.
                                requestMessageNfo = new HttpMessageInfo
                                {
                                    Url             = reqUrl,
                                    Method          = new HttpMethod(context.Request.Method),
                                    IsEncrypted     = context.Request.IsHttps,
                                    Headers         = context.Request.Headers.ToNameValueCollection(),
                                    MessageProtocol = MessageProtocol.Http,
                                    MessageType     = MessageType.Request,
                                    RemoteAddress   = context.Connection.RemoteIpAddress,
                                    RemotePort      = (ushort)context.Connection.RemotePort,
                                    LocalAddress    = context.Connection.LocalIpAddress,
                                    LocalPort       = (ushort)context.Connection.LocalPort,
                                    BodyInternal    = requestBody
                                };

                                _wholeBodyInspectionCb?.Invoke(requestMessageNfo);

                                if (requestMessageNfo.ProxyNextAction == ProxyNextAction.DropConnection)
                                {
                                    // User wants to block this request after inspecting the content.
                                    // Apply whatever the user did here and then quit.
                                    context.Response.ClearAllHeaders();
                                    await context.Response.ApplyMessageInfo(requestMessageNfo, context.RequestAborted);

                                    return;
                                }

                                // Since the user may have modified things, we'll now re-create
                                // the request no matter what.
                                requestMsg           = new HttpRequestMessage(requestMessageNfo.Method, requestMessageNfo.Url);
                                initialFailedHeaders = requestMsg.PopulateHeaders(requestMessageNfo.Headers);

                                // Set our content, even if it's empty. Don't worry about ByteArrayContent
                                // and friends setting other headers, we're gonna blow relevant headers away
                                // below and then set them properly.
                                requestMsg.Content = new ByteArrayContent(requestBody);
                                requestMsg.Content.Headers.Clear();

                                requestMsg.Content.Headers.TryAddWithoutValidation("Content-Length", requestBody.Length.ToString());
                            }
                            else
                            {
                                if (requestHasZeroContentLength)
                                {
                                    requestMsg.Content = new ByteArrayContent(requestBody);
                                    requestMsg.Content.Headers.Clear();
                                    requestMsg.Content.Headers.TryAddWithoutValidation("Content-Length", "0");
                                }
                            }
                        }
                    }
                    break;

                    case ProxyNextAction.AllowButRequestStreamedContentInspection:
                    {
                        requestMessageNfo = new HttpMessageInfo
                        {
                            Url             = reqUrl,
                            Method          = new HttpMethod(context.Request.Method),
                            IsEncrypted     = context.Request.IsHttps,
                            Headers         = context.Request.Headers.ToNameValueCollection(),
                            MessageProtocol = MessageProtocol.Http,
                            MessageType     = MessageType.Request,
                            RemoteAddress   = context.Connection.RemoteIpAddress,
                            RemotePort      = (ushort)context.Connection.RemotePort,
                            LocalAddress    = context.Connection.LocalIpAddress,
                            LocalPort       = (ushort)context.Connection.LocalPort
                        };

                        // We have a body and the user wants to just stream-inspect it.
                        var wrappedStream = new InspectionStream(requestMessageNfo, context.Request.Body)
                        {
                            StreamRead  = OnWrappedStreamRead,
                            StreamWrite = OnWrappedStreamWrite
                        };

                        requestMsg.Content = new StreamContent(wrappedStream);
                    }
                    break;

                    default:
                    {
                        if (context.Request.ContentLength.HasValue && context.Request.ContentLength.Value > 0)
                        {
                            // We have a body, but the user doesn't want to inspect it.
                            // So, we'll just set our content to wrap the context's input
                            // stream.
                            requestMsg.Content = new StreamContent(context.Request.Body);
                        }
                    }
                    break;
                    }
                }

                // Ensure that content type is set properly because ByteArrayContent and friends will
                // modify these fields.
                // To explain these further, these headers almost always fail because
                // they apply to the .Content property only (content-specific headers),
                // so once we have a .Content property created, we'll go ahead and
                // pour over the failed headers and try to apply to them to the content.
                initialFailedHeaders = requestMsg.PopulateHeaders(initialFailedHeaders);
#if VERBOSE_WARNINGS
                foreach (string key in initialFailedHeaders)
                {
                    LoggerProxy.Default.Warn(string.Format("Failed to add HTTP header with key {0} and with value {1}.", key, initialFailedHeaders[key]));
                }
#endif

                if (Diagnostics.Collector.IsDiagnosticsEnabled)
                {
                    diagSession.ServerRequestHeaders = requestMsg.Headers.ToString();
                }

                // Lets start sending the request upstream. We're going to ask the client to return
                // control to us when the headers are complete. This way we're not buffering entire
                // responses into memory, and if the user doesn't request to inspect the content, we
                // can just async stream the content transparently and Kestrel is so cool and sweet
                // and nice, it'll automatically stream as chunked content.
                HttpResponseMessage response = null;

                try
                {
                    response = await s_client.SendAsync(requestMsg, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);

                    diagSession.StatusCode = (int)(response?.StatusCode ?? 0);
                }
                catch (HttpRequestException e)
                {
                    LoggerProxy.Default.Error(e);

                    if (e.InnerException is WebException && e.InnerException.InnerException is System.Security.Authentication.AuthenticationException)
                    {
                        requestMessageNfo = new HttpMessageInfo
                        {
                            Url             = reqUrl,
                            Method          = new HttpMethod(context.Request.Method),
                            IsEncrypted     = context.Request.IsHttps,
                            Headers         = context.Request.Headers.ToNameValueCollection(),
                            MessageProtocol = MessageProtocol.Http,
                            MessageType     = MessageType.Request,
                            RemoteAddress   = context.Connection.RemoteIpAddress,
                            RemotePort      = (ushort)context.Connection.RemotePort,
                            LocalAddress    = context.Connection.LocalIpAddress,
                            LocalPort       = (ushort)context.Connection.LocalPort,
                            BodyInternal    = null
                        };

                        _badCertificateCb?.Invoke(requestMessageNfo);

                        context.Response.ClearAllHeaders();
                        await context.Response.ApplyMessageInfo(requestMessageNfo, context.RequestAborted);

                        return;
                    }
                    else if (e.InnerException is WebException)
                    {
                        var webException = e.InnerException as WebException;

                        if (webException.Response != null)
                        {
                            diagSession.StatusCode = (int?)(webException.Response as HttpWebResponse)?.StatusCode ?? 0;
                        }
                    }
                }
                catch (Exception e)
                {
                    LoggerProxy.Default.Error(e);
                }

                if (response == null)
                {
                    return;
                }

                // Blow away all response headers. We wanna clone these now from our upstream request.
                context.Response.ClearAllHeaders();

                // Ensure our client's response status code is set to match ours.
                context.Response.StatusCode = (int)response.StatusCode;

                var responseHeaders = response.ExportAllHeaders();

                bool responseHasZeroContentLength = false;
                bool responseIsFixedLength        = false;

                foreach (var kvp in responseHeaders.ToIHeaderDictionary())
                {
                    foreach (var value in kvp.Value)
                    {
                        if (kvp.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
                        {
                            responseIsFixedLength = true;

                            if (value.Length <= 0 && value.Equals("0"))
                            {
                                responseHasZeroContentLength = true;
                            }
                        }
                    }
                }

                if (Diagnostics.Collector.IsDiagnosticsEnabled)
                {
                    diagSession.ServerResponseHeaders = responseHeaders.ToString();
                }

                // Match the HTTP version of the client on the upstream request. We don't want to
                // transparently pass around headers that are wrong for the client's HTTP version.
                Version upstreamReqVersionMatch = null;

                Match match = s_httpVerRegex.Match(context.Request.Protocol);
                if (match != null && match.Success)
                {
                    upstreamReqVersionMatch = Version.Parse(match.Value);
                    requestMsg.Version      = upstreamReqVersionMatch;
                }

                // For later reference...
                bool upstreamIsHttp1 = upstreamReqVersionMatch != null && upstreamReqVersionMatch.Major == 1 && upstreamReqVersionMatch.Minor == 0;

                // Let's call the message begin handler for the response. Unless of course, the user has asked us NOT to do this.
                if (requestMessageNfo.ProxyNextAction != ProxyNextAction.AllowAndIgnoreContentAndResponse)
                {
                    var responseMessageNfo = new HttpMessageInfo
                    {
                        Url             = reqUrl,
                        IsEncrypted     = context.Request.IsHttps,
                        Headers         = response.ExportAllHeaders(),
                        MessageProtocol = MessageProtocol.Http,
                        MessageType     = MessageType.Response,
                        RemoteAddress   = context.Connection.RemoteIpAddress,
                        RemotePort      = (ushort)context.Connection.RemotePort,
                        LocalAddress    = context.Connection.LocalIpAddress,
                        LocalPort       = (ushort)context.Connection.LocalPort
                    };

                    _newMessageCb?.Invoke(responseMessageNfo);

                    if (responseMessageNfo.ProxyNextAction == ProxyNextAction.DropConnection)
                    {
                        // Apply whatever the user did here and then quit.
                        context.Response.ClearAllHeaders();
                        await context.Response.ApplyMessageInfo(responseMessageNfo, context.RequestAborted);

                        return;
                    }

                    context.Response.ClearAllHeaders();
                    context.Response.PopulateHeaders(responseMessageNfo.Headers);

                    switch (responseMessageNfo.ProxyNextAction)
                    {
                    case ProxyNextAction.AllowButRequestContentInspection:
                    {
                        using (var upstreamResponseStream = await response.Content.ReadAsStreamAsync())
                        {
                            using (var ms = new MemoryStream())
                            {
                                await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(upstreamResponseStream, ms, s_maxInMemoryData, context.RequestAborted);

                                var responseBody = ms.ToArray();
                                diagSession.ServerResponseBody = responseBody;

                                responseMessageNfo = new HttpMessageInfo
                                {
                                    Url             = reqUrl,
                                    IsEncrypted     = context.Request.IsHttps,
                                    Headers         = response.ExportAllHeaders(),
                                    MessageProtocol = MessageProtocol.Http,
                                    MessageType     = MessageType.Response,
                                    RemoteAddress   = context.Connection.RemoteIpAddress,
                                    RemotePort      = (ushort)context.Connection.RemotePort,
                                    LocalAddress    = context.Connection.LocalIpAddress,
                                    LocalPort       = (ushort)context.Connection.LocalPort,
                                    BodyInternal    = responseBody
                                };

                                _wholeBodyInspectionCb?.Invoke(responseMessageNfo);

                                if (responseMessageNfo.ProxyNextAction == ProxyNextAction.DropConnection)
                                {
                                    // Apply whatever the user did here and then quit.
                                    context.Response.ClearAllHeaders();
                                    await context.Response.ApplyMessageInfo(responseMessageNfo, context.RequestAborted);

                                    return;
                                }

                                context.Response.ClearAllHeaders();
                                context.Response.PopulateHeaders(responseMessageNfo.Headers);

                                // User inspected but allowed the content. Just write to the response
                                // body and then move on with your life fam.
                                //
                                // However, don't try to write a body if it's zero length. Also, do
                                // not try to write a body, even if present, if the status is 204.
                                // Kestrel will not let us do this, and so far I can't find a way to
                                // remove this technically correct strict-compliance.
                                if (!responseHasZeroContentLength && (responseBody.Length > 0 && context.Response.StatusCode != 204))
                                {
                                    // If the request is HTTP1.0, we need to pull all the data so we
                                    // can properly set the content-length by adding the header in.
                                    if (upstreamIsHttp1)
                                    {
                                        context.Response.Headers.Add("Content-Length", responseBody.Length.ToString());
                                    }

                                    await context.Response.Body.WriteAsync(responseBody, 0, responseBody.Length);
                                }
                                else
                                {
                                    if (responseHasZeroContentLength)
                                    {
                                        context.Response.Headers.Add("Content-Length", "0");
                                    }
                                }

                                // Ensure we exit here, because if we fall past this scope then the
                                // response is going to get mangled.
                                return;
                            }
                        }
                    }

                    case ProxyNextAction.AllowButRequestStreamedContentInspection:
                    {
                        responseMessageNfo = new HttpMessageInfo
                        {
                            Url             = reqUrl,
                            IsEncrypted     = context.Request.IsHttps,
                            Headers         = response.ExportAllHeaders(),
                            MessageProtocol = MessageProtocol.Http,
                            MessageType     = MessageType.Response,
                            RemoteAddress   = context.Connection.RemoteIpAddress,
                            RemotePort      = (ushort)context.Connection.RemotePort,
                            LocalAddress    = context.Connection.LocalIpAddress,
                            LocalPort       = (ushort)context.Connection.LocalPort
                        };

                        using (var responseStream = await response.Content.ReadAsStreamAsync())
                        {
                            // We have a body and the user wants to just stream-inspect it.
                            using (var wrappedStream = new InspectionStream(responseMessageNfo, responseStream))
                            {
                                wrappedStream.StreamRead  = OnWrappedStreamRead;
                                wrappedStream.StreamWrite = OnWrappedStreamWrite;

                                await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(wrappedStream, context.Response.Body, null, context.RequestAborted);
                            }
                        }

                        return;
                    }
                    }
                } // if (requestMessageNfo.ProxyNextAction != ProxyNextAction.AllowAndIgnoreContentAndResponse)


                // If we made it here, then the user just wants to let the response be streamed in
                // without any inspection etc, so do exactly that.
                using (var responseStream = await response.Content.ReadAsStreamAsync())
                {
                    context.Response.StatusCode = (int)response.StatusCode;
                    context.Response.PopulateHeaders(response.ExportAllHeaders());

                    if (!responseHasZeroContentLength && (upstreamIsHttp1 || responseIsFixedLength))
                    {
                        using (var ms = new MemoryStream())
                        {
                            await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(responseStream, ms, s_maxInMemoryData, context.RequestAborted);

                            var responseBody = ms.ToArray();

                            context.Response.Headers.Remove("Content-Length");

                            context.Response.Headers.Add("Content-Length", responseBody.Length.ToString());

                            await context.Response.Body.WriteAsync(responseBody, 0, responseBody.Length);
                        }
                    }
                    else
                    {
                        context.Response.Headers.Remove("Content-Length");

                        if (responseHasZeroContentLength)
                        {
                            context.Response.Headers.Add("Content-Length", "0");
                        }
                        else
                        {
                            await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(responseStream, context.Response.Body, null, context.RequestAborted);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (!(e is TaskCanceledException) && !(e is OperationCanceledException))
                {
                    // Ignore task cancelled exceptions.
                    LoggerProxy.Default.Error(e);
                }
            }
            finally
            {
                if (Diagnostics.Collector.IsDiagnosticsEnabled)
                {
                    diagSession.DateEnded = DateTime.Now;
                    Diagnostics.Collector.ReportSession(diagSession);
                }
            }
        }
コード例 #2
0
        public override async Task Handle(HttpContext context)
        {
            // Use this to collect information about the HTTP request's server and client parts.
            Diagnostics.DiagnosticsWebSession diagSession = new Diagnostics.DiagnosticsWebSession();
            diagSession.DateStarted = DateTime.Now;

            try
            {
                // Use helper to get the full, proper URL for the request.
                //var fullUrl = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(context.Request);
                var fullUrl = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(context.Request);

                // Next we need to try and parse the URL as a URI, because the websocket client
                // requires this for connecting upstream.
                Uri reqUrl = null;

                if (!Uri.TryCreate(fullUrl, UriKind.RelativeOrAbsolute, out reqUrl))
                {
                    LoggerProxy.Default.Error("Failed to parse HTTP URL.");
                    return;
                }

                // Create a new request to send out upstream.
                var requestMsg = new HttpRequestMessage(new HttpMethod(context.Request.Method), fullUrl);
                diagSession.ClientRequestUri = fullUrl;

                if (context.Connection.ClientCertificate != null)
                {
                    // TODO - Handle client certificates.
                }

                // Build request headers into this, so we can pass the result to message begin/end callbacks.
                var reqHeaderBuilder = new StringBuilder();

                var failedInitialHeaders = new List <Tuple <string, string> >();

                bool   requestHasContentLengthHeader = false;
                bool   requestHasZeroContentLength   = false;
                string contentTypeValue = null;

                // Clone headers from the real client request to our upstream HTTP request.
                foreach (var hdr in context.Request.Headers)
                {
                    try
                    {
                        if (hdr.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
                        {
                            requestHasZeroContentLength   = hdr.Value.ToString().Equals("0");
                            requestHasContentLengthHeader = true;
                        }
                    }
                    catch { }

                    try
                    {
                        if (hdr.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
                        {
                            contentTypeValue = hdr.Value.ToString();
                        }
                    }
                    catch { }

                    try
                    {
                        reqHeaderBuilder.AppendFormat("{0}: {1}\r\n", hdr.Key, hdr.Value.ToString());
                    }
                    catch { }

                    if (ForbiddenHttpHeaders.IsForbidden(hdr.Key))
                    {
                        continue;
                    }

                    // Content-Type is typically a header that's attached to a content body.. We have to add this manual check in here because
                    // we do some nasty reflection farther down which removes Content-Type from the global invalid headers list.
                    // In other words, we can't guarantee that Content-Type is going to be found as an invalid header according to .NET
                    // because we manipulate its internal invalid header list.
                    if (hdr.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase) || !requestMsg.Headers.TryAddWithoutValidation(hdr.Key, hdr.Value.ToString()))
                    {
                        string hName  = hdr.Key != null ? hdr.Key : string.Empty;
                        string hValue = hdr.Value.ToString() != null?hdr.Value.ToString() : string.Empty;

                        if (hName.Length > 0 && hValue.Length > 0)
                        {
                            failedInitialHeaders.Add(new Tuple <string, string>(hName, hValue));
                        }
                    }
                }

                // Match the HTTP version of the client on the upstream request. We don't want to
                // transparently pass around headers that are wrong for the client's HTTP version.
                Version upstreamReqVersionMatch = null;

                Match match = s_httpVerRegex.Match(context.Request.Protocol);
                if (match != null && match.Success)
                {
                    upstreamReqVersionMatch = Version.Parse(match.Value);
                    requestMsg.Version      = upstreamReqVersionMatch;
                }

                // Add trailing CRLF to the request headers string.
                reqHeaderBuilder.Append("\r\n");

                diagSession.ClientRequestHeaders = reqHeaderBuilder.ToString();

                bool upstreamIsHttp1 = upstreamReqVersionMatch != null && upstreamReqVersionMatch.Major == 1 && upstreamReqVersionMatch.Minor == 0;

                // Since headers are complete at this stage, let's do our first call to message begin
                // for the request side.
                ProxyNextAction requestNextAction = ProxyNextAction.AllowAndIgnoreContentAndResponse;
                string          requestBlockResponseContentType = string.Empty;
                byte[]          requestBlockResponse            = null;
                m_msgBeginCb?.Invoke(reqUrl, reqHeaderBuilder.ToString(), m_nullBody, context.Request.IsHttps ? MessageType.Https : MessageType.Http, MessageDirection.Request, out requestNextAction, out requestBlockResponseContentType, out requestBlockResponse);

                if (requestNextAction == ProxyNextAction.DropConnection)
                {
                    if (requestBlockResponse != null)
                    {
                        // User wants to block this request with a custom response.
                        await DoCustomResponse(context, requestBlockResponseContentType, requestBlockResponse);
                    }
                    else
                    {
                        // User wants to block this request with a generic 204 response.
                        Do204(context);
                    }

                    return;
                }

                // Get the request body into memory.
                using (var ms = new MemoryStream())
                {
                    await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(context.Request.Body, ms, null, context.RequestAborted);

                    var requestBody = ms.ToArray();

                    // If we don't have a body, there's no sense in calling the message end callback.
                    if (requestBody.Length > 0)
                    {
                        diagSession.ClientRequestBody = requestBody;

                        // We have a body and the user previously instructed us to give them the
                        // content, if any, for inspection.
                        if (requestNextAction == ProxyNextAction.AllowButRequestContentInspection)
                        {
                            // We'll now call the message end function for the request side.
                            bool shouldBlockRequest = false;
                            requestBlockResponseContentType = string.Empty;
                            requestBlockResponse            = null;
                            m_msgEndCb?.Invoke(reqUrl, reqHeaderBuilder.ToString(), requestBody, context.Request.IsHttps ? MessageType.Https : MessageType.Http, MessageDirection.Request, out shouldBlockRequest, out requestBlockResponseContentType, out requestBlockResponse);

                            if (shouldBlockRequest)
                            {
                                // User wants to block this request after inspecting the content.

                                if (requestBlockResponse != null)
                                {
                                    // User wants to block this request with a custom response.
                                    await DoCustomResponse(context, requestBlockResponseContentType, requestBlockResponse);
                                }
                                else
                                {
                                    // User wants to block this request with a generic 204 response.
                                    Do204(context);
                                }

                                return;
                            }
                        }

                        // Set our content, even if it's empty. Don't worry about ByteArrayContent
                        // and friends setting other headers, we're gonna blow relevant headers away
                        // below and then set them properly.
                        requestMsg.Content = new ByteArrayContent(requestBody);

                        requestMsg.Content.Headers.Clear();

                        requestMsg.Content.Headers.TryAddWithoutValidation("Content-Length", requestBody.Length.ToString());
                    }
                    else
                    {
                        if (requestHasZeroContentLength)
                        {
                            requestMsg.Content = new ByteArrayContent(requestBody);
                            requestMsg.Content.Headers.Clear();
                            requestMsg.Content.Headers.TryAddWithoutValidation("Content-Length", "0");
                        }
                    }
                }

                if (contentTypeValue != null && requestMsg.Content == null)
                {
                    // FIXME: Parse out charset properly.
                    string[] contentTypeParts = contentTypeValue.Split(';');
                    for (int i = 1; i < contentTypeParts.Length; i++)
                    {
                        contentTypeParts[i] = contentTypeParts[i].Trim();
                    }

                    // This bit of reflection here is ugly-ugly-ugly. It fixes a bug where clients use and
                    // depend on the Content-Type header in a GET request to determine the return message
                    // from the server.
                    // Some alternate fixes.
                    // 1. Use .NET core (see farther down).
                    // 2. Re-implement in its entirety the HttpRequestMessage class, which is a bit overkill

                    // Note to the reader: NEVER EVER DO THIS IF THERE ARE OTHER OPTIONS.
                    var field = typeof(System.Net.Http.Headers.HttpRequestHeaders)
                                .GetField("invalidHeaders", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
                                ?? typeof(System.Net.Http.Headers.HttpRequestHeaders)
                                .GetField("s_invalidHeaders", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

                    if (field != null)
                    {
                        var invalidFields = (HashSet <string>)field.GetValue(null);

                        if (invalidFields.Contains("Content-Type"))
                        {
                            invalidFields.Remove("Content-Type");
                            LoggerProxy.Default.Info("Removing Content-Type from list of invalid headers.");
                        }
                    }
                    else
                    {
                        LoggerProxy.Default.Info("invalidHeaders fields not found.");
                    }

                    try
                    {
                        requestMsg.Headers.Add("Content-Type", contentTypeValue);
                    }
                    catch (Exception ex)
                    {
                        LoggerProxy.Default.Error(ex);
                    }

                    // This is an alternate fix, which works in .NET core 1.1 according to https://stackoverflow.com/a/44495081
                    //requestMsg.Content = new StringContent("", Encoding.UTF8, contentTypeParts[0]);
                }

                // Ensure that content type is set properly because ByteArrayContent and friends will
                // modify these fields.
                foreach (var et in failedInitialHeaders)
                {
                    if (et.Item1.Equals("Content-Type", StringComparison.OrdinalIgnoreCase) || !requestMsg.Headers.TryAddWithoutValidation(et.Item1, et.Item2))
                    {
                        if (requestMsg.Content != null)
                        {
                            if (!requestMsg.Content.Headers.TryAddWithoutValidation(et.Item1, et.Item2))
                            {
                                LoggerProxy.Default.Warn(string.Format("Failed to add HTTP header with key {0} and with value {1}.", et.Item1, et.Item2));
                            }
                        }
                    }
                }

                if (Diagnostics.Collector.IsDiagnosticsEnabled)
                {
                    diagSession.ServerRequestHeaders = requestMsg.Headers.ToString();
                }

                // Lets start sending the request upstream. We're going to as the client to return
                // control to us when the headers are complete. This way we're not buffering entire
                // responses into memory, and if the user doesn't request to inspect the content, we
                // can just async stream the content transparently and Kestrel is so cool and sweet
                // and nice, it'll automatically stream as chunked content.
                HttpResponseMessage response = null;

                try
                {
                    response = await s_client.SendAsync(requestMsg, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);

                    diagSession.StatusCode = (int)(response?.StatusCode ?? 0);
                }
                catch (HttpRequestException ex)
                {
                    LoggerProxy.Default.Error(ex.GetType().Name);
                    LoggerProxy.Default.Error(ex); // Getting an error here on portal.worldpay.us

                    if (ex.InnerException is WebException && ex.InnerException.InnerException is System.Security.Authentication.AuthenticationException)
                    {
                        if (m_onBadCertificate != null)
                        {
                            string customResponseContentType = null;
                            byte[] customResponse            = null;

                            m_onBadCertificate(reqUrl, ex, out customResponseContentType, out customResponse);

                            if (customResponse != null)
                            {
                                await DoCustomResponse(context, customResponseContentType, customResponse);

                                return;
                            }
                            else
                            {
                                Do204(context);
                            }
                        }
                    }
                    else if (ex.InnerException is WebException)
                    {
                        var webException = ex.InnerException as WebException;

                        if (webException.Response != null)
                        {
                            diagSession.StatusCode = (int?)(webException.Response as HttpWebResponse)?.StatusCode ?? 0;
                        }
                    }
                }
                catch (TaskCanceledException e)
                {
                    // Just swallow these exceptions. There doesn't seem to be any ill effects coming from these anyway.
                }
                catch (Exception e)
                {
                    LoggerProxy.Default.Error(e);
                }

                if (response == null)
                {
                    return;
                }

                // Blow away all response headers. We wanna clone these now from our upstream request.
                context.Response.Headers.Clear();

                // Ensure our client's response status code is set to match ours.
                context.Response.StatusCode = (int)response.StatusCode;

                // Build response headers into this, so we can pass the result to message begin/end callbacks.
                var resHeaderBuilder = new StringBuilder();

                bool responseHasZeroContentLength = false;
                bool responseIsFixedLength        = false;

                // Iterate over all upstream response headers. Note that response.Content.Headers is
                // not ALL headers. Headers are split up into different properties according to
                // logical grouping.
                foreach (var hdr in response.Content.Headers)
                {
                    try
                    {
                        if (hdr.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase) && hdr.Value.ToString().Equals("0"))
                        {
                            responseIsFixedLength = true;

                            if (hdr.Value.ToString().Equals("0"))
                            {
                                responseHasZeroContentLength = true;
                            }
                        }
                    }
                    catch { }

                    try
                    {
                        resHeaderBuilder.AppendFormat("{0}: {1}\r\n", hdr.Key, string.Join(", ", hdr.Value));
                    }
                    catch { }

                    if (ForbiddenHttpHeaders.IsForbidden(hdr.Key))
                    {
                        continue;
                    }

                    try
                    {
                        /*IEnumerable<string> valueEnumerable = response.Content.Headers.GetValues(hdr.Key);
                         * StringBuilder strBuilder = new StringBuilder();
                         * foreach(var value in valueEnumerable)
                         * {
                         *  strBuilder.Append($"'{value}',");
                         * }
                         *
                         * LoggerProxy.Default.Info($"{hdr.Key} ::: {strBuilder.ToString()}");*/

                        context.Response.Headers.Add(hdr.Key, new Microsoft.Extensions.Primitives.StringValues(hdr.Value.ToArray()));
                    }
                    catch (Exception e)
                    {
                        LoggerProxy.Default.Error(e);
                    }
                }

                // As mentioned above, headers are split up into different properties. We need to now
                // clone over the generic headers.
                foreach (var hdr in response.Headers)
                {
                    try
                    {
                        if (hdr.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase) && hdr.Value.ToString().Equals("0"))
                        {
                            responseIsFixedLength = true;

                            if (hdr.Value.ToString().Equals("0"))
                            {
                                responseHasZeroContentLength = true;
                            }
                        }
                    }
                    catch { }

                    try
                    {
                        resHeaderBuilder.AppendFormat("{0}: {1}\r\n", hdr.Key, string.Join(", ", hdr.Value));
                    }
                    catch { }

                    if (ForbiddenHttpHeaders.IsForbidden(hdr.Key))
                    {
                        continue;
                    }

                    try
                    {
                        context.Response.Headers.Add(hdr.Key, new Microsoft.Extensions.Primitives.StringValues(hdr.Value.ToArray()));
                    }
                    catch (Exception e)
                    {
                        LoggerProxy.Default.Error(e);
                    }
                }

                resHeaderBuilder.Append("\r\n");

                diagSession.ServerResponseHeaders = resHeaderBuilder.ToString();
                // FIXME: Sadly this proxy doesn't have access to raw server headers?

                // Now that we have response headers, let's call the message begin handler for the
                // response. Unless of course, the user has asked us NOT to do this.
                if (requestNextAction != ProxyNextAction.AllowAndIgnoreContentAndResponse)
                {
                    ProxyNextAction responseNextAction = ProxyNextAction.AllowAndIgnoreContent;
                    string          responseBlockResponseContentType = string.Empty;
                    byte[]          responseBlockResponse            = null;

                    m_msgBeginCb?.Invoke(reqUrl, resHeaderBuilder.ToString(), m_nullBody, context.Request.IsHttps ? MessageType.Https : MessageType.Http, MessageDirection.Response, out responseNextAction, out responseBlockResponseContentType, out responseBlockResponse);

                    if (responseNextAction == ProxyNextAction.DropConnection)
                    {
                        if (responseBlockResponse != null)
                        {
                            // User wants to block this response with a custom response.
                            await DoCustomResponse(context, responseBlockResponseContentType, responseBlockResponse);
                        }
                        else
                        {
                            // User wants to block this response with a generic 204 response.
                            Do204(context);
                        }
                    }

                    if (responseNextAction == ProxyNextAction.AllowButRequestContentInspection)
                    {
                        using (var upstreamResponseStream = await response.Content.ReadAsStreamAsync())
                        {
                            using (var ms = new MemoryStream())
                            {
                                await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(upstreamResponseStream, ms, null, context.RequestAborted);

                                var responseBody = ms.ToArray();
                                diagSession.ServerResponseBody = responseBody;

                                bool shouldBlockResponse = false;
                                responseBlockResponseContentType = string.Empty;
                                responseBlockResponse            = null;
                                m_msgEndCb?.Invoke(reqUrl, resHeaderBuilder.ToString(), responseBody, context.Request.IsHttps ? MessageType.Https : MessageType.Http, MessageDirection.Response, out shouldBlockResponse, out responseBlockResponseContentType, out responseBlockResponse);

                                if (shouldBlockResponse)
                                {
                                    if (responseBlockResponse != null)
                                    {
                                        // User wants to block this response with a custom response.
                                        await DoCustomResponse(context, responseBlockResponseContentType, responseBlockResponse);
                                    }
                                    else
                                    {
                                        // User wants to block this response with a generic 204 response.
                                        Do204(context);
                                    }

                                    return;
                                }

                                // User inspected but allowed the content. Just write to the response
                                // body and then move on with your life fam.
                                //
                                // However, don't try to write a body if it's zero length. Also, do
                                // not try to write a body, even if present, if the status is 204.
                                // Kestrel will not let us do this, and so far I can't find a way to
                                // remove this technically correct strict-compliance.
                                if (!responseHasZeroContentLength && (responseBody.Length > 0 && context.Response.StatusCode != 204))
                                {
                                    // If the request is HTTP1.0, we need to pull all the data so we
                                    // can properly set the content-length by adding the header in.
                                    if (upstreamIsHttp1)
                                    {
                                        context.Response.Headers.Add("Content-Length", responseBody.Length.ToString());
                                    }

                                    await context.Response.Body.WriteAsync(responseBody, 0, responseBody.Length);
                                }
                                else
                                {
                                    if (responseHasZeroContentLength)
                                    {
                                        context.Response.Headers.Add("Content-Length", "0");
                                    }
                                }

                                // Ensure we exit here, because if we fall past this scope then the
                                // response is going to get mangled.
                                return;
                            }
                        }
                    }
                }

                // If we made it here, then the user just wants to let the response be streamed in
                // without any inspection etc, so do exactly that.

                using (var responseStream = await response.Content.ReadAsStreamAsync())
                {
                    if (!responseHasZeroContentLength && (upstreamIsHttp1 || responseIsFixedLength))
                    {
                        using (var ms = new MemoryStream())
                        {
                            await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(responseStream, ms, null, context.RequestAborted);

                            var responseBody = ms.ToArray();
                            diagSession.ServerResponseBody = responseBody;

                            context.Response.Headers.Add("Content-Length", responseBody.Length.ToString());

                            await context.Response.Body.WriteAsync(responseBody, 0, responseBody.Length);
                        }
                    }
                    else
                    {
                        if (responseHasZeroContentLength)
                        {
                            context.Response.Headers.Add("Content-Length", "0");
                        }
                        else
                        {
                            await Microsoft.AspNetCore.Http.Extensions.StreamCopyOperation.CopyToAsync(responseStream, context.Response.Body, null, context.RequestAborted);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (!(e is TaskCanceledException) && !(e is OperationCanceledException))
                {
                    // Ignore task cancelled exceptions.
                    LoggerProxy.Default.Error(e);
                }
            }
            finally
            {
                diagSession.DateEnded = DateTime.Now;
                Diagnostics.Collector.ReportSession(diagSession);
            }
        }