]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Author: Mark Nottingham <mnot@pobox.com>
authorAmos Jeffries <squid3@treenet.co.nz>
Sat, 15 Aug 2009 02:14:54 +0000 (14:14 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Sat, 15 Aug 2009 02:14:54 +0000 (14:14 +1200)
Bug 2390: New hier_code ACL type

I needed to log only forward requests; forward_log looked interesting, but
isn't flexible like access_log. The easiest thing to do seemed to be to add a
hier_code ACL type.

Adapted to Squid-3 by Amos Jeffries

16 files changed:
src/AclRegs.cc
src/HierarchyLogEntry.h
src/Makefile.am
src/access_log.cc
src/acl/HierCode.cc [new file with mode: 0644]
src/acl/HierCode.h [new file with mode: 0644]
src/acl/HierCodeData.cc [new file with mode: 0644]
src/acl/HierCodeData.h [new file with mode: 0644]
src/acl/Makefile.am
src/cf.data.pre
src/enums.h
src/forward.cc
src/forward.h
src/hier_code.cc [new file with mode: 0644]
src/hier_code.h [new file with mode: 0644]
src/peer_select.cc

index 3101d97248bab28da68a561bfd1ab46d4ffadfe2..f91c4581d5f3d22550e02e3e0057e97993379887 100644 (file)
@@ -20,6 +20,8 @@
 #include "acl/ExtUser.h"
 #include "acl/FilledChecklist.h"
 #include "acl/Gadgets.h"
+#include "acl/HierCodeData.h"
+#include "acl/HierCode.h"
 #include "acl/HttpHeaderData.h"
 #include "acl/HttpRepHeader.h"
 #include "acl/HttpReqHeader.h"
@@ -79,6 +81,8 @@ ACL::Prototype ACLExtUser::UserRegistryProtoype(&ACLExtUser::UserRegistryEntry_,
 ACLExtUser ACLExtUser::UserRegistryEntry_(new ACLUserData, "ext_user");
 ACL::Prototype ACLExtUser::RegexRegistryProtoype(&ACLExtUser::RegexRegistryEntry_, "ext_user_regex" );
 ACLExtUser ACLExtUser::RegexRegistryEntry_(new ACLRegexData, "ext_user_regex");
+ACL::Prototype ACLHierCode::RegistryProtoype(&ACLHierCode::RegistryEntry_, "hier_code");
+ACLStrategised<hier_code> ACLHierCode::RegistryEntry_(new ACLHierCodeData, ACLHierCodeStrategy::Instance(), "hier_code");
 ACL::Prototype ACLHTTPRepHeader::RegistryProtoype(&ACLHTTPRepHeader::RegistryEntry_, "rep_header");
 ACLStrategised<HttpHeader*> ACLHTTPRepHeader::RegistryEntry_(new ACLHTTPHeaderData, ACLHTTPRepHeaderStrategy::Instance(), "rep_header");
 ACL::Prototype ACLHTTPReqHeader::RegistryProtoype(&ACLHTTPReqHeader::RegistryEntry_, "req_header");
index cb18a27519088e6a5247f938eaab07183c68b85e..4617b26d384cb822579898a56ad2a87c0e5d5aeb 100644 (file)
 #ifndef SQUID_HTTPHIERARCHYLOGENTRY_H
 #define SQUID_HTTPHIERARCHYLOGENTRY_H
 
+#include "hier_code.h"
 #include "rfc2181.h"
 #include "PingData.h"
 
-/** todo Cleanup: break hier_code type out. We don't need the rest. */
+/* for lookup_t and http_status */
 #include "enums.h"
 
 class HierarchyLogEntry
index 393c334b0c7a1516e2a65662f2d0602e9b1c2bf1..906cfc7ae515b3dcb5cde63ac11820ec834528ae 100644 (file)
@@ -354,6 +354,8 @@ squid_SOURCES = \
        gopher.cc \
        helper.cc \
        helper.h \
+       hier_code.h \
+       hier_code.cc \
        HierarchyLogEntry.h \
        $(HTCPSOURCE) \
        http.cc \
@@ -1088,6 +1090,8 @@ tests_testCacheManager_SOURCES = \
        fqdncache.cc \
        ftp.cc \
        gopher.cc \
+       hier_code.h \
+       hier_code.cc \
        helper.cc \
        $(HTCPSOURCE) \
        http.cc \
@@ -1258,6 +1262,8 @@ tests_testEvent_SOURCES = \
        fqdncache.cc \
        ftp.cc \
        gopher.cc \
+       hier_code.h \
+       hier_code.cc \
        helper.cc \
        $(HTCPSOURCE) \
        http.cc \
@@ -1407,6 +1413,8 @@ tests_testEventLoop_SOURCES = \
        ftp.cc \
        gopher.cc \
        helper.cc \
+       hier_code.h \
+       hier_code.cc \
        $(HTCPSOURCE) \
        http.cc \
        HttpBody.cc \
@@ -1544,6 +1552,8 @@ tests_test_http_range_SOURCES = \
        ftp.cc \
        gopher.cc \
        helper.cc \
+       hier_code.h \
+       hier_code.cc \
        $(HTCPSOURCE) \
        http.cc \
        HttpBody.cc \
@@ -1697,6 +1707,8 @@ tests_testHttpRequest_SOURCES = \
        ftp.cc \
        gopher.cc \
        helper.cc \
+       hier_code.h \
+       hier_code.cc \
        $(HTCPSOURCE) \
        http.cc \
        HttpBody.cc \
@@ -2049,6 +2061,8 @@ tests_testURL_SOURCES = \
        ftp.cc \
        gopher.cc \
        helper.cc \
+       hier_code.h \
+       hier_code.cc \
        $(HTCPSOURCE) \
        http.cc \
        HttpBody.cc \
index 2630214a486e45766c948aa94db85def29c2eca2..9ecb48b3a972735ee297935cf827f64f6b4facdb 100644 (file)
@@ -41,6 +41,7 @@
 
 #include "acl/Checklist.h"
 
+#include "hier_code.h"
 #include "HttpReply.h"
 #include "HttpRequest.h"
 #include "MemBuf.h"
diff --git a/src/acl/HierCode.cc b/src/acl/HierCode.cc
new file mode 100644 (file)
index 0000000..70bf4f4
--- /dev/null
@@ -0,0 +1,24 @@
+#include "squid.h"
+#include "acl/HierCode.h"
+#include "acl/HierCodeData.h"
+#include "acl/Checklist.h"
+#include "HttpRequest.h"
+
+/* explicit template instantiation required for some systems */
+
+template class ACLStrategised<hier_code>;
+
+
+int
+ACLHierCodeStrategy::match (ACLData<MatchType> * &data, ACLFilledChecklist *checklist)
+{
+    return data->match (checklist->request->hier.code);
+}
+
+ACLHierCodeStrategy *
+ACLHierCodeStrategy::Instance()
+{
+    return &Instance_;
+}
+
+ACLHierCodeStrategy ACLHierCodeStrategy::Instance_;
diff --git a/src/acl/HierCode.h b/src/acl/HierCode.h
new file mode 100644 (file)
index 0000000..60661e7
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef SQUID_ACLHIERCODE_H
+#define SQUID_ACLHIERCODE_H
+
+#include "acl/Strategy.h"
+#include "acl/Strategised.h"
+#include "hier_code.h"
+
+/// \ingroup ACLAPI
+class ACLHierCodeStrategy : public ACLStrategy<hier_code>
+{
+
+public:
+    virtual int match (ACLData<MatchType> * &, ACLFilledChecklist *);
+    virtual bool requiresRequest() const {return true;}
+
+    static ACLHierCodeStrategy *Instance();
+
+    /**
+     * Not implemented to prevent copies of the instance.
+     \par
+     * Not private to prevent brain dead g+++ warnings about
+     * private constructors with no friends
+     */
+    ACLHierCodeStrategy(ACLHierCodeStrategy const &);
+
+private:
+    static ACLHierCodeStrategy Instance_;
+    ACLHierCodeStrategy() {}
+
+    ACLHierCodeStrategy &operator=(ACLHierCodeStrategy const &);
+};
+
+/// \ingroup ACLAPI
+class ACLHierCode
+{
+
+private:
+    static ACL::Prototype RegistryProtoype;
+    static ACLStrategised<hier_code> RegistryEntry_;
+};
+
+#endif /* SQUID_ACLHIERCODE_H */
diff --git a/src/acl/HierCodeData.cc b/src/acl/HierCodeData.cc
new file mode 100644 (file)
index 0000000..de8f984
--- /dev/null
@@ -0,0 +1,72 @@
+#include "squid.h"
+#include "acl/HierCodeData.h"
+#include "acl/Checklist.h"
+#include "hier_code.h"
+#include "wordlist.h"
+
+ACLHierCodeData::ACLHierCodeData()
+{
+    // initialize mask to NULL
+    memset(values, 0, sizeof(values));
+}
+
+ACLHierCodeData::ACLHierCodeData(ACLHierCodeData const &old)
+{
+    memcpy(values, old.values, sizeof(values) );
+}
+
+ACLHierCodeData::~ACLHierCodeData()
+{ }
+
+bool
+ACLHierCodeData::match(hier_code toFind)
+{
+    return values[toFind];
+}
+
+wordlist *
+ACLHierCodeData::dump()
+{
+    wordlist *W = NULL;
+
+    for (hier_code iter=HIER_NONE; iter<HIER_MAX; ++iter) {
+        if (!values[iter]) continue;
+        wordlistAdd(&W, hier_strings[iter]);
+    }
+
+    return W;
+}
+
+void
+ACLHierCodeData::parse()
+{
+    char *t = NULL;
+
+    while ((t = strtokFile())) {
+        for (hier_code iter = HIER_NONE; iter <= HIER_MAX; ++iter) {
+            if (iter == HIER_MAX) {
+                fatalf("ERROR: No such hier_code '%s'",t);
+                return;
+            }
+            if (strcmp(hier_strings[iter],t) == 0) {
+                values[iter] = true;
+                break; // back to while-loop
+            }
+        }
+    }
+}
+
+bool
+ACLHierCodeData::empty() const
+{
+    for (hier_code iter = HIER_NONE; iter <= HIER_MAX; ++iter) {
+        if (values[iter]) return false; // not empty.
+    }
+    return true;
+}
+
+ACLData<hier_code> *
+ACLHierCodeData::clone() const
+{
+    return new ACLHierCodeData(*this);
+}
diff --git a/src/acl/HierCodeData.h b/src/acl/HierCodeData.h
new file mode 100644 (file)
index 0000000..4331a5d
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef SQUID_ACLHIERCODEDATA_H
+#define SQUID_ACLHIERCODEDATA_H
+
+#include "acl/Acl.h"
+#include "acl/Data.h"
+#include "CbDataList.h"
+#include "hier_code.h"
+
+/// \ingroup ACLAPI
+class ACLHierCodeData : public ACLData<hier_code>
+{
+
+public:
+    MEMPROXY_CLASS(ACLHierCodeData);
+
+    ACLHierCodeData();
+    ACLHierCodeData(ACLHierCodeData const &);
+    ACLHierCodeData &operator= (ACLHierCodeData const &);
+    virtual ~ACLHierCodeData();
+    bool match(hier_code);
+    wordlist *dump();
+    void parse();
+    bool empty() const;
+    virtual ACLData<hier_code> *clone() const;
+
+    // mask of codes this ACL might match.
+    bool values[HIER_MAX];
+};
+
+MEMPROXY_CLASS_INLINE(ACLHierCodeData);
+
+#endif /* SQUID_ACLHIERCODEDATA_H */
index f98c937549f61ce72dd88cb991b135452e258a0c..f1aa28869737b1b754290b3912de5e0bca6003e9 100644 (file)
@@ -47,6 +47,10 @@ libacls_la_SOURCES = \
        DomainData.h \
        ExtUser.cc \
        ExtUser.h \
+       HierCodeData.cc \
+       HierCodeData.h \
+       HierCode.cc \
+       HierCode.h \
        HttpHeaderData.cc \
        HttpHeaderData.h \
        HttpRepHeader.cc \
index 830b622442bfbc9ba041d388494b94083fa25aed..6a9e3962f86b16f304452c527680f1c9bfeedabe 100644 (file)
@@ -677,6 +677,14 @@ DOC_START
        acl aclname tag tagvalue ...
          # string match on tag returned by external acl helper [slow]
 
+       acl aclname hier_code codename ...
+         # string match against squid hierarchy code(s); [fast]
+         #  e.g., DIRECT, PARENT_HIT, NONE, etc.
+         #
+         # NOTE: This has no effect in http_access rules. It only has
+         # effect in rules that affect the reply data stream such as
+         # http_reply_access.
+
 Examples:
 acl macaddress arp 09:00:2b:23:45:67
 acl myexample dst_as 1241
index c3bfb0c3aa600a88f54e36347a4f621768063507..e6bb8ca1a32acd603c1bf2d220bf718c8d483f6e 100644 (file)
@@ -155,32 +155,6 @@ typedef enum {
     SC_ENUM_END
 } http_hdr_sc_type;
 
-typedef enum {
-    HIER_NONE,
-    HIER_DIRECT,
-    SIBLING_HIT,
-    PARENT_HIT,
-    DEFAULT_PARENT,
-    SINGLE_PARENT,
-    FIRSTUP_PARENT,
-    FIRST_PARENT_MISS,
-    CLOSEST_PARENT_MISS,
-    CLOSEST_PARENT,
-    CLOSEST_DIRECT,
-    NO_DIRECT_FAIL,
-    SOURCE_FASTEST,
-    ROUNDROBIN_PARENT,
-#if USE_CACHE_DIGESTS
-    CD_PARENT_HIT,
-    CD_SIBLING_HIT,
-#endif
-    CARP,
-    ANY_OLD_PARENT,
-    USERHASH_PARENT,
-    SOURCEHASH_PARENT,
-    HIER_MAX,
-    PINNED
-} hier_code;
 
 /// \ingroup ServerProtocolICPAPI
 typedef enum {
index 6f0895726b162ad6cd654339eddd83cb405629d2..4328f6c83ece07a4c71a3a96483155c679561fe4 100644 (file)
@@ -39,6 +39,7 @@
 #include "event.h"
 #include "errorpage.h"
 #include "fde.h"
+#include "hier_code.h"
 #include "HttpReply.h"
 #include "HttpRequest.h"
 #include "MemObject.h"
index a1a737fb123fc13a115f8aa8c8907c67b5e8616c..1e7eedebef4f346e5be5f5e98cd517d749b051f3 100644 (file)
@@ -7,11 +7,11 @@ class ErrorState;
 class HttpRequest;
 
 #include "comm.h"
+#include "hier_code.h"
 #include "ip/IpAddress.h"
 
 class FwdServer
 {
-
 public:
     peer *_peer;                /* NULL --> origin server */
     hier_code code;
@@ -20,7 +20,6 @@ public:
 
 class FwdState : public RefCountable
 {
-
 public:
     typedef RefCount<FwdState> Pointer;
     ~FwdState();
diff --git a/src/hier_code.cc b/src/hier_code.cc
new file mode 100644 (file)
index 0000000..4a5157e
--- /dev/null
@@ -0,0 +1,29 @@
+#include "config.h"
+#include "hier_code.h"
+
+const char *hier_strings[] = {
+    "NONE",          /* HIER_NONE */
+    "DIRECT",        /* HIER_DIRECT */
+    "SIBLING_HIT",
+    "PARENT_HIT",
+    "DEFAULT_PARENT",
+    "SINGLE_PARENT",
+    "FIRSTUP_PARENT",
+    "FIRST_PARENT_MISS",
+    "CLOSEST_PARENT_MISS",
+    "CLOSEST_PARENT",
+    "CLOSEST_DIRECT",
+    "NO_DIRECT_FAIL",
+    "SOURCE_FASTEST",
+    "ROUNDROBIN_PARENT",
+#if USE_CACHE_DIGESTS
+    "CD_PARENT_HIT",
+    "CD_SIBLING_HIT",
+#endif
+    "CARP",
+    "ANY_PARENT",
+    "USERHASH",
+    "SOURCEHASH",
+    "PINNED",
+    "HIER_MAX"
+};
diff --git a/src/hier_code.h b/src/hier_code.h
new file mode 100644 (file)
index 0000000..5b91d24
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef SQUID__HIER_CODE_H
+#define SQUID__HIER_CODE_H
+
+typedef enum {
+    HIER_NONE,
+    HIER_DIRECT,
+    SIBLING_HIT,
+    PARENT_HIT,
+    DEFAULT_PARENT,
+    SINGLE_PARENT,
+    FIRSTUP_PARENT,
+    FIRST_PARENT_MISS,
+    CLOSEST_PARENT_MISS,
+    CLOSEST_PARENT,
+    CLOSEST_DIRECT,
+    NO_DIRECT_FAIL,
+    SOURCE_FASTEST,
+    ROUNDROBIN_PARENT,
+#if USE_CACHE_DIGESTS
+    CD_PARENT_HIT,
+    CD_SIBLING_HIT,
+#endif
+    CARP,
+    ANY_OLD_PARENT,
+    USERHASH_PARENT,
+    SOURCEHASH_PARENT,
+    PINNED,
+    HIER_MAX
+} hier_code;
+
+extern const char *hier_strings[];
+
+inline const hier_code operator++(hier_code &i) { return (hier_code)(i+1); }
+
+#endif /* SQUID__HIER_CODE_H */
index 7caf9c5ec5801bc4911b90b9de72d9265c58aca2..72e9eea8a3056c504e5e8f451aaf04b232f7d839 100644 (file)
@@ -36,6 +36,7 @@
 #include "event.h"
 #include "PeerSelectState.h"
 #include "Store.h"
+#include "hier_code.h"
 #include "ICP.h"
 #include "HttpRequest.h"
 #include "acl/FilledChecklist.h"
 #include "SquidTime.h"
 #include "icmp/net_db.h"
 
-const char *hier_strings[] = {
-    "NONE",
-    "DIRECT",
-    "SIBLING_HIT",
-    "PARENT_HIT",
-    "DEFAULT_PARENT",
-    "SINGLE_PARENT",
-    "FIRST_UP_PARENT",
-    "FIRST_PARENT_MISS",
-    "CLOSEST_PARENT_MISS",
-    "CLOSEST_PARENT",
-    "CLOSEST_DIRECT",
-    "NO_DIRECT_FAIL",
-    "SOURCE_FASTEST",
-    "ROUNDROBIN_PARENT",
-#if USE_CACHE_DIGESTS
-    "CD_PARENT_HIT",
-    "CD_SIBLING_HIT",
-#endif
-    "CARP",
-    "ANY_PARENT",
-    "USERHASH",
-    "SOURCEHASH",
-    "INVALID CODE"
-};
-
 static struct {
     int timeouts;
 } PeerStats;
@@ -624,7 +599,6 @@ void
 peerSelectInit(void)
 {
     memset(&PeerStats, '\0', sizeof(PeerStats));
-    assert(sizeof(hier_strings) == (HIER_MAX + 1) * sizeof(char *));
 }
 
 static void