james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1095388 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/stream/RawFieldParser.java test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
Date Wed, 20 Apr 2011 12:17:00 GMT
Author: olegk
Date: Wed Apr 20 12:16:59 2011
New Revision: 1095388

URL: http://svn.apache.org/viewvc?rev=1095388&view=rev
Log:
RawFieldPsrser: handling of comments in field values

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java?rev=1095388&r1=1095387&r2=1095388&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
Wed Apr 20 12:16:59 2011
@@ -124,6 +124,8 @@ public class RawFieldParser {
             } else if (CharsetUtil.isWhitespace(current)) {
                 skipWhiteSpace(buf, cursor);
                 whitespace = true;
+            } else if (current == '(') {
+                skipComment(buf, cursor);
             } else {
                 if (dst.length() > 0 && whitespace) {
                     dst.append(' ');
@@ -145,6 +147,8 @@ public class RawFieldParser {
             } else if (CharsetUtil.isWhitespace(current)) {
                 skipWhiteSpace(buf, cursor);
                 whitespace = true;
+            } else if (current == '(') {
+                skipComment(buf, cursor);
             } else if (current == '\"') {
                 if (dst.length() > 0 && whitespace) {
                     dst.append(' ');
@@ -177,6 +181,43 @@ public class RawFieldParser {
         cursor.updatePos(pos);
     }
 
+    static void skipComment(final ByteSequence buf, final ParserCursor cursor) {
+        if (cursor.atEnd()) {
+            return;
+        }
+        int pos = cursor.getPos();
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        char current = (char) (buf.byteAt(pos) & 0xff);
+        if (current != '(') {
+            return;
+        }
+        pos++;
+        indexFrom++;
+
+        int level = 1;
+        boolean escaped = false;
+        for (int i = indexFrom; i < indexTo; i++, pos++) {
+            current = (char) (buf.byteAt(i) & 0xff);
+            if (escaped) {
+                escaped = false;
+            } else {
+                if (current == '\\') {
+                    escaped = true;
+                } else if (current == '(') {
+                    level++;
+                } else if (current == ')') {
+                    level--;
+                }
+            }
+            if (level <= 0) {
+                pos++;
+                break;
+            }
+        }
+        cursor.updatePos(pos);
+    }
+
     static void copyContent(final ByteSequence buf, final ParserCursor cursor, final int[]
delimiters,
             final StringBuilder dst) {
         int pos = cursor.getPos();
@@ -184,7 +225,7 @@ public class RawFieldParser {
         int indexTo = cursor.getUpperBound();
         for (int i = indexFrom; i < indexTo; i++) {
             char current = (char) (buf.byteAt(i) & 0xff);
-            if (isOneOf(current, delimiters) || CharsetUtil.isWhitespace(current)) {
+            if (isOneOf(current, delimiters) || CharsetUtil.isWhitespace(current) || current
== '(') {
                 break;
             } else {
                 pos++;

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java?rev=1095388&r1=1095387&r2=1095388&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
Wed Apr 20 12:16:59 2011
@@ -105,6 +105,23 @@ public class RawFieldParserTest extends 
         Assert.assertEquals("stuff and more stuff  ", strbuf1.toString());
     }
 
+    public void testSkipComments() throws Exception {
+        String s = "(some (((maybe))human readable) stuff())";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+
+        RawFieldParser.skipComment(raw, cursor);
+        Assert.assertTrue(cursor.atEnd());
+    }
+
+    public void testSkipCommentsWithQuotedPairs() throws Exception {
+        String s = "(some (((\\)maybe))human readable\\() stuff())";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+
+        RawFieldParser.skipComment(raw, cursor);
+        Assert.assertTrue(cursor.atEnd());
+    }
 
     public void testTokenParsingTokensWithUnquotedBlanks() throws Exception {
         String s = "  stuff and   \tsome\tmore  stuff  ;";
@@ -114,6 +131,14 @@ public class RawFieldParserTest extends 
         Assert.assertEquals("stuff and some more stuff", result);
     }
 
+    public void testTokenParsingTokensWithComments() throws Exception {
+        String s = " (blah-blah)  stuff(blah-blah) and some mo(blah-blah)re  stuff (blah-blah)
;";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        String result = RawFieldParser.parseToken(raw, cursor, new int[] { ';' });
+        Assert.assertEquals("stuff and some more stuff", result);
+    }
+
     public void testTokenParsingMixedValuesAndQuotedValues() throws Exception {
         String s = "  stuff and    \" some more \"   \"stuff  ;";
         ByteSequence raw = ContentUtil.encode(s);
@@ -122,6 +147,14 @@ public class RawFieldParserTest extends 
         Assert.assertEquals("stuff and  some more  stuff  ;", result);
     }
 
+    public void testTokenParsingQuotedValuesWithComments() throws Exception {
+        String s = " (blah blah)  \"(stuff)(and)(some)(more)(stuff)\" (yada yada) ";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        String result = RawFieldParser.parseValue(raw, cursor, new int[] { ';' });
+        Assert.assertEquals("(stuff)(and)(some)(more)(stuff)", result);
+    }
+
     public void testBasicParsing() throws Exception {
         String s = "raw: stuff;\r\n  more stuff";
         ByteSequence raw = ContentUtil.encode(s);
@@ -247,6 +280,14 @@ public class RawFieldParserTest extends 
         assertEquals("test", param.getName());
         assertEquals("stuff", param.getValue());
 
+        s = "test  = text(text of some kind)/stuff(stuff of some kind)";
+        buf = ContentUtil.encode(s);
+        cursor = new ParserCursor(0, s.length());
+
+        param = parser.parseParameter(buf, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("text/stuff", param.getValue());
+
         s = "test  = \"  stuff\\\"\"";
         buf = ContentUtil.encode(s);
         cursor = new ParserCursor(0, s.length());
@@ -353,6 +394,23 @@ public class RawFieldParserTest extends 
         assertEquals("value3", params.get(4).getValue());
     }
 
+    public void testRawBodyParseWithComments() {
+        ByteSequence buf = ContentUtil.encode(
+                "  text/(nothing special)plain ; charset=(latin)ISO-8859-1; "
+                + "boundary=foo(bar);");
+        RawFieldParser parser = new RawFieldParser();
+        ParserCursor cursor = new ParserCursor(0, buf.length());
+        RawBody body = parser.parseRawBody(buf, cursor);
+        assertNotNull(body);
+        assertEquals("text/plain", body.getValue());
+        List<NameValuePair> params = body.getParams();
+        assertEquals(2, params.size());
+        assertEquals("charset", params.get(0).getName());
+        assertEquals("ISO-8859-1", params.get(0).getValue());
+        assertEquals("boundary", params.get(1).getName());
+        assertEquals("foo", params.get(1).getValue());
+    }
+
     public void testRawBodyParseEmptyParam() {
         ByteSequence buf = ContentUtil.encode(
                 "multipart/alternative;; boundary=\"boundary\"");



Mime
View raw message