- Allow port lists in url patterns.
authorFabian Keil <fk@fabiankeil.de>
Sun, 2 Sep 2007 13:42:11 +0000 (13:42 +0000)
committerFabian Keil <fk@fabiankeil.de>
Sun, 2 Sep 2007 13:42:11 +0000 (13:42 +0000)
- Ditch unused url_spec member pathlen.

project.h
urlmatch.c

index efc2400..3101a96 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.98 2007/07/14 07:31:26 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.99 2007/07/21 11:51:36 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
  *
  * Revisions   :
  *    $Log: project.h,v $
+ *    Revision 1.99  2007/07/21 11:51:36  fabiankeil
+ *    As Hal noticed, checking dispatch_cgi() as the last cruncher
+ *    looks like a bug if CGI requests are blocked unintentionally,
+ *    so don't do it unless the user enabled the new config option
+ *    "allow-cgi-request-crunching".
+ *
  *    Revision 1.98  2007/07/14 07:31:26  fabiankeil
  *    Add new csp->content_type flag (CT_DECLARED).
  *
@@ -894,10 +900,9 @@ struct url_spec
    int    dcount;      /**< How many parts to this domain? (length of dvec)   */
    int    unanchored;  /**< Bitmap - flags are ANCHOR_LEFT and ANCHOR_RIGHT.  */
 
-   int   port;         /**< The port number, or 0 to match all ports.         */
+   char  *port_list;   /**< List of acceptable ports, or NULL to match all ports */
 
-   char *path;         /**< The source for the regex.                         */
-   size_t pathlen;     /**< ==strlen(path).  Needed for prefix matching.  FIXME: Now obsolete?     */
+   char  *path;        /**< The source for the regex.                         */
    regex_t *preg;      /**< Regex for matching path part                      */
    regex_t *tag_regex; /**< Regex for matching tags                           */
 };
@@ -905,7 +910,7 @@ struct url_spec
 /**
  * If you declare a static url_spec, this is the value to initialize it to zero.
  */
-#define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL, NULL }
+#define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL }
 
 /**
  * Constant for host part matching in URLs.  If set, indicates that the start of
index 74287ff..96d8877 100644 (file)
@@ -1,4 +1,4 @@
-const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.17 2007/04/15 16:39:21 fabiankeil Exp $";
+const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.18 2007/07/30 16:42:21 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
@@ -33,6 +33,11 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.17 2007/04/15 16:39:21 fabianke
  *
  * Revisions   :
  *    $Log: urlmatch.c,v $
+ *    Revision 1.18  2007/07/30 16:42:21  fabiankeil
+ *    Move the method check into unknown_method()
+ *    and loop through the known methods instead
+ *    of using a screen-long OR chain.
+ *
  *    Revision 1.17  2007/04/15 16:39:21  fabiankeil
  *    Introduce tags as alternative way to specify which
  *    actions apply to a request. At the moment tags can be
@@ -161,6 +166,10 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.17 2007/04/15 16:39:21 fabianke
 #include "ssplit.h"
 #include "miscutil.h"
 #include "errlog.h"
+/*
+ * XXX: only for match_portlist() which I will relocate soonish.
+ */
+#include "filters.h"
 
 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
 
@@ -807,20 +816,20 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
    }
 
    /* Only reached for URL patterns */
-   if ((p = strchr(buf, '/')) != NULL)
+   p = strchr(buf, '/');
+   if (NULL != p)
    {
-      if (NULL == (url->path = strdup(p)))
+      url->path = strdup(p);
+      if (NULL == url->path)
       {
          freez(url->spec);
          return JB_ERR_MEMORY;
       }
-      url->pathlen = strlen(url->path);
       *p = '\0';
    }
    else
    {
-      url->path    = NULL;
-      url->pathlen = 0;
+      url->path = NULL;
    }
    if (url->path)
    {
@@ -856,14 +865,20 @@ jb_err create_url_spec(struct url_spec * url, const char * buf)
          return JB_ERR_PARSE;
       }
    }
-   if ((p = strchr(buf, ':')) == NULL)
+
+   p = strchr(buf, ':');
+   if (NULL != p)
    {
-      url->port = 0;
+      *p++ = '\0';
+      url->port_list = strdup(p);
+      if (NULL == url->port_list)
+      {
+         return JB_ERR_MEMORY;
+      }
    }
    else
    {
-      *p++ = '\0';
-      url->port = atoi(p);
+      url->port_list = NULL;
    }
 
    if (buf[0] != '\0')
@@ -974,6 +989,7 @@ void free_url_spec(struct url_spec *url)
    freez(url->dbuffer);
    freez(url->dvec);
    freez(url->path);
+   freez(url->port_list);
    if (url->preg)
    {
       regfree(url->preg);
@@ -1003,17 +1019,22 @@ void free_url_spec(struct url_spec *url)
 int url_match(const struct url_spec *pattern,
               const struct http_request *url)
 {
+   int port_matches;
+   int domain_matches;
+   int path_matches;
+
    if (pattern->tag_regex != NULL)
    {
       /* It's a tag pattern and shouldn't be matched against URLs */
       return 0;
    } 
 
-   return ((pattern->port == 0) || (pattern->port == url->port))
-       && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
-       && ((pattern->path == NULL) ||
-            (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)
-      );
+   port_matches = (NULL == pattern->port_list) || match_portlist(pattern->port_list, url->port);
+   domain_matches = (NULL == pattern->dbuffer) || (0 == domain_match(pattern, url));
+   path_matches = (NULL == pattern->path) || (0 == regexec(pattern->preg, url->path, 0, NULL, 0));
+
+   return (port_matches && domain_matches && path_matches);
+
 }