Fixed chunked input filter to parse the header correctly. Performs strict parsing...
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 19 Jul 2006 13:00:42 +0000 (13:00 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 19 Jul 2006 13:00:42 +0000 (13:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@423453 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/http11/Constants.java
java/org/apache/coyote/http11/filters/ChunkedInputFilter.java

index 6907e84..3a5ee85 100644 (file)
@@ -83,6 +83,12 @@ public final class Constants {
      * COLON.\r
      */\r
     public static final byte COLON = (byte) ':';\r
+    \r
+    /**\r
+     * SEMI_COLON.\r
+     */\r
+    public static final byte SEMI_COLON = (byte) ';';\r
+\r
 \r
 \r
     /**\r
index 73834da..fe46344 100644 (file)
@@ -27,9 +27,11 @@ import org.apache.coyote.http11.Constants;
 import org.apache.coyote.http11.InputFilter;\r
 \r
 /**\r
- * Chunked input filter.\r
+ * Chunked input filter. Parses chunked data according to\r
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1">http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1</a><br>\r
  * \r
  * @author Remy Maucherat\r
+ * @author Filip Hanik\r
  */\r
 public class ChunkedInputFilter implements InputFilter {\r
 \r
@@ -127,7 +129,7 @@ public class ChunkedInputFilter implements InputFilter {
 \r
         if (remaining <= 0) {\r
             if (!parseChunkHeader()) {\r
-                throw new IOException("Invalid chunk");\r
+                throw new IOException("Invalid chunk header");\r
             }\r
             if (endChunk) {\r
                 parseEndChunk();\r
@@ -234,6 +236,12 @@ public class ChunkedInputFilter implements InputFilter {
 \r
     /**\r
      * Parse the header of a chunk.\r
+     * A chunk header can look like \r
+     * A10CRLF\r
+     * F23;chunk-extension to be ignoredCRLF\r
+     * The letters before CRLF but after the trailer mark, must be valid hex digits, \r
+     * we should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid header\r
+     * according to spec\r
      */\r
     protected boolean parseChunkHeader()\r
         throws IOException {\r
@@ -241,6 +249,7 @@ public class ChunkedInputFilter implements InputFilter {
         int result = 0;\r
         boolean eol = false;\r
         boolean readDigit = false;\r
+        boolean trailer = false;\r
 \r
         while (!eol) {\r
 \r
@@ -252,11 +261,18 @@ public class ChunkedInputFilter implements InputFilter {
             if (buf[pos] == Constants.CR) {\r
             } else if (buf[pos] == Constants.LF) {\r
                 eol = true;\r
-            } else {\r
+            } else if (buf[pos] == Constants.SEMI_COLON) {\r
+                trailer = true;\r
+            } else if (!trailer) { \r
+                //don't read data after the trailer\r
                 if (HexUtils.DEC[buf[pos]] != -1) {\r
                     readDigit = true;\r
                     result *= 16;\r
                     result += HexUtils.DEC[buf[pos]];\r
+                } else {\r
+                    //we shouldn't allow invalid, non hex characters\r
+                    //in the chunked header\r
+                    return false;\r
                 }\r
             }\r
 \r