From d9bdaf0f8c14d829489c0d56a5bc0185a1a83868 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 23 Feb 2009 23:44:30 +0100 Subject: [PATCH] Restrict IDN conversion to hostname --- midori/sokoke.c | 38 ++++++++++++++++++++++++++++++-------- tests/magic-uri.c | 4 ++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/midori/sokoke.c b/midori/sokoke.c index 98d9384d..c179692a 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -132,16 +132,35 @@ static gchar* sokoke_idn_to_punycode (gchar* uri) { #if HAVE_LIBIDN + gchar* proto; gchar* hostname; + gchar* path; char *s; uint32_t *q; int rc; gchar *result; - if (g_str_has_prefix (uri, "http://")) - hostname = &uri[7]; - else if (g_str_has_prefix (uri, "https://")) - hostname = &uri[8]; + if ((proto = g_utf8_strchr (uri, -1, ':'))) + { + gulong offset = g_utf8_pointer_to_offset (uri, proto); + 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 hostname = uri; @@ -153,10 +172,13 @@ sokoke_idn_to_punycode (gchar* uri) if (rc != IDNA_SUCCESS) return uri; - if (g_str_has_prefix (uri, "http://")) - result = g_strdup_printf ("http://%s", s); - else if (g_str_has_prefix (uri, "https://")) - result = g_strdup_printf ("https://%s", s); + if (proto) + { + result = g_strdup_printf ("%s://%s%s", proto, s, path ? path : ""); + g_free (proto); + if (path) + g_free (hostname); + } else result = g_strdup (s); g_free (uri); diff --git a/tests/magic-uri.c b/tests/magic-uri.c index ed74c43a..0ada79c6 100644 --- a/tests/magic-uri.c +++ b/tests/magic-uri.c @@ -107,6 +107,10 @@ main (int argc, test_input ("http://東京理科大学.jp", "http://東京理科大学.jp"); test_input ("https://青のネコ.co.jp", "https://青のネコ.co.jp"); #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 東京理科大学", SM "東京理科大学"); -- 2.39.5