Midori.URI.parse_hostname clearer semantics and tests

This commit is contained in:
Christian Dywan 2011-11-18 01:42:02 +01:00
parent 532fa299d4
commit 4c2fd067ff
6 changed files with 30 additions and 13 deletions

View file

@ -109,7 +109,7 @@ addons_install_response (GtkWidget* infobar,
WebKitNetworkRequest* request; WebKitNetworkRequest* request;
WebKitDownload* download; WebKitDownload* download;
hostname = midori_uri_parse (uri, &path); hostname = midori_uri_parse_hostname (uri, &path);
temp_uri = NULL; temp_uri = NULL;
filename = NULL; filename = NULL;
folder = NULL; folder = NULL;
@ -259,7 +259,7 @@ addons_notify_load_status_cb (MidoriView* view,
else else
{ {
gchar* path; gchar* path;
gchar* hostname = midori_uri_parse (uri, &path); gchar* hostname = midori_uri_parse_hostname (uri, &path);
if (!strcmp (hostname, "userscripts.org") if (!strcmp (hostname, "userscripts.org")
&& (g_str_has_prefix (path, "/scripts/show/") && (g_str_has_prefix (path, "/scripts/show/")
|| g_str_has_prefix (path, "/scripts/review/"))) || g_str_has_prefix (path, "/scripts/review/")))

View file

@ -47,7 +47,7 @@ colorful_tabs_view_notify_uri_cb (MidoriView* view,
label = midori_view_get_proxy_tab_label (view); label = midori_view_get_proxy_tab_label (view);
if (!midori_uri_is_blank (midori_view_get_display_uri (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) && katze_object_get_enum (view, "load-status") == MIDORI_LOAD_FINISHED)
{ {
icon = midori_view_get_icon (view); icon = midori_view_get_icon (view);

View file

@ -16,15 +16,14 @@ namespace GLib {
namespace Midori { namespace Midori {
public class URI : Object { public class URI : Object {
public static string parse (string? uri, out string path) { public static string? parse_hostname (string? uri, out string path) {
/* path may be null. /* path may be null. */
If there's no hostname, the original URI is returned */
if (uri == null) if (uri == null)
return uri; return uri;
unowned string? hostname = uri.chr (-1, '/'); unowned string? hostname = uri.chr (-1, '/');
if (hostname == null || hostname[1] != '/' if (hostname == null || hostname[1] != '/'
|| hostname.chr (-1, ' ') != null) || hostname.chr (-1, ' ') != null)
return uri; return null;
hostname = hostname.offset (2); hostname = hostname.offset (2);
if (&path != null) { if (&path != null) {
if ((path = hostname.chr (-1, '/')) != null) if ((path = hostname.chr (-1, '/')) != null)
@ -38,7 +37,7 @@ namespace Midori {
if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null) if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null)
proto = uri.split ("://")[0]; proto = uri.split ("://")[0];
string? path = null; string? path = null;
string hostname = parse (uri, out path); string? hostname = parse_hostname (uri, out path) ?? uri;
string encoded = hostname_to_ascii (hostname); string encoded = hostname_to_ascii (hostname);
if (encoded != null) { if (encoded != null) {
return (proto ?? "") return (proto ?? "")
@ -67,7 +66,7 @@ namespace Midori {
else if (!unescaped.validate ()) else if (!unescaped.validate ())
return uri; return uri;
string path; string path;
string hostname = parse (unescaped, out path); string hostname = parse_hostname (unescaped, out path);
string decoded = hostname_to_unicode (hostname); string decoded = hostname_to_unicode (hostname);
if (decoded != null) if (decoded != null)
return "http://" + decoded + path; return "http://" + decoded + path;

View file

@ -1339,13 +1339,14 @@ midori_view_web_view_database_quota_exceeded_cb (WebKitWebView* web_view,
MidoriView* view) MidoriView* view)
{ {
const gchar* uri = webkit_web_frame_get_uri (web_frame); 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."), gchar* message = g_strdup_printf (_("%s wants to save an HTML5 database."),
hostname && *hostname ? hostname : uri); hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message, midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, message,
G_CALLBACK (midori_view_database_response_cb), database, G_CALLBACK (midori_view_database_response_cb), database,
_("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT, _("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT,
NULL); NULL);
g_free (hostname);
g_free (message); g_free (message);
} }
@ -1368,13 +1369,14 @@ midori_view_web_view_geolocation_decision_cb (WebKitWebView* w
MidoriView* view) MidoriView* view)
{ {
const gchar* uri = webkit_web_frame_get_uri (web_frame); 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."), gchar* message = g_strdup_printf (_("%s wants to know your location."),
hostname && *hostname ? hostname : uri); hostname && *hostname ? hostname : uri);
midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION, midori_view_add_info_bar (view, GTK_MESSAGE_QUESTION,
message, G_CALLBACK (midori_view_location_response_cb), decision, message, G_CALLBACK (midori_view_location_response_cb), decision,
_("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT, _("_Deny"), GTK_RESPONSE_REJECT, _("_Allow"), GTK_RESPONSE_ACCEPT,
NULL); NULL);
g_free (hostname);
g_free (message); g_free (message);
return TRUE; return TRUE;
} }

View file

@ -1433,8 +1433,7 @@ sokoke_prefetch_uri (MidoriWebSettings* settings,
if (settings && !katze_object_get_boolean (settings, "enable-dns-prefetching")) if (settings && !katze_object_get_boolean (settings, "enable-dns-prefetching"))
return FALSE; return FALSE;
if (!(hostname = midori_uri_parse (uri, NULL)) if (!(hostname = midori_uri_parse_hostname (uri, NULL))
|| !strcmp (hostname, uri)
|| g_hostname_is_ip_address (hostname) || g_hostname_is_ip_address (hostname)
|| !midori_uri_is_http (uri)) || !midori_uri_is_http (uri))
{ {

View file

@ -69,6 +69,9 @@ test_input (const gchar* input,
static void static void
magic_uri_uri (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", "ftp://ftp.mozilla.org");
test_input ("ftp://ftp.mozilla.org/pub", "ftp://ftp.mozilla.org/pub"); test_input ("ftp://ftp.mozilla.org/pub", "ftp://ftp.mozilla.org/pub");
test_input ("http://www.example.com", "http://www.example.com"); 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:123@bar.baz", "http://foo:123@bar.baz");
/* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */ /* test_input ("foo:f1o2o3@bar.baz", "http://f1o2o3:foo@bar.baz"); */
/* test_input ("foo:foo@bar.baz", "http://foo: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 static void