changeset 1634:38b254933dbb

use the most specific cookiesrc rule
author corvid <corvid@lavabit.com>
date Mon, 22 Mar 2010 19:45:34 +0000
parents 970b8815d82b
children cbcc7fd52ca3
files dpi/cookies.c src/cookies.c
diffstat 2 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/cookies.c	Mon Mar 22 17:36:52 2010 +0000
+++ b/dpi/cookies.c	Mon Mar 22 19:45:34 2010 +0000
@@ -1311,7 +1311,6 @@
    char line[LINE_MAXLEN];
    char domain[LINE_MAXLEN];
    char rule[LINE_MAXLEN];
-   int i, j;
    bool_t enabled = FALSE;
 
    /* Get a file pointer */
@@ -1336,8 +1335,7 @@
       dStrstrip(line);
 
       if (line[0] != '\0' && line[0] != '#') {
-         i = 0;
-         j = 0;
+         int i = 0, j = 0;
 
          /* Get the domain */
          while (line[i] != '\0' && !dIsspace(line[i]))
@@ -1372,8 +1370,17 @@
             default_action = cc.action;
             dFree(cc.domain);
          } else {
+            int i;
+            uint_t len = strlen(cc.domain);
+
+            /* Insert into list such that longest rules come first. */
             a_List_add(ccontrol, num_ccontrol, num_ccontrol_max);
-            ccontrol[num_ccontrol++] = cc;
+            for (i = num_ccontrol++;
+                 i > 0 && (len > strlen(ccontrol[i-1].domain));
+                 i--) {
+               ccontrol[i] = ccontrol[i-1];
+            }
+            ccontrol[i] = cc;
          }
 
          if (cc.action != COOKIE_DENY)
@@ -1387,7 +1394,9 @@
 }
 
 /*
- * Check the rules for an appropriate action for this domain
+ * Check the rules for an appropriate action for this domain.
+ * The rules are ordered by domain length, with longest first, so the
+ * first match is the most specific.
  */
 static CookieControlAction Cookies_control_check_domain(const char *domain)
 {
--- a/src/cookies.c	Mon Mar 22 17:36:52 2010 +0000
+++ b/src/cookies.c	Mon Mar 22 19:45:34 2010 +0000
@@ -234,7 +234,6 @@
    char line[LINE_MAXLEN];
    char domain[LINE_MAXLEN];
    char rule[LINE_MAXLEN];
-   int i, j;
    bool_t enabled = FALSE;
 
    /* Get a file pointer */
@@ -259,8 +258,7 @@
       dStrstrip(line);
 
       if (line[0] != '\0' && line[0] != '#') {
-         i = 0;
-         j = 0;
+         int i = 0, j = 0;
 
          /* Get the domain */
          while (line[i] != '\0' && !dIsspace(line[i]))
@@ -295,8 +293,17 @@
             default_action = cc.action;
             dFree(cc.domain);
          } else {
+            int i;
+            uint_t len = strlen(cc.domain);
+
+            /* Insert into list such that longest rules come first. */
             a_List_add(ccontrol, num_ccontrol, num_ccontrol_max);
-            ccontrol[num_ccontrol++] = cc;
+            for (i = num_ccontrol++;
+                 i > 0 && (len > strlen(ccontrol[i-1].domain));
+                 i--) {
+               ccontrol[i] = ccontrol[i-1];
+            }
+            ccontrol[i] = cc;
          }
 
          if (cc.action != COOKIE_DENY)
@@ -310,7 +317,9 @@
 }
 
 /*
- * Check the rules for an appropriate action for this domain
+ * Check the rules for an appropriate action for this domain.
+ * The rules are ordered by domain length, with longest first, so the
+ * first match is the most specific.
  */
 static CookieControlAction Cookies_control_check_domain(const char *domain)
 {