Skip to content

Commit de85eb2

Browse files
Metrics parity.
1 parent 4aa24e4 commit de85eb2

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,34 @@ protected override bool TryParseRequest(ReadResult result, out bool endConnectio
845845
{
846846
var reader = new SequenceReader<byte>(result.Buffer);
847847

848-
// Use non-throwing parser path for performance
849-
var parseResult = TryParseRequestNoThrow(ref reader);
848+
// Use non-throwing parser path for performance.
849+
// Note: The parser itself doesn't throw, but handler callbacks (OnStartLine, OnHeader)
850+
// can still throw BadHttpRequestException for semantic errors (e.g., invalid request target).
851+
// We catch those here to ensure metrics are recorded via OnBadRequest.
852+
HttpParseResult parseResult;
853+
try
854+
{
855+
parseResult = TryParseRequestNoThrow(ref reader);
856+
}
857+
#pragma warning disable CS0618 // Type or member is obsolete
858+
catch (BadHttpRequestException ex)
859+
{
860+
OnBadRequest(result.Buffer, ex);
861+
862+
Input.AdvanceTo(reader.Position, result.Buffer.End);
863+
864+
SetBadRequestState(ex);
865+
endConnection = true;
866+
return true;
867+
}
868+
#pragma warning restore CS0618 // Type or member is obsolete
869+
catch (Exception)
870+
{
871+
// Record OtherError for unexpected exceptions that escape the parser
872+
KestrelMetrics.AddConnectionEndReason(MetricsContext, ConnectionEndReason.OtherError);
873+
throw;
874+
}
875+
850876
var isConsumed = parseResult.IsComplete;
851877

852878
// Handle parse errors without exceptions

0 commit comments

Comments
 (0)