Repository: trafficserver
Updated Branches:
  refs/heads/master adae7cd16 -> 5054186f9


TS-3108: Add port matching condition to header_rewrite


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

Branch: refs/heads/master
Commit: 5054186f9083640583d366e732f18f846b65a6c2
Parents: adae7cd
Author: Phil Sorber <[email protected]>
Authored: Thu Oct 2 13:47:12 2014 -0600
Committer: Phil Sorber <[email protected]>
Committed: Thu Oct 2 13:47:12 2014 -0600

----------------------------------------------------------------------
 CHANGES                              |  4 +++-
 plugins/header_rewrite/conditions.cc | 29 +++++++++++++++++++++++++++++
 plugins/header_rewrite/conditions.h  | 18 ++++++++++++++++++
 plugins/header_rewrite/factory.cc    |  2 ++
 plugins/header_rewrite/lulu.cc       | 23 +++++++++++++++++++++++
 plugins/header_rewrite/lulu.h        |  2 +-
 6 files changed, 76 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index c21cedc..867e8a3 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,7 +1,9 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache Traffic Server 5.2.0
 
-  *) [TS=3068] Remove usage of Boost.
+  *) [TS-3108] Add port matching condition to header_rewrite.
+
+  *) [TS-3068] Remove usage of Boost.
 
   *) [TS-2289] Removed old unused AIO modes.
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/plugins/header_rewrite/conditions.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/conditions.cc 
b/plugins/header_rewrite/conditions.cc
index 0f78ace..be3d28b 100644
--- a/plugins/header_rewrite/conditions.cc
+++ b/plugins/header_rewrite/conditions.cc
@@ -491,3 +491,32 @@ ConditionClientIp::append_value(std::string &s, const 
Resources &res)
     s.append(ip);
   }
 }
+
+void
+ConditionIncomingPort::initialize(Parser &p)
+{
+  Condition::initialize(p);
+
+  Matchers<uint16_t>* match = new Matchers<uint16_t>(_cond_op);
+  match->set(static_cast<uint16_t>(strtoul(p.get_arg().c_str(), NULL, 10)));
+  _matcher = match;
+}
+
+bool
+ConditionIncomingPort::eval(const Resources &res)
+{
+  uint16_t port = getPort(TSHttpTxnIncomingAddrGet(res.txnp));
+  bool rval = static_cast<const Matchers<uint16_t>*>(_matcher)->test(port);
+  TSDebug(PLUGIN_NAME, "Evaluating INCOMING-PORT(): %d: rval: %d", port, rval);
+  return rval;
+}
+
+void
+ConditionIncomingPort::append_value(std::string &s, const Resources &res)
+{
+  std::ostringstream oss;
+  uint16_t port = getPort(TSHttpTxnIncomingAddrGet(res.txnp));
+  oss << port;
+  s += oss.str();
+  TSDebug(PLUGIN_NAME, "Appending %d to evaluation value -> %s", port, 
s.c_str());
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/plugins/header_rewrite/conditions.h
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/conditions.h 
b/plugins/header_rewrite/conditions.h
index 0b76f42..fbb843d 100644
--- a/plugins/header_rewrite/conditions.h
+++ b/plugins/header_rewrite/conditions.h
@@ -348,4 +348,22 @@ protected:
   bool eval(const Resources &res);
 };
 
+class ConditionIncomingPort : public Condition
+{
+public:
+  ConditionIncomingPort()
+  {
+    TSDebug(PLUGIN_NAME_DBG, "Calling CTOR for ConditionIncomingPort");
+  }
+
+  void initialize(Parser& p);
+  void append_value(std::string &s, const Resources &res);
+
+protected:
+  bool eval(const Resources &res);
+
+private:
+  DISALLOW_COPY_AND_ASSIGN(ConditionIncomingPort);
+};
+
 #endif // __CONDITIONS_H

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/plugins/header_rewrite/factory.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/factory.cc 
b/plugins/header_rewrite/factory.cc
index 1d99ea5..eb44369 100644
--- a/plugins/header_rewrite/factory.cc
+++ b/plugins/header_rewrite/factory.cc
@@ -113,6 +113,8 @@ condition_factory(const std::string& cond)
     c = new ConditionInternalTransaction();
   } else if (c_name == "CLIENT-IP") {
     c = new ConditionClientIp();
+  } else if (c_name == "INCOMING-PORT") {
+    c = new ConditionIncomingPort();
   } else {
     TSError("%s: unknown condition: %s", PLUGIN_NAME, c_name.c_str());
     return NULL;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/plugins/header_rewrite/lulu.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/lulu.cc b/plugins/header_rewrite/lulu.cc
index 9db1f56..ec5253f 100644
--- a/plugins/header_rewrite/lulu.cc
+++ b/plugins/header_rewrite/lulu.cc
@@ -61,3 +61,26 @@ getIP(sockaddr const* s_sockaddr)
 
   return "";
 }
+
+// Returns the port of a sockaddr
+uint16_t
+getPort(sockaddr const* s_sockaddr)
+{
+  switch (s_sockaddr->sa_family) {
+  case AF_INET:
+    {
+      const struct sockaddr_in *s_sockaddr_in = reinterpret_cast<const struct 
sockaddr_in *>(s_sockaddr);
+      return ntohs(s_sockaddr_in->sin_port);
+    }
+    break;
+  case AF_INET6:
+    {
+      const struct sockaddr_in6 *s_sockaddr_in6 = reinterpret_cast<const 
struct sockaddr_in6 *>(s_sockaddr);
+      return ntohs(s_sockaddr_in6->sin6_port);
+    }
+    break;
+  default:
+    return 0;
+    break;
+  }
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/5054186f/plugins/header_rewrite/lulu.h
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/lulu.h b/plugins/header_rewrite/lulu.h
index 2652953..dcbb09e 100644
--- a/plugins/header_rewrite/lulu.h
+++ b/plugins/header_rewrite/lulu.h
@@ -30,7 +30,7 @@
 
 std::string getIP(sockaddr const* s_sockaddr);
 char* getIP(sockaddr const* s_sockaddr, char res[INET6_ADDRSTRLEN]);
-
+uint16_t getPort(sockaddr const* s_sockaddr);
 
 // Memory barriers
 #if defined(__i386__)

Reply via email to