From ae22f65a39c2fb3d34f298f2da737c1d383ba476 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli Date: Mon, 27 Jul 2015 16:53:51 +0200 Subject: [PATCH] Migrate to generic LookupTable, document it and clean it up --- src/auth/digest/Config.cc | 58 +++------------------------------------ src/base/LookupTable.h | 18 ++++++++++-- 2 files changed, 20 insertions(+), 56 deletions(-) diff --git a/src/auth/digest/Config.cc b/src/auth/digest/Config.cc index 3a5f1fc5d4..5e7c053a2b 100644 --- a/src/auth/digest/Config.cc +++ b/src/auth/digest/Config.cc @@ -35,8 +35,6 @@ #include "StrList.h" #include "wordlist.h" -#include - /* 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 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::Record DigestAttrs_Exp[] = { @@ -123,11 +75,11 @@ static const LookupTable::Record {"nc", DIGEST_NC}, {"cnonce", DIGEST_CNONCE}, {"response", DIGEST_RESPONSE}, - {nullptr, DIGEST_ENUM_END} + {nullptr, DIGEST_INVALID_ATTR} }; LookupTable -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: diff --git a/src/base/LookupTable.h b/src/base/LookupTable.h index 88f9d66769..8c5c608ca2 100644 --- a/src/base/LookupTable.h +++ b/src/base/LookupTable.h @@ -6,12 +6,26 @@ #include /** - * SBuf -> enum lookup table. + * SBuf -> enum lookup table + * + * How to use: + * enum enum_type { ... }; + * static const LookupTable::Record initializerTable[] { + * {"key1", ENUM_1}, {"key2", ENUM_2}, ... {nullptr, ENUM_INVALID_VALUE} + * }; + * LookupTable 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 +template class LookupTable { public: + /// element of the lookup table initialization list typedef struct { const char *name; EnumType id; -- 2.47.3