Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting Servlet to Unicode-Domains

I'm having problems with sending redirections to servlet with Unicode-URLs.

i.e. consider the following url in Turkish

http://türkçeisimtescil.com

It works if you paste it into your browser's address bar. However it is translated to

http://xn--trkeisimtescil-ijb74a.com

by your browser upon your request.

Let's say I have first URL with UTF8-specific chars and I get it successfully from DB. I want to redirect my servlet to that URL.

However when I just do response.sendRedirect(url); (according to headers) it redirects me to www.t%1frk%e7eisimtescil.com

I tried even response.sendRedirect("http://www.t\u011Frk\u00E7eisimtescil.com"); (inline encoding) and the response is exactly the same.

Maybe if I obtain türkçeisimtescil.com on the headers, browser will convert it to xn--.. format and it will succeed.

I could not figure out where the encoding got broken. Any helps are appreciated.

like image 856
ahmet alp balkan Avatar asked Aug 12 '10 18:08

ahmet alp balkan


1 Answers

That's an Internationalized Domain Name (IDN). Its conversion between ASCII and Unicode is specified in RFC 3490. In Java, you can use java.net.IDN to convert between the one and other. You can use java.net.URL to obtain the host part from the URL.

String host = new URL("http://türkçeisimtescil.com").getHost();
String idn = IDN.toASCII(host);
String newURL = "http://" + idn;
like image 144
BalusC Avatar answered Oct 21 '22 22:10

BalusC