This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit ccb625b1d2c19d16ec468163182a76296e7ec0d7
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Jul 23 15:10:52 2024 +0100

    Create an HttpParser per Protocol rather than per Processor
    
    Parser configuration depends on Protocol so this reduces the number of
    HttpParser instances created. Particularly helpful for HTTP/2 which does
    not recycle Processors.
---
 java/org/apache/coyote/http11/AbstractHttp11Protocol.java | 9 +++++++++
 java/org/apache/coyote/http11/Http11Processor.java        | 9 ++-------
 java/org/apache/coyote/http2/StreamProcessor.java         | 3 +--
 webapps/docs/changelog.xml                                | 5 +++++
 4 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/java/org/apache/coyote/http11/AbstractHttp11Protocol.java 
b/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
index 9e6d358d62..89301097e0 100644
--- a/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
+++ b/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
@@ -45,6 +45,7 @@ import org.apache.coyote.http11.upgrade.UpgradeGroupInfo;
 import org.apache.coyote.http11.upgrade.UpgradeProcessorExternal;
 import org.apache.coyote.http11.upgrade.UpgradeProcessorInternal;
 import org.apache.tomcat.util.buf.StringUtils;
+import org.apache.tomcat.util.http.parser.HttpParser;
 import org.apache.tomcat.util.modeler.Registry;
 import org.apache.tomcat.util.modeler.Util;
 import org.apache.tomcat.util.net.AbstractEndpoint;
@@ -59,6 +60,7 @@ public abstract class AbstractHttp11Protocol<S> extends 
AbstractProtocol<S> {
 
     private final CompressionConfig compressionConfig = new 
CompressionConfig();
 
+    private HttpParser httpParser = null;
 
     public AbstractHttp11Protocol(AbstractEndpoint<S,?> endpoint) {
         super(endpoint);
@@ -83,6 +85,8 @@ public abstract class AbstractHttp11Protocol<S> extends 
AbstractProtocol<S> {
         for (UpgradeProtocol upgradeProtocol : upgradeProtocols) {
             upgradeProtocol.setHttp11Protocol(this);
         }
+
+        httpParser = new HttpParser(relaxedPathChars, relaxedQueryChars);
     }
 
 
@@ -121,6 +125,11 @@ public abstract class AbstractHttp11Protocol<S> extends 
AbstractProtocol<S> {
     }
 
 
+    public HttpParser getHttpParser() {
+        return httpParser;
+    }
+
+
     // ------------------------------------------------ HTTP specific 
properties
     // ------------------------------------------ managed in the 
ProtocolHandler
 
diff --git a/java/org/apache/coyote/http11/Http11Processor.java 
b/java/org/apache/coyote/http11/Http11Processor.java
index e17bdc2518..7daa79549a 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -92,9 +92,6 @@ public class Http11Processor extends AbstractProcessor {
     private final Http11OutputBuffer outputBuffer;
 
 
-    private final HttpParser httpParser;
-
-
     /**
      * Tracks how many internal filters are in the filter library so they are 
skipped when looking for pluggable
      * filters.
@@ -153,9 +150,7 @@ public class Http11Processor extends AbstractProcessor {
         super(adapter);
         this.protocol = protocol;
 
-        httpParser = new HttpParser(protocol.getRelaxedPathChars(), 
protocol.getRelaxedQueryChars());
-
-        inputBuffer = new Http11InputBuffer(request, 
protocol.getMaxHttpRequestHeaderSize(), httpParser);
+        inputBuffer = new Http11InputBuffer(request, 
protocol.getMaxHttpRequestHeaderSize(), protocol.getHttpParser());
         request.setInputBuffer(inputBuffer);
 
         outputBuffer = new Http11OutputBuffer(response, 
protocol.getMaxHttpResponseHeaderSize());
@@ -757,7 +752,7 @@ public class Http11Processor extends AbstractProcessor {
         // Validate the characters in the URI. %nn decoding will be checked at
         // the point of decoding.
         for (int i = uriBC.getStart(); i < uriBC.getEnd(); i++) {
-            if (!httpParser.isAbsolutePathRelaxed(uriB[i])) {
+            if (!protocol.getHttpParser().isAbsolutePathRelaxed(uriB[i])) {
                 badRequest("http11processor.request.invalidUri");
                 break;
             }
diff --git a/java/org/apache/coyote/http2/StreamProcessor.java 
b/java/org/apache/coyote/http2/StreamProcessor.java
index 89a6116854..6a66a793c2 100644
--- a/java/org/apache/coyote/http2/StreamProcessor.java
+++ b/java/org/apache/coyote/http2/StreamProcessor.java
@@ -473,8 +473,7 @@ class StreamProcessor extends AbstractProcessor {
      * The checks performed below are based on the checks in Http11InputBuffer.
      */
     private boolean validateRequest() {
-        HttpParser httpParser = new 
HttpParser(handler.getProtocol().getHttp11Protocol().getRelaxedPathChars(),
-                
handler.getProtocol().getHttp11Protocol().getRelaxedQueryChars());
+        HttpParser httpParser = 
handler.getProtocol().getHttp11Protocol().getHttpParser();
 
         // Method name must be a token
         String method = request.method().toString();
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 26b47b83a7..f42b648a2e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -145,6 +145,11 @@
         Ensure that HTTP/2 stream input buffers are only created when there is 
a
         request body to be read. (markt)
       </fix>
+      <scode>
+        Refactor creation of HttpParser instances from the Processor level to
+        the Protocol level since the parser configuration depends on the
+        protocol and the parser is, otherwise, stateless. (markt)
+      </scode>
     </changelog>
   </subsection>
   <subsection name="jdbc-pool">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to