Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 21758 invoked from network); 8 Nov 2007 18:32:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 8 Nov 2007 18:32:12 -0000 Received: (qmail 51289 invoked by uid 500); 8 Nov 2007 18:31:59 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 51242 invoked by uid 500); 8 Nov 2007 18:31:59 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 51231 invoked by uid 99); 8 Nov 2007 18:31:59 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 08 Nov 2007 10:31:59 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED,HTTP_ESCAPED_HOST X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 08 Nov 2007 18:32:11 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id DADA61A9832; Thu, 8 Nov 2007 10:31:49 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r593244 - in /geronimo/specs/trunk/geronimo-javamail_1.4_spec/src: main/java/javax/mail/URLName.java test/java/javax/mail/URLNameTest.java Date: Thu, 08 Nov 2007 18:31:49 -0000 To: scm@geronimo.apache.org From: rickmcguire@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071108183149.DADA61A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rickmcguire Date: Thu Nov 8 10:31:48 2007 New Revision: 593244 URL: http://svn.apache.org/viewvc?rev=593244&view=rev Log: GERONIMO-3591 javamail URLName() class does not HTTP encode URL username elements Modified: geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java Modified: geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java?rev=593244&r1=593243&r2=593244&view=diff ============================================================================== --- geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java (original) +++ geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java Thu Nov 8 10:31:48 2007 @@ -19,6 +19,7 @@ package javax.mail; +import java.io.ByteArrayOutputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -28,6 +29,8 @@ * @version $Rev$ $Date$ */ public class URLName { + private static final String nonEncodedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.*"; + private String file; private String host; private String password; @@ -109,6 +112,8 @@ } else { this.password = null; } + username = encode(username); + password = encode(password); updateFullURL(); } @@ -151,9 +156,9 @@ if (host != null) { buf.append("//"); if (username != null) { - buf.append(username); + buf.append(encode(username)); if (password != null) { - buf.append(':').append(password); + buf.append(':').append(encode(password)); } buf.append('@'); } @@ -237,5 +242,69 @@ public String getUsername() { return username; + } + + /** + * Perform an HTTP encoding to the username and + * password elements of the URLName. + * + * @param v The input (uncoded) string. + * + * @return The HTTP encoded version of the string. + */ + private static String encode(String v) { + // make sure we don't operate on a null string + if (v == null) { + return null; + } + boolean needsEncoding = false; + for (int i = 0; i < v.length(); i++) { + // not in the list of things that don't need encoding? + if (nonEncodedChars.indexOf(v.charAt(i)) == -1) { + // got to do this the hard way + needsEncoding = true; + break; + } + } + // just fine the way it is. + if (!needsEncoding) { + return v; + } + + // we know we're going to be larger, but not sure by how much. + // just give a little extra + StringBuffer encoded = new StringBuffer(v.length() + 10); + + // we get the bytes so that we can have the default encoding applied to + // this string. This will flag the ones we need to give special processing to. + byte[] data = v.getBytes(); + + for (int i = 0; i < data.length; i++) { + // pick this up as a one-byte character The 7-bit ascii ones will be fine + // here. + char ch = (char)(data[i] & 0xff); + // blanks get special treatment + if (ch == ' ') { + encoded.append('+'); + } + // not in the list of things that don't need encoding? + else if (nonEncodedChars.indexOf(ch) == -1) { + // forDigit() uses the lowercase letters for the radix. The HTML specifications + // require the uppercase letters. + char firstChar = Character.toUpperCase(Character.forDigit((ch >> 4) & 0xf, 16)); + char secondChar = Character.toUpperCase(Character.forDigit(ch & 0xf, 16)); + + // now append the encoded triplet. + encoded.append('%'); + encoded.append(firstChar); + encoded.append(secondChar); + } + else { + // just add this one to the buffer + encoded.append(ch); + } + } + // convert to string form. + return encoded.toString(); } } Modified: geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java?rev=593244&r1=593243&r2=593244&view=diff ============================================================================== --- geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java (original) +++ geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java Thu Nov 8 10:31:48 2007 @@ -115,6 +115,22 @@ } catch (MalformedURLException e) { fail(); } + + s = "http://john%40gmail.com:doe@www.apache.org/file/"; + name = new URLName(s); + assertEquals(s, name.toString()); + assertEquals("http", name.getProtocol()); + assertEquals("www.apache.org", name.getHost()); + assertEquals(-1, name.getPort()); + assertEquals("/file/", name.getFile()); + assertNull(name.getRef()); + assertEquals("john@gmail.com", name.getUsername()); + assertEquals("doe", name.getPassword()); + try { + assertEquals(new URL(s), name.getURL()); + } catch (MalformedURLException e) { + fail(); + } s = "file/file2"; name = new URLName(s); @@ -266,6 +282,21 @@ assertEquals("doe", name.getPassword()); try { assertEquals(new URL("http://john:doe@www.apache.org/file/file2"), name.getURL()); + } catch (MalformedURLException e) { + fail(); + } + + name = new URLName("http", "www.apache.org", -1, "/file/file2", "john@gmail.com", "doe"); + assertEquals("http://john%40gmail.com:doe@www.apache.org/file/file2", name.toString()); + assertEquals("http", name.getProtocol()); + assertEquals("www.apache.org", name.getHost()); + assertEquals(-1, name.getPort()); + assertEquals("/file/file2", name.getFile()); + assertNull(name.getRef()); + assertEquals("john@gmail.com", name.getUsername()); + assertEquals("doe", name.getPassword()); + try { + assertEquals(new URL("http://john%40gmail.com:doe@www.apache.org/file/file2"), name.getURL()); } catch (MalformedURLException e) { fail(); }