From 21d457cfb8b9c04c4745b3822ec472de7f5c52f6 Mon Sep 17 00:00:00 2001 From: Petar Penkov Date: Wed, 10 Aug 2016 15:07:32 +0000 Subject: [PATCH] TS-110: Added @host parameter to regex_remap to allow matching on host field. --- doc/admin-guide/plugins/regex_remap.en.rst | 12 +++++++++++- plugins/regex_remap/README | 11 ++++++++++- plugins/regex_remap/regex_remap.cc | 18 +++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/doc/admin-guide/plugins/regex_remap.en.rst b/doc/admin-guide/plugins/regex_remap.en.rst index 6f2ce8b6c6f..2d84fa79460 100644 --- a/doc/admin-guide/plugins/regex_remap.en.rst +++ b/doc/admin-guide/plugins/regex_remap.en.rst @@ -61,6 +61,7 @@ be used to modify the plugin instance behavior :: @pparam=[no-]method [default: off] @pparam=[no-]query-string [default: on] @pparam=[no-]matrix-parameters [default: off] + @pparam=[no-]host [default: off] If you wish to match on the HTTP method used (e.g. "``GET``\ "), you must use the option ``@pparam=method``. e.g. :: @@ -81,11 +82,20 @@ again, to turn this off use the option 'no-query-string', e.g. :: ... @pparam=maps.reg @pparam=no-query-string -Finally, you can also include the matrix parameters in the string, using +You can also include the matrix parameters in the string, using the option 'matrix-parameters', e.g. :: ... @pparam=maps.reg @pparam=matrix-parameters +Finally, to match on the host as well, use the option 'host', e.g. :: + + ... @pparam=maps.reg @pparam=host + +With this enabled, the string that you will need to match will look like + + //host/path?query=bar + + A typical regex would look like :: ^/(ogre.*)/more http://www.ogre.com/$h/$0/$1 diff --git a/plugins/regex_remap/README b/plugins/regex_remap/README index 9b5b7a97bcb..f7f353c5ebe 100644 --- a/plugins/regex_remap/README +++ b/plugins/regex_remap/README @@ -29,6 +29,7 @@ parameters can be used to modify the plugin instance behavior: @pparam=[no-]method [default: off] @pparam=[no-]query-string [default: on] @pparam=[no-]matrix-parameters [default: off] + @pparam=[no-]host [default: off] @@ -53,11 +54,19 @@ to turn this off use the option 'no-query-string', e.g. ... @pparam=maps.reg @pparam=no-query-string -Finally, you can also include the matrix parameters in the string, using the +You can also include the matrix parameters in the string, using the option 'matrix-parameters', e.g. ... @pparam=maps.reg @pparam=matrix-parameters +Finally, to match on the host as well, use the option 'host', e.g. :: + + ... @pparam=maps.reg @pparam=host + +With this enabled, the string that you will need to match will look like + + //host/path?query=bar + Note that the path to the plugin must be absolute, and by default it is diff --git a/plugins/regex_remap/regex_remap.cc b/plugins/regex_remap/regex_remap.cc index 355420a26e5..7e4cbd2d04b 100644 --- a/plugins/regex_remap/regex_remap.cc +++ b/plugins/regex_remap/regex_remap.cc @@ -664,6 +664,7 @@ struct RemapInstance { method(false), query_string(true), matrix_params(false), + host(false), hits(0), misses(0), filename("unknown") @@ -676,6 +677,7 @@ struct RemapInstance { bool method; bool query_string; bool matrix_params; + bool host; int hits; int misses; std::string filename; @@ -759,15 +761,19 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSE } else if (strncmp(argv[i], "method", 6) == 0) { ri->method = true; } else if (strncmp(argv[i], "no-method", 9) == 0) { - ri->method = true; + ri->method = false; } else if (strncmp(argv[i], "query-string", 12) == 0) { ri->query_string = true; } else if (strncmp(argv[i], "no-query-string", 15) == 0) { ri->query_string = false; - } else if (strncmp(argv[i], "matrix-parameters", 15) == 0) { + } else if (strncmp(argv[i], "matrix-parameters", 17) == 0) { ri->matrix_params = true; - } else if (strncmp(argv[i], "no-matrix-parameters", 18) == 0) { + } else if (strncmp(argv[i], "no-matrix-parameters", 20) == 0) { ri->matrix_params = false; + } else if (strncmp(argv[i], "host", 4) == 0) { + ri->host = true; + } else if (strncmp(argv[i], "no-host", 7) == 0) { + ri->host = false; } else { TSError("[%s] invalid option '%s'", PLUGIN_NAME, argv[i]); } @@ -975,6 +981,12 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri) } } + if (ri->host && req_url.host && req_url.host_len > 0) { + memcpy(match_buf + match_len, "//", 2); + memcpy(match_buf + match_len + 2, req_url.host, req_url.host_len); + match_len += (req_url.host_len + 2); + } + *(match_buf + match_len) = '/'; match_len++; if (req_url.path && req_url.path_len > 0) {