[tomcat] branch 7.0.x updated: Refactor sis.available() for more accurate return value

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[tomcat] branch 7.0.x updated: Refactor sis.available() for more accurate return value

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

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


The following commit(s) were added to refs/heads/7.0.x by this push:
     new 917b3b4  Refactor sis.available() for more accurate return value
917b3b4 is described below

commit 917b3b402a512bb9a432a972499fd49b5f984cb3
Author: Mark Thomas <[hidden email]>
AuthorDate: Mon Jul 6 19:33:00 2020 +0100

    Refactor sis.available() for more accurate return value
---
 java/org/apache/coyote/InputBuffer.java                    |  9 +++++++++
 java/org/apache/coyote/ajp/AbstractAjpProcessor.java       | 14 +++++++++-----
 java/org/apache/coyote/http11/InputFilter.java             | 11 +----------
 java/org/apache/coyote/http11/InternalAprInputBuffer.java  | 14 ++++++++++----
 java/org/apache/coyote/http11/InternalInputBuffer.java     | 14 ++++++++++----
 java/org/apache/coyote/http11/InternalNioInputBuffer.java  | 14 ++++++++++----
 .../apache/coyote/http11/filters/BufferedInputFilter.java  |  9 +++++++--
 .../apache/coyote/http11/filters/ChunkedInputFilter.java   |  8 +++++++-
 .../apache/coyote/http11/filters/IdentityInputFilter.java  |  3 ++-
 webapps/docs/changelog.xml                                 |  9 +++++++++
 10 files changed, 74 insertions(+), 31 deletions(-)

diff --git a/java/org/apache/coyote/InputBuffer.java b/java/org/apache/coyote/InputBuffer.java
index a38cc5c..cbb502f 100644
--- a/java/org/apache/coyote/InputBuffer.java
+++ b/java/org/apache/coyote/InputBuffer.java
@@ -45,4 +45,13 @@ public interface InputBuffer {
         throws IOException;
 
 
+    /**
+     * Obtain an estimate of the number of bytes that can be read without
+     * blocking. Typically, this will be the number of available bytes known to
+     * be buffered.
+     *
+     * @return The number of bytes that can be read without blocking
+     */
+    public int available();
+
 }
