Restrict IDN conversion to hostname

This commit is contained in:
Christian Dywan 2009-02-23 23:44:30 +01:00
parent d8bcecc99d
commit d9bdaf0f8c
2 changed files with 34 additions and 8 deletions

View file

@ -132,16 +132,35 @@ static gchar*
sokoke_idn_to_punycode (gchar* uri) sokoke_idn_to_punycode (gchar* uri)
{ {
#if HAVE_LIBIDN #if HAVE_LIBIDN
gchar* proto;
gchar* hostname; gchar* hostname;
gchar* path;
char *s; char *s;
uint32_t *q; uint32_t *q;
int rc; int rc;
gchar *result; gchar *result;
if (g_str_has_prefix (uri, "http://")) if ((proto = g_utf8_strchr (uri, -1, ':')))
hostname = &uri[7]; {
else if (g_str_has_prefix (uri, "https://")) gulong offset = g_utf8_pointer_to_offset (uri, proto);
hostname = &uri[8]; gchar* buffer = g_malloc0 (offset + 1);
g_utf8_strncpy (buffer, uri, offset);
proto = buffer;
}
path = NULL;
if ((hostname = g_utf8_strchr (uri, -1, '/')))
{
if (hostname[1] == '/')
hostname += 2;
if ((path = g_utf8_strchr (hostname, -1, '/')))
{
gulong offset = g_utf8_pointer_to_offset (hostname, path);
gchar* buffer = g_malloc0 (offset);
g_utf8_strncpy (buffer, hostname, offset);
hostname = buffer;
}
}
else else
hostname = uri; hostname = uri;
@ -153,10 +172,13 @@ sokoke_idn_to_punycode (gchar* uri)
if (rc != IDNA_SUCCESS) if (rc != IDNA_SUCCESS)
return uri; return uri;
if (g_str_has_prefix (uri, "http://")) if (proto)
result = g_strdup_printf ("http://%s", s); {
else if (g_str_has_prefix (uri, "https://")) result = g_strdup_printf ("%s://%s%s", proto, s, path ? path : "");
result = g_strdup_printf ("https://%s", s); g_free (proto);
if (path)
g_free (hostname);
}
else else
result = g_strdup (s); result = g_strdup (s);
g_free (uri); g_free (uri);

View file

@ -107,6 +107,10 @@ main (int argc,
test_input ("http://東京理科大学.jp", "http://東京理科大学.jp"); test_input ("http://東京理科大学.jp", "http://東京理科大学.jp");
test_input ("https://青のネコ.co.jp", "https://青のネコ.co.jp"); test_input ("https://青のネコ.co.jp", "https://青のネコ.co.jp");
#endif #endif
test_input ("http://en.wikipedia.org/wiki/Kölsch_language",
"http://en.wikipedia.org/wiki/Kölsch_language");
test_input ("en.wikipedia.org/wiki/Kölsch_language",
"http://en.wikipedia.org/wiki/Kölsch_language");
test_input ("sm Küchenzubehör", SM "Küchenzubehör"); test_input ("sm Küchenzubehör", SM "Küchenzubehör");
test_input ("sm 東京理科大学", SM "東京理科大学"); test_input ("sm 東京理科大学", SM "東京理科大学");