diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/HttpParser.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/HttpParser.cs index ba8c0260e5..a9751e245c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/HttpParser.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/HttpParser.cs @@ -5,18 +5,21 @@ using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.System; using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines; -using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http { public class HttpParser : IHttpParser where TRequestHandler : IHttpHeadersHandler, IHttpRequestLineHandler { - public HttpParser(IKestrelTrace log) + private bool _showErrorDetails; + + public HttpParser() : this(showErrorDetails: true) { - Log = log; } - private IKestrelTrace Log { get; } + public HttpParser(bool showErrorDetails) + { + _showErrorDetails = showErrorDetails; + } // byte types don't have a data type annotation so we pre-cast them; to avoid in-place casts private const byte ByteCR = (byte)'\r'; @@ -488,7 +491,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http private unsafe BadHttpRequestException GetInvalidRequestException(RequestRejectionReason reason, byte* detail, int length) => BadHttpRequestException.GetException( reason, - Log.IsEnabled(LogLevel.Information) + _showErrorDetails ? new Span(detail, length).GetAsciiStringEscaped(Constants.MaxExceptionDetailSize) : string.Empty); } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs index 8d1c26f453..2eb204c0eb 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs @@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core return new ServiceContext { Log = trace, - HttpParserFactory = frameParser => new HttpParser(frameParser.Frame.ServiceContext.Log), + HttpParserFactory = frameParser => new HttpParser(frameParser.Frame.ServiceContext.Log.IsEnabled(LogLevel.Information)), ThreadPool = threadPool, SystemClock = systemClock, DateHeaderValueManager = dateHeaderValueManager, @@ -207,4 +207,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core } } } -} \ No newline at end of file +} diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/HttpParserTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/HttpParserTests.cs index b9cc700f1a..c98818f506 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/HttpParserTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/HttpParserTests.cs @@ -417,7 +417,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests Assert.Equal(buffer.End, examined); } - private IHttpParser CreateParser(IKestrelTrace log) => new HttpParser(log); + private IHttpParser CreateParser(IKestrelTrace log) => new HttpParser(log.IsEnabled(LogLevel.Information)); public static IEnumerable RequestLineValidData => HttpParsingData.RequestLineValidData; @@ -463,4 +463,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests } } } -} \ No newline at end of file +} diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/FrameWritingBenchmark.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/FrameWritingBenchmark.cs index 3755e5df5c..13d7827c7e 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/FrameWritingBenchmark.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/FrameWritingBenchmark.cs @@ -94,7 +94,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance DateHeaderValueManager = new DateHeaderValueManager(), ServerOptions = new KestrelServerOptions(), Log = new MockTrace(), - HttpParserFactory = f => new HttpParser(log: null) + HttpParserFactory = f => new HttpParser() }; var frameContext = new FrameContext { diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/KestrelHttpParserBenchmark.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/KestrelHttpParserBenchmark.cs index c19bd6ddec..858c973400 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/KestrelHttpParserBenchmark.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/KestrelHttpParserBenchmark.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance public class KestrelHttpParserBenchmark : IHttpRequestLineHandler, IHttpHeadersHandler { - private readonly HttpParser _parser = new HttpParser(log: null); + private readonly HttpParser _parser = new HttpParser(); private ReadableBuffer _buffer; diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/RequestParsingBenchmark.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/RequestParsingBenchmark.cs index ab6b4e93d4..507e4aec80 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/RequestParsingBenchmark.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/RequestParsingBenchmark.cs @@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance { var serviceContext = new ServiceContext { - HttpParserFactory = f => new HttpParser(f.Frame.ServiceContext.Log), + HttpParserFactory = f => new HttpParser(), ServerOptions = new KestrelServerOptions() }; var frameContext = new FrameContext diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs index 9aaf5a61f5..c912dc8932 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs @@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance { var serviceContext = new ServiceContext { - HttpParserFactory = f => new HttpParser(f.Frame.ServiceContext.Log), + HttpParserFactory = f => new HttpParser(), ServerOptions = new KestrelServerOptions() }; var frameContext = new FrameContext diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeadersWritingBenchmark.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeadersWritingBenchmark.cs index 5d5db64188..c514ab2f09 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeadersWritingBenchmark.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeadersWritingBenchmark.cs @@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance DateHeaderValueManager = new DateHeaderValueManager(), ServerOptions = new KestrelServerOptions(), Log = new MockTrace(), - HttpParserFactory = f => new HttpParser(log: null) + HttpParserFactory = f => new HttpParser() }; var frameContext = new FrameContext diff --git a/test/shared/TestServiceContext.cs b/test/shared/TestServiceContext.cs index b9f3d59be6..349ef91a43 100644 --- a/test/shared/TestServiceContext.cs +++ b/test/shared/TestServiceContext.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; +using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Testing { @@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.Testing DateHeaderValueManager = new DateHeaderValueManager(SystemClock); ConnectionManager = new FrameConnectionManager(Log); DateHeaderValue = DateHeaderValueManager.GetDateHeaderValues().String; - HttpParserFactory = frameAdapter => new HttpParser(frameAdapter.Frame.ServiceContext.Log); + HttpParserFactory = frameAdapter => new HttpParser(frameAdapter.Frame.ServiceContext.Log.IsEnabled(LogLevel.Information)); ServerOptions = new KestrelServerOptions { AddServerHeader = false