This is an automated email from the ASF dual-hosted git repository.

gancho pushed a commit to branch 8.1.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 0cc117aa4869cef92d4d0b2e06329489d55d58a1
Author: Gancho Tenev <gan...@apache.org>
AuthorDate: Tue Sep 17 10:06:50 2019 -0700

    cachekey: allow multiple values for `--key-type`
    
    Allow multiple target types to be specified for `--key-type` so the
    operator can apply the same modifications to both cache key and parent
    selection url at the same time without chaining cachekey plugin
    instances.
    
    Instead of:
    
        @plugin=cachekey.so \
            @pparam=--key-type=parent_selection_url \
            @pparam=--remove-all-params=true
        @plugin=cachekey.so \
            @pparam=--key-type=cache_key \
            @pparam=--remove-all-params=true
    
    to write:
    
        @plugin=cachekey.so \
            @pparam=--key-type=parent_selection_url,cache_key \
            @pparam=--remove-all-params=true
    
    (cherry picked from commit db8cd14acede7460a5996864c52e1b206695e405)
---
 doc/admin-guide/plugins/cachekey.en.rst | 25 ++++++++++++++---
 plugins/cachekey/configs.cc             | 29 +++++++++++++-------
 plugins/cachekey/configs.h              |  6 +++--
 plugins/cachekey/plugin.cc              | 48 ++++++++++++++++++---------------
 4 files changed, 70 insertions(+), 38 deletions(-)

diff --git a/doc/admin-guide/plugins/cachekey.en.rst 
b/doc/admin-guide/plugins/cachekey.en.rst
index 35a4968..6199c93 100644
--- a/doc/admin-guide/plugins/cachekey.en.rst
+++ b/doc/admin-guide/plugins/cachekey.en.rst
@@ -53,11 +53,11 @@ Key type
 
 The plugin manipulates the `cache key` by default. If `parent selection URL` 
manipulation is needed the following option can be used:
 
-* ``--key-type=[cache_key|parent_selection_url]`` (default: ``cache_key``)
+* ``--key-type=<list of target types>``  (default: ``cache_key``) - list of 
``cache_key`` or ``parent_selection_url``, if multiple ``--key-type`` options 
are specified then all values are combined together.
+
+An instance of this plugin can be used for applying manipulations to `cache 
key`, `parent selection URL` or both depending on the need. See `simultaneous 
cache key and parent selection URL manipulation`_
+for examples of how to apply the **same** set of manupulations to both targets 
with a single plugin instance or applying **diferent** sets of manipulations to 
each target using separate plugin instances.
 
-One instance of this plugin can used either for `cache key` or `parent 
selection URL` manupulation but never both.
-If `simultaneous cache key and parent selection URL manipulation`_ is needed 
two separate instances of the plugin
-have to be loaded for each key type.
 
 Cache key structure and related plugin parameters
 =================================================
@@ -664,3 +664,20 @@ For this purpose two separate instances are loaded for 
that remap rule:
 
 In the example above the first instance of the plugin sets the prefix to the 
parent selection URI and
 the second instance of the plugin sets the prefix to the cache key.
+
+The **same** string manipulations can be applied to both cache key and parent 
selection url more concisely without chaining cachekey plugin instances by 
specifying multiple target types `--key-type`.
+
+Instead of::
+
+    @plugin=cachekey.so \
+        @pparam=--key-type=parent_selection_url \
+        @pparam=--remove-all-params=true
+    @plugin=cachekey.so \
+        @pparam=--key-type=cache_key \
+        @pparam=--remove-all-params=true
+
+one could write::
+
+    @plugin=cachekey.so \
+        @pparam=--key-type=parent_selection_url,cache_key \
+        @pparam=--remove-all-params=true
diff --git a/plugins/cachekey/configs.cc b/plugins/cachekey/configs.cc
index 9321232..05d93ca 100644
--- a/plugins/cachekey/configs.cc
+++ b/plugins/cachekey/configs.cc
@@ -529,6 +529,10 @@ Configs::init(int argc, const char *argv[], bool 
perRemapConfig)
 bool
 Configs::finalize()
 {
+  if (_keyTypes.empty()) {
+    CacheKeyDebug("setting cache key");
+    _keyTypes = {CACHE_KEY};
+  }
   return _query.finalize() && _headers.finalize() && _cookies.finalize();
 }
 
