From 555f7a57e44e56049fc48f92c6ba2282780c479c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20St=C3=B6sel?= Date: Tue, 16 Mar 2010 23:58:34 +0100 Subject: [PATCH] Implement sokoke_resolve_hostname sokoke_prefetch_uri is extended take a callback and user data. sokoke_resolve_hostname is implemented for resolving hostnames based on a maximum timeout. sokoke_magic_uri resolves localhost and uris with a / to verify if there is a local domain, otherwise falls back to search. Thanks to Andy Kittner for input on proceeding the event loop while resolving asynchronously. --- midori/midori-browser.c | 2 +- midori/midori-view.c | 2 +- midori/sokoke.c | 50 ++++++++++++++++++++++++++++++++++++++--- midori/sokoke.h | 7 +++++- tests/magic-uri.c | 33 ++++++++++++++------------- 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 94cf7ece..909aced2 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -2806,7 +2806,7 @@ midori_browser_menu_item_select_cb (GtkWidget* menuitem, if (item) { tooltip = g_strdup (katze_item_get_uri (item)); - sokoke_prefetch_uri (tooltip); + sokoke_prefetch_uri (tooltip, NULL, NULL); } } _midori_browser_set_statusbar_text (browser, tooltip); diff --git a/midori/midori-view.c b/midori/midori-view.c index 67cf8c15..3f9d2158 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -1386,7 +1386,7 @@ webkit_web_view_hovering_over_link_cb (WebKitWebView* web_view, MidoriView* view) { #if !(WEBKIT_CHECK_VERSION (2, 18, 0) && defined (HAVE_LIBSOUP_2_29_3)) - sokoke_prefetch_uri (link_uri); + sokoke_prefetch_uri (link_uri, NULL, NULL); #endif katze_assign (view->link_uri, g_strdup (link_uri)); diff --git a/midori/sokoke.c b/midori/sokoke.c index a26078a6..8bcafe6b 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -640,6 +640,45 @@ gchar* sokoke_search_uri (const gchar* uri, return search; } +static void +sokoke_resolve_hostname_cb (SoupAddress *address, + guint status, + gpointer data) +{ + if (status == SOUP_STATUS_OK) + *(gint *)data = 1; + else + *(gint *)data = 2; +} + +/** + * sokoke_resolve_hostname + * @hostname: a string typed by a user + * + * Takes a string that was typed by a user, + * resolves the hostname, and returns the status. + * + * Return value: %TRUE if is a valid host, else %FALSE + **/ +gboolean +sokoke_resolve_hostname (const gchar* hostname) +{ + gchar* uri; + gint host_resolved = 0; + + uri = g_strconcat ("http://", hostname, NULL); + if (sokoke_prefetch_uri (uri, sokoke_resolve_hostname_cb, + &host_resolved)) + { + GTimer* timer = g_timer_new (); + while (!host_resolved && g_timer_elapsed (timer, NULL) < 10) + g_main_context_iteration (NULL, FALSE); + g_timer_destroy (timer); + } + g_free (uri); + return host_resolved == 1 ? TRUE : FALSE; +} + /** * sokoke_magic_uri: * @uri: a string typed by a user @@ -682,7 +721,8 @@ sokoke_magic_uri (const gchar* uri) ((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) && search[0] && !g_ascii_isalpha (search[1])) return sokoke_idn_to_punycode (g_strconcat ("http://", uri, NULL)); - if (!strncmp (uri, "localhost", 9) && (uri[9] == '\0' || uri[9] == '/')) + if ((!strcmp (uri, "localhost") || strchr (uri, '/')) + && sokoke_resolve_hostname (uri)) return g_strconcat ("http://", uri, NULL); if (!search) { @@ -1690,7 +1730,9 @@ sokoke_file_chooser_dialog_new (const gchar* title, * Return value: %TRUE on success **/ gboolean -sokoke_prefetch_uri (const char* uri) +sokoke_prefetch_uri (const char* uri, + SoupAddressCallback callback, + gpointer user_data) { #define MAXHOSTS 50 static gchar* hosts = NULL; @@ -1727,7 +1769,7 @@ sokoke_prefetch_uri (const char* uri) gchar* new_hosts; address = soup_address_new (s_uri->host, SOUP_ADDRESS_ANY_PORT); - soup_address_resolve_async (address, 0, 0, 0, 0); + soup_address_resolve_async (address, 0, 0, callback, user_data); g_object_unref (address); if (host_count > MAXHOSTS) @@ -1739,6 +1781,8 @@ sokoke_prefetch_uri (const char* uri) new_hosts = g_strdup_printf ("%s|%s", hosts, s_uri->host); katze_assign (hosts, new_hosts); } + else if (callback) + callback (NULL, SOUP_STATUS_OK, user_data); soup_uri_free (s_uri); return TRUE; } diff --git a/midori/sokoke.h b/midori/sokoke.h index 927cfee9..e91bc692 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -221,7 +221,12 @@ sokoke_file_chooser_dialog_new (const gchar* title, GtkFileChooserAction action); gboolean -sokoke_prefetch_uri (const char* uri); +sokoke_prefetch_uri (const char* uri, + SoupAddressCallback callback, + gpointer user_data); + +gboolean +sokoke_resolve_hostname (const gchar* hostname); gchar * sokoke_accept_languages (const gchar* const * lang_names); diff --git a/tests/magic-uri.c b/tests/magic-uri.c index 9886bdbb..07c00cb2 100644 --- a/tests/magic-uri.c +++ b/tests/magic-uri.c @@ -95,9 +95,12 @@ magic_uri_uri (void) test_input ("example.com", "http://example.com"); test_input ("www.google..com", "http://www.google..com"); test_input ("/home/user/midori.html", "file:///home/user/midori.html"); - test_input ("localhost", "http://localhost"); - test_input ("localhost:8000", "http://localhost:8000"); - test_input ("localhost/rss", "http://localhost/rss"); + if (sokoke_resolve_hostname ("localhost")) + { + test_input ("localhost", "http://localhost"); + test_input ("localhost:8000", "http://localhost:8000"); + test_input ("localhost/rss", "http://localhost/rss"); + } test_input ("10.0.0.1", "http://10.0.0.1"); test_input ("192.168.1.1", "http://192.168.1.1"); test_input ("192.168.1.1:8000", "http://192.168.1.1:8000"); @@ -255,18 +258,18 @@ magic_uri_format (void) static void magic_uri_prefetch (void) { - g_assert (!sokoke_prefetch_uri (NULL)); - g_assert (sokoke_prefetch_uri ("http://google.com")); - g_assert (sokoke_prefetch_uri ("http://google.com")); - g_assert (sokoke_prefetch_uri ("http://googlecom")); - g_assert (sokoke_prefetch_uri ("http://1kino.com")); - g_assert (sokoke_prefetch_uri ("http://")); - g_assert (!sokoke_prefetch_uri ("http:/")); - g_assert (!sokoke_prefetch_uri ("http")); - g_assert (!sokoke_prefetch_uri ("ftp://ftphost.org")); - g_assert (!sokoke_prefetch_uri ("http://10.0.0.1")); - g_assert (!sokoke_prefetch_uri ("about:blank")); - g_assert (!sokoke_prefetch_uri ("javascript: alert()")); + g_assert (!sokoke_prefetch_uri (NULL, NULL, NULL)); + g_assert (sokoke_prefetch_uri ("http://google.com", NULL, NULL)); + g_assert (sokoke_prefetch_uri ("http://google.com", NULL, NULL)); + g_assert (sokoke_prefetch_uri ("http://googlecom", NULL, NULL)); + g_assert (sokoke_prefetch_uri ("http://1kino.com", NULL, NULL)); + g_assert (sokoke_prefetch_uri ("http://", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("http:/", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("http", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("ftp://ftphost.org", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("http://10.0.0.1", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("about:blank", NULL, NULL)); + g_assert (!sokoke_prefetch_uri ("javascript: alert()", NULL, NULL)); } int