james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r894516 - in /james/mime4j/branches/cycleclean/core/src: main/java/org/apache/james/mime4j/field/ main/java/org/apache/james/mime4j/parser/ test/java/org/apache/james/mime4j/parser/
Date Tue, 29 Dec 2009 23:38:48 GMT
Author: bago
Date: Tue Dec 29 23:38:47 2009
New Revision: 894516

URL: http://svn.apache.org/viewvc?rev=894516&view=rev
Log:
Added support for obsolete syntax header names (WSP between the header name and the ":") (MIME4J-150).
Also fixed typo in former event name. Added a warn to log obsolete headers.

Modified:
    james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
    james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
    james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/Event.java
    james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/RawField.java
    james/mime4j/branches/cycleclean/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java

Modified: james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java?rev=894516&r1=894515&r2=894516&view=diff
==============================================================================
--- james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
(original)
+++ james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
Tue Dec 29 23:38:47 2009
@@ -30,7 +30,7 @@
 public class DefaultFieldParser extends DelegatingFieldParser {
 
 	private static final Pattern FIELD_NAME_PATTERN = Pattern
-    .compile("^([\\x21-\\x39\\x3b-\\x7e]+):");
+    .compile("^([\\x21-\\x39\\x3b-\\x7e]+)[\\x20\\x09]*:");
 	
     private static final DefaultFieldParser PARSER = new DefaultFieldParser();
     

Modified: james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java?rev=894516&r1=894515&r2=894516&view=diff
==============================================================================
--- james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
(original)
+++ james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
Tue Dec 29 23:38:47 2009
@@ -201,24 +201,33 @@
             fieldbuf.setLength(len);
             
             boolean valid = true;
+            boolean obsoleteSyntax = false;
             
             int pos = fieldbuf.indexOf((byte) ':');
             if (pos <= 0) {
-                monitor(Event.INALID_HEADER);
                 valid = false;
             } else {
                 for (int i = 0; i < pos; i++) {
                     if (!fieldChars.get(fieldbuf.byteAt(i) & 0xff)) {
-                        monitor(Event.INALID_HEADER);
-                        valid = false;
-                        break;
+                    	for (; i < pos; i++) {
+                    		int j = fieldbuf.byteAt(i) & 0xff;
+							if (j != 0x20 && j != 0x09) {
+		                        valid = false;
+		                        break;
+							} else {
+								obsoleteSyntax = true;
+							}
+                    	}
                     }
                 }
             }
             if (valid) {
+                if (obsoleteSyntax) warn(Event.OBSOLETE_HEADER);
                 field = new RawField(fieldbuf, pos);
-                body.addField(field);            
+                body.addField(field);
                 return true;
+            } else {
+                monitor(Event.INVALID_HEADER);
             }
         }
     }

Modified: james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/Event.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/Event.java?rev=894516&r1=894515&r2=894516&view=diff
==============================================================================
--- james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/Event.java
(original)
+++ james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/Event.java
Tue Dec 29 23:38:47 2009
@@ -33,8 +33,11 @@
         = new Event("Unexpected end of headers detected. " +
                 "Higher level boundary detected or EOF reached.");
     /** Indicates that unexpected end of headers detected.*/
-    public static final Event INALID_HEADER 
+    public static final Event INVALID_HEADER 
         = new Event("Invalid header encountered");
+    /** Indicates that an obsolete syntax header has been detected */
+    public static final Event OBSOLETE_HEADER 
+        = new Event("Obsolete header encountered");
     
     private final String code;
     

Modified: james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/RawField.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/RawField.java?rev=894516&r1=894515&r2=894516&view=diff
==============================================================================
--- james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/RawField.java
(original)
+++ james/mime4j/branches/cycleclean/core/src/main/java/org/apache/james/mime4j/parser/RawField.java
Tue Dec 29 23:38:47 2009
@@ -65,7 +65,10 @@
     }
 
     private String parseName() {
-        return ContentUtil.decode(raw, 0, colonIdx);
+    	// make sure we ignore ending WSP (obsolete rfc822 syntax)
+    	int endIdx = colonIdx;
+    	while (endIdx > 0 && raw.byteAt(endIdx - 1) == 0x20 || raw.byteAt(endIdx
- 1) == 0x09) endIdx--;
+        return ContentUtil.decode(raw, 0, endIdx);
     }
 
     private String parseBody() {

Modified: james/mime4j/branches/cycleclean/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/cycleclean/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java?rev=894516&r1=894515&r2=894516&view=diff
==============================================================================
--- james/mime4j/branches/cycleclean/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
(original)
+++ james/mime4j/branches/cycleclean/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
Tue Dec 29 23:38:47 2009
@@ -115,6 +115,85 @@
         }
     }
 
+    public void testObsoleteSyntaxEntity() throws Exception {
+        String message = 
+            "To 	: Road Runner <runner@example.org>\r\n" +
+            "From	: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date  	:Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject	:Mail\r\n" +
+            "     \r\n" +
+            " with a folded subject \r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
+        
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE);
+        
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("To", entity.getField().getName());
+        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("From", entity.getField().getName());
+        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Date", entity.getField().getName());
+        assertEquals("Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Subject", entity.getField().getName());
+        assertEquals("Mail      with a folded subject ", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Content-Type", entity.getField().getName());
+        assertEquals(" text/plain", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+        try {
+            entity.getField().getName();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        try {
+            entity.getField().getBody();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        
+        entity.advance();
+        assertEquals(EntityStates.T_BODY, entity.getState());
+        assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
+        entity.advance();
+        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
+        try {
+            entity.getContentStream();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        entity.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
+        try {
+            entity.advance();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+    }
+
     public void testMultipartEntity() throws Exception {
         String message = 
             "To: Road Runner <runner@example.org>\r\n" +



Mime
View raw message