TS-2250: support inline configuration overrides in conf_remap

Add support to the conf_remap plugin for accepting configuration
overrides in the argument list. This makes it easier to override
only a few configuration variables (the common case), because a
separate configuration file is not required.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/70f8e10a
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/70f8e10a
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/70f8e10a

Branch: refs/heads/master
Commit: 70f8e10a17b3fc87f5e0bce444387ed0d98622bb
Parents: ad891bc
Author: James Peach <[email protected]>
Authored: Sun Feb 2 16:46:34 2014 -0800
Committer: James Peach <[email protected]>
Committed: Mon Feb 3 10:37:38 2014 -0800

----------------------------------------------------------------------
 CHANGES                                 |  2 +
 doc/reference/plugins/conf_remap.en.rst | 20 ++++++--
 plugins/conf_remap/conf_remap.cc        | 71 ++++++++++++++++++++++++----
 3 files changed, 81 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/70f8e10a/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index d11a535..be006e8 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache Traffic Server 4.2.0
 
+  *) [TS-2550] Add inline configuration overrised to the conf_remap plugin.
+
   *) [TS-2546] Move xptr and _xstrdup to ink_memory.{h,cc}.
 
   *) [TS-2180] Small memory leak in RecCore.cc, from previous refactoring.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/70f8e10a/doc/reference/plugins/conf_remap.en.rst
----------------------------------------------------------------------
diff --git a/doc/reference/plugins/conf_remap.en.rst 
b/doc/reference/plugins/conf_remap.en.rst
index b16dbd9..bcd974a 100644
--- a/doc/reference/plugins/conf_remap.en.rst
+++ b/doc/reference/plugins/conf_remap.en.rst
@@ -25,15 +25,27 @@ directives dependent on actual remapping rules. The plugin 
is built
 and installed as part of the normal Apache Traffic Server installation
 process.
 
-If you want to achieve this behaviour now, configure a remap rule
-like this::
+The `conf_remap` plugin accepts configuration directives in the
+arguments list or in a separate configuration file. In both cases,
+only string and integer directives are supported.
 
-    map http://cdn.example.com/ http://some-server.example.com 
@plugin=conf_remap.so @pparam=/etc/trafficserver/cdn.conf
+When using a separate configuration file, the standard
+:file:`records.config` syntax is used, for example::
 
-where `cdn.conf` would look like :file:`records.config`. For example::
+    map http://cdn.example.com/ http://some-server.example.com \
+      @plugin=conf_remap.so @pparam=/etc/trafficserver/cdn.conf
+
+where `cdn.conf` contains::
 
     CONFIG proxy.config.url_remap.pristine_host_hdr INT 1
 
+When using inline arguments, the `conf_remap` plugin accepts a
+``key=value`` syntax, where the ``KEY`` is the name of the configuration
+directive and ``VALUE`` is the desired value, for example::
+
+    map http://cdn.example.com/ http://some-server.example.com \
+      @plugin=conf_remap.so @pparam=proxy.config.url_remap.pristine_host_hdr=1
+
 Doing this, you will override your global default configuration on
 a per mapping rule. For more details on the APIs, functionality, and a
 complete list of all overridable configurations, see 
:ref:`ts-overridable-config`.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/70f8e10a/plugins/conf_remap/conf_remap.cc
----------------------------------------------------------------------
diff --git a/plugins/conf_remap/conf_remap.cc b/plugins/conf_remap/conf_remap.cc
index 55897db..ecdbe35 100644
--- a/plugins/conf_remap/conf_remap.cc
+++ b/plugins/conf_remap/conf_remap.cc
@@ -50,6 +50,7 @@ struct RemapConfigs
   };
 
   bool parse_file(const char *filename);
+  bool parse_inline(const char *arg);
 
   Item _items[MAX_OVERRIDABLE_CONFIGS];
   int _current;
@@ -73,6 +74,50 @@ str_to_datatype(const char* str)
   return type;
 }
 
+// Parse an inline key=value config pair.
+bool
+RemapConfigs::parse_inline(const char *arg)
+{
+  const char * sep;
+  std::string key;
+  std::string value;
+
+  TSOverridableConfigKey name;
+  TSRecordDataType type;
+
+  // Each token should be a status code then a URL, separated by '='.
+  sep = strchr(arg, '=');
+  if (sep == NULL) {
+    return false;
+  }
+
+  key = std::string(arg, std::distance(arg, sep));
+  value = std::string(sep + 1, std::distance(sep + 1, arg + strlen(arg)));
+
+  if (TSHttpTxnConfigFind(key.c_str(), -1 /* len */, &name, &type) != 
TS_SUCCESS) {
+    TSError("%s: invalid configuration variable '%s'", PLUGIN_NAME, 
key.c_str());
+    return false;
+  }
+
+  switch (type) {
+    case TS_RECORDDATATYPE_INT:
+      _items[_current]._data.rec_int = strtoll(value.c_str(), NULL, 10);
+      break;
+    case TS_RECORDDATATYPE_STRING:
+      _items[_current]._data.rec_string = TSstrdup(value.c_str());
+      _items[_current]._data_len = value.size();
+      break;
+    default:
+      TSError("%s: configuration variable '%s' is of an unsupported type", 
PLUGIN_NAME, key.c_str());
+      return false;
+  }
+
+  _items[_current]._name = name;
+  _items[_current]._type = type;
+  ++_current;
+  return true;
+}
+
 // Config file parser, somewhat borrowed from P_RecCore.i
 bool
 RemapConfigs::parse_file(const char* filename)
@@ -214,23 +259,33 @@ TSRemapInit(TSRemapInterface* api_info, char *errbuf, int 
errbuf_size)
 TSReturnCode
 TSRemapNewInstance(int argc, char* argv[], void** ih, char* /* errbuf 
ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */)
 {
+  RemapConfigs* conf = new(RemapConfigs);
+
   if (argc < 3) {
     TSError("Unable to create remap instance, need configuration file");
     return TS_ERROR;
-  } else {
-    RemapConfigs* conf = new(RemapConfigs);
+  }
 
-    for (int i=2; i < argc; ++i) {
-      if (conf->parse_file(argv[i])) {
-        *ih = static_cast<void*>(conf);
-      } else {
-        *ih = NULL;
-        delete conf;
+  for (int i = 2; i < argc; ++i) {
+    if (strchr(argv[i], '=') != NULL) {
+      // Parse as an inline key=value pair ...
+      if (!conf->parse_inline(argv[i])) {
+        goto fail;
+      }
+    } else {
+      // Parse as a config file ...
+      if (!conf->parse_file(argv[i])) {
+        goto fail;
       }
     }
   }
 
+  *ih = static_cast<void*>(conf);
   return TS_SUCCESS;
+
+fail:
+  delete conf;
+  return TS_ERROR;
 }
 
 void

Reply via email to