tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1405133 - in /tomcat/trunk: java/org/apache/tomcat/util/http/parser/HttpParser2.java test/org/apache/tomcat/util/http/parser/TestHttpParser2.java
Date Fri, 02 Nov 2012 19:57:31 GMT
Author: markt
Date: Fri Nov  2 19:57:30 2012
New Revision: 1405133

URL: http://svn.apache.org/viewvc?rev=1405133&view=rev
Log:
Improve EOF handling with some test cases

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/http/parser/HttpParser2.java
    tomcat/trunk/test/org/apache/tomcat/util/http/parser/TestHttpParser2.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/http/parser/HttpParser2.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/parser/HttpParser2.java?rev=1405133&r1=1405132&r2=1405133&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/http/parser/HttpParser2.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/http/parser/HttpParser2.java Fri Nov  2 19:57:30
2012
@@ -173,7 +173,7 @@ public class HttpParser2 {
             int c = input.read();
             if (c != constant.charAt(i)) {
                 if (optional) {
-                    input.skip(i);
+                    input.skip(-(i+1));
                     return false;
                 } else {
                     throw new IllegalArgumentException(
@@ -186,9 +186,9 @@ public class HttpParser2 {
     }
 
     private static void skipLws(StringReader input) throws IOException {
-        char c = (char) input.read();
+        int c = input.read();
         while (c == 32 || c == 9) {
-            c = (char) input.read();
+            c = input.read();
         }
 
         // Skip back so non-LWS character is available for next read
@@ -198,10 +198,10 @@ public class HttpParser2 {
     private static String readToken(StringReader input) throws IOException {
         StringBuilder result = new StringBuilder();
 
-        char c = (char) input.read();
-        while (c != 65535 && isToken[c]) {
-            result.append(c);
-            c = (char) input.read();
+        int c = input.read();
+        while (c != -1 && isToken[c]) {
+            result.append((char) c);
+            c = input.read();
         }
         // Skip back so non-token character is available for next read
         input.skip(-1);
@@ -212,7 +212,7 @@ public class HttpParser2 {
     private static String readQuotedString(StringReader input)
             throws IOException {
 
-        char c = (char) input.read();
+        int c = input.read();
         if (c != '"') {
             throw new IllegalArgumentException(
                     "TODO i18n: Quoted string must start with a quote");
@@ -220,15 +220,18 @@ public class HttpParser2 {
 
         StringBuilder result = new StringBuilder();
 
-        c = (char) input.read();
+        c = input.read();
         while (c != '"') {
-            if (c == '\\') {
-                c = (char) input.read();
+            if (c == -1) {
+                throw new IllegalArgumentException(
+                        "TODO i18n: Quoted string must end with a quote");
+            } else if (c == '\\') {
+                c = input.read();
                 result.append(c);
             } else {
-                result.append(c);
+                result.append((char) c);
             }
-            c = (char) input.read();
+            c = input.read();
         }
 
         return result.toString();
@@ -236,7 +239,7 @@ public class HttpParser2 {
 
     private static String readTokenOrQuotedString(StringReader input)
             throws IOException {
-        char c = (char) input.read();
+        int c = input.read();
         input.skip(-1);
 
         if (c == '"') {
@@ -253,10 +256,10 @@ public class HttpParser2 {
     private static String readLhex(StringReader input) throws IOException {
         StringBuilder result = new StringBuilder();
 
-        char c = (char) input.read();
-        while (isHex[c]) {
-            result.append(c);
-            c = (char) input.read();
+        int c = input.read();
+        while (c != -1 && isHex[c]) {
+            result.append((char) c);
+            c = input.read();
         }
         // Skip back so non-hex character is available for next read
         input.skip(-1);

Modified: tomcat/trunk/test/org/apache/tomcat/util/http/parser/TestHttpParser2.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/http/parser/TestHttpParser2.java?rev=1405133&r1=1405132&r2=1405133&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/util/http/parser/TestHttpParser2.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/util/http/parser/TestHttpParser2.java Fri Nov  2 19:57:30
2012
@@ -113,4 +113,43 @@ public class TestHttpParser2 {
         Assert.assertEquals("auth", result.get("qop"));
         Assert.assertEquals("9926cb3c334ede11", result.get("cnonce"));
     }
+
+    @Test
+    public void testEndWithLhex() throws Exception {
+        String header = "Digest nc=00000001";
+
+        StringReader input = new StringReader(header);
+
+        Map<String,String> result = HttpParser2.parseAuthorizationDigest(input);
+
+        Assert.assertEquals("00000001", result.get("nc"));
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testUnclosedQuotedString1() throws Exception {
+        String header = "Digest username=\"test";
+
+        StringReader input = new StringReader(header);
+
+        HttpParser2.parseAuthorizationDigest(input);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testUnclosedQuotedString2() throws Exception {
+        String header = "Digest username=\"test\\";
+
+        StringReader input = new StringReader(header);
+
+        HttpParser2.parseAuthorizationDigest(input);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNonTokenDirective() throws Exception {
+        String header = "Digest user{name=\"test\"";
+
+        StringReader input = new StringReader(header);
+
+        HttpParser2.parseAuthorizationDigest(input);
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message