Repository: trafficserver
Updated Branches:
  refs/heads/master 135f88e11 -> 692437fad


TS-4290 Refactors statement, and overall cleanup

This closes #545


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

Branch: refs/heads/master
Commit: 692437fad5a25e72a89b652481c8b179d7686597
Parents: 135f88e
Author: Leif Hedstrom <[email protected]>
Authored: Mon Mar 28 16:41:27 2016 -0600
Committer: Leif Hedstrom <[email protected]>
Committed: Mon Apr 4 15:42:34 2016 -0600

----------------------------------------------------------------------
 plugins/header_rewrite/conditions.cc | 79 +++++++++++++++++++++++---
 plugins/header_rewrite/conditions.h  |  9 +--
 plugins/header_rewrite/statement.cc  | 93 ++++---------------------------
 plugins/header_rewrite/statement.h   |  3 -
 4 files changed, 87 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/692437fa/plugins/header_rewrite/conditions.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/conditions.cc 
b/plugins/header_rewrite/conditions.cc
index 63800a6..e197183 100644
--- a/plugins/header_rewrite/conditions.cc
+++ b/plugins/header_rewrite/conditions.cc
@@ -669,6 +669,51 @@ ConditionTransactCount::append_value(std::string &s, 
Resources const &res)
 
 
 // ConditionNow: time related conditions, such as time since epoch (default), 
hour, day etc.
+// Time related functionality for statements. We return an int64_t here, to 
assure that
+// gettimeofday() / Epoch does not lose bits.
+int64_t
+ConditionNow::get_now_qualified(NowQualifiers qual) const
+{
+  time_t now;
+
+  // First short circuit for the Epoch qualifier, since it needs less data
+  time(&now);
+  if (NOW_QUAL_EPOCH == qual) {
+    return static_cast<int64_t>(now);
+  } else {
+    struct tm res;
+
+    localtime_r(&now, &res);
+    switch (qual) {
+    case NOW_QUAL_YEAR:
+      return static_cast<int64_t>(res.tm_year + 1900); // This makes more sense
+      break;
+    case NOW_QUAL_MONTH:
+      return static_cast<int64_t>(res.tm_mon);
+      break;
+    case NOW_QUAL_DAY:
+      return static_cast<int64_t>(res.tm_mday);
+      break;
+    case NOW_QUAL_HOUR:
+      return static_cast<int64_t>(res.tm_hour);
+      break;
+    case NOW_QUAL_MINUTE:
+      return static_cast<int64_t>(res.tm_min);
+      break;
+    case NOW_QUAL_WEEKDAY:
+      return static_cast<int64_t>(res.tm_wday);
+      break;
+    case NOW_QUAL_YEARDAY:
+      return static_cast<int64_t>(res.tm_yday);
+      break;
+    default:
+      TSReleaseAssert(!"All cases should have been handled");
+      break;
+    }
+  }
+  return 0;
+}
+
 void
 ConditionNow::initialize(Parser &p)
 {
@@ -679,16 +724,33 @@ ConditionNow::initialize(Parser &p)
   _matcher = match;
 }
 
-
 void
 ConditionNow::set_qualifier(const std::string &q)
 {
   Condition::set_qualifier(q);
 
   TSDebug(PLUGIN_NAME, "\tParsing %%{NOW:%s} qualifier", q.c_str());
-  _now_qual = parse_now_qualifier(q);
-}
 
