You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mime4j-dev@james.apache.org by ol...@apache.org on 2014/03/07 16:59:20 UTC

svn commit: r1575302 - in /james/mime4j/trunk: core/src/main/java/org/apache/james/mime4j/stream/ core/src/test/java/org/apache/james/mime4j/stream/ dom/src/test/java/org/apache/james/mime4j/field/address/

Author: olegk
Date: Fri Mar  7 15:59:20 2014
New Revision: 1575302

URL: http://svn.apache.org/r1575302
Log:
MIMEJ-237: fixed bug in RawFieldParser#parseValue() causing incorrect parsing of field values with quoted and unquoted content

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
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.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=1575302&r1=1575301&r2=1575302&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 Fri Mar  7 15:59:20 2014
@@ -209,7 +209,7 @@ public class RawFieldParser {
                 if (dst.length() > 0 && whitespace) {
                     dst.append(' ');
                 }
-                copyContent(buf, cursor, delimiters, dst);
+                copyUnquotedContent(buf, cursor, delimiters, dst);
                 whitespace = false;
             }
         }
@@ -333,6 +333,34 @@ public class RawFieldParser {
     }
 
     /**
+     * Transfers content into the destination buffer until a whitespace character, a comment,
+     * a quote, or any of the given delimiters is encountered.
+     *
+     * @param buf buffer with the sequence of bytes to be parsed
+     * @param cursor defines the bounds and current position of the buffer
+     * @param delimiters set of delimiting characters. Can be <code>null</code> if the value
+     *  is delimited by a whitespace, a quote or a comment only.
+     * @param dst destination buffer
+     */
+    public void copyUnquotedContent(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters,
+                            final StringBuilder dst) {
+        int pos = cursor.getPos();
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        for (int i = indexFrom; i < indexTo; i++) {
+            char current = (char) (buf.byteAt(i) & 0xff);
+            if ((delimiters != null && delimiters.get(current))
+                    || CharsetUtil.isWhitespace(current) || current == '(' || current == '\"') {
+                break;
+            } else {
+                pos++;
+                dst.append(current);
+            }
+        }
+        cursor.updatePos(pos);
+    }
+
+    /**
      * Transfers content enclosed with quote marks into the destination buffer.
      *
      * @param buf buffer with the sequence of bytes to be parsed

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=1575302&r1=1575301&r2=1575302&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 Fri Mar  7 15:59:20 2014
@@ -163,6 +163,15 @@ public class RawFieldParserTest {
     }
 
     @Test
+    public void testTokenParsingMixedValuesAndQuotedValues2() throws Exception {
+        String s = "stuff\"more\"stuff;";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        String result = parser.parseValue(raw, cursor, RawFieldParser.INIT_BITSET(';'));
+        Assert.assertEquals("stuffmorestuff", result);
+    }
+
+    @Test
     public void testTokenParsingQuotedValuesWithComments() throws Exception {
         String s = " (blah blah)  \"(stuff)(and)(some)(more)(stuff)\" (yada yada) ";
         ByteSequence raw = ContentUtil.encode(s);

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java?rev=1575302&r1=1575301&r2=1575302&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java Fri Mar  7 15:59:20 2014
@@ -95,6 +95,20 @@ public class LenientAddressBuilderTest {
     }
 
     @Test
+    public void testEmbeddedQuotes() throws Exception {
+        String s = "=?utf-8?Q?\"Dupont,_Gr=C3=A9goire\" <gr...@gmail.com>";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+
+        Address address = parser.parseAddress(raw, cursor, RawFieldParser.INIT_BITSET(','));
+        Assert.assertNotNull(address);
+        Assert.assertTrue(address instanceof Mailbox);
+        Mailbox mailbox = (Mailbox) address;
+        Assert.assertEquals("greg@gmail.com", mailbox.getAddress());
+        Assert.assertEquals("=?utf-8?Q?Dupont,_Gr=C3=A9goire", mailbox.getName());
+    }
+
+    @Test
     public void testParseAddressTruncated() throws Exception {
         String s = "<  some  one  ";
         ByteSequence raw = ContentUtil.encode(s);