]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Reimplemented Surrogate-Control lookup as LookupTable, removed httpHeaderNameById
authorFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 2 Aug 2015 18:06:48 +0000 (20:06 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 2 Aug 2015 18:06:48 +0000 (20:06 +0200)
src/HttpHdrSc.cc
src/HttpHeader.cc
src/HttpHeaderTools.cc
src/HttpHeaderTools.h

index f3bbb1768fb35337fb465c69eb8f8e10627463a7..b8ea350165dcd3fe2385eb084bb619dab03e5c12 100644 (file)
@@ -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 <map>
+#include <vector>
 
 /* 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<http_hdr_sc_type>::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<http_hdr_sc_type> scLookupTable(SC_ENUM_END, ScAttrs);
+std::vector<HttpHeaderFieldStat> 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<http_hdr_sc_type>(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",
index 8f9fc2dd837e5a84f8db3f392155f7ac66baf5dd..bdf556cb0145aabc706019102f1c4af2c0c6002f 100644 (file)
@@ -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
 {
index efdaaf4800bd8277ed7ff8cf0487c013ab58b504..1083bdfb491278e32e1a6f971af000768432c977 100644 (file)
@@ -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;
index 1550d0fdcbaf7bfb262f945314b12792559c03f9..a12d9ca1bd210ad8002a960c75cf5c93f6bb9ee2 100644 (file)
@@ -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;