TS-2834 header_rewrite: internal transaction and client IP conditions
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/05f64b6e Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/05f64b6e Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/05f64b6e Branch: refs/heads/5.0.x Commit: 05f64b6ebcf35b1916ff6663fac57e4d5b3049fa Parents: 52682da Author: Manjesh Nilange <[email protected]> Authored: Thu May 22 15:01:55 2014 -0700 Committer: Leif Hedstrom <[email protected]> Committed: Thu May 29 14:29:54 2014 -0600 ---------------------------------------------------------------------- plugins/header_rewrite/conditions.cc | 49 ++++++++++++++++++++++++++++++- plugins/header_rewrite/conditions.h | 19 ++++++++++++ plugins/header_rewrite/factory.cc | 4 +++ 3 files changed, 71 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/05f64b6e/plugins/header_rewrite/conditions.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/conditions.cc b/plugins/header_rewrite/conditions.cc index 9010152..721a781 100644 --- a/plugins/header_rewrite/conditions.cc +++ b/plugins/header_rewrite/conditions.cc @@ -21,13 +21,13 @@ // #include <sys/time.h> #include <unistd.h> +#include <arpa/inet.h> #include "ts/ts.h" #include "conditions.h" #include "lulu.h" - // ConditionStatus void ConditionStatus::initialize(Parser& p) @@ -445,3 +445,50 @@ bool ConditionCookie::eval(const Resources& res) TSDebug(PLUGIN_NAME, "Evaluating COOKIE(%s): %s: rval: %d", _qualifier.c_str(), s.c_str(), rval); return rval; } + +bool ConditionInternalTransaction::eval(const Resources& res) +{ + return TSHttpIsInternalRequest(res.txnp) == TS_SUCCESS; +} + +void ConditionClientIp::initialize(Parser &p) +{ + Condition::initialize(p); + + Matchers<std::string>* match = new Matchers<std::string>(_cond_op); + match->set(p.get_arg()); + + _matcher = match; +} + +bool ConditionClientIp::eval(const Resources &res) +{ + std::string s; + append_value(s, res); + bool rval = static_cast<const Matchers<std::string>*>(_matcher)->test(s); + return rval; +} + +void ConditionClientIp::append_value(std::string &s, const Resources &res) +{ + const sockaddr *sockaddress = TSHttpTxnClientAddrGet(res.txnp); + if (sockaddress == NULL) { + return; + } + + char buf[INET6_ADDRSTRLEN] = { 0 }; + + if (sockaddress->sa_family == AF_INET) + { + inet_ntop(AF_INET, &(((struct sockaddr_in *) sockaddress)->sin_addr), buf, INET_ADDRSTRLEN); + } + else if (sockaddress->sa_family == AF_INET6) + { + inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) sockaddress)->sin6_addr), buf, INET6_ADDRSTRLEN); + } + else + { + return; + } + s.append(buf); +} http://git-wip-us.apache.org/repos/asf/trafficserver/blob/05f64b6e/plugins/header_rewrite/conditions.h ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/conditions.h b/plugins/header_rewrite/conditions.h index deb3142..55e0106 100644 --- a/plugins/header_rewrite/conditions.h +++ b/plugins/header_rewrite/conditions.h @@ -330,4 +330,23 @@ private: TSMutex _mutex; }; +class ConditionInternalTransaction : public Condition +{ +public: + void append_value(std::string &/* s ATS_UNUSED */, const Resources &/* res ATS_UNUSED */) { } + +protected: + bool eval(const Resources &res); +}; + +class ConditionClientIp : public Condition +{ +public: + void initialize(Parser& p); + void append_value(std::string &s, const Resources &res); + +protected: + bool eval(const Resources &res); +}; + #endif // __CONDITIONS_H http://git-wip-us.apache.org/repos/asf/trafficserver/blob/05f64b6e/plugins/header_rewrite/factory.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/factory.cc b/plugins/header_rewrite/factory.cc index 66087fc..c1b8a2e 100644 --- a/plugins/header_rewrite/factory.cc +++ b/plugins/header_rewrite/factory.cc @@ -107,6 +107,10 @@ condition_factory(const std::string& cond) c = new ConditionUrl(true); } else if (c_name == "DBM") { c = new ConditionDBM(); + } else if (c_name == "INTERNAL-TRANSACTION") { + c = new ConditionInternalTransaction(); + } else if (c_name == "%<chi>") { + c = new ConditionClientIp(); } else { TSError("%s: unknown condition: %s", PLUGIN_NAME, c_name.c_str()); return NULL;
