Revision: 14691
Author: adrian.chadd
Date: Sun May 23 05:26:34 2010
Log: Break out matchDomainName() into libsqurl so it can be separately
unit tested.


http://code.google.com/p/lusca-cache/source/detail?r=14691

Added:
 /branches/LUSCA_HEAD/libsqurl/domain.c
 /branches/LUSCA_HEAD/libsqurl/domain.h
Modified:
 /branches/LUSCA_HEAD/libsqurl/Makefile.am
 /branches/LUSCA_HEAD/src/acl.c
 /branches/LUSCA_HEAD/src/neighbors.c
 /branches/LUSCA_HEAD/src/protos.h
 /branches/LUSCA_HEAD/src/url.c

=======================================
--- /dev/null
+++ /branches/LUSCA_HEAD/libsqurl/domain.c      Sun May 23 05:26:34 2010
@@ -0,0 +1,91 @@
+#include "../include/config.h"
+
+#include <stdio.h>
+#if HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include "../include/util.h"
+
+/*
+ * matchDomainName() compares a hostname with a domainname according
+ * to the following rules:
+ *
+ *    HOST          DOMAIN        MATCH?
+ * ------------- -------------    ------
+ *    foo.com       foo.com         YES
+ *   .foo.com       foo.com         YES
+ *  x.foo.com       foo.com          NO
+ *    foo.com      .foo.com         YES
+ *   .foo.com      .foo.com         YES
+ *  x.foo.com      .foo.com         YES
+ *
+ *  We strip leading dots on hosts (but not domains!) so that
+ *  ".foo.com" is is always the same as "foo.com".
+ *
+ *  Return values:
+ *     0 means the host matches the domain
+ *     1 means the host is greater than the domain
+ *    -1 means the host is less than the domain
+ */
+
+int
+matchDomainName(const char *h, const char *d)
+{
+    int dl;
+    int hl;
+    while ('.' == *h)
+       h++;
+    hl = strlen(h);
+    dl = strlen(d);
+    /*
+     * Start at the ends of the two strings and work towards the
+     * beginning.
+     */
+    while (xtolower(h[--hl]) == xtolower(d[--dl])) {
+       if (hl == 0 && dl == 0) {
+           /*
+            * We made it all the way to the beginning of both
+            * strings without finding any difference.
+            */
+           return 0;
+       }
+       if (0 == hl) {
+           /*
+            * The host string is shorter than the domain string.
+            * There is only one case when this can be a match.
+            * If the domain is just one character longer, and if
+            * that character is a leading '.' then we call it a
+            * match.
+            */
+           if (1 == dl && '.' == d[0])
+               return 0;
+           else
+               return -1;
+       }
+       if (0 == dl) {
+           /*
+            * The domain string is shorter than the host string.
+            * This is a match only if the first domain character
+            * is a leading '.'.
+            */
+           if ('.' == d[0])
+               return 0;
+           else
+               return 1;
+       }
+    }
+    /*
+     * We found different characters in the same position (from the end).
+     */
+    /*
+     * If one of those character is '.' then its special.  In order
+     * for splay tree sorting to work properly, "x-foo.com" must
+     * be greater than ".foo.com" even though '-' is less than '.'.
+     */
+    if ('.' == d[dl])
+       return 1;
+    if ('.' == h[hl])
+       return -1;
+    return (xtolower(h[hl]) - xtolower(d[dl]));
+}
=======================================
--- /dev/null
+++ /branches/LUSCA_HEAD/libsqurl/domain.h      Sun May 23 05:26:34 2010
@@ -0,0 +1,6 @@
+#ifndef        __LUSCA_LIBSQURL_DOMAIN_H__
+#define        __LUSCA_LIBSQURL_DOMAIN_H__
+
+extern int matchDomainName(const char *h, const char *d);
+
+#endif
=======================================
--- /branches/LUSCA_HEAD/libsqurl/Makefile.am   Sun May 23 03:46:54 2010
+++ /branches/LUSCA_HEAD/libsqurl/Makefile.am   Sun May 23 05:26:34 2010
@@ -2,7 +2,8 @@

 libsqurl_a_SOURCES = \
        url.c \
-       proto.c
+       proto.c \
+       domain.c

 noinst_LIBRARIES = \
        libsqurl.a
=======================================
--- /branches/LUSCA_HEAD/src/acl.c      Mon Apr 19 02:27:56 2010
+++ /branches/LUSCA_HEAD/src/acl.c      Sun May 23 05:26:34 2010
@@ -37,6 +37,9 @@
 #include "splay.h"
 #include "client_db.h"

