Add and use parse_forwarder_address() to reduce code duplication.
authorFabian Keil <fk@fabiankeil.de>
Fri, 17 Apr 2009 11:38:28 +0000 (11:38 +0000)
committerFabian Keil <fk@fabiankeil.de>
Fri, 17 Apr 2009 11:38:28 +0000 (11:38 +0000)
filters.c
loadcfg.c
urlmatch.c
urlmatch.h

index 245fa0f..602bb0b 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,4 +1,4 @@
-const char filters_rcs[] = "$Id: filters.c,v 1.117 2009/04/17 11:35:28 fabiankeil Exp $";
+const char filters_rcs[] = "$Id: filters.c,v 1.118 2009/04/17 11:37:03 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
@@ -40,6 +40,9 @@ const char filters_rcs[] = "$Id: filters.c,v 1.117 2009/04/17 11:35:28 fabiankei
  *
  * Revisions   :
  *    $Log: filters.c,v $
+ *    Revision 1.118  2009/04/17 11:37:03  fabiankeil
+ *    Allow IPv6 addresses in forward-override{}.
+ *
  *    Revision 1.117  2009/04/17 11:35:28  fabiankeil
  *    Fix compiler warning.
  *
@@ -2805,30 +2808,9 @@ const static struct forward_spec *get_forward_override_settings(struct client_st
       if (NULL != socks_proxy)
       {
          /* Parse the SOCKS proxy host[:port] */
-         char *p = socks_proxy;
-         fwd->gateway_host = strdup(socks_proxy);
-
-         if ((*fwd->gateway_host == '[')
-            && (NULL != (p = strchr(fwd->gateway_host, ']'))))
-         {
-            *p++ = '\0';
-            memmove(fwd->gateway_host, fwd->gateway_host + 1,
-               (size_t)(p - fwd->gateway_host));
-            if (*p == ':')
-            {
-               fwd->gateway_port = (int)strtol(++p, NULL, 0);
-            }
-         }
-         else if (NULL != (p = strchr(fwd->gateway_host, ':')))
-         {
-            *p++ = '\0';
-            fwd->gateway_port = (int)strtol(p, NULL, 0);
-         }
-
-         if (fwd->gateway_port <= 0)
-         {
-            fwd->gateway_port = 1080;
-         }
+         fwd->gateway_port = 1080;
+         parse_forwarder_address(socks_proxy,
+            &fwd->gateway_host, &fwd->gateway_port);
 
          http_parent = vec[2];
       }
@@ -2844,30 +2826,9 @@ const static struct forward_spec *get_forward_override_settings(struct client_st
    /* Parse http forwarding settings */
    if (strcmp(http_parent, ".") != 0)
    {
-      char *p = http_parent;
-      fwd->forward_host = strdup(http_parent);
-
-      if ((*fwd->forward_host == '[')
-         && (NULL != (p = strchr(fwd->forward_host, ']'))))
-      {
-         *p++ = '\0';
-         memmove(fwd->forward_host, fwd->forward_host + 1,
-            (size_t)(p - fwd->forward_host));
-         if (*p == ':')
-         {
-            fwd->forward_port = (int)strtol(++p, NULL, 0);
-         }
-      }
-      else if (NULL != (p = strchr(fwd->forward_host, ':')))
-      {
-         *p++ = '\0';
-         fwd->forward_port = (int)strtol(p, NULL, 0);
-      }
-
-      if (fwd->forward_port <= 0)
-      {
-         fwd->forward_port = 8000;
-      }
+      fwd->forward_port = 8000;
+      parse_forwarder_address(http_parent,
+         &fwd->forward_host, &fwd->forward_port);
    }
 
    assert (NULL != fwd);
index d6fa0d7..4c1eb7a 100644 (file)
--- a/loadcfg.c
+++ b/loadcfg.c
@@ -1,4 +1,4 @@
-const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.94 2009/04/17 11:27:49 fabiankeil Exp $";
+const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.95 2009/04/17 11:34:34 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
@@ -35,6 +35,9 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.94 2009/04/17 11:27:49 fabiankeil
  *
  * Revisions   :
  *    $Log: loadcfg.c,v $
+ *    Revision 1.95  2009/04/17 11:34:34  fabiankeil
+ *    Style cosmetics for the IPv6 code.
+ *
  *    Revision 1.94  2009/04/17 11:27:49  fabiankeil
  *    Petr Pisar's privoxy-3.0.12-ipv6-3.diff.
  *
@@ -1199,29 +1202,9 @@ struct configuration_spec * load_config(void)
 
             if (strcmp(p, ".") != 0)
             {
-               cur_fwd->forward_host = strdup(p);
-
-               if ((*cur_fwd->forward_host == '[')
-                  && (NULL != (p = strchr(cur_fwd->forward_host, ']'))))
-               {
-                  *p++ = '\0';
-                  memmove(cur_fwd->forward_host, cur_fwd->forward_host + 1,
-                     (size_t)(p - cur_fwd->forward_host));
-                  if (*p == ':')
-                  {
-                     cur_fwd->forward_port = atoi(++p);
-                  }
-               }
-               else if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
-               {
-                  *p++ = '\0';
-                  cur_fwd->forward_port = atoi(p);
-               }
-
-               if (cur_fwd->forward_port <= 0)
-               {
-                  cur_fwd->forward_port = 8000;
-               }
+               cur_fwd->forward_port = 8000;
+               parse_forwarder_address(p, &cur_fwd->forward_host,
+                  &cur_fwd->forward_port);
             }
 
             /* Add to list. */
@@ -1272,31 +1255,12 @@ struct configuration_spec * load_config(void)
             /* Parse the SOCKS proxy host[:port] */
             p = vec[1];
 
+            /* XXX: This check looks like a bug. */
             if (strcmp(p, ".") != 0)
             {
-               cur_fwd->gateway_host = strdup(p);
-
-               if ((*cur_fwd->gateway_host == '[')
-                  && (NULL != (p = strchr(cur_fwd->gateway_host, ']'))))
-               {
-                  *p++ = '\0';
-                  memmove(cur_fwd->gateway_host, cur_fwd->gateway_host + 1,
-                     (size_t)(p - cur_fwd->gateway_host));
-                  if (*p == ':')
-                  {
-                     cur_fwd->gateway_port = atoi(++p);
-                  }
-               }
-               else if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
-               {
-                  *p++ = '\0';
-                  cur_fwd->gateway_port = atoi(p);
-               }
-
-               if (cur_fwd->gateway_port <= 0)
-               {
-                  cur_fwd->gateway_port = 1080;
-               }
+               cur_fwd->gateway_port = 1080;
+               parse_forwarder_address(p, &cur_fwd->gateway_host,
+                  &cur_fwd->gateway_port);
             }
 
             /* Parse the parent HTTP proxy host[:port] */
@@ -1304,29 +1268,9 @@ struct configuration_spec * load_config(void)
 
             if (strcmp(p, ".") != 0)
             {
-               cur_fwd->forward_host = strdup(p);
-
-               if ((*cur_fwd->forward_host == '[')
-                  && (NULL != (p = strchr(cur_fwd->forward_host, ']'))))
-               {
-                  *p++ = '\0';
-                  memmove(cur_fwd->forward_host, cur_fwd->forward_host + 1,
-                     (size_t)(p - cur_fwd->forward_host));
-                  if (*p == ':')
-                  {
-                     cur_fwd->forward_port = atoi(++p);
-                  }
-               }
-               else if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
-               {
-                  *p++ = '\0';
-                  cur_fwd->forward_port = atoi(p);
-               }
-
-               if (cur_fwd->forward_port <= 0)
-               {
-                  cur_fwd->forward_port = 8000;
-               }
+               cur_fwd->forward_port = 8000;
+               parse_forwarder_address(p, &cur_fwd->forward_host,
+                  &cur_fwd->forward_port);
             }
 
             /* Add to list. */
@@ -1385,58 +1329,18 @@ struct configuration_spec * load_config(void)
             /* Parse the SOCKS proxy host[:port] */
             p = vec[1];
 
-            cur_fwd->gateway_host = strdup(p);
-
-            if ((*cur_fwd->gateway_host == '[')
-               && (NULL != (p = strchr(cur_fwd->gateway_host, ']'))))
-            {
-               *p++ = '\0';
-               memmove(cur_fwd->gateway_host, cur_fwd->gateway_host + 1,
-                  (size_t)(p - cur_fwd->gateway_host));
-               if (*p == ':')
-               {
-                  cur_fwd->gateway_port = atoi(++p);
-               }
-            }
-            else if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
-            {
-               *p++ = '\0';
-               cur_fwd->gateway_port = atoi(p);
-            }
-
-            if (cur_fwd->gateway_port <= 0)
-            {
-               cur_fwd->gateway_port = 1080;
-            }
+            cur_fwd->gateway_port = 1080;
+            parse_forwarder_address(p, &cur_fwd->gateway_host,
+               &cur_fwd->gateway_port);
 
             /* Parse the parent HTTP proxy host[:port] */
             p = vec[2];
 
             if (strcmp(p, ".") != 0)
             {
-               cur_fwd->forward_host = strdup(p);
-
-               if (*cur_fwd->forward_host == '[' && 
-                     NULL != (p = strchr(cur_fwd->forward_host, ']')))
-               {
-                  *p++ = '\0';
-                  memmove(cur_fwd->forward_host, cur_fwd->forward_host + 1,
-                        (size_t) (p - cur_fwd->forward_host));
-                  if (*p == ':')
-                  {
-                     cur_fwd->forward_port = atoi(++p);
-                  }
-               }
-               else if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
-               {
-                  *p++ = '\0';
-                  cur_fwd->forward_port = atoi(p);
-               }
-
-               if (cur_fwd->forward_port <= 0)
-               {
-                  cur_fwd->forward_port = 8000;
-               }
+               cur_fwd->forward_port = 8000;
+               parse_forwarder_address(p, &cur_fwd->forward_host,
+                  &cur_fwd->forward_port);
             }
 
             /* Add to list. */
index 916edb1..b1e72b9 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.48 2009/04/17 11:27:49 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.49 2009/04/17 11:34:35 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -33,6 +33,9 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.48 2009/04/17 11:27:49 fabianke
  *
  * Revisions   :
  *    $Log: urlmatch.c,v $
+ *    Revision 1.49  2009/04/17 11:34:35  fabiankeil
+ *    Style cosmetics for the IPv6 code.
+ *
  *    Revision 1.48  2009/04/17 11:27:49  fabiankeil
  *    Petr Pisar's privoxy-3.0.12-ipv6-3.diff.
  *
@@ -1506,6 +1509,59 @@ int match_portlist(const char *portlist, int port)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  parse_forwarder_address
+ *
+ * Description :  Parse out the host and port from a forwarder address.
+ *
+ * Parameters  :
+ *          1  :  address = The forwarder address to parse.
+ *          2  :  hostname = Used to return the hostname. NULL on error.
+ *          3  :  port = Used to return the port. Untouched if no port
+ *                       is specified.
+ *
+ * Returns     :  JB_ERR_OK on success
+ *                JB_ERR_MEMORY on out of memory
+ *                JB_ERR_PARSE on malformed address.
+ *
+ *********************************************************************/
+jb_err parse_forwarder_address(char *address, char **hostname, int *port)
+{
+   char *p = address;
+
+   if ((*address == '[') && (NULL == strchr(address, ']')))
+   {
+      /* XXX: Should do some more validity checks here. */
+      return JB_ERR_PARSE;
+   }
+
+   *hostname = strdup(address);
+   if (NULL == *hostname)
+   {
+      return JB_ERR_MEMORY;
+   }
+
+   if ((**hostname == '[') && (NULL != (p = strchr(*hostname, ']'))))
+   {
+      *p++ = '\0';
+      memmove(*hostname, (*hostname + 1), (size_t)(p - *hostname));
+      if (*p == ':')
+      {
+         *port = (int)strtol(++p, NULL, 0);
+      }
+   }
+   else if (NULL != (p = strchr(*hostname, ':')))
+   {
+      *p++ = '\0';
+      *port = (int)strtol(p, NULL, 0);
+   }
+
+   return JB_ERR_OK;
+
+}
+
+
 /*
   Local Variables:
   tab-width: 3
index 483a0bc..254d52f 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef URLMATCH_H_INCLUDED
 #define URLMATCH_H_INCLUDED
-#define URLMATCH_H_VERSION "$Id: urlmatch.h,v 1.12 2008/05/04 16:18:32 fabiankeil Exp $"
+#define URLMATCH_H_VERSION "$Id: urlmatch.h,v 1.13 2009/03/02 19:18:11 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.h,v $
  *
  * Revisions   :
  *    $Log: urlmatch.h,v $
+ *    Revision 1.13  2009/03/02 19:18:11  fabiankeil
+ *    Streamline parse_http_request()'s prototype. As
+ *    cparser pointed out it doesn't actually use csp.
+ *
  *    Revision 1.12  2008/05/04 16:18:32  fabiankeil
  *    Provide parse_http_url() with a third parameter to specify
  *    whether or not URLs without protocol are acceptable.
@@ -113,6 +117,7 @@ extern int url_match(const struct url_spec *pattern,
 extern jb_err create_url_spec(struct url_spec *url, char *buf);
 extern void free_url_spec(struct url_spec *url);
 extern int match_portlist(const char *portlist, int port);
+extern jb_err parse_forwarder_address(char *address, char **hostname, int *port);
 
 
 /* Revision control strings from this header and associated .c file */