+  if (q == "EPOCH") {
+    _now_qual = NOW_QUAL_EPOCH;
+  } else if (q == "YEAR") {
+    _now_qual = NOW_QUAL_YEAR;
+  } else if (q == "MONTH") {
+    _now_qual = NOW_QUAL_MONTH;
+  } else if (q == "DAY") {
+    _now_qual = NOW_QUAL_DAY;
+  } else if (q == "HOUR") {
+    _now_qual = NOW_QUAL_HOUR;
+  } else if (q == "MINUTE") {
+    _now_qual = NOW_QUAL_MINUTE;
+  } else if (q == "WEEKDAY") {
+    _now_qual = NOW_QUAL_WEEKDAY;
+  } else if (q == "YEARDAY") {
+    _now_qual = NOW_QUAL_YEARDAY;
+  } else {
+    TSError("[%s] Unknown Now() qualifier: %s", PLUGIN_NAME, q.c_str());
+  }
+}
 
 void
 ConditionNow::append_value(std::string &s, const Resources & /* res ATS_UNUSED 
*/)
@@ -700,7 +762,6 @@ ConditionNow::append_value(std::string &s, const Resources 
& /* res ATS_UNUSED *
   TSDebug(PLUGIN_NAME, "Appending NOW() to evaluation value -> %s", s.c_str());
 }
 
-
 bool
 ConditionNow::eval(const Resources &res)
 {
@@ -715,7 +776,7 @@ ConditionNow::eval(const Resources &res)
 // ConditionGeo: Geo-based information (integer). See ConditionGeoCountry for 
the string version.
 #if HAVE_GEOIP_H
 const char *
-ConditionGeo::get_geo_string(const sockaddr *addr)
+ConditionGeo::get_geo_string(const sockaddr *addr) const
 {
   const char *ret = NULL;
   int v = 4;
@@ -777,7 +838,7 @@ ConditionGeo::get_geo_string(const sockaddr *addr)
 }
 
 int64_t
-ConditionGeo::get_geo_int(const sockaddr *addr)
+ConditionGeo::get_geo_int(const sockaddr *addr) const
 {
   int64_t ret = -1;
   int v = 4;
@@ -851,14 +912,14 @@ ConditionGeo::get_geo_int(const sockaddr *addr)
 // No Geo library avaiable, these are just stubs.
 
 const char *
-ConditionGeo::get_geo_string(const sockaddr *addr)
+ConditionGeo::get_geo_string(const sockaddr *addr) const
 {
   TSError("[%s] No Geo library available!", PLUGIN_NAME);
   return NULL;
 }
 
 int64_t
-ConditionGeo::get_geo_int(const sockaddr *addr)
+ConditionGeo::get_geo_int(const sockaddr *addr) const
 {
   TSError("[%s] No Geo library available!", PLUGIN_NAME);
   return 0;
@@ -906,7 +967,7 @@ ConditionGeo::set_qualifier(const std::string &q)
     _geo_qual = GEO_QUAL_ASN_NAME;
     is_int_type(false);
   } else {
-    TSError("[%s] Unknown Geo qualifier: %s", PLUGIN_NAME, q.c_str());
+    TSError("[%s] Unknown Geo() qualifier: %s", PLUGIN_NAME, q.c_str());
   }
 }
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/692437fa/plugins/header_rewrite/conditions.h
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/conditions.h 
b/plugins/header_rewrite/conditions.h
index bb918a3..6e39fad 100644
--- a/plugins/header_rewrite/conditions.h
+++ b/plugins/header_rewrite/conditions.h
@@ -410,11 +410,12 @@ protected:
   bool eval(const Resources &res);
 
 private:
+  int64_t get_now_qualified(NowQualifiers qual) const;
+
   DISALLOW_COPY_AND_ASSIGN(ConditionNow);
   NowQualifiers _now_qual;
 };
 
-
 // GeoIP class for the "integer" based Geo information pieces
 class ConditionGeo : public Condition
 {
@@ -440,13 +441,13 @@ public:
     _int_type = flag;
   }
 
-  int64_t get_geo_int(const sockaddr *addr);
-  const char *get_geo_string(const sockaddr *addr);
-
 protected:
   bool eval(const Resources &res);
 
 private:
+  int64_t get_geo_int(const sockaddr *addr) const;
+  const char *get_geo_string(const sockaddr *addr) const;
+
   DISALLOW_COPY_AND_ASSIGN(ConditionGeo);
 
   GeoQualifiers _geo_qual;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/692437fa/plugins/header_rewrite/statement.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/statement.cc 
b/plugins/header_rewrite/statement.cc
index 53e4e46..6ead25f 100644
--- a/plugins/header_rewrite/statement.cc
+++ b/plugins/header_rewrite/statement.cc
@@ -53,8 +53,9 @@ Statement::set_hook(TSHttpHookID hook)
 {
   bool ret = std::find(_allowed_hooks.begin(), _allowed_hooks.end(), hook) != 
_allowed_hooks.end();
 
-  if (ret)
+  if (ret) {
     _hook = hook;
+  }
 
   return ret;
 }
@@ -72,99 +73,29 @@ Statement::initialize_hooks()
   add_allowed_hook(TS_REMAP_PSEUDO_HOOK);
 }
 
-// Time related functionality for statements. We return an int64_t here, to 
assure that
-// gettimeofday() / Epoch does not lose bits.
-int64_t
-Statement::get_now_qualified(NowQualifiers qual) const
-{
-  time_t now;
-
-  // First short circuit for the Epoch qualifier, since it needs less data
-  time(&now);
-  if (NOW_QUAL_EPOCH == qual) {
-    return static_cast<int64_t>(now);
-  } else {
-    struct tm res;
-
-    localtime_r(&now, &res);
-    switch (qual) {
-    case NOW_QUAL_YEAR:
-      return static_cast<int64_t>(res.tm_year + 1900); // This makes more sense
-      break;
-    case NOW_QUAL_MONTH:
-      return static_cast<int64_t>(res.tm_mon);
-      break;
-    case NOW_QUAL_DAY:
-      return static_cast<int64_t>(res.tm_mday);
-      break;
-    case NOW_QUAL_HOUR:
-      return static_cast<int64_t>(res.tm_hour);
-      break;
-    case NOW_QUAL_MINUTE:
-      return static_cast<int64_t>(res.tm_min);
-      break;
-    case NOW_QUAL_WEEKDAY:
-      return static_cast<int64_t>(res.tm_wday);
-      break;
-    case NOW_QUAL_YEARDAY:
-      return static_cast<int64_t>(res.tm_yday);
-      break;
-    default:
-      TSReleaseAssert(!"All cases should have been handled");
-      break;
-    }
-  }
-  return 0;
-}
-
 
-// Parse URL qualifiers
+// Parse URL qualifiers, this one is special since it's used in a few places.
 UrlQualifiers
 Statement::parse_url_qualifier(const std::string &q) const
 {
   UrlQualifiers qual = URL_QUAL_NONE;
 
-  if (q == "HOST")
+  if (q == "HOST") {
     qual = URL_QUAL_HOST;
-  else if (q == "PORT")
+  } else if (q == "PORT") {
     qual = URL_QUAL_PORT;
-  else if (q == "PATH")
+  } else if (q == "PATH") {
     qual = URL_QUAL_PATH;
-  else if (q == "QUERY")
+  } else if (q == "QUERY") {
     qual = URL_QUAL_QUERY;
-  else if (q == "MATRIX")
+  } else if (q == "MATRIX") {
     qual = URL_QUAL_MATRIX;
-  else if (q == "SCHEME")
+  } else if (q == "SCHEME") {
     qual = URL_QUAL_SCHEME;
-  else if (q == "URL")
+  } else if (q == "URL") {
     qual = URL_QUAL_URL;
-
-  return qual;
-}
-
-
-// Parse NOW qualifiers
-NowQualifiers
-Statement::parse_now_qualifier(const std::string &q) const
-{
-  NowQualifiers qual = NOW_QUAL_EPOCH; // Default is seconds since epoch
-
-  if (q == "EPOCH") {
-    qual = NOW_QUAL_EPOCH;
-  } else if (q == "YEAR") {
-    qual = NOW_QUAL_YEAR;
-  } else if (q == "MONTH") {
-    qual = NOW_QUAL_MONTH;
-  } else if (q == "DAY") {
-    qual = NOW_QUAL_DAY;
-  } else if (q == "HOUR") {
-    qual = NOW_QUAL_HOUR;
-  } else if (q == "MINUTE") {
-    qual = NOW_QUAL_MINUTE;
-  } else if (q == "WEEKDAY") {
-    qual = NOW_QUAL_WEEKDAY;
-  } else if (q == "YEARDAY") {
-    qual = NOW_QUAL_YEARDAY;
+  } else {
+    TSError("[%s] Invalid URL() qualifier: %s", PLUGIN_NAME, q.c_str());
   }
 
   return qual;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/692437fa/plugins/header_rewrite/statement.h
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/statement.h 
b/plugins/header_rewrite/statement.h
index a161b83..dad4ddb 100644
--- a/plugins/header_rewrite/statement.h
+++ b/plugins/header_rewrite/statement.h
@@ -136,9 +136,6 @@ protected:
 
   UrlQualifiers parse_url_qualifier(const std::string &q) const;
 
-  NowQualifiers parse_now_qualifier(const std::string &q) const;
-  int64_t get_now_qualified(NowQualifiers qual) const;
-
   void
   require_resources(const ResourceIDs ids)
   {

Reply via email to