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

Code before:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Diagnostics;
using System.IO.Pipelines;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;

internal partial class Http1Connection : HttpProtocol, IRequestProcessor, IHttpOutputAborter
{
    internal static ReadOnlySpan<byte> Http2GoAwayHttp11RequiredBytes => [0, 0, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13];

    // Pre-allocated static error response fragments for fast-path bad request handling.
    // Split into prefix (status + Content-Length + Connection) and suffix (final CRLF)
    // so we can insert the cached Date header value between them without allocating.
    // Header order matches the normal response path: Content-Length, Connection, Date.
    private static ReadOnlySpan<byte> BadRequestResponsePrefix =>
        "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> HttpVersionNotSupportedResponsePrefix =>
        "HTTP/1.1 505 HTTP Version Not Supported\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> RequestTimeoutResponsePrefix =>
        "HTTP/1.1 408 Request Timeout\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> HeadersTooLargeResponsePrefix =>
        "HTTP/1.1 431 Request Header Fields Too Large\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> RequestLineTooLongResponsePrefix =>
        "HTTP/1.1 414 URI Too Long\r\nContent-Length: 0\r\nConnection: close"u8;
    // Suffix is just the final CRLF to end headers. Date header is written between prefix and suffix.
    private static ReadOnlySpan<byte> ErrorResponseSuffix => "\r\n\r\n"u8;
    private static ReadOnlySpan<byte> ServerHeaderBytes => "\r\nServer: Kestrel"u8;

    private const byte ByteCR = (byte)'\r';
    private const byte ByteLF = (byte)'\n';
    private const byte ByteAsterisk = (byte)'*';
    private const byte ByteForwardSlash = (byte)'/';
    private const string Asterisk = "*";
    private const string ForwardSlash = "/";

    private readonly HttpConnectionContext _context;

Review comment (suggestion):
Should we add a comment here and in `KestrelBadHttpRequestException.GetException` about considering updating the pre-allocated section if we add another error status code.

Code after:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Diagnostics;
using System.IO.Pipelines;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;

internal partial class Http1Connection : HttpProtocol, IRequestProcessor, IHttpOutputAborter
{
    internal static ReadOnlySpan<byte> Http2GoAwayHttp11RequiredBytes => [0, 0, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13];

    // Pre-allocated static error response fragments for fast-path bad request handling.
    // Split into prefix (status + Content-Length + Connection) and suffix (final CRLF)
    // so we can insert the cached Date header value between them without allocating.
    // Keep in sync with KestrelBadHttpRequestException.GetException().
    // Header order matches the normal response path: Content-Length, Connection, Date.
    private static ReadOnlySpan<byte> BadRequestResponsePrefix =>
        "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> HttpVersionNotSupportedResponsePrefix =>
        "HTTP/1.1 505 HTTP Version Not Supported\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> RequestTimeoutResponsePrefix =>
        "HTTP/1.1 408 Request Timeout\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> HeadersTooLargeResponsePrefix =>
        "HTTP/1.1 431 Request Header Fields Too Large\r\nContent-Length: 0\r\nConnection: close"u8;
    private static ReadOnlySpan<byte> RequestLineTooLongResponsePrefix =>
        "HTTP/1.1 414 URI Too Long\r\nContent-Length: 0\r\nConnection: close"u8;
    // Suffix is just the final CRLF to end headers. Date header is written between prefix and suffix.
    private static ReadOnlySpan<byte> ErrorResponseSuffix => "\r\n\r\n"u8;
    private static ReadOnlySpan<byte> ServerHeaderBytes => "\r\nServer: Kestrel"u8;

    private const byte ByteCR = (byte)'\r';
    private const byte ByteLF = (byte)'\n';
    private const byte ByteAsterisk = (byte)'*';
    private const byte ByteForwardSlash = (byte)'/';
    private const string Asterisk = "*";
    private const string ForwardSlash = "/";