james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1575304 - in /james/mime4j/branches/apache-mime4j-0.7: 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/
Date Fri, 07 Mar 2014 16:01:28 GMT
Author: olegk
Date: Fri Mar  7 16:01:28 2014
New Revision: 1575304

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

Modified:
    james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
    james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
    james/mime4j/branches/apache-mime4j-0.7/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java

Modified: james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java?rev=1575304&r1=1575303&r2=1575304&view=diff
==============================================================================
--- james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(original)
+++ james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
Fri Mar  7 16:01:28 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/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java?rev=1575304&r1=1575303&r2=1575304&view=diff
==============================================================================
--- james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
(original)
+++ james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
Fri Mar  7 16:01:28 2014
@@ -154,6 +154,14 @@ public class RawFieldParserTest extends 
         Assert.assertEquals("stuff and  some more  stuff  ;", result);
     }
 
+    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);
+    }
+
     public void testTokenParsingQuotedValuesWithComments() throws Exception {
         String s = " (blah blah)  \"(stuff)(and)(some)(more)(stuff)\" (yada yada) ";
         ByteSequence raw = ContentUtil.encode(s);

Modified: james/mime4j/branches/apache-mime4j-0.7/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/apache-mime4j-0.7/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java?rev=1575304&r1=1575303&r2=1575304&view=diff
==============================================================================
--- james/mime4j/branches/apache-mime4j-0.7/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
(original)
+++ james/mime4j/branches/apache-mime4j-0.7/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
Fri Mar  7 16:01:28 2014
@@ -21,6 +21,7 @@ package org.apache.james.mime4j.field.ad
 
 import java.util.List;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.james.mime4j.dom.address.Address;
@@ -88,6 +89,19 @@ public class LenientAddressBuilderTest e
         assertEquals("some  one@somehost.somewhere.com", mailbox.getAddress());
     }
 
+    public void testEmbeddedQuotes() throws Exception {
+        String s = "=?utf-8?Q?\"Dupont,_Gr=C3=A9goire\" <greg@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());
+    }
+
     public void testParseAddressTruncated() throws Exception {
         String s = "<  some  one  ";
         ByteSequence raw = ContentUtil.encode(s);



Mime
View raw message