You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2025/01/10 08:46:32 UTC

(tomcat) branch 11.0.x updated (4efa851446 -> 3f3998dc76)

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

markt pushed a change to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


    from 4efa851446 Avoid caching bogus 0 content length
     new 607dfe52c8 Refactor so the buffered data is used directly rather than copied
     new 3f3998dc76 Replace the unused buffer with a zero length, static instance.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 java/org/apache/catalina/connector/InputBuffer.java   | 19 ++++++++++++++-----
 .../http11/filters/SavedRequestInputFilter.java       |  9 ++++-----
 webapps/docs/changelog.xml                            |  9 +++++++++
 3 files changed, 27 insertions(+), 10 deletions(-)


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


(tomcat) 01/02: Refactor so the buffered data is used directly rather than copied

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 607dfe52c8b7e1f03236ae5a0f7baeb3389d377a
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Jan 9 14:01:18 2025 +0000

    Refactor so the buffered data is used directly rather than copied
    
    This also aligns with other uses of o.a.catalina.connector.InputBuffer
    where a pre-populated buffer replaces InputBuffer#bb rather than the
    data being copied into the existing InputBuffer#bb
---
 .../apache/coyote/http11/filters/SavedRequestInputFilter.java    | 9 ++++-----
 webapps/docs/changelog.xml                                       | 4 ++++
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/coyote/http11/filters/SavedRequestInputFilter.java b/java/org/apache/coyote/http11/filters/SavedRequestInputFilter.java
index 9bead7331e..84b6ae8f7b 100644
--- a/java/org/apache/coyote/http11/filters/SavedRequestInputFilter.java
+++ b/java/org/apache/coyote/http11/filters/SavedRequestInputFilter.java
@@ -49,11 +49,10 @@ public class SavedRequestInputFilter implements InputFilter {
             return -1;
         }
 
-        ByteBuffer byteBuffer = handler.getByteBuffer();
-        byteBuffer.position(byteBuffer.limit()).limit(byteBuffer.capacity());
-        input.subtract(byteBuffer);
-
-        return byteBuffer.remaining();
+        int len = input.getLength();
+        handler.setByteBuffer(ByteBuffer.wrap(input.getBytes(), input.getStart(), len));
+        input.setStart(input.getEnd());
+        return len;
     }
 
     /**
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 4ed3acdc65..3d12fb0c8e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -144,6 +144,10 @@
         processing poller events for the NIO Connector as this may occur in
         normal usage. (markt)
       </fix>
+      <scode>
+        Refactor the <code>SavedRequestInputFilter</code> so the buffered data
+        is used directly rather than copied. (markt)
+      </scode>
     </changelog>
   </subsection>
 </section>


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


(tomcat) 02/02: Replace the unused buffer with a zero length, static instance.

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3f3998dc76324369701a85198d0685c20f0ca487
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Jan 9 15:19:00 2025 +0000

    Replace the unused buffer with a zero length, static instance.
---
 java/org/apache/catalina/connector/InputBuffer.java | 19 ++++++++++++++-----
 webapps/docs/changelog.xml                          |  5 +++++
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/connector/InputBuffer.java b/java/org/apache/catalina/connector/InputBuffer.java
index 45d64bc8e5..6d5db65ec1 100644
--- a/java/org/apache/catalina/connector/InputBuffer.java
+++ b/java/org/apache/catalina/connector/InputBuffer.java
@@ -56,6 +56,8 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A
 
     private static final Log log = LogFactory.getLog(InputBuffer.class);
 
+    private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
+
     public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
 
     // The buffer can be used for byte[] and char[] reading
@@ -72,8 +74,11 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A
 
     // ----------------------------------------------------- Instance Variables
 
-    /**
-     * The byte buffer.
+    /*
+     * The byte buffer. Data is always injected into this class by calling {@link #setByteBuffer(ByteBuffer)} rather
+     * than copying data into any existing buffer. It is initialised to an empty buffer as there are code paths that
+     * access the buffer when it is expected to be empty and an empty buffer gives cleaner code than lots of null
+     * checks.
      */
     private ByteBuffer bb;
 
@@ -147,8 +152,8 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A
      */
     public InputBuffer(int size, org.apache.coyote.Request coyoteRequest) {
         this.size = size;
-        bb = ByteBuffer.allocate(size);
-        clear(bb);
+        // Will be replaced when there is data to read so initialise to empty buffer.
+        bb = EMPTY_BUFFER;
         cb = CharBuffer.allocate(size);
         clear(cb);
         readLimit = size;
@@ -175,7 +180,11 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A
         }
         readLimit = size;
         markPos = -1;
-        clear(bb);
+        /*
+         * This buffer will have been replaced if there was data to read so re-initialise to an empty buffer to clear
+         * any reference to an injected buffer.
+         */
+        bb = EMPTY_BUFFER;
         closed = false;
 
         if (conv != null) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 3d12fb0c8e..5c211b1239 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -148,6 +148,11 @@
         Refactor the <code>SavedRequestInputFilter</code> so the buffered data
         is used directly rather than copied. (markt)
       </scode>
+      <scode>
+        Replace the unused buffer in
+        <code>org.apache.catalina.connector.InputBuffer</code> with a static,
+        zero length buffer. (markt)
+      </scode>
     </changelog>
   </subsection>
 </section>


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