hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r437761 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/nio/impl/ test/java/org/apache/http/nio/ test/java/org/apache/http/nio/mockup/
Date Mon, 28 Aug 2006 17:28:16 GMT
Author: olegk
Date: Mon Aug 28 10:28:14 2006
New Revision: 437761

URL: http://svn.apache.org/viewvc?rev=437761&view=rev
Log:
Fixed bug in NIOHttpDataTransmitter. More test coverage

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataReceiver.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataTransmitter.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataTransmitter.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataReceiver.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataReceiver.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataReceiver.java
Mon Aug 28 10:28:14 2006
@@ -24,7 +24,11 @@
         }
         this.session = session;
         this.mutex = new Object();
-        initBuffer(buffersize);
+        int linebuffersize = buffersize;
+        if (linebuffersize > 512) {
+            linebuffersize = 512;
+        }
+        initBuffer(buffersize, linebuffersize);
     }
     
     public void consumeInput() throws IOException {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataTransmitter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataTransmitter.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataTransmitter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/AsyncHttpDataTransmitter.java
Mon Aug 28 10:28:14 2006
@@ -24,7 +24,11 @@
         }
         this.session = session;
         this.mutex = new Object();
-        initBuffer(buffersize);
+        int linebuffersize = buffersize;
+        if (linebuffersize > 512) {
+            linebuffersize = 512;
+        }
+        initBuffer(buffersize, linebuffersize);
     }
 
     public void produceOutput() throws IOException {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
Mon Aug 28 10:28:14 2006
@@ -65,12 +65,12 @@
     
     private int maxLineLen = -1;
     
-    protected void initBuffer(int buffersize) {
+    protected void initBuffer(int buffersize, int linebuffersize) {
         this.buffer = ByteBuffer.allocateDirect(buffersize);
         this.buffer.flip();
         this.charset = Charset.forName("US-ASCII");
         this.chardecoder = createCharDecoder();
-        this.chbuffer = CharBuffer.allocate(1024);
+        this.chbuffer = CharBuffer.allocate(linebuffersize);
     }
 
     public void reset(final HttpParams params) {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataTransmitter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataTransmitter.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataTransmitter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataTransmitter.java
Mon Aug 28 10:28:14 2006
@@ -64,11 +64,11 @@
     private CharsetEncoder charencoder = null;
     private CharBuffer chbuffer = null;
 
-    protected void initBuffer(int buffersize) {
+    protected void initBuffer(int buffersize, int linebuffersize) {
         this.buffer = ByteBuffer.allocateDirect(buffersize);
         this.charset = Charset.forName("US-ASCII");
         this.charencoder = createCharEncoder();
-        this.chbuffer = CharBuffer.allocate(1024);
+        this.chbuffer = CharBuffer.allocate(linebuffersize);
     }
     
     public void reset(final HttpParams params) {
@@ -162,7 +162,7 @@
                     // terminate the encoding process
                     eol = true;
                 }
-                this.chbuffer.put(buffer.buffer(), offset, offset + l);
+                this.chbuffer.put(buffer.buffer(), offset, l);
                 this.chbuffer.flip();
                 
                 boolean retry = true;
@@ -173,7 +173,6 @@
                     }
                     retry = !result.isUnderflow();
                 }
-                
                 this.chbuffer.compact();
                 offset += l;
                 remaining -= l;

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
Mon Aug 28 10:28:14 2006
@@ -28,11 +28,11 @@
 
 package org.apache.http.nio;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.nio.channels.Channels;
-import java.nio.channels.ReadableByteChannel;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
 
 import org.apache.http.impl.DefaultHttpParams;
 import org.apache.http.io.CharArrayBuffer;
@@ -44,8 +44,6 @@
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
 
-import junit.framework.*;
-
 /**
  * Simple tests for {@link NIOHttpDataTransmitter} and {@link NIOHttpDataReceiver}.
  *
@@ -72,37 +70,6 @@
         return new TestSuite(TestNIOHttpTransmitterAndReceiver.class);
     }
 
-    public void testInit() throws Exception {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        new NIOHttpDataTransmitterMockup(Channels.newChannel(out), 10); 
-        try {
-            new NIOHttpDataTransmitterMockup(Channels.newChannel(out), -10); 
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new NIOHttpDataTransmitterMockup(null, 1024); 
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        new NIOHttpDataReceiverMockup(Channels.newChannel(in), 10); 
-        try {
-            new NIOHttpDataReceiverMockup(Channels.newChannel(in), -10); 
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new NIOHttpDataReceiverMockup((ReadableByteChannel)null, 1024); 
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-    }
-    
     public void testBasicReadWriteLine() throws Exception {
         
         String[] teststrs = new String[5];
@@ -209,7 +176,7 @@
         teststrs[3] = "";
         teststrs[4] = "And goodbye";
         
-        NIOHttpDataTransmitterMockup transmitter = new NIOHttpDataTransmitterMockup(); 
+        NIOHttpDataTransmitterMockup transmitter = new NIOHttpDataTransmitterMockup(32, 8);

         for (int i = 0; i < teststrs.length; i++) {
             transmitter.writeLine(teststrs[i]);
         }
@@ -218,7 +185,7 @@
         transmitter.writeLine((CharArrayBuffer)null);
         transmitter.flush();
         
-        NIOHttpDataReceiverMockup receiver = new NIOHttpDataReceiverMockup(transmitter.getData(),
1024);
+        NIOHttpDataReceiverMockup receiver = new NIOHttpDataReceiverMockup(transmitter.getData(),
32, 8);
 
         assertTrue(receiver.isDataAvailable(0));
         
@@ -318,13 +285,13 @@
         HttpParams params = new DefaultHttpParams();
         String s = "a very looooooooooooooooooooooooooooooooooooooong line\r\n     ";
         byte[] tmp = s.getBytes("US-ASCII"); 
-        NIOHttpDataReceiverMockup receiver1 = new NIOHttpDataReceiverMockup(tmp, 5);
+        NIOHttpDataReceiverMockup receiver1 = new NIOHttpDataReceiverMockup(tmp, 5, 5);
         // no limit
         params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 0);
         receiver1.reset(params);
         assertNotNull(receiver1.readLine());
         
-        NIOHttpDataReceiverMockup receiver2 = new NIOHttpDataReceiverMockup(tmp, 5);
+        NIOHttpDataReceiverMockup receiver2 = new NIOHttpDataReceiverMockup(tmp, 5, 5);
         // 15 char limit
         params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 15);
         receiver2.reset(params);
@@ -387,4 +354,16 @@
         assertNull(receiver.readLine());
     }
 
+    public void testInvalidInput() throws Exception {
+        String s = "line1\r\nline2";
+        byte[] tmp = s.getBytes("US-ASCII"); 
+        NIOHttpDataReceiverMockup receiver1 = new NIOHttpDataReceiverMockup(tmp);
+        try {
+            receiver1.readLine(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataReceiverMockup.java
Mon Aug 28 10:28:14 2006
@@ -19,28 +19,34 @@
     
     private final ReadableByteChannel channel;
     
-    public NIOHttpDataReceiverMockup(final ReadableByteChannel channel, int buffersize) {
+    public NIOHttpDataReceiverMockup(
+            final ReadableByteChannel channel, 
+            int buffersize, 
+            int linebuffersize) {
         super();
         if (channel == null) {
             throw new IllegalArgumentException("Channel may not be null");
         }
         this.channel = channel;
-        initBuffer(buffersize);
+        initBuffer(buffersize, linebuffersize);
     }
 
     public NIOHttpDataReceiverMockup(final byte[] bytes) {
-        this(bytes, BUFFER_SIZE);
+        this(bytes, BUFFER_SIZE, BUFFER_SIZE);
     }
 
-    public NIOHttpDataReceiverMockup(final byte[] bytes, int buffersize) {
+    public NIOHttpDataReceiverMockup(
+            final byte[] bytes, 
+            int buffersize, 
+            int linebuffersize) {
         super();
         this.channel = Channels.newChannel(new ByteArrayInputStream(bytes));
-        initBuffer(buffersize);
+        initBuffer(buffersize, linebuffersize);
     }
 
     public NIOHttpDataReceiverMockup(final String s, final String charset, int buffersize)

         throws UnsupportedEncodingException {
-        this(s.getBytes(charset), buffersize);
+        this(s.getBytes(charset), buffersize, buffersize);
     }
     
     public NIOHttpDataReceiverMockup(final String s, final String charset) 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java?rev=437761&r1=437760&r2=437761&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/NIOHttpDataTransmitterMockup.java
Mon Aug 28 10:28:14 2006
@@ -20,19 +20,30 @@
     
     private final WritableByteChannel channel;
     
-    public NIOHttpDataTransmitterMockup(final WritableByteChannel channel, int buffersize)
{
+    public NIOHttpDataTransmitterMockup(
+            final WritableByteChannel channel, 
+            int buffersize,
+            int linebuffersize) {
         super();
         if (channel == null) {
             throw new IllegalArgumentException("Channel may not be null");
         }
         this.channel = channel;
-        initBuffer(buffersize);
+        initBuffer(buffersize, linebuffersize);
+    }
+
+    public NIOHttpDataTransmitterMockup(
+            int buffersize,
+            int linebuffersize) {
+        super();
+        this.channel = Channels.newChannel(this.buffer);
+        initBuffer(buffersize, linebuffersize);
     }
 
     public NIOHttpDataTransmitterMockup() {
         super();
         this.channel = Channels.newChannel(this.buffer);
-        initBuffer(BUFFER_SIZE);
+        initBuffer(BUFFER_SIZE, BUFFER_SIZE);
     }
 
     protected void flushBuffer() throws IOException {



Mime
View raw message