From 0c611f8d6a2b94b849ceac8fc20c533f2d98e323 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 7 Nov 2011 23:07:57 +0100 Subject: [PATCH] Move link fingers to Midori.URI and add tests --- katze/midori-uri.vala | 26 ++++++++++++++++++++++++++ midori/midori-view.c | 24 +++++++----------------- tests/magic-uri.c | 17 +++++++++++++++++ toolbars/midori-transferbar.c | 35 ++++++++--------------------------- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/katze/midori-uri.vala b/katze/midori-uri.vala index 6b1a612f..d8dd5ded 100644 --- a/katze/midori-uri.vala +++ b/katze/midori-uri.vala @@ -126,5 +126,31 @@ namespace Midori { && uri.chr (-1, ' ') == null && (URI.is_location (uri) || uri.chr (-1, '.') != null); } + public static GLib.ChecksumType get_fingerprint (string uri, + out string checksum, out string label) { + + /* http://foo.bar/baz/spam.eggs#!algo!123456 */ + unowned string delimiter = "#!md5!"; + unowned string display = _("MD5-Checksum:"); + GLib.ChecksumType type = GLib.ChecksumType.MD5; + unowned string? fragment = uri.str (delimiter); + if (fragment == null) { + delimiter = "#!sha1!"; + display = _("SHA1-Checksum:"); + type = GLib.ChecksumType.SHA1; + fragment = uri.str (delimiter); + } + if (fragment == null) { + type = (GLib.ChecksumType)int.MAX; + display = null; + } + + if (&checksum != null) + checksum = fragment != null + ? fragment.offset (delimiter.length) : null; + if (&label != null) + label = display; + return type; + } } } diff --git a/midori/midori-view.c b/midori/midori-view.c index 3d16b8e8..5f511947 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -2729,7 +2729,8 @@ webkit_web_view_mime_type_decision_cb (GtkWidget* web_view, WebKitWebDataSource* datasource; WebKitNetworkRequest* original_request; const gchar* original_uri; - gchar** fingerprint; + gchar* fingerprint; + gchar* fplabel; #if GTK_CHECK_VERSION (2, 14, 0) GIcon* icon; GtkWidget* image; @@ -2793,22 +2794,11 @@ webkit_web_view_mime_type_decision_cb (GtkWidget* web_view, datasource = webkit_web_frame_get_provisional_data_source (web_frame); original_request = webkit_web_data_source_get_initial_request (datasource); original_uri = webkit_network_request_get_uri (original_request); - fingerprint = g_strsplit (original_uri, "#!md5!", 2); - if (fingerprint && fingerprint[0] && fingerprint[1]) - gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), - "%s\n%s %s", file_type, _("MD5-Checksum:"), fingerprint[1]); - else - { - g_strfreev (fingerprint); - fingerprint = g_strsplit (original_uri, "#!sha1!", 2); - if (fingerprint && fingerprint[0] && fingerprint[1]) - gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), - "%s\n%s %s", file_type, _("SHA1-Checksum:"), fingerprint[1]); - else - gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), - "%s", file_type); - } - g_strfreev (fingerprint); + midori_uri_get_fingerprint (original_uri, &fingerprint, &fplabel); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), + "%s\n%s %s", file_type, fplabel ? fplabel : "", fingerprint ? fingerprint : ""); + g_free (fingerprint); + g_free (fplabel); g_free (file_type); gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE); diff --git a/tests/magic-uri.c b/tests/magic-uri.c index b9a211cc..a7cba6bd 100644 --- a/tests/magic-uri.c +++ b/tests/magic-uri.c @@ -208,6 +208,22 @@ magic_uri_performance (void) g_print ("\nTime needed for URI tests: %f ", g_test_timer_elapsed ()); } +static void +magic_uri_fingerprint (void) +{ + const gchar* uri; + uri = "http://midori-0.4.1.tar.bz2#!md5!33dde203cd71ae2b1d2adcc7f5739f65"; + g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_MD5); + uri = "http://midori-0.4.1.tar.bz2#!md5!33DDE203CD71AE2B1D2ADCC7F5739F65"; + g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_MD5); + uri = "http://midori-0.4.1.tar.bz2#!sha1!0c499459b1049feabf86dce89f49020139a9efd9"; + g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_CHECKSUM_SHA1); + uri = "http://midori-0.4.1.tar.bz2#!sha256!123456"; + g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_MAXINT); + uri = "http://midori-0.4.1.tar.bz2#abcdefg"; + g_assert_cmpint (midori_uri_get_fingerprint (uri, NULL, NULL), ==, G_MAXINT); +} + static void magic_uri_format (void) { @@ -269,6 +285,7 @@ main (int argc, 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/fingerprint", magic_uri_fingerprint); g_test_add_func ("/magic-uri/format", magic_uri_format); g_test_add_func ("/magic-uri/prefetch", magic_uri_prefetch); diff --git a/toolbars/midori-transferbar.c b/toolbars/midori-transferbar.c index 52426ff8..bbe1bfac 100644 --- a/toolbars/midori-transferbar.c +++ b/toolbars/midori-transferbar.c @@ -12,6 +12,7 @@ #include "midori-transferbar.h" #include "midori-browser.h" +#include "midori-core.h" #include "sokoke.h" #include @@ -103,7 +104,8 @@ midori_transferbar_download_notify_status_cb (WebKitDownload* download, MidoriBrowser* browser = midori_browser_get_for_widget (button); WebKitNetworkRequest* request; const gchar* original_uri; - gchar** fingerprint; + GChecksumType checksum_type; + gchar* fingerprint; gboolean verified = TRUE; icon = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU); @@ -129,45 +131,24 @@ midori_transferbar_download_notify_status_cb (WebKitDownload* download, original_uri = g_object_get_data (G_OBJECT (request), "midori-original-uri"); if (!original_uri) original_uri = webkit_download_get_uri (download); - fingerprint = g_strsplit (original_uri, "#!md5!", 2); - if (fingerprint && fingerprint[0] && fingerprint[1]) + checksum_type = midori_uri_get_fingerprint (original_uri, &fingerprint, NULL); + if (fingerprint != NULL) { gchar* filename = g_filename_from_uri ( webkit_download_get_destination_uri (download), NULL, NULL); gchar* contents; gsize length; gboolean y = g_file_get_contents (filename, &contents, &length, NULL); - gchar* checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5, + gchar* checksum = g_compute_checksum_for_data (checksum_type, (guchar*)contents, length); g_free (filename); g_free (contents); /* Checksums are case-insensitive */ - if (!y || g_ascii_strcasecmp (fingerprint[1], checksum) != 0) + if (!y || g_ascii_strcasecmp (fingerprint, checksum) != 0) verified = FALSE; g_free (checksum); } - else - { - gchar* filename = g_filename_from_uri ( - webkit_download_get_destination_uri (download), NULL, NULL); - g_strfreev (fingerprint); - fingerprint = g_strsplit (original_uri, "#!sha1!", 2); - if (fingerprint && fingerprint[0] && fingerprint[1]) - { - gchar* contents; - gsize length; - gboolean y = g_file_get_contents (filename, &contents, &length, NULL); - gchar* checksum = g_compute_checksum_for_data (G_CHECKSUM_SHA1, - (guchar*)contents, length); - g_free (contents); - /* Checksums are case-insensitive */ - if (!y || g_ascii_strcasecmp (fingerprint[1], checksum) != 0) - verified = FALSE; - g_free (checksum); - } - g_free (filename); - } - g_strfreev (fingerprint); + g_free (fingerprint); if (verified) { if (!sokoke_is_app_or_private ())