You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ga...@apache.org on 2017/10/20 16:40:12 UTC

[trafficserver] branch master updated: cachekey: ability to base the key on pristine URI.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3ad5bf5  cachekey: ability to base the key on pristine URI.
3ad5bf5 is described below

commit 3ad5bf5fcc5e99e7493befff59dd8a6e18df107c
Author: Gancho Tenev <ga...@apache.com>
AuthorDate: Thu Oct 19 13:31:39 2017 -0700

    cachekey: ability to base the key on pristine URI.
    
    Added ability to base the cache key on the pristine URI
    rather then on the URI set during remap.
    
    New option added (see plugin doc):
    --uri-type=[remap|pristine] (default:remap)
    
    In addition changed the CacheKey constructor to use in-class member initialization.
---
 doc/admin-guide/plugins/cachekey.en.rst  |  7 +++++++
 plugins/experimental/cachekey/configs.cc | 28 ++++++++++++++++++++++++++++
 plugins/experimental/cachekey/configs.h  | 24 ++++++++++++++++++++----
 plugins/experimental/cachekey/plugin.cc  | 26 +++++++++++++++++++++++++-
 4 files changed, 80 insertions(+), 5 deletions(-)

diff --git a/doc/admin-guide/plugins/cachekey.en.rst b/doc/admin-guide/plugins/cachekey.en.rst
index b36e6ee..bc126ca 100644
--- a/doc/admin-guide/plugins/cachekey.en.rst
+++ b/doc/admin-guide/plugins/cachekey.en.rst
@@ -39,6 +39,13 @@ This plugin allows some common cache key manipulations based on various HTTP req
 * capture and replace strings from the URI and include them in the cache key
 * do more - please find more examples below.
 
+URI type
+========
+
+The plugin manipulates the ``remap`` URI (value set during URI remap) by default. If manipulation needs to be based on the ``pristine`` URI (the value before URI remapping takes place) one could use the following option:
+
+* ``--uri-type=[remap|pristine]`` (default: ``remap``)
+
 Cache key structure and related plugin parameters
 =================================================
 
diff --git a/plugins/experimental/cachekey/configs.cc b/plugins/experimental/cachekey/configs.cc
index 788b56d..80f2e54 100644
--- a/plugins/experimental/cachekey/configs.cc
+++ b/plugins/experimental/cachekey/configs.cc
@@ -344,6 +344,7 @@ Configs::init(int argc, char *argv[])
     {const_cast<char *>("remove-prefix"), optional_argument, nullptr, 'q'},
     {const_cast<char *>("remove-path"), optional_argument, nullptr, 'r'},
     {const_cast<char *>("separator"), optional_argument, nullptr, 's'},
+    {const_cast<char *>("uri-type"), optional_argument, nullptr, 't'},
     {nullptr, 0, nullptr, 0},
   };
 
@@ -442,6 +443,9 @@ Configs::init(int argc, char *argv[])
     case 's': /* separator */
       setSeparator(optarg);
       break;
+    case 't': /* uri-type */
+      setUriType(optarg);
+      break;
     }
   }
 
@@ -486,3 +490,27 @@ Configs::getSeparator()
 {
   return _separator;
 }
+
+void
+Configs::setUriType(const char *arg)
+{
+  if (nullptr != arg) {
+    if (5 == strlen(arg) && 0 == strncasecmp(arg, "remap", 5)) {
+      _uriType = CacheKeyUriType::REMAP;
+      CacheKeyDebug("using remap URI type");
+    } else if (8 == strlen(arg) && 0 == strncasecmp(arg, "pristine", 8)) {
+      _uriType = CacheKeyUriType::PRISTINE;
+      CacheKeyDebug("using pristine URI type");
+    } else {
+      CacheKeyError("unrecognized URI type '%s', using default 'remap'", arg);
+    }
+  } else {
+    CacheKeyError("found an empty URI type, using default 'remap'");
+  }
+}
+
+CacheKeyUriType
+Configs::getUriType()
+{
+  return _uriType;
+}
diff --git a/plugins/experimental/cachekey/configs.h b/plugins/experimental/cachekey/configs.h
index 9bf71cb..9ae29f9 100644
--- a/plugins/experimental/cachekey/configs.h
+++ b/plugins/experimental/cachekey/configs.h
@@ -27,6 +27,11 @@
 #include "pattern.h"
 #include "common.h"
 
+enum CacheKeyUriType {
+  REMAP,
+  PRISTINE,
+};
+
 /**
  * @brief Plug-in configuration elements (query / headers / cookies).
  *
@@ -122,7 +127,7 @@ private:
 class Configs
 {
 public:
-  Configs() : _prefixToBeRemoved(false), _pathToBeRemoved(false), _separator("/") {}
+  Configs() {}
   /**
    * @brief initializes plugin configuration.
    * @param argc number of plugin parameters
@@ -157,6 +162,16 @@ public:
    */
   const String &getSeparator();
 
+  /**
+   * @brief sets the URI Type.
+   */
+  void setUriType(const char *arg);
+
+  /**
+   * @brief get URI type.
+   */
+  CacheKeyUriType getUriType();
+
   /* Make the following members public to avoid unnecessary accessors */
   ConfigQuery _query;        /**< @brief query parameter related configuration */
   ConfigHeaders _headers;    /**< @brief headers related configuration */
@@ -178,9 +193,10 @@ private:
    */
   bool loadClassifiers(const String &args, bool blacklist = true);
 
-  bool _prefixToBeRemoved; /**< @brief instructs the prefix (i.e. host:port) not to added to the cache key */
-  bool _pathToBeRemoved;   /**< @brief instructs the path not to added to the cache key */
-  String _separator;       /**< @brief a separator used to separate the cache key elements extracted from the URI */
+  bool _prefixToBeRemoved  = false; /**< @brief instructs the prefix (i.e. host:port) not to added to the cache key */
+  bool _pathToBeRemoved    = false; /**< @brief instructs the path not to added to the cache 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 */
 };
 
 #endif // PLUGINS_EXPERIMENTAL_CACHEKEY_CONFIGS_H_
diff --git a/plugins/experimental/cachekey/plugin.cc b/plugins/experimental/cachekey/plugin.cc
index d413bd3..c86e65b 100644
--- a/plugins/experimental/cachekey/plugin.cc
+++ b/plugins/experimental/cachekey/plugin.cc
@@ -91,8 +91,26 @@ TSRemapDoRemap(void *instance, TSHttpTxn txn, TSRemapRequestInfo *rri)
   Configs *config = (Configs *)instance;
 
   if (nullptr != config) {
+    TSMBuffer bufp;
+    TSMLoc urlLoc;
+    TSMLoc hdrLoc;
+
+    /* Get the URI and header to base the cachekey on.
+     * @TODO it might make sense to add more supported URI types */
+    if (PRISTINE == config->getUriType()) {
+      if (TS_SUCCESS != TSHttpTxnPristineUrlGet(txn, &bufp, &urlLoc)) {
+        /* Failing here is unlikely. No action seems the only reasonable thing to do from within this plug-in */
+        CacheKeyError("failed to get pristine URI handle");
+        return TSREMAP_NO_REMAP;
+      }
+    } else {
+      bufp   = rri->requestBufp;
+      urlLoc = rri->requestUrl;
+    }
+    hdrLoc = rri->requestHdrp;
+
     /* Initial cache key facility from the requested URL. */
-    CacheKey cachekey(txn, rri->requestBufp, rri->requestUrl, rri->requestHdrp, config->getSeparator());
+    CacheKey cachekey(txn, bufp, urlLoc, hdrLoc, config->getSeparator());
 
     /* Append custom prefix or the host:port */
     if (!config->prefixToBeRemoved()) {
@@ -126,6 +144,12 @@ TSRemapDoRemap(void *instance, TSHttpTxn txn, TSRemapRequestInfo *rri)
       CacheKeyError("failed to set cache key for url %.*s", len, url);
       TSfree(url);
     }
+
+    if (PRISTINE == config->getUriType()) {
+      if (TS_SUCCESS != TSHandleMLocRelease(bufp, TS_NULL_MLOC, urlLoc)) {
+        CacheKeyError("failed to release pristine URI handle");
+      }
+    }
   }
 
   return TSREMAP_NO_REMAP;

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].