public void DetectsTextContentTypes(string contentType, bool isText, string expectedEncoding)
        {
            var response = new MockResponse(200);

            response.AddHeader(new HttpHeader("Content-Type", contentType));

            Assert.AreEqual(isText, ResponseClassifier.IsTextResponse(response, out var encoding));
            Assert.AreEqual(encoding?.EncodingName, expectedEncoding);
        }
Beispiel #2
0
        private static async Task ProcessAsync(HttpPipelineMessage message, ReadOnlyMemory <HttpPipelinePolicy> pipeline, bool async)
        {
            if (!s_eventSource.IsEnabled())
            {
                if (async)
                {
                    await ProcessNextAsync(pipeline, message).ConfigureAwait(false);
                }
                else
                {
                    ProcessNext(pipeline, message);
                }
                return;
            }

            s_eventSource.Request(message.Request);

            Encoding requestTextEncoding = null;

            if (message.Request.TryGetHeader(HttpHeader.Names.ContentType, out var contentType) && IsTextContentType(contentType))
            {
                requestTextEncoding = Encoding.UTF8;
            }

            if (message.Request.Content != null)
            {
                if (requestTextEncoding != null)
                {
                    if (async)
                    {
                        await s_eventSource.RequestContentTextAsync(message.Request, requestTextEncoding, message.Cancellation);
                    }
                    else
                    {
                        s_eventSource.RequestContentText(message.Request, requestTextEncoding, message.Cancellation);
                    }
                }
                else
                {
                    if (async)
                    {
                        await s_eventSource.RequestContentAsync(message.Request, message.Cancellation);
                    }
                    else
                    {
                        s_eventSource.RequestContent(message.Request, message.Cancellation);
                    }
                }
            }

            var before = Stopwatch.GetTimestamp();

            if (async)
            {
                await ProcessNextAsync(pipeline, message).ConfigureAwait(false);
            }
            else
            {
                ProcessNext(pipeline, message);
            }

            var after = Stopwatch.GetTimestamp();

            bool isError = message.ResponseClassifier.IsErrorResponse(message.Response);

            var textResponse = ResponseClassifier.IsTextResponse(message.Response, out Encoding responseTextEncoding);

            bool wrapResponseStream = s_eventSource.ShouldLogContent(isError) && message.Response.ContentStream?.CanSeek == false;

            if (wrapResponseStream)
            {
                message.Response.ContentStream = new LoggingStream(
                    message.Response.ClientRequestId, s_eventSource, message.Response.ContentStream, isError, responseTextEncoding);
            }

            if (isError)
            {
                s_eventSource.ErrorResponse(message.Response);

                if (!wrapResponseStream && message.Response.ContentStream != null)
                {
                    if (textResponse)
                    {
                        if (async)
                        {
                            await s_eventSource.ErrorResponseContentTextAsync(message.Response, responseTextEncoding, message.Cancellation).ConfigureAwait(false);
                        }
                        else
                        {
                            s_eventSource.ErrorResponseContentText(message.Response, responseTextEncoding, message.Cancellation);
                        }
                    }
                    else
                    {
                        if (async)
                        {
                            await s_eventSource.ErrorResponseContentAsync(message.Response, message.Cancellation).ConfigureAwait(false);
                        }
                        else
                        {
                            s_eventSource.ErrorResponseContent(message.Response);
                        }
                    }
                }
            }

            s_eventSource.Response(message.Response);

            if (!wrapResponseStream && message.Response.ContentStream != null)
            {
                if (textResponse)
                {
                    await s_eventSource.ResponseContentTextAsync(message.Response, responseTextEncoding, message.Cancellation).ConfigureAwait(false);
                }
                else
                {
                    await s_eventSource.ResponseContentAsync(message.Response, message.Cancellation).ConfigureAwait(false);
                }
            }

            var elapsedMilliseconds = (after - before) * 1000 / s_frequency;

            if (elapsedMilliseconds > s_delayWarningThreshold)
            {
                s_eventSource.ResponseDelay(message.Response, elapsedMilliseconds);
            }
        }