Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions doc/admin-guide/plugins/cachekey.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=================================================
Expand Down Expand Up @@ -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
29 changes: 19 additions & 10 deletions plugins/cachekey/configs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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'");
Expand All @@ -606,10 +615,10 @@ Configs::getUriType()
return _uriType;
}

CacheKeyKeyType
CacheKeyKeyTypeSet &
Configs::getKeyType()
{
return _keyType;
return _keyTypes;
}

const char *
Expand Down
6 changes: 4 additions & 2 deletions plugins/cachekey/configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down Expand Up @@ -203,7 +205,7 @@ class Configs
/**
* @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 */
Expand Down Expand Up @@ -231,5 +233,5 @@ class Configs
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 */
};
48 changes: 26 additions & 22 deletions plugins/cachekey/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down