+#include "../libsqurl/domain.h"
+
+
 static void aclParseDomainList(void *curlist);
 static void aclParseUserList(void **current);
 static void aclParseIpList(void *curlist);
=======================================
--- /branches/LUSCA_HEAD/src/neighbors.c        Wed Apr 21 07:10:06 2010
+++ /branches/LUSCA_HEAD/src/neighbors.c        Sun May 23 05:26:34 2010
@@ -36,6 +36,8 @@
 #include "squid.h"
 #include "icmp.h"

+#include "../libsqurl/domain.h"
+
 /* count mcast group peers every 15 minutes */
 #define MCAST_COUNT_RATE 900

=======================================
--- /branches/LUSCA_HEAD/src/protos.h   Sun May 23 04:11:01 2010
+++ /branches/LUSCA_HEAD/src/protos.h   Sun May 23 05:26:34 2010
@@ -790,7 +790,6 @@
 extern char *urlMakeAbsolute(request_t *, const char *);
extern char *urlRInternal(const char *host, u_short port, const char *dir, const char *name);
 extern char *urlInternal(const char *dir, const char *name);
-extern int matchDomainName(const char *host, const char *domain);
 extern int urlCheckRequest(const request_t *);
 extern char *urlCanonicalClean(const request_t *);
 extern char *urlHostname(const char *url);
=======================================
--- /branches/LUSCA_HEAD/src/url.c      Sun May 23 04:11:01 2010
+++ /branches/LUSCA_HEAD/src/url.c      Sun May 23 05:26:34 2010
@@ -36,6 +36,7 @@
 #include "squid.h"

 #include "../libsqurl/proto.h"
+#include "../libsqurl/domain.h"
 #include "../libsqurl/url.h"

 static request_t *urnParse(method_t * method, char *urn);
@@ -466,89 +467,6 @@
        xstrncpy(buf, rfc1738_escape_unescaped(buf), MAX_URL);
     return buf;
 }
-
-/*
- * matchDomainName() compares a hostname with a domainname according
- * to the following rules:
- *
- *    HOST          DOMAIN        MATCH?
- * ------------- -------------    ------
- *    foo.com       foo.com         YES
- *   .foo.com       foo.com         YES
- *  x.foo.com       foo.com          NO
- *    foo.com      .foo.com         YES
- *   .foo.com      .foo.com         YES
- *  x.foo.com      .foo.com         YES
- *
- *  We strip leading dots on hosts (but not domains!) so that
- *  ".foo.com" is is always the same as "foo.com".
- *
- *  Return values:
- *     0 means the host matches the domain
- *     1 means the host is greater than the domain
- *    -1 means the host is less than the domain
- */
-
-int
-matchDomainName(const char *h, const char *d)
-{
-    int dl;
-    int hl;
-    while ('.' == *h)
-       h++;
-    hl = strlen(h);
-    dl = strlen(d);
-    /*
-     * Start at the ends of the two strings and work towards the
-     * beginning.
-     */
-    while (xtolower(h[--hl]) == xtolower(d[--dl])) {
-       if (hl == 0 && dl == 0) {
-           /*
-            * We made it all the way to the beginning of both
-            * strings without finding any difference.
-            */
-           return 0;
-       }
-       if (0 == hl) {
-           /*
-            * The host string is shorter than the domain string.
-            * There is only one case when this can be a match.
-            * If the domain is just one character longer, and if
-            * that character is a leading '.' then we call it a
-            * match.
-            */
-           if (1 == dl && '.' == d[0])
-               return 0;
-           else
-               return -1;
-       }
-       if (0 == dl) {
-           /*
-            * The domain string is shorter than the host string.
-            * This is a match only if the first domain character
-            * is a leading '.'.
-            */
-           if ('.' == d[0])
-               return 0;
-           else
-               return 1;
-       }
-    }
-    /*
-     * We found different characters in the same position (from the end).
-     */
-    /*
-     * If one of those character is '.' then its special.  In order
-     * for splay tree sorting to work properly, "x-foo.com" must
-     * be greater than ".foo.com" even though '-' is less than '.'.
-     */
-    if ('.' == d[dl])
-       return 1;
-    if ('.' == h[hl])
-       return -1;
-    return (xtolower(h[hl]) - xtolower(d[dl]));
-}

 int
 urlCheckRequest(const request_t * r)

--
You received this message because you are subscribed to the Google Groups 
"lusca-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/lusca-commit?hl=en.

Reply via email to