Add tests for trailing dots in hostnames.
[privoxy.git] / urlmatch.c
index 317344d..b390e35 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.53 2009/05/19 17:40:36 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.57 2009/06/03 16:44:15 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -1117,7 +1117,7 @@ jb_err create_url_spec(struct url_spec *url, char *buf)
       return JB_ERR_MEMORY;
    }
 
-   /* Is it tag pattern? */
+   /* Is it tag pattern? */
    if (0 == strncmpic("TAG:", url->spec, 4))
    {
       /* The pattern starts with the first character after "TAG:" */
@@ -1125,7 +1125,7 @@ jb_err create_url_spec(struct url_spec *url, char *buf)
       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);
 }
 
@@ -1173,6 +1173,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
@@ -1189,22 +1253,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));
 
 }