Repository: dotnet/aspnetcore
File: src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs (C#)

Code before:
                && requestData.Length >= PrefaceLineLength)
            {
                var clientPrefaceRequestLine = Http2.Http2Connection.ClientPreface.Slice(0, PrefaceLineLength);
                var currentRequestLine = requestData.Slice(0, PrefaceLineLength).ToSpan();
                if (currentRequestLine.SequenceEqual(clientPrefaceRequestLine))
                {
                    Log.PossibleInvalidHttpVersionDetected(ConnectionId, Http.HttpVersion.Http11, Http.HttpVersion.Http2);

                    // Can't write GOAWAY here. Set flag so TryProduceInvalidRequestResponse writes GOAWAY.
                    _http2PrefaceDetected = true;
                }
            }
        }
    }

    protected override Task TryProduceInvalidRequestResponse()
    {
        if (_http2PrefaceDetected)
        {
            _context.Transport.Output.Write(Http2GoAwayHttp11RequiredBytes);
            return _context.Transport.Output.FlushAsync().GetAsTask();
        }

        // Fast path: if the app hasn't started processing yet, use static pre-allocated response
        // fragments to avoid the overhead of normal response machinery (header formatting, etc.)
        // Skip fast path for responses that need custom headers (e.g., 405 with Allow header).
#pragma warning disable CS0618 // Type or member is obsolete
        if (_requestProcessingStatus < RequestProcessingStatus.AppStarted && !_connectionAborted
            && (_requestRejectedException is not BadHttpRequestException kestrelEx
                || StringValues.IsNullOrEmpty(kestrelEx.AllowedHeader)))
#pragma warning restore CS0618 // Type or member is obsolete
        {
            var output = _context.Transport.Output;
            output.Write(GetStaticErrorResponsePrefix());
            output.Write(ServiceContext.DateHeaderValueManager.GetDateHeaderValues().Bytes);
            if (ServerOptions.AddServerHeader)
            {
                output.Write(ServerHeaderBytes);
            }
            output.Write(ErrorResponseSuffix);
            return output.FlushAsync().GetAsTask();
        }

        return base.TryProduceInvalidRequestResponse();
    }

    /// <summary>
    /// Gets the appropriate static error response prefix (status line) based on the rejection exception.
    /// </summary>
    private ReadOnlySpan<byte> GetStaticErrorResponsePrefix()
    {

Review comment (suggestion):
```suggestion
        // Skip fast path for responses that need custom headers (e.g., 405 with AllowedHeader set).
```

Code after:
            if (_requestCount == 1
                && requestData.Length >= PrefaceLineLength)
            {
                var clientPrefaceRequestLine = Http2.Http2Connection.ClientPreface.Slice(0, PrefaceLineLength);
                var currentRequestLine = requestData.Slice(0, PrefaceLineLength).ToSpan();
                if (currentRequestLine.SequenceEqual(clientPrefaceRequestLine))
                {
                    Log.PossibleInvalidHttpVersionDetected(ConnectionId, Http.HttpVersion.Http11, Http.HttpVersion.Http2);

                    // Can't write GOAWAY here. Set flag so TryProduceInvalidRequestResponse writes GOAWAY.
                    _http2PrefaceDetected = true;
                }
            }
        }
    }

    protected override Task TryProduceInvalidRequestResponse()
    {
        if (_http2PrefaceDetected)
        {
            _context.Transport.Output.Write(Http2GoAwayHttp11RequiredBytes);
            return _context.Transport.Output.FlushAsync().GetAsTask();
        }

        // Fast path: if the app hasn't started processing yet, use static pre-allocated response
        // fragments to avoid the overhead of normal response machinery (header formatting, etc.)
        // Skip fast path for responses that need custom headers (e.g., 405 with AllowedHeader set).
#pragma warning disable CS0618 // Type or member is obsolete
        if (_requestProcessingStatus < RequestProcessingStatus.AppStarted && !_connectionAborted
            && (_requestRejectedException is not BadHttpRequestException kestrelEx
                || StringValues.IsNullOrEmpty(kestrelEx.AllowedHeader)))
#pragma warning restore CS0618 // Type or member is obsolete
        {
            var output = _context.Transport.Output;
            output.Write(GetStaticErrorResponsePrefix());
            output.Write(ServiceContext.DateHeaderValueManager.GetDateHeaderValues().Bytes);
            if (ServerOptions.AddServerHeader)
            {
                output.Write(ServerHeaderBytes);
            }
            output.Write(ErrorResponseSuffix);
            return output.FlushAsync().GetAsTask();
        }

        return base.TryProduceInvalidRequestResponse();
    }

    /// <summary>
    /// Gets the appropriate static error response prefix (status line) based on the rejection exception.
    /// </summary>
    private ReadOnlySpan<byte> GetStaticErrorResponsePrefix()