Make it harder to mistake url_match()'s
[privoxy.git] / urlmatch.c
index 54089a7..1c4963a 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.23 2008/04/05 12:19:20 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.27 2008/04/08 15:44:33 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -33,6 +33,22 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.23 2008/04/05 12:19:20 fabianke
  *
  * Revisions   :
  *    $Log: urlmatch.c,v $
+ *    Revision 1.27  2008/04/08 15:44:33  fabiankeil
+ *    Save a bit of memory (and a few cpu cycles) by not bothering to
+ *    compile slash-only path regexes that don't affect the result.
+ *
+ *    Revision 1.26  2008/04/07 16:57:18  fabiankeil
+ *    - Use free_url_spec() more consistently.
+ *    - Let it reset url->dcount just in case.
+ *
+ *    Revision 1.25  2008/04/06 15:18:38  fabiankeil
+ *    Oh well, rename the --enable-pcre-host-patterns option to
+ *    --enable-extended-host-patterns as it's not really PCRE syntax.
+ *
+ *    Revision 1.24  2008/04/06 14:54:26  fabiankeil
+ *    Use PCRE syntax in host patterns when configured
+ *    with --enable-pcre-host-patterns.
+ *
  *    Revision 1.23  2008/04/05 12:19:20  fabiankeil
  *    Factor compile_host_pattern() out of create_url_spec().
  *
@@ -635,7 +651,7 @@ jb_err parse_http_request(const char *req,
 }
 
 
-#ifdef FEATURE_PCRE_HOST_PATTERNS
+#ifdef FEATURE_EXTENDED_HOST_PATTERNS
 /*********************************************************************
  *
  * Function    :  compile_host_pattern
@@ -730,10 +746,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
    url->dbuffer = strdup(host_pattern);
    if (NULL == url->dbuffer)
    {
-      freez(url->spec);
-      freez(url->path);
-      regfree(url->preg);
-      freez(url->preg);
+      free_url_spec(url);
       return JB_ERR_MEMORY;
    }
 
@@ -752,12 +765,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
 
    if (url->dcount < 0)
    {
-      freez(url->spec);
-      freez(url->path);
-      regfree(url->preg);
-      freez(url->preg);
-      freez(url->dbuffer);
-      url->dcount = 0;
+      free_url_spec(url);
       return JB_ERR_MEMORY;
    }
    else if (url->dcount != 0)
@@ -770,12 +778,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
       url->dvec = (char **)malloc(size);
       if (NULL == url->dvec)
       {
-         freez(url->spec);
-         freez(url->path);
-         regfree(url->preg);
-         freez(url->preg);
-         freez(url->dbuffer);
-         url->dcount = 0;
+         free_url_spec(url);
          return JB_ERR_MEMORY;
       }
 
@@ -907,7 +910,7 @@ static int domain_match(const struct url_spec *pattern, const struct http_reques
    }
 
 }
-#endif /* def FEATURE_PCRE_HOST_PATTERNS */
+#endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
 
 
 /*********************************************************************
@@ -975,12 +978,8 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
             errlen = sizeof(rebuf) - 1;
          }
          rebuf[errlen] = '\0';
-
          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
-
-         freez(url->spec);
-         regfree(url->tag_regex);
-         freez(url->tag_regex);
+         free_url_spec(url);
 
          return JB_ERR_PARSE;
       }
@@ -991,11 +990,23 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
    p = strchr(buf, '/');
    if (NULL != p)
    {
-      url->path = strdup(p);
-      if (NULL == url->path)
+      if (*(p+1) != '\0')
       {
-         freez(url->spec);
-         return JB_ERR_MEMORY;
+         url->path = strdup(p);
+         if (NULL == url->path)
+         {
+            free_url_spec(url);
+            return JB_ERR_MEMORY;
+         }
+      }
+      else
+      {
+         /*
+          * The path pattern is a single slash and can
+          * be ignored as it won't affect the result.
+          */
+         assert(NULL == url->path);
+         url->path = NULL;
       }
       *p = '\0';
    }
@@ -1007,8 +1018,7 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
    {
       if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
       {
-         freez(url->spec);
-         freez(url->path);
+         free_url_spec(url);
          return JB_ERR_MEMORY;
       }
 
@@ -1025,14 +1035,9 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
             errlen = sizeof(rebuf) - (size_t)1;
          }
          rebuf[errlen] = '\0';
-
          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
             url->spec, rebuf);
-
-         freez(url->spec);
-         freez(url->path);
-         regfree(url->preg);
-         freez(url->preg);
+         free_url_spec(url);
 
          return JB_ERR_PARSE;
       }
@@ -1081,7 +1086,7 @@ void free_url_spec(struct url_spec *url)
    if (url == NULL) return;
 
    freez(url->spec);
-#ifdef FEATURE_PCRE_HOST_PATTERNS
+#ifdef FEATURE_EXTENDED_HOST_PATTERNS
    if (url->host_regex)
    {
       regfree(url->host_regex);
@@ -1090,7 +1095,8 @@ void free_url_spec(struct url_spec *url)
 #else
    freez(url->dbuffer);
    freez(url->dvec);
-#endif /* ndef FEATURE_PCRE_HOST_PATTERNS */
+   url->dcount = 0;
+#endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
    freez(url->path);
    freez(url->port_list);
    if (url->preg)
@@ -1120,16 +1126,16 @@ void free_url_spec(struct url_spec *url)
  *
  *********************************************************************/
 int url_match(const struct url_spec *pattern,
-              const struct http_request *url)
+              const struct http_request *http)
 {
    /* XXX: these should probably be functions. */
-#define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, url->port))
-#ifdef FEATURE_PCRE_HOST_PATTERNS
-#define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, url->host, 0, NULL, 0)))
+#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, url)))
+#define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
 #endif
-#define PATH_MATCHES ((NULL == pattern->path) || (0 == regexec(pattern->preg, url->path, 0, NULL, 0)))
+#define PATH_MATCHES ((NULL == pattern->path) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
 
    if (pattern->tag_regex != NULL)
    {