]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/HttpHdrCc.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / HttpHdrCc.cc
index 8853c21865dc1c218e20d42e577900c7f06795ed..b95b133e615c330e5c1ab6783d286763df17c4c3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
@@ -15,7 +15,7 @@
 #include "HttpHeaderFieldStat.h"
 #include "HttpHeaderStat.h"
 #include "HttpHeaderTools.h"
-#include "SBuf.h"
+#include "sbuf/SBuf.h"
 #include "StatHist.h"
 #include "Store.h"
 #include "StrList.h"
@@ -40,6 +40,7 @@ static LookupTable<HttpHdrCcType>::Record CcAttrs[] = {
     {"min-fresh", HttpHdrCcType::CC_MIN_FRESH},
     {"only-if-cached", HttpHdrCcType::CC_ONLY_IF_CACHED},
     {"stale-if-error", HttpHdrCcType::CC_STALE_IF_ERROR},
+    {"immutable", HttpHdrCcType::CC_IMMUTABLE},
     {"Other,", HttpHdrCcType::CC_OTHER}, /* ',' will protect from matches */
     {nullptr, HttpHdrCcType::CC_ENUM_END}
 };
@@ -47,7 +48,8 @@ LookupTable<HttpHdrCcType> ccLookupTable(HttpHdrCcType::CC_OTHER,CcAttrs);
 std::vector<HttpHeaderFieldStat> ccHeaderStats(HttpHdrCcType::CC_ENUM_END);
 
 /// used to walk a table of http_header_cc_type structs
-HttpHdrCcType &operator++ (HttpHdrCcType &aHeader)
+static HttpHdrCcType &
+operator++ (HttpHdrCcType &aHeader)
 {
     int tmp = (int)aHeader;
     aHeader = (HttpHdrCcType)(++tmp);
@@ -60,7 +62,7 @@ httpHdrCcInitModule(void)
 {
     // check invariant on initialization table
     for (unsigned int j = 0; CcAttrs[j].name != nullptr; ++j) {
-        assert (static_cast<int>(CcAttrs[j].id) == j);
+        assert(static_cast<decltype(j)>(CcAttrs[j].id) == j);
     }
 }
 
@@ -70,12 +72,31 @@ HttpHdrCc::clear()
     *this=HttpHdrCc();
 }
 
+/// set a data member to a new value, and set the corresponding mask-bit.
+/// if setting is false, then the mask-bit is cleared.
+void
+HttpHdrCc::setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting)
+{
+    if (setting) {
+        if (new_value < 0) {
+            debugs(65, 3, "rejecting negative-value Cache-Control directive " << hdr
+                   << " value " << new_value);
+            return;
+        }
+    } else {
+        new_value = -1; //rely on the convention that "unknown" is -1
+    }
+
+    value = new_value;
+    setMask(hdr,setting);
+}
+
 bool
 HttpHdrCc::parse(const String & str)
 {
     const char *item;
     const char *p;      /* '=' parameter */
-    const char *pos = NULL;
+    const char *pos = nullptr;
     int ilen;
     int nlen;
 
@@ -202,6 +223,9 @@ HttpHdrCc::parse(const String & str)
         case HttpHdrCcType::CC_ONLY_IF_CACHED:
             onlyIfCached(true);
             break;
+        case HttpHdrCcType::CC_IMMUTABLE:
+            Immutable(true);
+            break;
 
         case HttpHdrCcType::CC_OTHER:
             if (other.size())
@@ -234,27 +258,54 @@ HttpHdrCc::packInto(Packable * p) const
         if (isSet(flag) && flag != HttpHdrCcType::CC_OTHER) {
 
             /* print option name for all options */
-            p->appendf((pcount ? ", %s": "%s") , CcAttrs[flag].name);
+            p->appendf((pcount ? ", %s": "%s"), CcAttrs[flag].name);
 
             /* for all options having values, "=value" after the name */
             switch (flag) {
+            case HttpHdrCcType::CC_PUBLIC:
+                break;
+            case HttpHdrCcType::CC_PRIVATE:
+                if (private_.size())
+                    p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(private_));
+                break;
+
+            case HttpHdrCcType::CC_NO_CACHE:
+                if (no_cache.size())
+                    p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(no_cache));
+                break;
+            case HttpHdrCcType::CC_NO_STORE:
+                break;
+            case HttpHdrCcType::CC_NO_TRANSFORM:
+                break;
+            case HttpHdrCcType::CC_MUST_REVALIDATE:
+                break;
+            case HttpHdrCcType::CC_PROXY_REVALIDATE:
+                break;
             case HttpHdrCcType::CC_MAX_AGE:
-                p->appendf("=%d", maxAge());
+                p->appendf("=%d", max_age);
                 break;
             case HttpHdrCcType::CC_S_MAXAGE:
-                p->appendf("=%d", sMaxAge());
+                p->appendf("=%d", s_maxage);
                 break;
             case HttpHdrCcType::CC_MAX_STALE:
                 /* max-stale's value is optional.
                   If we didn't receive it, don't send it */
-                if (maxStale()!=MAX_STALE_ANY)
-                    p->appendf("=%d", maxStale());
+                if (max_stale != MAX_STALE_ANY)
+                    p->appendf("=%d", max_stale);
                 break;
             case HttpHdrCcType::CC_MIN_FRESH:
-                p->appendf("=%d", minFresh());
+                p->appendf("=%d", min_fresh);
+                break;
+            case HttpHdrCcType::CC_ONLY_IF_CACHED:
+                break;
+            case HttpHdrCcType::CC_STALE_IF_ERROR:
+                p->appendf("=%d", stale_if_error);
                 break;
-            default:
-                /* do nothing, directive was already printed */
+            case HttpHdrCcType::CC_IMMUTABLE:
+                break;
+            case HttpHdrCcType::CC_OTHER:
+            case HttpHdrCcType::CC_ENUM_END:
+                // done below after the loop
                 break;
             }
 
@@ -281,7 +332,7 @@ httpHdrCcStatDumper(StoreEntry * sentry, int, double val, double, int count)
 {
     extern const HttpHeaderStat *dump_stat; /* argh! */
     const int id = static_cast<int>(val);
-    const bool valid_id = id < HttpHdrCcType::CC_ENUM_END;
+    const bool valid_id = id >= 0 && id < static_cast<int>(HttpHdrCcType::CC_ENUM_END);
     const char *name = valid_id ? CcAttrs[id].name : "INVALID";
 
     if (count || valid_id)
@@ -300,7 +351,3 @@ operator<< (std::ostream &s, HttpHdrCcType c)
     return s;
 }
 
-#if !_USE_INLINE_
-#include "HttpHdrCc.cci"
-#endif
-