harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r964687 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/http/ main/java/org/apache/harmony/luni/util/ test/api/common/org/apache/harmony/luni/tests/internal/net/www/pr...
Date Fri, 16 Jul 2010 05:50:04 GMT
Author: regisxu
Date: Fri Jul 16 05:50:04 2010
New Revision: 964687

URL: http://svn.apache.org/viewvc?rev=964687&view=rev
Log:
Apply patch for HARMONY-6581: [classlib][luni] HttpURLConnectionImpl throws IAE if URL has
space or special character in it

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/util/UtilTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java?rev=964687&r1=964686&r2=964687&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java
Fri Jul 16 05:50:04 2010
@@ -52,6 +52,7 @@ import java.util.TimeZone;
 import org.apache.harmony.luni.util.Base64;
 import org.apache.harmony.luni.internal.nls.Messages;
 import org.apache.harmony.luni.util.PriviAction;
+import org.apache.harmony.luni.util.Util;
 
 /**
  * This subclass extends <code>HttpURLConnection</code> which in turns extends
@@ -615,6 +616,7 @@ public class HttpURLConnectionImpl exten
             return;
         }
         try {
+            url = new URL(Util.encodeURL(url.toString()));
             uri = url.toURI();
         } catch (URISyntaxException e1) {
             // ignore

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java?rev=964687&r1=964686&r2=964687&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
Fri Jul 16 05:50:04 2010
@@ -324,7 +324,53 @@ public final class Util {
         }
         return result.toString();
     }
-	
+
+
+    /**
+     * Encode the given url string as RFC-1738 required.
+     * 
+     * @param urlString
+     * @return encoded URL string
+     */
+    public static String encodeURL(String urlStr) {
+        String digits = "0123456789ABCDEF"; //$NON-NLS-1$
+
+        StringBuilder buf = new StringBuilder(urlStr.length() + 16);
+        for (int i = 0; i < urlStr.length(); i++) {
+            char ch = urlStr.charAt(i);
+            if ('%' == ch) {
+                if (i + 1 < urlStr.length()
+                        && i + 2 < urlStr.length()
+                        && digits.indexOf(Character.toUpperCase(urlStr
+                                .charAt(i + 1))) != -1
+                        && digits.indexOf(Character.toUpperCase(urlStr
+                                .charAt(i + 2))) != -1) {
+                    buf.append(ch);
+                    buf.append(urlStr.charAt(i + 1));
+                    buf.append(urlStr.charAt(i + 2));
+                    i += 2;
+                } else {
+                    buf.append("%25"); //$NON-NLS-1$
+                }
+            } else if ("\"<>%\\^[]`+$,{}`~| ".indexOf(ch) == -1) { //$NON-NLS-1$
+                buf.append(ch);
+            } else {
+                byte[] bytes = null;
+                try {
+                    bytes = new String(new char[] { ch }).getBytes("UTF-8"); //$NON-NLS-1$
+                } catch (UnsupportedEncodingException e) {
+                    throw new AssertionError(e);
+                }
+                for (int j = 0; j < bytes.length; j++) {
+                    buf.append('%');
+                    buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
+                    buf.append(digits.charAt(bytes[j] & 0xf));
+                }
+            }
+        }
+        return buf.toString();
+    }
+
 	public static String toASCIILowerCase(String s) {
         int len = s.length();
 		StringBuilder buffer = new StringBuilder(len);

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java?rev=964687&r1=964686&r2=964687&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
Fri Jul 16 05:50:04 2010
@@ -466,6 +466,34 @@ public class HttpURLConnectionTest exten
     }
 
     /**
+     * @tests the url with space
+     */
+    public void testConnectWithSpaceinURL() throws Exception {
+        String jettyURLwithSpace = "http://localhost:" + jettyPort
+                + "/servlet with space";
+        HttpURLConnection httpURLConnect = (HttpURLConnection) new URL(
+                jettyURLwithSpace).openConnection();
+        httpURLConnect.setDoOutput(true);
+        httpURLConnect.connect();
+        assertEquals(200, httpURLConnect.getResponseCode());
+        assertEquals("OK", httpURLConnect.getResponseMessage());
+    }
+
+    /**
+     * @tests the url with space
+     */
+    public void testConnectWithSpaceinURL1() throws Exception {
+        String jettyURLwithSpace = "http://localhost:" + jettyPort
+                + "/servlet with space?arg1=value>1&arg2=%aval%1Aue&arg3=#";
+        HttpURLConnection httpURLConnect = (HttpURLConnection) new URL(
+                jettyURLwithSpace).openConnection();
+        httpURLConnect.setDoOutput(true);
+        httpURLConnect.connect();
+        assertEquals(200, httpURLConnect.getResponseCode());
+        assertEquals("OK", httpURLConnect.getResponseMessage());
+    }
+
+    /**
      * @tests org.apache.harmony.luni.internal.net.www.http.getOutputStream()
      */
     public void testGetOutputStream() throws Exception {

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/util/UtilTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/util/UtilTest.java?rev=964687&r1=964686&r2=964687&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/util/UtilTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/util/UtilTest.java
Fri Jul 16 05:50:04 2010
@@ -55,4 +55,12 @@ public class UtilTest extends TestCase {
         }
     }
 
+    public void testEncodeURL() {
+        assertEquals("http://test.com/%25", Util.encodeURL("http://test.com/%"));
+        assertEquals("http://test.com/%250", Util.encodeURL("http://test.com/%0"));
+        assertEquals("http://test.com/%2b", Util.encodeURL("http://test.com/%2b"));
+        assertEquals("http://test.com/%2b/index", Util.encodeURL("http://test.com/%2b/index"));
+        assertEquals("http://test.com/%252z", Util.encodeURL("http://test.com/%2z"));
+        assertEquals("http://test.com/with%20space", Util.encodeURL("http://test.com/with
space"));
+    }
 }



Mime
View raw message