From: Francesco Chemolli Date: Thu, 23 Jul 2015 15:12:49 +0000 (+0200) Subject: Implement std::map-based auth/digest lookup table, part 1 of 2 (add code) X-Git-Tag: merge-candidate-3-v1~35 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a15e94ec565484c1dd81e2ad927339e8a264e7a9;p=thirdparty%2Fsquid.git Implement std::map-based auth/digest lookup table, part 1 of 2 (add code) --- diff --git a/src/auth/digest/Config.cc b/src/auth/digest/Config.cc index 49ca98201a..415acd1b12 100644 --- a/src/auth/digest/Config.cc +++ b/src/auth/digest/Config.cc @@ -34,6 +34,8 @@ #include "StrList.h" #include "wordlist.h" +#include + /* digest_nonce_h still uses explicit alloc()/freeOne() MemPool calls. * XXX: convert to MEMPROXY_CLASS() API */ @@ -78,6 +80,35 @@ static const HttpHeaderFieldAttrs DigestAttrs[DIGEST_ENUM_END] = { class HttpHeaderFieldInfo; static HttpHeaderFieldInfo *DigestFieldsInfo = NULL; +/* a SBuf -> http_digest_attr_type lookup table, without C typecasts. + * 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; +}; +DigestFieldsLookupTable_t::DigestFieldsLookupTable_t() { + for (int i = 0; i < DIGEST_ENUM_END; ++i) { + const SBuf s(DigestAttrs[i].name); + lookupTable[s] = static_cast(DigestAttrs[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; + /* * * Nonce Functions @@ -816,6 +847,8 @@ Auth::Digest::Config::decode(char const *proxy_auth, const char *aRequestRealm) /* find type */ http_digest_attr_type t = (http_digest_attr_type)httpHeaderIdByName(item, nlen, DigestFieldsInfo, DIGEST_ENUM_END); + http_digest_attr_type t2 = DigestFieldsLookupTable.lookup(keyName); + assert( t == t2 ); switch (t) { case DIGEST_USERNAME: