parse_toggle_state() only returns 0 or 1, so check for 1 directly.
[privoxy.git] / urlmatch.c
index 19388b1..ebaf63c 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.50 2009/04/17 11:38:28 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.60 2011/04/19 13:00:47 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -58,7 +58,13 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.50 2009/04/17 11:38:28 fabianke
 
 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
 
-enum regex_anchoring {NO_ANCHORING, LEFT_ANCHORED, RIGHT_ANCHORED};
+enum regex_anchoring
+{
+   NO_ANCHORING,
+   LEFT_ANCHORED,
+   RIGHT_ANCHORED,
+   RIGHT_ANCHORED_HOST
+};
 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern);
 
 /*********************************************************************
@@ -86,12 +92,15 @@ void free_http_request(struct http_request *http)
    freez(http->path);
    freez(http->ver);
    freez(http->host_ip_addr_str);
+#ifndef FEATURE_EXTENDED_HOST_PATTERNS
    freez(http->dbuffer);
    freez(http->dvec);
    http->dcount = 0;
+#endif
 }
 
 
+#ifndef FEATURE_EXTENDED_HOST_PATTERNS
 /*********************************************************************
  *
  * Function    :  init_domain_components
@@ -155,6 +164,7 @@ jb_err init_domain_components(struct http_request *http)
 
    return JB_ERR_OK;
 }
+#endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
 
 
 /*********************************************************************
@@ -377,10 +387,12 @@ jb_err parse_http_url(const char *url, struct http_request *http, int require_pr
       }
    }
 
-   /*
-    * Split domain name so we can compare it against wildcards
-    */
+#ifdef FEATURE_EXTENDED_HOST_PATTERNS
+   return JB_ERR_OK;
+#else
+   /* Split domain name so we can compare it against wildcards */
    return init_domain_components(http);
+#endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
 
 }
 
@@ -540,9 +552,10 @@ jb_err parse_http_request(const char *req, struct http_request *http)
  *
  * Parameters  :
  *          1  :  pattern = The pattern to compile.
- *          2  :  anchoring = How the regex should be anchored.
- *                            Can be either one of NO_ANCHORING,
- *                            LEFT_ANCHORED or RIGHT_ANCHORED.
+ *          2  :  anchoring = How the regex should be modified
+ *                            before compilation. Can be either
+ *                            one of NO_ANCHORING, LEFT_ANCHORED,
+ *                            RIGHT_ANCHORED or RIGHT_ANCHORED_HOST.
  *          3  :  url     = In case of failures, the spec member is
  *                          logged and the structure freed.
  *          4  :  regex   = Where the compiled regex should be stored.
@@ -576,6 +589,9 @@ static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchorin
       case RIGHT_ANCHORED:
          fmt = "%s$";
          break;
+      case RIGHT_ANCHORED_HOST:
+         fmt = "%s\\.?$";
+         break;
       case LEFT_ANCHORED:
          fmt = "^%s";
          break;
@@ -725,7 +741,7 @@ static jb_err compile_url_pattern(struct url_spec *url, char *buf)
  *********************************************************************/
 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
 {
-   return compile_pattern(host_pattern, RIGHT_ANCHORED, url, &url->host_regex);
+   return compile_pattern(host_pattern, RIGHT_ANCHORED_HOST, url, &url->host_regex);
 }
 
 #else
@@ -913,7 +929,7 @@ static int simplematch(const char *pattern, const char *text)
       ||   ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
       {
          /* 
-          * Sucess: Go ahead
+          * Success: Go ahead
           */
          pat++;
       }
@@ -1111,15 +1127,15 @@ jb_err create_url_spec(struct url_spec *url, char *buf)
       return JB_ERR_MEMORY;
    }
 
-   /* Is it tag pattern? */
-   if (0 == strncmpic("TAG:", url->spec, 4))
+   /* Is it tag pattern? */
+   if (0 == strncmpic(url->spec, "TAG:", 4))
    {
       /* The pattern starts with the first character after "TAG:" */
       const char *tag_pattern = buf + 4;
       return compile_pattern(tag_pattern, NO_ANCHORING, url, &url->tag_regex);
    }
 
-   /* If it isn't a tag pattern it must be a URL pattern. */
+   /* If it isn't a tag pattern it must be an URL pattern. */
    return compile_url_pattern(url, buf);
 }
 
@@ -1167,6 +1183,70 @@ void free_url_spec(struct url_spec *url)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  port_matches
+ *
+ * Description :  Compares a port against a port list.
+ *
+ * Parameters  :
+ *          1  :  port      = The port to check.
+ *          2  :  port_list = The list of port to compare with.
+ *
+ * Returns     :  TRUE for yes, FALSE otherwise.
+ *
+ *********************************************************************/
+static int port_matches(const int port, const char *port_list)
+{
+   return ((NULL == port_list) || match_portlist(port_list, port));
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  host_matches
+ *
+ * Description :  Compares a host against a host pattern.
+ *
+ * Parameters  :
+ *          1  :  url = The URL to match
+ *          2  :  pattern = The URL pattern
+ *
+ * Returns     :  TRUE for yes, FALSE otherwise.
+ *
+ *********************************************************************/
+static int host_matches(const struct http_request *http,
+                        const struct url_spec *pattern)
+{
+#ifdef FEATURE_EXTENDED_HOST_PATTERNS
+   return ((NULL == pattern->host_regex)
+      || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)));
+#else
+   return ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)));
+#endif
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  path_matches
+ *
+ * Description :  Compares a path against a path pattern.
+ *
+ * Parameters  :
+ *          1  :  path = The path to match
+ *          2  :  pattern = The URL pattern
+ *
+ * Returns     :  TRUE for yes, FALSE otherwise.
+ *
+ *********************************************************************/
+static int path_matches(const char *path, const struct url_spec *pattern)
+{
+   return ((NULL == pattern->preg)
+      || (0 == regexec(pattern->preg, path, 0, NULL, 0)));
+}
+
+
 /*********************************************************************
  *
  * Function    :  url_match
@@ -1183,22 +1263,14 @@ void free_url_spec(struct url_spec *url)
 int url_match(const struct url_spec *pattern,
               const struct http_request *http)
 {
-   /* XXX: these should probably be functions. */
-#define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
-#ifdef FEATURE_EXTENDED_HOST_PATTERNS
-#define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
-#else
-#define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
-#endif
-#define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
-
    if (pattern->tag_regex != NULL)
    {
       /* It's a tag pattern and shouldn't be matched against URLs */
       return 0;
    } 
 
-   return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
+   return (port_matches(http->port, pattern->port_list)
+      && host_matches(http, pattern) && path_matches(http->path, pattern));
 
 }
 
@@ -1222,7 +1294,7 @@ int match_portlist(const char *portlist, int port)
 {
    char *min, *max, *next, *portlist_copy;
 
-   min = next = portlist_copy = strdup(portlist);
+   min = portlist_copy = strdup(portlist);
 
    /*
     * Zero-terminate first item and remember offset for next
@@ -1235,7 +1307,7 @@ int match_portlist(const char *portlist, int port)
    /*
     * Loop through all items, checking for match
     */
-   while(min)
+   while (NULL != min)
    {
       if (NULL == (max = strchr(min, (int) '-')))
       {