From: Francesco Chemolli Date: Mon, 27 Jul 2015 09:30:32 +0000 (+0200) Subject: Implement base/LookupTable as proposed by Amos X-Git-Tag: merge-candidate-3-v1~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a0655385a30fea80cf17b5cfe17a3f55246b82a8;p=thirdparty%2Fsquid.git Implement base/LookupTable as proposed by Amos --- diff --git a/src/auth/digest/Config.cc b/src/auth/digest/Config.cc index f68a72296c..3a5f1fc5d4 100644 --- a/src/auth/digest/Config.cc +++ b/src/auth/digest/Config.cc @@ -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::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 +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 index 0000000000..88f9d66769 --- /dev/null +++ b/src/base/LookupTable.h @@ -0,0 +1,40 @@ +#ifndef SQUID_LOOKUPTABLE_H_ +#define SQUID_LOOKUPTABLE_H_ + +#include "SBuf.h" + +#include + +/** + * SBuf -> enum lookup table. + */ +template +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 lookupTable_t; + lookupTable_t lookupTable; + EnumType invalidValue; +}; + +#endif /* SQUID_LOOKUPTABLE_H_ */ diff --git a/src/base/Makefile.am b/src/base/Makefile.am index 8643df4da2..f0a3a23fe3 100644 --- a/src/base/Makefile.am +++ b/src/base/Makefile.am @@ -25,6 +25,7 @@ libbase_la_SOURCES = \ CharacterSet.cc \ InstanceId.h \ Lock.h \ + LookupTable.h \ LruMap.h \ Packable.h \ RunnersRegistry.cc \