added punycode infrastructure

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@679320 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ortwin Glueck 2008-07-24 09:21:13 +00:00
parent ad0d0b1f17
commit 114e1d22fe
4 changed files with 178 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}