Fix BZ 44620 https://issues.apache.org/bugzilla/show_bug.cgi?id=44620
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 24 Mar 2008 20:52:42 +0000 (20:52 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 24 Mar 2008 20:52:42 +0000 (20:52 +0000)
Move the code that handles writing more than the output buffer can accept in a single write to the method that does the writing. This then protects all calls to addToBB() from the issue described in BZ 44620 and does not require the socket.appWriteBufSize to be >= maxHttpHeaderSize

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@640572 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/http11/InternalNioOutputBuffer.java

index 8539213..598c9a2 100644 (file)
@@ -614,11 +614,21 @@ public class InternalNioOutputBuffer
 
     int total = 0;
     private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException {
-        while (socket.getBufHandler().getWriteBuffer().remaining() < length) {
-            flushBuffer();
+        while (length > 0) {
+            int thisTime = length;
+            if (socket.getBufHandler().getWriteBuffer().position() ==
+                    socket.getBufHandler().getWriteBuffer().capacity()
+                    || socket.getBufHandler().getWriteBuffer().remaining()==0) {
+                flushBuffer();
+            }
+            if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
+                thisTime = socket.getBufHandler().getWriteBuffer().remaining();
+            }
+            socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime);
+            length = length - thisTime;
+            offset = offset + thisTime;
+            total += thisTime;
         }
-        socket.getBufHandler().getWriteBuffer().put(buf, offset, length);
-        total += length;
         NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
         if ( ka!= null ) ka.access();//prevent timeouts for just doing client writes
     }
@@ -794,18 +804,7 @@ public class InternalNioOutputBuffer
             int len = chunk.getLength();
             int start = chunk.getStart();
             byte[] b = chunk.getBuffer();
-            while (len > 0) {
-                int thisTime = len;
-                if (socket.getBufHandler().getWriteBuffer().position() == socket.getBufHandler().getWriteBuffer().capacity() ||socket.getBufHandler().getWriteBuffer().remaining()==0) {
-                    flushBuffer();
-                }
-                if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
-                    thisTime = socket.getBufHandler().getWriteBuffer().remaining();
-                }
-                addToBB(b,start,thisTime);
-                len = len - thisTime;
-                start = start + thisTime;
-            }
+            addToBB(b, start, len);
             return chunk.getLength();
 
         }