return TRUE;
}
+#if defined (HAVE_LIBSOUP_2_27_90) || HAVE_LIBIDN
+/**
+ * sokoke_hostname_from_uri:
+ * @uri: an URI string
+ * @path: location of a string pointer
+ *
+ * Returns the hostname of the specified URI,
+ * and stores the path in @path.
+ * @path is at least set to ""
+ *
+ * Return value: a newly allocated hostname
+ **/
+static gchar*
+sokoke_hostname_from_uri (const gchar* uri,
+ gchar** path)
+{
+ gchar* hostname;
+
+ *path = "";
+ 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 + 1);
+ g_utf8_strncpy (buffer, hostname, offset);
+ hostname = buffer;
+ }
+ else
+ hostname = g_strdup (hostname);
+ }
+ else
+ hostname = g_strdup (uri);
+ return hostname;
+}
+#endif
+
+/**
+ * sokoke_idn_to_punycode:
+ * @uri: an URI string
+ *
+ * The specified URI is parsed and the hostname
+ * part of it is encoded if it is not ASCII.
+ *
+ * If libIDN is not available at compile time,
+ * this code will pass the string unaltered.
+ *
+ * The called function owns the passed string.
+ *
+ * Return value: a newly allocated ASCII URI
+ **/
gchar*
sokoke_idn_to_punycode (gchar* uri)
{
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 + 1);
- g_utf8_strncpy (buffer, hostname, offset);
- hostname = buffer;
- }
- else
- hostname = g_strdup (hostname);
- }
- else
- hostname = g_strdup (uri);
+ hostname = sokoke_hostname_from_uri (uri, &path);
if (!(q = stringprep_utf8_to_ucs4 (hostname, -1, NULL)))
{
return search;
}
+/**
+ * sokoke_magic_uri:
+ * @uri: a string typed by a user
+ * @search_engines: search engines
+ *
+ * Takes a string that was typed by a user,
+ * guesses what it is, and returns an URI.
+ *
+ * Return value: a newly allocated URI
+ **/
gchar*
sokoke_magic_uri (const gchar* uri,
KatzeArray* search_engines)
return search;
}
+
/**
* sokoke_format_uri_for_display:
* @uri: an URI string
{
gchar* unescaped = g_uri_unescape_string (uri, NULL);
#ifdef HAVE_LIBSOUP_2_27_90
- gchar* decoded = g_hostname_to_unicode (unescaped);
+ gchar* path;
+ gchar* hostname = sokoke_hostname_from_uri (unescaped, &path);
+ gchar* decoded = g_hostname_to_unicode (hostname);
+
if (decoded)
{
+ gchar* result = g_strconcat ("http://", decoded, path, NULL);
g_free (unescaped);
- return decoded;
+ g_free (decoded);
+ g_free (hostname);
+ return result;
}
+ g_free (hostname);
return unescaped;
#elif HAVE_LIBIDN
gchar* decoded;
#define SM "http://www.searchmash.com/search/"
+static void
+sokoke_assert_str_equal (const gchar* input,
+ const gchar* result,
+ const gchar* expected)
+{
+ if (g_strcmp0 (result, expected))
+ {
+ g_error ("Input: %s\nExpected: %s\nResult: %s",
+ input ? input : "NULL",
+ expected ? expected : "NULL",
+ result ? result : "NULL");
+ }
+}
+
static void
test_input (const gchar* input,
const gchar* expected)
}
gchar* uri = sokoke_magic_uri (input, search_engines);
- if (g_strcmp0 (uri, expected))
- {
- g_error ("Input: %s\nExpected: %s\nResult: %s",
- input ? input : "NULL",
- expected ? expected : "NULL",
- uri ? uri : "NULL");
- }
+ sokoke_assert_str_equal (input, uri, expected);
g_free (uri);
}
g_print ("\nTime needed for URI tests: %f ", g_test_timer_elapsed ());
}
+static void
+magic_uri_format (void)
+{
+ typedef struct
+ {
+ const gchar* before;
+ const gchar* after;
+ } URIItem;
+
+ static const URIItem items[] = {
+ { "http://www.csszengarden.com", NULL },
+ { "http://live.gnome.org/GTK+/3.0/Tasks", NULL },
+ { "http://www.johannkönig.com/index.php?ausw=home", NULL },
+ { "http://digilife.bz/wiki/index.php?Python%E3%81%AE%E9%96%8B%E7%99%BA%E6%89%8B%E9%A0%86",
+ "http://digilife.bz/wiki/index.php?Pythonの開発手順" },
+ { "http://die-welt.net/~evgeni/LenovoBatteryLinux/", NULL },
+ { "http://wiki.c3sl.ufpr.br/multiseat/index.php/Xephyr_Solution", NULL },
+ };
+ guint i;
+
+ for (i = 0; i < G_N_ELEMENTS (items); i++)
+ {
+ gchar* result = sokoke_format_uri_for_display (items[i].before);
+ const gchar* after = items[i].after ? items[i].after : items[i].before;
+ sokoke_assert_str_equal (items[i].before, result, after);
+ g_free (result);
+ }
+}
+
int
main (int argc,
char** argv)
g_test_add_func ("/magic-uri/search", magic_uri_search);
g_test_add_func ("/magic-uri/pseudo", magic_uri_pseudo);
g_test_add_func ("/magic-uri/performance", magic_uri_performance);
+ g_test_add_func ("/magic-uri/format", magic_uri_format);
return g_test_run ();
}