]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Migrate to generic LookupTable, document it and clean it up
authorFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 27 Jul 2015 14:53:51 +0000 (16:53 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 27 Jul 2015 14:53:51 +0000 (16:53 +0200)
src/auth/digest/Config.cc
src/base/LookupTable.h

index 3a5f1fc5d4b90bd37a5732b2c6ad05e351846ffc..5e7c053a2b29c4579268c1556c2e9f4c74d36214 100644 (file)
@@ -35,8 +35,6 @@
 #include "StrList.h"
 #include "wordlist.h"
 
-#include <map>
-
 /* digest_nonce_h still uses explicit alloc()/freeOne() MemPool calls.
  * XXX: convert to MEMPROXY_CLASS() API
  */
@@ -63,54 +61,8 @@ enum http_digest_attr_type {
     DIGEST_NC,
     DIGEST_CNONCE,
     DIGEST_RESPONSE,
-    DIGEST_ENUM_END
-};
-
-struct HttpDigestFieldAttrs {
-    const char *name;
-    http_digest_attr_type id;
-};
-
-static const HttpDigestFieldAttrs DigestAttrs[] = {
-    {"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}
-};
-
-/* a SBuf -> http_digest_attr_type lookup table.
- * Implementaiton can be improved by using an unordered_map with custom hasher
- * but the focus here is API correctness.
- */
-class DigestFieldsLookupTable_t {
-public:
-    DigestFieldsLookupTable_t();
-    http_digest_attr_type lookup(const SBuf &key) const;
-private:
-    /* could be unordered_map but let's skip the requirement to hash for now */
-    typedef std::map<const SBuf, http_digest_attr_type> lookupTable_t;
-    lookupTable_t lookupTable;
+    DIGEST_INVALID_ATTR
 };
-DigestFieldsLookupTable_t::DigestFieldsLookupTable_t() {
-    for (auto i : DigestAttrs) {
-        lookupTable[SBuf(i.name)] = i.id;
-    }
-}
-inline http_digest_attr_type
-DigestFieldsLookupTable_t::lookup(const SBuf &key) const
-{
-    auto r = lookupTable.find(key);
-    if (r == lookupTable.end())
-        return DIGEST_ENUM_END; // original returns HDR_BAD_HDR(-1)
-    return r->second;
-}
-DigestFieldsLookupTable_t DigestFieldsLookupTable;
-
 
 static const LookupTable<http_digest_attr_type>::Record
   DigestAttrs_Exp[] = {
@@ -123,11 +75,11 @@ static const LookupTable<http_digest_attr_type>::Record
     {"nc", DIGEST_NC},
     {"cnonce", DIGEST_CNONCE},
     {"response", DIGEST_RESPONSE},
-    {nullptr, DIGEST_ENUM_END}
+    {nullptr, DIGEST_INVALID_ATTR}
 };
 
 LookupTable<http_digest_attr_type>
-ExpDigestFieldsLookupTable(DIGEST_ENUM_END, DigestAttrs_Exp);
+ExpDigestFieldsLookupTable(DIGEST_INVALID_ATTR, DigestAttrs_Exp);
 
 /*
  *
@@ -860,9 +812,7 @@ 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);
+        const http_digest_attr_type t = ExpDigestFieldsLookupTable.lookup(keyName);
 
         switch (t) {
         case DIGEST_USERNAME:
index 88f9d667693239745a7a75f43b13b8873070939d..8c5c608ca24709c79325417b87873db6b6777dc7 100644 (file)
@@ -6,12 +6,26 @@
 #include <map>
 
 /**
- * SBuf -> enum lookup table.
+ * SBuf -> enum lookup table
+ *
+ * How to use:
+ * enum enum_type { ... };
+ * static const LookupTable<enum_type>::Record initializerTable[] {
+ *   {"key1", ENUM_1}, {"key2", ENUM_2}, ... {nullptr, ENUM_INVALID_VALUE}
+ * };
+ * LookupTable<enum_type> lookupTableInstance(ENUM_INVALID_VALUE, initializerTable);
+ *
+ * then in the code:
+ * SBuf s(string_to_lookup);
+ * enum_type item = lookupTableInstance.lookup(s);
+ * if (item != ENUM_INVALID_VALUE) { // do stuff }
+ *
  */
-template<class EnumType>
+template<typename EnumType>
 class LookupTable
 {
 public:
+    /// element of the lookup table initialization list
     typedef struct {
         const char *name;
         EnumType id;