Repository: netty/netty
File: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java (Java)

Code before:
                        "The HTTP method name contain illegal characters: " + method.asciiName());
            }
        }

        if (!isEncodingSafeStartLineToken(uri)) {
            throw new IllegalArgumentException("The URI contain illegal characters: " + uri);
        }
    }

    /**
     * Validate that the given request line token is safe for verbatim encoding to the network.
     * This does not fully check that the token – HTTP method, version, or URI – is valid and formatted correctly.
     * Only that the token does not contain characters that would break or
     * desynchronize HTTP message parsing of the start line wherein the token would be included.
     * <p>
     * See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-request-line">RFC 9112, 3.</a>
     *
     * @param token The token to check.
     * @return {@code true} if the token is safe to encode verbatim into the HTTP message output stream,
     * otherwise {@code false}.
     */
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int lenBytes = token.length();
        for (int i = 0; i < lenBytes; i++) {
            char ch = token.charAt(i);
            // this is to help AOT compiled code which cannot profile the switch
            if (ch <= ' ') {
                switch (ch) {
                    case '\n':
                    case '\r':
                    case ' ':
                        return false;
                }
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'. This method respects the value of the
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }

    /**

Review comment (suggestion):
I could have similarly use a lookup table with 128 boolean and handle the US-ASCII case as

If (c < 128) {
   If (invalid[c & 0x7F]) return false;
}

In case is frequent that chars are below SPACE

Code after:
            }
        }

        if (!isEncodingSafeStartLineToken(uri)) {
            throw new IllegalArgumentException("The URI contain illegal characters: " + uri);
        }
    }

    /**
     * Validate that the given request line token is safe for verbatim encoding to the network.
     * This does not fully check that the token – HTTP method, version, or URI – is valid and formatted correctly.
     * Only that the token does not contain characters that would break or
     * desynchronize HTTP message parsing of the start line wherein the token would be included.
     * <p>
     * See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-request-line">RFC 9112, 3.</a>
     *
     * @param token The token to check.
     * @return {@code true} if the token is safe to encode verbatim into the HTTP message output stream,
     * otherwise {@code false}.
     */
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int lenBytes = token.length();
        for (int i = 0; i < lenBytes; i++) {
            char ch = token.charAt(i);
            // this is to help AOT compiled code which cannot profile the switch
            if (ch <= ' ') {
                switch (ch) {
                    case '\n':
                    case '\r':
                    case ' ':
                        return false;
                }
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'. This method respects the value of the
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }

    /**
     * Sets the value of the {@code "Connection"} header depending on the