From 2558a8ff33cd5b39e1273508d757db8443daaf27 Mon Sep 17 00:00:00 2001
From: Francesco Nigro <nigro.fra@gmail.com>
Date: Fri, 5 Jan 2024 21:01:27 +0100
Subject: [PATCH] Save HTTP 2 pseudo-header lower-case validation

Motivation:

pseudo-header constants doesn't requires any further validation

Modifications:

anticipate pseudoheader lookup on http 2 header name validation, saving any further validation, if positive

Result:

Faster validations over known pseudo-headers
---
 .../codec/http2/DefaultHttp2Headers.java      | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java
index 60a2925873ca..eafb26336852 100644
--- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java
+++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java
@@ -24,8 +24,8 @@
 
 import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
 import static io.netty.handler.codec.http2.Http2Exception.connectionError;
-import static io.netty.handler.codec.http2.Http2Headers.PseudoHeaderName.getPseudoHeader;
 import static io.netty.handler.codec.http2.Http2Headers.PseudoHeaderName.hasPseudoHeaderFormat;
+import static io.netty.handler.codec.http2.Http2Headers.PseudoHeaderName.isPseudoHeader;
 import static io.netty.util.AsciiString.CASE_INSENSITIVE_HASHER;
 import static io.netty.util.AsciiString.CASE_SENSITIVE_HASHER;
 import static io.netty.util.AsciiString.isUpperCase;
@@ -47,6 +47,15 @@ public void validateName(CharSequence name) {
                         "empty headers are not allowed [%s]", name));
             }
 
+            if (hasPseudoHeaderFormat(name)) {
+                if (!isPseudoHeader(name)) {
+                    PlatformDependent.throwException(connectionError(
+                            PROTOCOL_ERROR, "Invalid HTTP/2 pseudo-header '%s' encountered.", name));
+                }
+                // no need for lower-case validation, we trust our own pseudo header constants
+                return;
+            }
+
             if (name instanceof AsciiString) {
                 final int index;
                 try {
@@ -72,14 +81,6 @@ public void validateName(CharSequence name) {
                     }
                 }
             }
-
-            if (hasPseudoHeaderFormat(name)) {
-                final Http2Headers.PseudoHeaderName pseudoHeader = getPseudoHeader(name);
-                if (pseudoHeader == null) {
-                    PlatformDependent.throwException(connectionError(
-                            PROTOCOL_ERROR, "Invalid HTTP/2 pseudo-header '%s' encountered.", name));
-                }
-            }
         }
     };
 
