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 mw...@apache.org on 2009/01/31 20:20:26 UTC
svn commit: r739587 - in /james/mime4j/trunk/src:
main/java/org/apache/james/mime4j/codec/EncoderUtil.java
main/java/org/apache/james/mime4j/field/Fields.java
test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
Author: mwiederkehr
Date: Sat Jan 31 19:20:25 2009
New Revision: 739587
URL: http://svn.apache.org/viewvc?rev=739587&view=rev
Log:
MIME4J-100: check if mimeType is valid; parameter keys are encoded lower-case; minor changes in EncoderUtil
Modified:
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java?rev=739587&r1=739586&r2=739587&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java Sat Jan 31 19:20:25 2009
@@ -22,6 +22,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.BitSet;
+import java.util.Locale;
import org.apache.james.mime4j.util.CharsetUtil;
@@ -149,6 +150,8 @@
* @return encoded result.
*/
public static String encodeHeaderParameter(String name, String value) {
+ name = name.toLowerCase(Locale.US);
+
// value := token / quoted-string
if (isToken(value)) {
return name + "=" + value;
@@ -401,33 +404,50 @@
return sb.toString();
}
- // RFC 2045 section 5.1
- private static boolean isToken(String str) {
+ /**
+ * Tests whether the specified string is a token as defined in RFC 2045
+ * section 5.1.
+ *
+ * @param str
+ * string to test.
+ * @return <code>true</code> if the specified string is a RFC 2045 token,
+ * <code>false</code> otherwise.
+ */
+ public static boolean isToken(String str) {
// token := 1*<any (US-ASCII) CHAR except SPACE, CTLs, or tspecials>
// tspecials := "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / "\" /
// <"> / "/" / "[" / "]" / "?" / "="
// CTL := 0.- 31., 127.
final int length = str.length();
+ if (length == 0)
+ return false;
+
for (int idx = 0; idx < length; idx++) {
char ch = str.charAt(idx);
if (!TOKEN_CHARS.get(ch))
return false;
}
+
return true;
}
private static boolean isAtomPhrase(String str) {
// atom = [CFWS] 1*atext [CFWS]
+ boolean containsAText = false;
+
final int length = str.length();
for (int idx = 0; idx < length; idx++) {
char ch = str.charAt(idx);
- if (!ATEXT_CHARS.get(ch) && !CharsetUtil.isWhitespace(ch))
+ if (ATEXT_CHARS.get(ch)) {
+ containsAText = true;
+ } else if (!CharsetUtil.isWhitespace(ch)) {
return false;
+ }
}
- return true;
+ return containsAText;
}
// RFC 5322 section 3.2.3
@@ -439,6 +459,9 @@
char prev = '.';
final int length = str.length();
+ if (length == 0)
+ return false;
+
for (int idx = 0; idx < length; idx++) {
char ch = str.charAt(idx);
Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java?rev=739587&r1=739586&r2=739587&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java Sat Jan 31 19:20:25 2009
@@ -42,6 +42,9 @@
public static ContentTypeField contentType(String mimeType,
Map<String, String> parameters) {
+ if (!isValidMimeType(mimeType))
+ throw new IllegalArgumentException();
+
if (parameters == null || parameters.isEmpty()) {
return parse(ContentTypeField.class, Field.CONTENT_TYPE, mimeType);
} else {
@@ -239,6 +242,19 @@
return parse(AddressListField.class, fieldName, fieldValue);
}
+ private static boolean isValidMimeType(String mimeType) {
+ if (mimeType == null)
+ return false;
+
+ int idx = mimeType.indexOf('/');
+ if (idx == -1)
+ return false;
+
+ String type = mimeType.substring(0, idx);
+ String subType = mimeType.substring(idx + 1);
+ return EncoderUtil.isToken(type) && EncoderUtil.isToken(subType);
+ }
+
private static <F extends Field> F parse(Class<F> fieldClass,
String fieldName, String fieldBody) {
try {
Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java?rev=739587&r1=739586&r2=739587&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java Sat Jan 31 19:20:25 2009
@@ -31,7 +31,7 @@
public class EncoderUtilTest extends TestCase {
public void testEncodeAddressDisplayName() throws Exception {
- assertEquals("", EncoderUtil.encodeAddressDisplayName(""));
+ assertEquals("\"\"", EncoderUtil.encodeAddressDisplayName(""));
assertEquals("test", EncoderUtil.encodeAddressDisplayName("test"));
assertEquals(" test ", EncoderUtil.encodeAddressDisplayName(" test "));
assertEquals(" test\ttest ", EncoderUtil