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

Code before:
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = charMask(token, i) |
                    charMask(token, i + 1) |
                    charMask(token, i + 2) |
                    charMask(token, i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = charMask(token, i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    private static long charMask(CharSequence token, int i) {
        char c = token.charAt(i);
        return c <= 64 ? 1L << c : 0;
    }

    /**
     * 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
     * protocol version of the specified message. This getMethod sets or removes
     * the {@code "Connection"} header depending on what the default keep alive
     * mode of the message's protocol version is, as specified by
     * {@link HttpVersion#isKeepAliveDefault()}.
     * <ul>
     * <li>If the connection is kept alive by default:
     *     <ul>
     *     <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
     *     <li>remove otherwise.</li>

Review comment (performance):
```suggestion
        return c < 64 ? 1L << c : 0;
```

I wonder if there's some prior work on how to do this the most performant way. But for now it's most important to fix this logic, performance can come later

Code after:
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = charMask(token, i) |
                    charMask(token, i + 1) |
                    charMask(token, i + 2) |
                    charMask(token, i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = charMask(token, i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    private static long charMask(CharSequence token, int i) {
        char c = token.charAt(i);
        return c < 64 ? 1L << c : 0;
    }

    /**
     * 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
     * protocol version of the specified message. This getMethod sets or removes
     * the {@code "Connection"} header depending on what the default keep alive
     * mode of the message's protocol version is, as specified by
     * {@link HttpVersion#isKeepAliveDefault()}.
     * <ul>
     * <li>If the connection is kept alive by default:
     *     <ul>
     *     <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
     *     <li>remove otherwise.</li>