Fix a harmless logic bug in mark_server_socket_tainted()
[privoxy.git] / urlmatch.c
index a8f887b..2a1ae8b 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.58 2009/06/03 16:44:41 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.71 2012/06/08 15:15:11 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -6,7 +6,7 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.58 2009/06/03 16:44:41 fabianke
  * Purpose     :  Declares functions to match URLs against URL
  *                patterns.
  *
- * Copyright   :  Written by and Copyright (C) 2001-2009
+ * Copyright   :  Written by and Copyright (C) 2001-2011
  *                the Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -135,11 +135,11 @@ jb_err init_domain_components(struct http_request *http)
    /* map to lower case */
    for (p = http->dbuffer; *p ; p++)
    {
-      *p = (char)tolower((int)(unsigned char)*p);
+      *p = (char)privoxy_tolower(*p);
    }
 
    /* split the domain name into components */
-   http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
+   http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec));
 
    if (http->dcount <= 0)
    {
@@ -154,11 +154,7 @@ jb_err init_domain_components(struct http_request *http)
    /* save a copy of the pointers in dvec */
    size = (size_t)http->dcount * sizeof(*http->dvec);
 
-   http->dvec = (char **)malloc(size);
-   if (NULL == http->dvec)
-   {
-      return JB_ERR_MEMORY;
-   }
+   http->dvec = malloc_or_die(size);
 
    memcpy(http->dvec, vec, size);
 
@@ -167,6 +163,53 @@ jb_err init_domain_components(struct http_request *http)
 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
 
 
+/*********************************************************************
+ *
+ * Function    :  url_requires_percent_encoding
+ *
+ * Description :  Checks if an URL contains invalid characters
+ *                according to RFC 3986 that should be percent-encoded.
+ *                Does not verify whether or not the passed string
+ *                actually is a valid URL.
+ *
+ * Parameters  :
+ *          1  :  url = URL to check
+ *
+ * Returns     :  True in case of valid URLs, false otherwise
+ *
+ *********************************************************************/
+int url_requires_percent_encoding(const char *url)
+{
+   static const char allowed_characters[128] = {
+      '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+      '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+      '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+      '\0', '\0', '\0', '!',  '\0', '#',  '$',  '%',  '&',  '\'',
+      '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',  '0',  '1',
+      '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  ':',  ';',
+      '\0', '=',  '\0', '?',  '@',  'A',  'B',  'C',  'D',  'E',
+      'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
+      'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  'X',  'Y',
+      'Z',  '[',  '\0', ']',  '\0', '_',  '\0', 'a',  'b',  'c',
+      'd',  'e',  'f',  'g',  'h',  'i',  'j',  'k',  'l',  'm',
+      'n',  'o',  'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
+      'x',  'y',  'z',  '\0', '\0', '\0', '~',  '\0'
+   };
+
+   while (*url != '\0')
+   {
+      const unsigned int i = (unsigned char)*url++;
+      if (i >= sizeof(allowed_characters) || '\0' == allowed_characters[i])
+      {
+         return TRUE;
+      }
+   }
+
+   return FALSE;
+
+}
+
+
 /*********************************************************************
  *
  * Function    :  parse_http_url
@@ -203,11 +246,11 @@ jb_err parse_http_url(const char *url, struct http_request *http, int require_pr
 
    /*
     * Check for * URI. If found, we're done.
-    */  
+    */
    if (*http->url == '*')
    {
-      if  ( NULL == (http->path = strdup("*"))
-         || NULL == (http->hostport = strdup("")) ) 
+      if (NULL == (http->path = strdup("*"))
+       || NULL == (http->hostport = strdup("")))
       {
          return JB_ERR_MEMORY;
       }
@@ -290,8 +333,8 @@ jb_err parse_http_url(const char *url, struct http_request *http, int require_pr
 
       freez(buf);
 
-      if ( (http->path == NULL)
-        || (http->hostport == NULL))
+      if ((http->path == NULL)
+       || (http->hostport == NULL))
       {
          return JB_ERR_MEMORY;
       }
@@ -411,7 +454,7 @@ jb_err parse_http_url(const char *url, struct http_request *http, int require_pr
  *********************************************************************/
 static int unknown_method(const char *method)
 {
-   static const char *known_http_methods[] = {
+   static const char * const known_http_methods[] = {
       /* Basic HTTP request type */
       "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
       /* webDAV extensions (RFC2518) */
@@ -420,14 +463,14 @@ static int unknown_method(const char *method)
        * Microsoft webDAV extension for Exchange 2000.  See:
        * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
        * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
-       */ 
+       */
       "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
       /*
        * Another Microsoft webDAV extension for Exchange 2000.  See:
        * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
        * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
        * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
-       */ 
+       */
       "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
       /*
        * Yet another WebDAV extension, this time for
@@ -483,7 +526,7 @@ jb_err parse_http_request(const char *req, struct http_request *http)
       return JB_ERR_MEMORY;
    }
 
-   n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
+   n = ssplit(buf, " \r\n", v, SZ(v));
    if (n != 3)
    {
       freez(buf);
@@ -534,7 +577,7 @@ jb_err parse_http_request(const char *req, struct http_request *http)
 
    if ( (http->cmd == NULL)
      || (http->gpc == NULL)
-     || (http->ver == NULL) )
+     || (http->ver == NULL))
    {
       return JB_ERR_MEMORY;
    }
@@ -728,7 +771,7 @@ static jb_err compile_url_pattern(struct url_spec *url, char *buf)
  *
  * Function    :  compile_host_pattern
  *
- * Description :  Parses and compiles a host pattern..
+ * Description :  Parses and compiles a host pattern.
  *
  * Parameters  :
  *          1  :  url = Target url_spec to be filled in.
@@ -779,7 +822,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
       url->unanchored |= ANCHOR_LEFT;
    }
 
-   /* 
+   /*
     * Split domain into components
     */
    url->dbuffer = strdup(host_pattern);
@@ -789,18 +832,18 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
       return JB_ERR_MEMORY;
    }
 
-   /* 
+   /*
     * Map to lower case
     */
    for (p = url->dbuffer; *p ; p++)
    {
-      *p = (char)tolower((int)(unsigned char)*p);
+      *p = (char)privoxy_tolower(*p);
    }
 
-   /* 
+   /*
     * Split the domain name into components
     */
-   url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
+   url->dcount = ssplit(url->dbuffer, ".", v, SZ(v));
 
    if (url->dcount < 0)
    {
@@ -809,17 +852,12 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter
    }
    else if (url->dcount != 0)
    {
-      /* 
+      /*
        * Save a copy of the pointers in dvec
        */
       size = (size_t)url->dcount * sizeof(*url->dvec);
-      
-      url->dvec = (char **)malloc(size);
-      if (NULL == url->dvec)
-      {
-         free_url_spec(url);
-         return JB_ERR_MEMORY;
-      }
+
+      url->dvec = malloc_or_die(size);
 
       memcpy(url->dvec, v, size);
    }
@@ -852,13 +890,13 @@ static int simplematch(const char *pattern, const char *text)
 {
    const unsigned char *pat = (const unsigned char *)pattern;
    const unsigned char *txt = (const unsigned char *)text;
-   const unsigned char *fallback = pat; 
+   const unsigned char *fallback = pat;
    int wildcard = 0;
-  
+
    unsigned char lastchar = 'a';
    unsigned i;
    unsigned char charmap[32];
-  
+
    while (*txt)
    {
 
@@ -876,15 +914,15 @@ static int simplematch(const char *pattern, const char *text)
       }
 
       /* '*' in the pattern?  */
-      if (*pat == '*') 
+      if (*pat == '*')
       {
-     
+
          /* The pattern ends afterwards? Speed up the return. */
          if (*++pat == '\0')
          {
             return 0;
          }
-     
+
          /* Else, set wildcard mode and remember position after '*' */
          wildcard = 1;
          fallback = pat;
@@ -898,7 +936,7 @@ static int simplematch(const char *pattern, const char *text)
          while (*++pat != ']')
          {
             if (!*pat)
-            { 
+            {
                return 1;
             }
             else if (*pat == '-')
@@ -910,7 +948,7 @@ static int simplematch(const char *pattern, const char *text)
                for (i = lastchar; i <= *pat; i++)
                {
                   charmap[i / 8] |= (unsigned char)(1 << (i % 8));
-               } 
+               }
             }
             else
             {
@@ -921,21 +959,21 @@ static int simplematch(const char *pattern, const char *text)
       } /* -END- if Character range specification */
 
 
-      /* 
-       * Char match, or char range match? 
+      /*
+       * Char match, or char range match?
        */
-      if ( (*pat == *txt)
-      ||   (*pat == '?')
-      ||   ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
+      if ((*pat == *txt)
+       || (*pat == '?')
+       || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))))
       {
-         /* 
-          * Sucess: Go ahead
+         /*
+          * Success: Go ahead
           */
          pat++;
       }
       else if (!wildcard)
       {
-         /* 
+         /*
           * No match && no wildcard: No luck
           */
          return 1;
@@ -962,7 +1000,7 @@ static int simplematch(const char *pattern, const char *text)
    }
 
    /* Cut off extra '*'s */
-   if(*pat == '*')  pat++;
+   if (*pat == '*') pat++;
 
    /* If this is the pattern's end, fine! */
    return(*pat);
@@ -1128,7 +1166,7 @@ jb_err create_url_spec(struct url_spec *url, char *buf)
    }
 
    /* Is it a tag pattern? */
-   if (0 == strncmpic("TAG:", url->spec, 4))
+   if (0 == strncmpic(url->spec, "TAG:", 4))
    {
       /* The pattern starts with the first character after "TAG:" */
       const char *tag_pattern = buf + 4;
@@ -1267,7 +1305,7 @@ int url_match(const struct url_spec *pattern,
    {
       /* It's a tag pattern and shouldn't be matched against URLs */
       return 0;
-   } 
+   }
 
    return (port_matches(http->port, pattern->port_list)
       && host_matches(http, pattern) && path_matches(http->path, pattern));
@@ -1327,7 +1365,7 @@ int match_portlist(const char *portlist, int port)
           * or, if max was omitted, between min and 65K
           */
          *max++ = '\0';
-         if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
+         if (port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
          {
             freez(portlist_copy);
             return(1);