hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject svn commit: r679320 - in /httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils: Idn.java JdkIdn.java Punycode.java Rfc3492Idn.java
Date Thu, 24 Jul 2008 09:21:14 GMT
Author: oglueck
Date: Thu Jul 24 02:21:13 2008
New Revision: 679320

URL: http://svn.apache.org/viewvc?rev=679320&view=rev
Log:
added punycode infrastructure

Added:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Idn.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/JdkIdn.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Punycode.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Rfc3492Idn.java

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Idn.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Idn.java?rev=679320&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Idn.java
(added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Idn.java
Thu Jul 24 02:21:13 2008
@@ -0,0 +1,16 @@
+package org.apache.http.client.utils;
+
+/**
+ * Abstraction of international domain name (IDN) conversion.
+ * 
+ * @author Ortwin Glück
+ */
+public interface Idn {
+    /**
+     * Converts a name from its punycode representation to Unicode.
+     * The name may be a single hostname or a dot-separated qualified domain name.
+     * @param punycode the Punycode representation
+     * @return the Unicode domain name
+     */
+    String toUnicode(String punycode);
+}
\ No newline at end of file

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/JdkIdn.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/JdkIdn.java?rev=679320&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/JdkIdn.java
(added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/JdkIdn.java
Thu Jul 24 02:21:13 2008
@@ -0,0 +1,42 @@
+package org.apache.http.client.utils;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Uses the java.net.IDN class through reflection.
+ * 
+ * @author Ortwin Glück
+ */
+public class JdkIdn implements Idn {
+    private Method toUnicode;
+
+    /**
+     * 
+     * @throws ClassNotFoundException if java.net.IDN is not available
+     */
+    public JdkIdn() throws ClassNotFoundException {
+        Class clazz = Class.forName("java.net.IDN");
+        try {
+            toUnicode = clazz.getMethod("toUnicode", String.class);
+        } catch (SecurityException e) {
+            // doesn't happen
+            throw new IllegalStateException(e.getMessage(), e);
+        } catch (NoSuchMethodException e) {
+            // doesn't happen
+            throw new IllegalStateException(e.getMessage(), e);
+        }
+    }
+
+    public String toUnicode(String punycode) {
+        try {
+            return (String) toUnicode.invoke(null, punycode);
+        } catch (IllegalAccessException e) {
+            throw new IllegalStateException(e.getMessage(), e);
+        } catch (InvocationTargetException e) {
+            Throwable t = e.getCause();
+            throw new RuntimeException(t.getMessage(), t);
+        }
+    }
+    
+}
\ No newline at end of file

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Punycode.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Punycode.java?rev=679320&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Punycode.java
(added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Punycode.java
Thu Jul 24 02:21:13 2008
@@ -0,0 +1,27 @@
+package org.apache.http.client.utils;
+
+
+/**
+ * Facade that provides conversion between Unicode and Punycode domain names.
+ * It will use an appropriate implementation.
+ * 
+ * @author Ortwin Glück
+ */
+public class Punycode {
+    private static Idn impl;
+    static {
+        init();
+    }
+    
+    public static String toUnicode(String punycode) {
+        return impl.toUnicode(punycode);
+    }
+    
+    private static void init() {
+        try {
+            impl = new JdkIdn();
+        } catch (Exception e) {
+            impl = new Rfc3492Idn();
+        }
+    }
+}

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Rfc3492Idn.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Rfc3492Idn.java?rev=679320&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Rfc3492Idn.java
(added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/Rfc3492Idn.java
Thu Jul 24 02:21:13 2008
@@ -0,0 +1,93 @@
+package org.apache.http.client.utils;
+
+import java.util.StringTokenizer;
+
+/**
+ * Implementation from pseudo code in RFC 3492.
+ * 
+ * @author Ortwin Glück
+ */
+public class Rfc3492Idn implements Idn {
+    private static final int base = 36;
+    private static final int tmin = 1;
+    private static final int tmax = 26;
+    private static final int skew = 38;
+    private static final int damp = 700;
+    private static final int initial_bias = 72;
+    private static final int initial_n = 128;
+    private static final char delimiter = '-';
+    private static final String ACE_PREFIX = "xn--";
+    
+    private int adapt(int delta, int numpoints, boolean firsttime) {
+        if (firsttime) delta = delta / damp;
+        else delta = delta / 2;
+        delta = delta + (delta / numpoints);
+        int k = 0;
+        while (delta > ((base - tmin) * tmax) / 2) {
+          delta = delta / (base - tmin);
+          k = k + base;
+        }
+        return k + (((base - tmin + 1) * delta) / (delta + skew));
+    }
+    
+    private int digit(char c) {
+        if ((c >= 'A') && (c <= 'Z')) return (c - 'A');
+        if ((c >= 'a') && (c <= 'z')) return (c - 'a');
+        if ((c >= '0') && (c <= '9')) return (c - '0') + 26;
+        throw new IllegalArgumentException("illegal digit: "+ c);
+    }
+
+    public String toUnicode(String punycode) {
+        StringBuffer unicode = new StringBuffer(punycode.length());
+        StringTokenizer tok = new StringTokenizer(punycode, ".");
+        while (tok.hasMoreTokens()) {
+            String t = tok.nextToken();
+            if (unicode.length() > 0) unicode.append('.');
+            if (t.startsWith(ACE_PREFIX)) t = decode(t.substring(4));
+            unicode.append(t);
+        }
+        return unicode.toString();
+    }
+    
+    protected String decode(String input) {
+        int n = initial_n;
+        int i = 0;
+        int bias = initial_bias;
+        StringBuffer output = new StringBuffer(input.length());
+        int lastdelim = input.lastIndexOf(delimiter);
+        if (lastdelim != -1) {
+            output.append(input.subSequence(0, lastdelim));
+            input = input.substring(lastdelim + 1);
+        }
+
+        while (input.length() > 0) {
+            int oldi = i;
+            int w = 1;
+            for (int k = base;; k += base) {
+                if (input.length() == 0) break;
+                char c = input.charAt(0);
+                input = input.substring(1);
+                int digit = digit(c);
+                i = i + digit * w; // FIXME fail on overflow
+                int t;
+                if (k <= bias + tmin) {
+                    t = tmin;
+                } else if (k >= bias + tmax) {
+                    t = tmax;
+                } else {
+                    t = k - bias;
+                }
+                if (digit < t) break;
+                w = w * (base - t); // FIXME fail on overflow
+            }
+            bias = adapt(i - oldi, output.length() + 1, (oldi == 0));
+            n = n + i / (output.length() + 1); // FIXME fail on overflow
+            i = i % (output.length() + 1);
+            // {if n is a basic code point then fail}
+            output.insert(i, (char) n);
+            i++;
+        }
+        return output.toString();
+    }
+    
+}
\ No newline at end of file



Mime
View raw message