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 <andkit@gmx.de> for input on proceeding
the event loop while resolving asynchronously.
This commit is contained in:
André Stösel 2010-03-16 23:58:34 +01:00 committed by Christian Dywan
parent 65785d3d80
commit 555f7a57e4
5 changed files with 73 additions and 21 deletions

View file

@ -2806,7 +2806,7 @@ midori_browser_menu_item_select_cb (GtkWidget* menuitem,
if (item) if (item)
{ {
tooltip = g_strdup (katze_item_get_uri (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); _midori_browser_set_statusbar_text (browser, tooltip);

View file

@ -1386,7 +1386,7 @@ webkit_web_view_hovering_over_link_cb (WebKitWebView* web_view,
MidoriView* view) MidoriView* view)
{ {
#if !(WEBKIT_CHECK_VERSION (2, 18, 0) && defined (HAVE_LIBSOUP_2_29_3)) #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 #endif
katze_assign (view->link_uri, g_strdup (link_uri)); katze_assign (view->link_uri, g_strdup (link_uri));

View file

@ -640,6 +640,45 @@ gchar* sokoke_search_uri (const gchar* uri,
return search; 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: * sokoke_magic_uri:
* @uri: a string typed by a user * @uri: a string typed by a user
@ -682,7 +721,8 @@ sokoke_magic_uri (const gchar* uri)
((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) && ((search = strchr (uri, ':')) || (search = strchr (uri, '@'))) &&
search[0] && !g_ascii_isalpha (search[1])) search[0] && !g_ascii_isalpha (search[1]))
return sokoke_idn_to_punycode (g_strconcat ("http://", uri, NULL)); 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); return g_strconcat ("http://", uri, NULL);
if (!search) if (!search)
{ {
@ -1690,7 +1730,9 @@ sokoke_file_chooser_dialog_new (const gchar* title,
* Return value: %TRUE on success * Return value: %TRUE on success
**/ **/
gboolean gboolean
sokoke_prefetch_uri (const char* uri) sokoke_prefetch_uri (const char* uri,
SoupAddressCallback callback,
gpointer user_data)
{ {
#define MAXHOSTS 50 #define MAXHOSTS 50
static gchar* hosts = NULL; static gchar* hosts = NULL;
@ -1727,7 +1769,7 @@ sokoke_prefetch_uri (const char* uri)
gchar* new_hosts; gchar* new_hosts;
address = soup_address_new (s_uri->host, SOUP_ADDRESS_ANY_PORT); 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); g_object_unref (address);
if (host_count > MAXHOSTS) 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); new_hosts = g_strdup_printf ("%s|%s", hosts, s_uri->host);
katze_assign (hosts, new_hosts); katze_assign (hosts, new_hosts);
} }
else if (callback)
callback (NULL, SOUP_STATUS_OK, user_data);
soup_uri_free (s_uri); soup_uri_free (s_uri);
return TRUE; return TRUE;
} }

View file

@ -221,7 +221,12 @@ sokoke_file_chooser_dialog_new (const gchar* title,
GtkFileChooserAction action); GtkFileChooserAction action);
gboolean 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 * gchar *
sokoke_accept_languages (const gchar* const * lang_names); sokoke_accept_languages (const gchar* const * lang_names);

View file

@ -95,9 +95,12 @@ magic_uri_uri (void)
test_input ("example.com", "http://example.com"); test_input ("example.com", "http://example.com");
test_input ("www.google..com", "http://www.google..com"); test_input ("www.google..com", "http://www.google..com");
test_input ("/home/user/midori.html", "file:///home/user/midori.html"); test_input ("/home/user/midori.html", "file:///home/user/midori.html");
if (sokoke_resolve_hostname ("localhost"))
{
test_input ("localhost", "http://localhost"); test_input ("localhost", "http://localhost");
test_input ("localhost:8000", "http://localhost:8000"); test_input ("localhost:8000", "http://localhost:8000");
test_input ("localhost/rss", "http://localhost/rss"); test_input ("localhost/rss", "http://localhost/rss");
}
test_input ("10.0.0.1", "http://10.0.0.1"); 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", "http://192.168.1.1");
test_input ("192.168.1.1:8000", "http://192.168.1.1:8000"); test_input ("192.168.1.1:8000", "http://192.168.1.1:8000");
@ -255,18 +258,18 @@ magic_uri_format (void)
static void static void
magic_uri_prefetch (void) magic_uri_prefetch (void)
{ {
g_assert (!sokoke_prefetch_uri (NULL)); g_assert (!sokoke_prefetch_uri (NULL, NULL, NULL));
g_assert (sokoke_prefetch_uri ("http://google.com")); g_assert (sokoke_prefetch_uri ("http://google.com", NULL, NULL));
g_assert (sokoke_prefetch_uri ("http://google.com")); g_assert (sokoke_prefetch_uri ("http://google.com", NULL, NULL));
g_assert (sokoke_prefetch_uri ("http://googlecom")); g_assert (sokoke_prefetch_uri ("http://googlecom", NULL, NULL));
g_assert (sokoke_prefetch_uri ("http://1kino.com")); g_assert (sokoke_prefetch_uri ("http://1kino.com", NULL, NULL));
g_assert (sokoke_prefetch_uri ("http://")); g_assert (sokoke_prefetch_uri ("http://", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("http:/")); g_assert (!sokoke_prefetch_uri ("http:/", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("http")); g_assert (!sokoke_prefetch_uri ("http", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("ftp://ftphost.org")); g_assert (!sokoke_prefetch_uri ("ftp://ftphost.org", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("http://10.0.0.1")); g_assert (!sokoke_prefetch_uri ("http://10.0.0.1", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("about:blank")); g_assert (!sokoke_prefetch_uri ("about:blank", NULL, NULL));
g_assert (!sokoke_prefetch_uri ("javascript: alert()")); g_assert (!sokoke_prefetch_uri ("javascript: alert()", NULL, NULL));
} }
int int