[Xfce4-commits] <midori:master> Update Adblock filters according to Last modified/ Expires

Christian Dywan noreply at xfce.org
Sat Sep 1 02:12:05 CEST 2012


Updating branch refs/heads/master
         to 9b0976083979ee2cf6f1856a79d83ab30db066de (commit)
       from db0ae6ab6082f83d314d54492a38eb098c40106d (commit)

commit 9b0976083979ee2cf6f1856a79d83ab30db066de
Author: Paweł Forysiuk <tuxator at o2.pl>
Date:   Sat Sep 1 01:57:17 2012 +0200

    Update Adblock filters according to Last modified/ Expires

 extensions/adblock.c |   85 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 84 insertions(+), 1 deletions(-)

diff --git a/extensions/adblock.c b/extensions/adblock.c
index 68a5551..8939c68 100644
--- a/extensions/adblock.c
+++ b/extensions/adblock.c
@@ -46,6 +46,9 @@ static GString* blockcss = NULL;
 static gboolean
 adblock_parse_file (gchar* path);
 
+static gboolean
+adblock_file_is_up_to_date (gchar* path);
+
 static void
 adblock_reload_rules (MidoriExtension* extension,
                       gboolean         custom_only);
@@ -223,7 +226,8 @@ adblock_reload_rules (MidoriExtension* extension,
                 continue;
             }
 
-            if (!adblock_parse_file (path))
+            if (!adblock_parse_file (path)
+            ||  !adblock_file_is_up_to_date (path))
             {
                 WebKitNetworkRequest* request;
                 WebKitDownload* download;
@@ -1513,6 +1517,85 @@ adblock_parse_line (gchar* line)
     return adblock_add_url_pattern ("", "uri", line);
 }
 
+static GDateMonth
+str_month_name_to_gdate (const gchar* month)
+{
+    guint i;
+    const gchar* months[] = {
+        "", "January", "February", "March", "April", "May", "June",
+        "July", "August", "September", "October", "November", "December"
+    };
+
+    for (i = 0; i < G_N_ELEMENTS (months); i++)
+    {
+        if (strncmp (month, months[i], 3) == 0)
+            return i;
+    }
+    return 0;
+}
+
+static gboolean
+adblock_file_is_up_to_date (gchar* path)
+{
+    FILE* file;
+    guint days_to_expire = 0;
+    gchar* timestamp = NULL;
+    gchar line[2000];
+
+    /* Check a chunk of header for update info */
+    if ((file = g_fopen (path, "r")))
+    {
+        guint i;
+        for (i = 0; i <= 10; i++)
+        {
+            fgets (line, 2000, file);
+            if (strncmp ("! Expires", line, 9) == 0)
+            {
+                gchar** parts = g_strsplit (line, " ", 4);
+                days_to_expire = atoi (parts[2]);
+                g_strfreev (parts);
+            }
+            if (strncmp ("! Last modified", line, 15) == 0)
+            {
+                gchar** parts = g_strsplit (line, ":", 2);
+                timestamp = g_strdup (parts[1] + 1);
+                g_strchomp (timestamp);
+                g_strfreev (parts);
+            }
+        }
+
+        if (days_to_expire && timestamp != NULL)
+        {
+            guint days_elapsed = 0;
+
+            GDate* current = g_date_new ();
+            GDate* mod_date = g_date_new ();
+            gchar** parts = g_strsplit (timestamp, " ", 4);
+
+            g_date_set_day (mod_date, atoi (parts[0]));
+            g_date_set_month (mod_date, str_month_name_to_gdate (parts[1]));
+            g_date_set_year (mod_date, atoi (parts[2]));
+            g_strfreev (parts);
+
+            g_date_set_time_t (current, time (NULL));
+            days_elapsed = g_date_days_between (mod_date, current);
+
+            g_date_free (current);
+            g_date_free (mod_date);
+            g_free (timestamp);
+
+            if (days_elapsed < days_to_expire)
+                return TRUE;
+            else
+                return FALSE;
+        }
+        else
+            g_free (timestamp);
+        return TRUE;
+    }
+    return FALSE;
+}
+
 static gboolean
 adblock_parse_file (gchar* path)
 {


More information about the Xfce4-commits mailing list