@@ -586,14 +590,19 @@ void
 Configs::setKeyType(const char *arg)
 {
   if (nullptr != arg) {
-    if (9 == strlen(arg) && 0 == strncasecmp(arg, "cache_key", 9)) {
-      _keyType = CacheKeyKeyType::CACHE_KEY;
-      CacheKeyDebug("setting cache key");
-    } else if (20 == strlen(arg) && 0 == strncasecmp(arg, 
"parent_selection_url", 20)) {
-      _keyType = CacheKeyKeyType::PARENT_SELECTION_URL;
-      CacheKeyDebug("setting parent selection URL");
-    } else {
-      CacheKeyError("unrecognized key type '%s', using default 'cache_key'", 
arg);
+    StringVector types;
+    ::commaSeparateString<StringVector>(types, arg);
+
+    for (auto type : types) {
+      if (9 == type.length() && 0 == strncasecmp(type.c_str(), "cache_key", 
9)) {
+        _keyTypes.insert(CacheKeyKeyType::CACHE_KEY);
+        CacheKeyDebug("setting cache key");
+      } else if (20 == type.length() && 0 == strncasecmp(type.c_str(), 
"parent_selection_url", 20)) {
+        _keyTypes.insert(CacheKeyKeyType::PARENT_SELECTION_URL);
+        CacheKeyDebug("setting parent selection URL");
+      } else {
+        CacheKeyError("unrecognized key type '%s', using default 'cache_key'", 
arg);
+      }
     }
   } else {
     CacheKeyError("found an empty key type, using default 'cache_key'");
@@ -606,10 +615,10 @@ Configs::getUriType()
   return _uriType;
 }
 
-CacheKeyKeyType
+CacheKeyKeyTypeSet &
 Configs::getKeyType()
 {
-  return _keyType;
+  return _keyTypes;
 }
 
 const char *
diff --git a/plugins/cachekey/configs.h b/plugins/cachekey/configs.h
index e066961..454fb36 100644
--- a/plugins/cachekey/configs.h
+++ b/plugins/cachekey/configs.h
@@ -41,6 +41,8 @@ enum CacheKeyKeyType {
 const char *getCacheKeyUriTypeName(CacheKeyUriType type);
 const char *getCacheKeyKeyTypeName(CacheKeyKeyType type);
 
+typedef std::set<CacheKeyKeyType> CacheKeyKeyTypeSet;
+
 /**
  * @brief Plug-in configuration elements (query / headers / cookies).
  *
@@ -203,7 +205,7 @@ public:
   /**
    * @brief get target URI type.
    */
-  CacheKeyKeyType getKeyType();
+  CacheKeyKeyTypeSet &getKeyType();
 
   /* Make the following members public to avoid unnecessary accessors */
   ConfigQuery _query;        /**< @brief query parameter related configuration 
*/
@@ -231,5 +233,5 @@ private:
   bool _canonicalPrefix    = false; /**< @brief keep the URI scheme and 
authority element used as input to transforming into key */
   String _separator        = "/";   /**< @brief a separator used to separate 
the cache key elements extracted from the URI */
   CacheKeyUriType _uriType = REMAP; /**< @brief shows which URI the cache key 
will be based on */
-  CacheKeyKeyType _keyType = CACHE_KEY; /**< @brief target URI to be modified, 
cache key or paren selection */
+  CacheKeyKeyTypeSet _keyTypes;     /**< @brief target URI to be modified, 
cache key or paren selection */
 };
diff --git a/plugins/cachekey/plugin.cc b/plugins/cachekey/plugin.cc
index 4e20806..a05117f 100644
--- a/plugins/cachekey/plugin.cc
+++ b/plugins/cachekey/plugin.cc
@@ -38,34 +38,38 @@ Configs *globalConfig = nullptr;
 static void
 setCacheKey(TSHttpTxn txn, Configs *config, TSRemapRequestInfo *rri = nullptr)
 {
-  /* Initial cache key facility from the requested URL. */
-  CacheKey cachekey(txn, config->getSeparator(), config->getUriType(), 
config->getKeyType(), rri);
+  const CacheKeyKeyTypeSet &keyTypes = config->getKeyType();
 
-  /* Append custom prefix or the host:port */
-  if (!config->prefixToBeRemoved()) {
-    cachekey.appendPrefix(config->_prefix, config->_prefixCapture, 
config->_prefixCaptureUri, config->canonicalPrefix());
-  }
-  /* Classify User-Agent and append the class name to the cache key if 
matched. */
-  cachekey.appendUaClass(config->_classifier);
+  for (auto type : keyTypes) {
+    /* Initial cache key facility from the requested URL. */
+    CacheKey cachekey(txn, config->getSeparator(), config->getUriType(), type, 
rri);
 
-  /* Capture from User-Agent header. */
-  cachekey.appendUaCaptures(config->_uaCapture);
+    /* Append custom prefix or the host:port */
+    if (!config->prefixToBeRemoved()) {
+      cachekey.appendPrefix(config->_prefix, config->_prefixCapture, 
config->_prefixCaptureUri, config->canonicalPrefix());
+    }
+    /* Classify User-Agent and append the class name to the cache key if 
matched. */
+    cachekey.appendUaClass(config->_classifier);
 
-  /* Append headers to the cache key. */
-  cachekey.appendHeaders(config->_headers);
+    /* Capture from User-Agent header. */
+    cachekey.appendUaCaptures(config->_uaCapture);
 
-  /* Append cookies to the cache key. */
-  cachekey.appendCookies(config->_cookies);
+    /* Append headers to the cache key. */
+    cachekey.appendHeaders(config->_headers);
 
-  /* Append the path to the cache key. */
-  if (!config->pathToBeRemoved()) {
-    cachekey.appendPath(config->_pathCapture, config->_pathCaptureUri);
-  }
-  /* Append query parameters to the cache key. */
-  cachekey.appendQuery(config->_query);
+    /* Append cookies to the cache key. */
+    cachekey.appendCookies(config->_cookies);
 
-  /* Set the cache key */
-  cachekey.finalize();
+    /* Append the path to the cache key. */
+    if (!config->pathToBeRemoved()) {
+      cachekey.appendPath(config->_pathCapture, config->_pathCaptureUri);
+    }
+    /* Append query parameters to the cache key. */
+    cachekey.appendQuery(config->_query);
+
+    /* Set the cache key */
+    cachekey.finalize();
+  }
 }
 
 static int

Reply via email to