From 4c2fd067ff39d86d8447643c13cea56c54575adb Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Fri, 18 Nov 2011 01:42:02 +0100 Subject: [PATCH] Midori.URI.parse_hostname clearer semantics and tests --- extensions/addons.c | 4 ++-- extensions/colorful-tabs.c | 2 +- katze/midori-uri.vala | 11 +++++------ midori/midori-view.c | 6 ++++-- midori/sokoke.c | 3 +-- tests/magic-uri.c | 17 +++++++++++++++++ 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/extensions/addons.c b/extensions/addons.c index 5ded3fa8..eb2d23fd 100644 --- a/extensions/addons.c +++ b/extensions/addons.c @@ -109,7 +109,7 @@ addons_install_response (GtkWidget* infobar, WebKitNetworkRequest* request; WebKitDownload* download; - hostname = midori_uri_parse (uri, &path); + hostname = midori_uri_parse_hostname (uri, &path); temp_uri = NULL; filename = NULL; folder = NULL; @@ -259,7 +259,7 @@ addons_notify_load_status_cb (MidoriView* view, else { gchar* path; - gchar* hostname = midori_uri_parse (uri, &path); + gchar* hostname = midori_uri_parse_hostname (uri, &path); if (!strcmp (hostname, "userscripts.org") && (g_str_has_prefix (path, "/scripts/show/") || g_str_has_prefix (path, "/scripts/review/"))) diff --git a/extensions/colorful-tabs.c b/extensions/colorful-tabs.c index c00122f8..bb261d58 100644 --- a/extensions/colorful-tabs.c +++ b/extensions/colorful-tabs.c @@ -47,7 +47,7 @@ colorful_tabs_view_notify_uri_cb (MidoriView* view, label = midori_view_get_proxy_tab_label (view); if (!midori_uri_is_blank (midori_view_get_display_uri (view)) - && (hostname = midori_uri_parse (midori_view_get_display_uri (view), NULL)) + && (hostname = midori_uri_parse_hostname (midori_view_get_display_uri (view), NULL)) && katze_object_get_enum (view, "load-status") == MIDORI_LOAD_FINISHED) { icon = midori_view_get_icon (view); diff --git a/katze/midori-uri.vala b/katze/midori-uri.vala index 48063571..8fb7bcab 100644 --- a/katze/midori-uri.vala +++ b/katze/midori-uri.vala @@ -16,15 +16,14 @@ namespace GLib { namespace Midori { public class URI : Object { - public static string parse (string? uri, out string path) { - /* path may be null. - If there's no hostname, the original URI is returned */ + public static string? parse_hostname (string? uri, out string path) { + /* path may be null. */ if (uri == null) return uri; unowned string? hostname = uri.chr (-1, '/'); if (hostname == null || hostname[1] != '/' || hostname.chr (-1, ' ') != null) - return uri; + return null; hostname = hostname.offset (2); if (&path != null) { if ((path = hostname.chr (-1, '/')) != null) @@ -38,7 +37,7 @@ namespace Midori { if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null) proto = uri.split ("://")[0]; string? path = null; - string hostname = parse (uri, out path); + string? hostname = parse_hostname (uri, out path) ?? uri; string encoded = hostname_to_ascii (hostname); if (encoded != null) { return (proto ?? "") @@ -67,7 +66,7 @@ namespace Midori { else if (!unescaped.validate ()) return uri; string path; - string hostname = parse (unescaped, out path); + string hostname = parse_hostname (unescaped, out path); string decoded = hostname_to_unicode (hostname); if (decoded != null) return "http://" + decoded + path; diff --git a/midori/midori-view.c b/midori/midori-view.c index 11452f76..204e854f 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -1339,13 +1339,14 @@ midori_view_web_view_database_quota_exceeded_cb (WebKitWebView* web_view, MidoriView* view) { const gchar* uri = webkit_web_frame_get_uri (web_frame); - const gchar* hostname = midori_uri_parse (uri, NULL); + gchar* hostname = midori_uri_parse_hostname (uri, NULL); gchar* message = g_strdup_printf (_("%s wants to save an HTML5 database."), hostname && *hostname ? hostname : uri); midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message, G_CALLBACK (midori_view_database_response_cb), database, _("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT, NULL); + g_free (hostname); g_free (message); } @@ -1368,13 +1369,14 @@ midori_view_web_view_geolocation_decision_cb (WebKitWebView* w MidoriView* view) { const gchar* uri = webkit_web_frame_get_uri (web_frame); - const gchar* hostname = midori_uri_parse (uri, NULL); + gchar* hostname = midori_uri_parse_hostname (uri, NULL); gchar* message = g_strdup_printf (_("%s wants to know your location."), hostname && *hostname ? hostname : uri); midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message, G_CALLBACK (midori_view_location_response_cb), decision, _("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT, NULL); + g_free (hostname); g_free (message); return TRUE; } diff --git a/midori/sokoke.c b/midori/sokoke.c index 48dc974e..bb6616dc 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1433,8 +1433,7 @@ sokoke_prefetch_uri (MidoriWebSettings* settings, if (settings && !katze_object_get_boolean (settings, "enable-dns-prefetching")) return FALSE; - if (!(hostname = midori_uri_parse (uri, NULL)) - || !strcmp (hostname, uri) + if (!(hostname = midori_uri_parse_hostname (uri, NULL)) || g_hostname_is_ip_address (hostname) || !midori_uri_is_http (uri)) { diff --git a/tests/magic-uri.c b/tests/magic-uri.c index 008d1b51..d596dd52 100644 --- a/tests/magic-uri.c +++ b/tests/magic-uri.c @@ -69,6 +69,9 @@ test_input (const gchar* input, static void magic_uri_uri (void) { + const gchar* uri; + gchar* path; + test_input ("ftp://ftp.mozilla.org", "ftp://ftp.mozilla.org"); test_input ("ftp://ftp.mozilla.org/pub", "ftp://ftp.mozilla.org/pub"); test_input ("http://www.example.com", "http://www.example.com"); @@ -93,6 +96,20 @@ magic_uri_uri (void) test_input ("foo:123@bar.baz", "http://foo:123@bar.baz"); /* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */ /* test_input ("foo:foo@bar.baz", "http://foo:foo@bar.baz"); */ + + uri = "http://bugs.launchpad.net/midori"; + g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL)); + uri = "https://bugs.launchpad.net/midori"; + g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, NULL)); + g_assert_cmpstr ("bugs.launchpad.net", ==, midori_uri_parse_hostname (uri, &path)); + g_assert_cmpstr ("/midori", ==, path); + uri = "http://айкидо.ru/users/kotyata"; + g_assert_cmpstr ("айкидо.ru", ==, midori_uri_parse_hostname (uri, &path)); + g_assert_cmpstr ("/users/kotyata", ==, path); + uri = "invalid:/uri.like/thing"; + g_assert_cmpstr (NULL, ==, midori_uri_parse_hostname (uri, NULL)); + uri = "invalid-uri.like:thing"; + g_assert_cmpstr (NULL, ==, midori_uri_parse_hostname (uri, NULL)); } static void