From: Francesco Chemolli Date: Sun, 2 Aug 2015 18:06:48 +0000 (+0200) Subject: Reimplemented Surrogate-Control lookup as LookupTable, removed httpHeaderNameById X-Git-Tag: merge-candidate-3-v1~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4277583a22134d6d7732870e556d51f72fbc7651;p=thirdparty%2Fsquid.git Reimplemented Surrogate-Control lookup as LookupTable, removed httpHeaderNameById --- diff --git a/src/HttpHdrSc.cc b/src/HttpHdrSc.cc index f3bbb1768f..b8ea350165 100644 --- a/src/HttpHdrSc.cc +++ b/src/HttpHdrSc.cc @@ -9,9 +9,9 @@ /* DEBUG: section 90 HTTP Cache Control Header */ #include "squid.h" +#include "base/LookupTable.h" #include "HttpHdrSc.h" #include "HttpHeader.h" -#include "HttpHeaderFieldInfo.h" #include "HttpHeaderFieldStat.h" #include "HttpHeaderStat.h" #include "HttpHeaderTools.h" @@ -20,6 +20,7 @@ #include "util.h" #include +#include /* a row in the table used for parsing surrogate-control header and statistics */ typedef struct { @@ -31,15 +32,16 @@ typedef struct { /* this table is used for parsing surrogate control header */ /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */ //todo: implement constraint -static const HttpHeaderFieldAttrs ScAttrs[SC_ENUM_END] = { - HttpHeaderFieldAttrs("no-store", (http_hdr_type)SC_NO_STORE), - HttpHeaderFieldAttrs("no-store-remote", (http_hdr_type)SC_NO_STORE_REMOTE), - HttpHeaderFieldAttrs("max-age", (http_hdr_type)SC_MAX_AGE), - HttpHeaderFieldAttrs("content", (http_hdr_type)SC_CONTENT), - HttpHeaderFieldAttrs("Other,", (http_hdr_type)SC_OTHER) /* ',' will protect from matches */ +static const LookupTable::Record ScAttrs[] { + {"no-store", SC_NO_STORE}, + {"no-store-remote", SC_NO_STORE_REMOTE}, + {"max-age", SC_MAX_AGE}, + {"content", SC_CONTENT}, + {"Other,", SC_OTHER}, /* ',' will protect from matches */ + {nullptr, SC_ENUM_END} /* SC_ENUM_END taken as invalid value */ }; - -HttpHeaderFieldInfo *ScFieldsInfo = NULL; +LookupTable scLookupTable(SC_ENUM_END, ScAttrs); +std::vector scHeaderStats(SC_ENUM_END); http_hdr_sc_type &operator++ (http_hdr_sc_type &aHeader) { @@ -58,14 +60,14 @@ int operator - (http_hdr_sc_type const &anSc, http_hdr_sc_type const &anSc2) void httpHdrScInitModule(void) { - ScFieldsInfo = httpHeaderBuildFieldsInfo(ScAttrs, SC_ENUM_END); + // check invariant on ScAttrs + for (int i = 0; ScAttrs[i].name != nullptr; ++i) + assert(i == ScAttrs[i].id); } void httpHdrScCleanModule(void) { - httpHeaderDestroyFieldsInfo(ScFieldsInfo, SC_ENUM_END); - ScFieldsInfo = NULL; } /* implementation */ @@ -94,7 +96,7 @@ HttpHdrSc::parse(const String * str) const char *pos = NULL; const char *target = NULL; /* ;foo */ const char *temp = NULL; /* temp buffer */ - int type; + http_hdr_sc_type type; int ilen, vlen; int initiallen; HttpHdrScTarget *sct; @@ -120,11 +122,9 @@ HttpHdrSc::parse(const String * str) } /* find type */ - /* TODO: use a type-safe map-based lookup */ - type = httpHeaderIdByName(item, ilen, - ScFieldsInfo, SC_ENUM_END); + type = scLookupTable.lookup(SBuf(item,ilen)); - if (type < 0) { + if (type == SC_ENUM_END) { debugs(90, 2, "hdr sc: unknown control-directive: near '" << item << "' in '" << str << "'"); type = SC_OTHER; } @@ -147,11 +147,11 @@ HttpHdrSc::parse(const String * str) safe_free (temp); - if (sct->isSet(static_cast(type))) { + if (sct->isSet(type)) { if (type != SC_OTHER) debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item << "' in '" << str << "'"); - ++ ScFieldsInfo[type].stat.repCount; + ++ scHeaderStats[type].repCount; continue; } @@ -245,8 +245,7 @@ HttpHdrScTarget::packInto(Packable * p) const if (isSet(flag) && flag != SC_OTHER) { /* print option name */ - p->appendf((pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH), - SQUIDSTRINGPRINT(ScFieldsInfo[flag].name)); + p->appendf((pcount ? ", %s" : "%s"), ScAttrs[flag].name); /* handle options with values */ @@ -307,8 +306,8 @@ httpHdrScTargetStatDumper(StoreEntry * sentry, int, double val, double, int coun { extern const HttpHeaderStat *dump_stat; /* argh! */ const int id = (int) val; - const int valid_id = id >= 0 && id < SC_ENUM_END; - const char *name = valid_id ? ScFieldsInfo[id].name.termedBuf() : "INVALID"; + const bool valid_id = id >= 0 && id < SC_ENUM_END; + const char *name = valid_id ? ScAttrs[id].name : "INVALID"; if (count || valid_id) storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n", @@ -320,8 +319,8 @@ httpHdrScStatDumper(StoreEntry * sentry, int, double val, double, int count) { extern const HttpHeaderStat *dump_stat; /* argh! */ const int id = (int) val; - const int valid_id = id >= 0 && id < SC_ENUM_END; - const char *name = valid_id ? ScFieldsInfo[id].name.termedBuf() : "INVALID"; + const bool valid_id = id >= 0 && id < SC_ENUM_END; + const char *name = valid_id ? ScAttrs[id].name : "INVALID"; if (count || valid_id) storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n", diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 8f9fc2dd83..bdf556cb01 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -1755,13 +1755,6 @@ httpHeaderIdByNameDef(const char *name, int name_len) return headerLookupTable.lookup(SBuf(name,name_len)); } -const char * -httpHeaderNameById(int id) -{ - assert(id >= 0 && id < HDR_ENUM_END); - return headerTable[id].name; -} - int HttpHeader::hasListMember(http_hdr_type id, const char *member, const char separator) const { diff --git a/src/HttpHeaderTools.cc b/src/HttpHeaderTools.cc index efdaaf4800..1083bdfb49 100644 --- a/src/HttpHeaderTools.cc +++ b/src/HttpHeaderTools.cc @@ -18,6 +18,7 @@ #include "ConfigParser.h" #include "fde.h" #include "globals.h" +#include "http/RegisteredHeaders.h" #include "HttpHdrContRange.h" #include "HttpHeader.h" #include "HttpHeaderFieldInfo.h" @@ -424,9 +425,8 @@ HeaderManglers::~HeaderManglers() void HeaderManglers::dumpAccess(StoreEntry * entry, const char *name) const { - for (int i = 0; i < HDR_ENUM_END; ++i) { - header_mangler_dump_access(entry, name, known[i], - httpHeaderNameById(i)); + for (int i = 0; headerTable[i].name != nullptr; ++i) { + header_mangler_dump_access(entry, name, known[i], headerTable[i].name); } typedef ManglersByName::const_iterator MBNCI; @@ -439,9 +439,8 @@ HeaderManglers::dumpAccess(StoreEntry * entry, const char *name) const void HeaderManglers::dumpReplacement(StoreEntry * entry, const char *name) const { - for (int i = 0; i < HDR_ENUM_END; ++i) { - header_mangler_dump_replacement(entry, name, known[i], - httpHeaderNameById(i)); + for (int i = 0; headerTable[i].name != nullptr; ++i) { + header_mangler_dump_replacement(entry, name, known[i],headerTable[i].name); } typedef ManglersByName::const_iterator MBNCI; diff --git a/src/HttpHeaderTools.h b/src/HttpHeaderTools.h index 1550d0fdcb..a12d9ca1bd 100644 --- a/src/HttpHeaderTools.h +++ b/src/HttpHeaderTools.h @@ -120,7 +120,6 @@ void httpHeaderDestroyFieldsInfo(HttpHeaderFieldInfo * info, int count); http_hdr_type httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * attrs, int end); http_hdr_type httpHeaderIdByNameDef(const SBuf &name); http_hdr_type httpHeaderIdByNameDef(const char *name, int name_len); -const char *httpHeaderNameById(int id); int httpHeaderHasConnDir(const HttpHeader * hdr, const char *directive); int httpHeaderParseInt(const char *start, int *val); void httpHeaderPutStrf(HttpHeader * hdr, http_hdr_type id, const char *fmt,...) PRINTF_FORMAT_ARG3;