diff --git a/java/org/apache/coyote/ajp/AbstractAjpProcessor.java b/java/org/apache/coyote/ajp/AbstractAjpProcessor.java
index 7016e41..93d01c3 100644
--- a/java/org/apache/coyote/ajp/AbstractAjpProcessor.java
+++ b/java/org/apache/coyote/ajp/AbstractAjpProcessor.java
@@ -1218,20 +1218,17 @@ public abstract class AbstractAjpProcessor<S> extends AbstractProcessor<S> {
 
     // ------------------------------------- InputStreamInputBuffer Inner Class
 
-
     /**
      * This class is an input buffer which will read its data from an input
      * stream.
      */
     protected class SocketInputBuffer implements InputBuffer {
 
-
         /**
          * Read bytes into the specified chunk.
          */
         @Override
-        public int doRead(ByteChunk chunk, Request req)
-        throws IOException {
+        public int doRead(ByteChunk chunk, Request req) throws IOException {
 
             if (endOfStream) {
                 return -1;
@@ -1250,9 +1247,16 @@ public abstract class AbstractAjpProcessor<S> extends AbstractProcessor<S> {
             chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength());
             empty = true;
             return chunk.getLength();
-
         }
 
+        @Override
+        public int available() {
+            if (empty) {
+                return 0;
+            } else {
+                return bodyBytes.getByteChunk().getLength();
+            }
+        }
     }
 
 
diff --git a/java/org/apache/coyote/http11/InputFilter.java b/java/org/apache/coyote/http11/InputFilter.java
index a90c88b..a854ffe 100644
--- a/java/org/apache/coyote/http11/InputFilter.java
+++ b/java/org/apache/coyote/http11/InputFilter.java
@@ -75,14 +75,5 @@ public interface InputFilter extends InputBuffer {
      * to consume extra bytes. The result of this method can't be negative (if
      * an error happens, an IOException should be thrown instead).
      */
-    public long end()
-        throws IOException;
-
-
-    /**
-     * Amount of bytes still available in a buffer.
-     */
-    public int available();
-
-
+    public long end() throws IOException;
 }
diff --git a/java/org/apache/coyote/http11/InternalAprInputBuffer.java b/java/org/apache/coyote/http11/InternalAprInputBuffer.java
index 6f153a9..3921015 100644
--- a/java/org/apache/coyote/http11/InternalAprInputBuffer.java
+++ b/java/org/apache/coyote/http11/InternalAprInputBuffer.java
@@ -686,14 +686,11 @@ public class InternalAprInputBuffer extends AbstractInputBuffer<Long> {
 
     // ------------------------------------- InputStreamInputBuffer Inner Class
 
-
     /**
      * This class is an input buffer which will read its data from an input
      * stream.
      */
-    protected class SocketInputBuffer
-        implements InputBuffer {
-
+    protected class SocketInputBuffer implements InputBuffer {
 
         /**
          * Read bytes into the specified chunk.
@@ -713,5 +710,14 @@ public class InternalAprInputBuffer extends AbstractInputBuffer<Long> {
 
             return (length);
         }
+
+        @Override
+        public int available() {
+            if (lastValid > pos) {
+                return lastValid - pos;
+            } else {
+                return 0;
+            }
+        }
     }
 }
diff --git a/java/org/apache/coyote/http11/InternalInputBuffer.java b/java/org/apache/coyote/http11/InternalInputBuffer.java
index 4782d50..48bdc85 100644
--- a/java/org/apache/coyote/http11/InternalInputBuffer.java
+++ b/java/org/apache/coyote/http11/InternalInputBuffer.java
@@ -595,14 +595,11 @@ public class InternalInputBuffer extends AbstractInputBuffer<Socket> {
 
     // ------------------------------------- InputStreamInputBuffer Inner Class
 
-
     /**
      * This class is an input buffer which will read its data from an input
      * stream.
      */
-    protected class InputStreamInputBuffer
-        implements InputBuffer {
-
+    protected class InputStreamInputBuffer implements InputBuffer {
 
         /**
          * Read bytes into the specified chunk.
@@ -622,5 +619,14 @@ public class InternalInputBuffer extends AbstractInputBuffer<Socket> {
 
             return (length);
         }
+
+        @Override
+        public int available() {
+            if (lastValid > pos) {
+                return lastValid - pos;
+            } else {
+                return 0;
+            }
+        }
     }
 }
diff --git a/java/org/apache/coyote/http11/InternalNioInputBuffer.java b/java/org/apache/coyote/http11/InternalNioInputBuffer.java
index 1da4c4c..339e186 100644
--- a/java/org/apache/coyote/http11/InternalNioInputBuffer.java
+++ b/java/org/apache/coyote/http11/InternalNioInputBuffer.java
@@ -867,14 +867,11 @@ public class InternalNioInputBuffer extends AbstractInputBuffer<NioChannel> {
 
     // ------------------------------------- InputStreamInputBuffer Inner Class
 
-
     /**
      * This class is an input buffer which will read its data from an input
      * stream.
      */
-    protected class SocketInputBuffer
-        implements InputBuffer {
-
+    protected class SocketInputBuffer implements InputBuffer {
 
         /**
          * Read bytes into the specified chunk.
@@ -894,5 +891,14 @@ public class InternalNioInputBuffer extends AbstractInputBuffer<NioChannel> {
 
             return (length);
         }
+
+        @Override
+        public int available() {
+            if (lastValid > pos) {
+                return lastValid - pos;
+            } else {
+                return 0;
+            }
+        }
     }
 }
diff --git a/java/org/apache/coyote/http11/filters/BufferedInputFilter.java b/java/org/apache/coyote/http11/filters/BufferedInputFilter.java
index 4b393a6..e10dcb5 100644
--- a/java/org/apache/coyote/http11/filters/BufferedInputFilter.java
+++ b/java/org/apache/coyote/http11/filters/BufferedInputFilter.java
@@ -137,7 +137,12 @@ public class BufferedInputFilter implements InputFilter {
 
     @Override
     public int available() {
-        return buffered.getLength();
+        int available = buffered.getLength();
+        if (available == 0) {
+            // No data buffered here. Try the next filter in the chain.
+            return buffer.available();
+        } else {
+            return available;
+        }
     }
-
 }
diff --git a/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java b/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
index 05e7680..5697512 100644
--- a/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
+++ b/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
@@ -265,7 +265,13 @@ public class ChunkedInputFilter implements InputFilter {
      */
     @Override
     public int available() {
-        return lastValid - pos;
+        int available = lastValid - pos;
+        if (available == 0) {
+            // No data buffered here. Try the next filter in the chain.
+            return buffer.available();
+        } else {
+            return available;
+        }
     }
 
 
diff --git a/java/org/apache/coyote/http11/filters/IdentityInputFilter.java b/java/org/apache/coyote/http11/filters/IdentityInputFilter.java
index f5f6cd0..79570c6 100644
--- a/java/org/apache/coyote/http11/filters/IdentityInputFilter.java
+++ b/java/org/apache/coyote/http11/filters/IdentityInputFilter.java
@@ -209,7 +209,8 @@ public class IdentityInputFilter implements InputFilter {
      */
     @Override
     public int available() {
-        return 0;
+        // No data buffered here. Try the next filter in the chain.
+        return buffer.available();
     }
 
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index bcac7a0..c7ad12a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -60,6 +60,15 @@
   issues do not "pop up" wrt. others).
 -->
 <section name="Tomcat 7.0.106 (violetagg)" rtext="in development">
+  <subsection name="Coyote">
+    <changelog>
+      <fix>
+        Refactor the implementation of
+        <code>ServletInputStream.available()</code> to provide a more accurate
+        return value, particularly when end of stream has been reached. (markt)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="WebSocket">
     <changelog>
       <fix>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]