]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Implement base/LookupTable as proposed by Amos
authorFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 27 Jul 2015 09:30:32 +0000 (11:30 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 27 Jul 2015 09:30:32 +0000 (11:30 +0200)
src/auth/digest/Config.cc
src/base/LookupTable.h [new file with mode: 0644]
src/base/Makefile.am

index f68a72296c4ee673f1df457d3f3619e345c9791e..3a5f1fc5d4b90bd37a5732b2c6ad05e351846ffc 100644 (file)
@@ -19,6 +19,7 @@
 #include "auth/digest/UserRequest.h"
 #include "auth/Gadgets.h"
 #include "auth/State.h"
+#include "base/LookupTable.h"
 #include "base64.h"
 #include "cache_cf.h"
 #include "event.h"
@@ -110,6 +111,24 @@ DigestFieldsLookupTable_t::lookup(const SBuf &key) const
 }
 DigestFieldsLookupTable_t DigestFieldsLookupTable;
 
+
+static const LookupTable<http_digest_attr_type>::Record
+  DigestAttrs_Exp[] = {
+    {"username", DIGEST_USERNAME},
+    {"realm", DIGEST_REALM},
+    {"qop", DIGEST_QOP},
+    {"algorithm", DIGEST_ALGORITHM},
+    {"uri", DIGEST_URI},
+    {"nonce", DIGEST_NONCE},
+    {"nc", DIGEST_NC},
+    {"cnonce", DIGEST_CNONCE},
+    {"response", DIGEST_RESPONSE},
+    {nullptr, DIGEST_ENUM_END}
+};
+
+LookupTable<http_digest_attr_type>
+ExpDigestFieldsLookupTable(DIGEST_ENUM_END, DigestAttrs_Exp);
+
 /*
  *
  * Nonce Functions
@@ -842,6 +861,8 @@ Auth::Digest::Config::decode(char const *proxy_auth, const char *aRequestRealm)
 
         /* find type */
         const http_digest_attr_type t = DigestFieldsLookupTable.lookup(keyName);
+        const http_digest_attr_type t2 = ExpDigestFieldsLookupTable.lookup(keyName);
+        assert(t==t2);
 
         switch (t) {
         case DIGEST_USERNAME:
diff --git a/src/base/LookupTable.h b/src/base/LookupTable.h
new file mode 100644 (file)
index 0000000..88f9d66
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef SQUID_LOOKUPTABLE_H_
+#define SQUID_LOOKUPTABLE_H_
+
+#include "SBuf.h"
+
+#include <map>
+
+/**
+ * SBuf -> enum lookup table.
+ */
+template<class EnumType>
+class LookupTable
+{
+public:
+    typedef struct {
+        const char *name;
+        EnumType id;
+    } Record;
+
+    LookupTable(const EnumType theInvalid, const Record data[]) :
+        invalidValue(theInvalid)
+    {
+        for (auto i = 0; data[i].name != nullptr; ++i) {
+            lookupTable[SBuf(data[i].name)] = data[i].id;
+        }
+    }
+    EnumType lookup(const SBuf &key) const {
+        auto r = lookupTable.find(key);
+        if (r == lookupTable.end())
+            return invalidValue;
+        return r->second;
+    }
+
+private:
+    typedef std::map<const SBuf, EnumType> lookupTable_t;
+    lookupTable_t lookupTable;
+    EnumType invalidValue;
+};
+
+#endif /* SQUID_LOOKUPTABLE_H_ */
index 8643df4da23cdf11bcce6418506a397bbe44fe6d..f0a3a23fe35a1028de3996f40d6fbb3668eec673 100644 (file)
@@ -25,6 +25,7 @@ libbase_la_SOURCES = \
        CharacterSet.cc \
        InstanceId.h \
        Lock.h \
+       LookupTable.h \
        LruMap.h \
        Packable.h \
        RunnersRegistry.cc \