hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1565592 - in /httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src: main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
Date Fri, 07 Feb 2014 09:19:14 GMT
Author: olegk
Date: Fri Feb  7 09:19:14 2014
New Revision: 1565592

URL: http://svn.apache.org/r1565592
Log:
Code consistency: tolerate lone LF at the end of the chunk

Modified:
    httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
    httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java

Modified: httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java?rev=1565592&r1=1565591&r2=1565592&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
(original)
+++ httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
Fri Feb  7 09:19:14 2014
@@ -41,7 +41,6 @@ import org.apache.http.annotation.NotThr
 import org.apache.http.impl.io.HttpTransportMetricsImpl;
 import org.apache.http.message.BufferedHeader;
 import org.apache.http.nio.reactor.SessionInputBuffer;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -84,22 +83,24 @@ public class ChunkDecoder extends Abstra
     }
 
     private void readChunkHead() throws IOException {
-        if (this.endOfChunk) {
-            if (this.buffer.length() < 2) {
-                return;
-            }
-            final int cr = this.buffer.read();
-            final int lf = this.buffer.read();
-            if (cr != HTTP.CR || lf != HTTP.LF) {
-                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
-            }
-            this.endOfChunk = false;
-        }
         if (this.lineBuf == null) {
             this.lineBuf = new CharArrayBuffer(32);
         } else {
             this.lineBuf.clear();
         }
+        if (this.endOfChunk) {
+            if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
+                if (!this.lineBuf.isEmpty()) {
+                    throw new MalformedChunkCodingException("CRLF expected at end of chunk");
+                }
+            } else {
+                if (this.buffer.length() > 2) {
+                    throw new MalformedChunkCodingException("CRLF expected at end of chunk");
+                }
+                return;
+            }
+            this.endOfChunk = false;
+        }
         if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
             int separator = this.lineBuf.indexOf(';');
             if (separator < 0) {

Modified: httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java?rev=1565592&r1=1565591&r2=1565592&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
(original)
+++ httpcomponents/httpcore/branches/4.3.x/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
Fri Feb  7 09:19:14 2014
@@ -143,6 +143,28 @@ public class TestChunkDecoder {
     }
 
     @Test
+    public void testMalformedChunk() throws Exception {
+        final String s = "5\r\n01234----------------------------------------------------------"
+
+                "-----------------------------------------------------------------------------"
+
+                "-----------------------------------------------------------------------------";
+        final ReadableByteChannel channel = new ReadableByteChannelMock(
+                new String[] {s}, Consts.ASCII);
+
+        final SessionInputBuffer inbuf = new SessionInputBufferImpl(32, 32, Consts.ASCII);
+        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
+        final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
+
+        final ByteBuffer dst = ByteBuffer.allocate(1024);
+
+        try {
+            decoder.read(dst);
+            Assert.fail("MalformedChunkCodingException should have been thrown");
+        } catch (final MalformedChunkCodingException ex) {
+            // expected
+        }
+    }
+
+    @Test
     public void testIncompleteChunkDecoding() throws Exception {
         final String[] chunks = {
                 "10;",
@@ -211,7 +233,7 @@ public class TestChunkDecoder {
 
     @Test
     public void testMalformedChunkEndingDecoding() throws Exception {
-        final String s = "5\r\n01234\r\n5\r\n56789\n\r6\r\nabcdef\r\n0\r\n\r\n";
+        final String s = "5\r\n01234\r\n5\r\n56789\r\r6\r\nabcdef\r\n0\r\n\r\n";
         final ReadableByteChannel channel = new ReadableByteChannelMock(
                 new String[] {s}, Consts.ASCII);
 



Mime
View raw message