TS-2834 Refactor the getIP() code, and rename the condition to CLIENT-IP. Also added a little bit of docs.
Long term, we should figure out how to unify conditions and the expander in a way that it integrates with the internal log token expansions. This closes #91 Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/896ce4d3 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/896ce4d3 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/896ce4d3 Branch: refs/heads/master Commit: 896ce4d33468a1e2094b103a720eee38fafa26df Parents: 05f64b6 Author: Leif Hedstrom <[email protected]> Authored: Thu May 29 14:10:34 2014 -0600 Committer: Leif Hedstrom <[email protected]> Committed: Thu May 29 14:30:13 2014 -0600 ---------------------------------------------------------------------- CHANGES | 3 ++ doc/reference/plugins/header_rewrite.en.rst | 2 + plugins/header_rewrite/Makefile.am | 6 ++- plugins/header_rewrite/conditions.cc | 44 +++++++---------- plugins/header_rewrite/expander.cc | 28 ----------- plugins/header_rewrite/factory.cc | 2 +- plugins/header_rewrite/lulu.cc | 63 ++++++++++++++++++++++++ plugins/header_rewrite/lulu.h | 7 +++ 8 files changed, 99 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index ab72cd1..dc132b3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,9 @@ -*- coding: utf-8 -*- Changes with Apache Traffic Server 5.0.0 + *) [TS-2834] header_rewrite: Add Internal transaction and client-IP + conditions. + *) [TS-2855] Add the TSHttpIsInternalSession API. *) [TS-2859] Remove DBG macros to not generate warnings from GCC 4.9. http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/doc/reference/plugins/header_rewrite.en.rst ---------------------------------------------------------------------- diff --git a/doc/reference/plugins/header_rewrite.en.rst b/doc/reference/plugins/header_rewrite.en.rst index 1df31d2..af71ab1 100644 --- a/doc/reference/plugins/header_rewrite.en.rst +++ b/doc/reference/plugins/header_rewrite.en.rst @@ -117,6 +117,8 @@ only be evaluated if the condition(s) are met:: cond %{PATH} operand [condition_flags] cond %{PARAMS} operand [condition_flags] cond %{QUERY} operand [condition_flags] + cond %{INTERNAL-TRANSACTION} [condition_flags] + cond %{CLIENT-IP} [condition_flags] The difference between HEADER and CLIENT-HEADER is that HEADER adapts to the hook it's running in, whereas CLIENT-HEADER always applies to the client http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/Makefile.am ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/Makefile.am b/plugins/header_rewrite/Makefile.am index ee17870..4ba0875 100644 --- a/plugins/header_rewrite/Makefile.am +++ b/plugins/header_rewrite/Makefile.am @@ -24,8 +24,10 @@ pkglib_LTLIBRARIES = header_rewrite.la header_rewrite_la_SOURCES = \ condition.cc \ conditions.cc \ + expander.cc \ factory.cc \ header_rewrite.cc \ + lulu.cc \ matcher.cc \ operator.cc \ operators.cc \ @@ -33,8 +35,8 @@ header_rewrite_la_SOURCES = \ regex_helper.cc \ resources.cc \ ruleset.cc \ - statement.cc \ - expander.cc + statement.cc + header_rewrite_la_LDFLAGS = $(BOOST_LDFLAGS) $(TS_PLUGIN_LDFLAGS) endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/conditions.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/conditions.cc b/plugins/header_rewrite/conditions.cc index 721a781..6bc97fa 100644 --- a/plugins/header_rewrite/conditions.cc +++ b/plugins/header_rewrite/conditions.cc @@ -384,7 +384,8 @@ ConditionDBM::eval(const Resources& res) // ConditionCookie: request or response header -void ConditionCookie::initialize(Parser& p) +void +ConditionCookie::initialize(Parser& p) { Condition::initialize(p); @@ -396,7 +397,8 @@ void ConditionCookie::initialize(Parser& p) require_resources(RSRC_CLIENT_REQUEST_HEADERS); } -void ConditionCookie::append_value(std::string& s, const Resources& res) +void +ConditionCookie::append_value(std::string& s, const Resources& res) { TSMBuffer bufp = res.client_bufp; TSMLoc hdr_loc = res.client_hdr_loc; @@ -436,7 +438,8 @@ out_release_field: TSHandleMLocRelease(bufp, hdr_loc, field_loc); } -bool ConditionCookie::eval(const Resources& res) +bool +ConditionCookie::eval(const Resources& res) { std::string s; @@ -446,12 +449,14 @@ bool ConditionCookie::eval(const Resources& res) return rval; } -bool ConditionInternalTransaction::eval(const Resources& res) +bool +ConditionInternalTransaction::eval(const Resources& res) { return TSHttpIsInternalRequest(res.txnp) == TS_SUCCESS; } -void ConditionClientIp::initialize(Parser &p) +void +ConditionClientIp::initialize(Parser &p) { Condition::initialize(p); @@ -461,34 +466,23 @@ void ConditionClientIp::initialize(Parser &p) _matcher = match; } -bool ConditionClientIp::eval(const Resources &res) +bool +ConditionClientIp::eval(const Resources &res) { std::string s; + append_value(s, res); bool rval = static_cast<const Matchers<std::string>*>(_matcher)->test(s); + TSDebug(PLUGIN_NAME, "Evaluating CLIENT-IP(): %s: rval: %d", s.c_str(), rval); return rval; } -void ConditionClientIp::append_value(std::string &s, const Resources &res) +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 }; + char ip[INET6_ADDRSTRLEN]; - 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; + if (getIP(TSHttpTxnClientAddrGet(res.txnp), ip)) { + s.append(ip); } - s.append(buf); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/expander.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/expander.cc b/plugins/header_rewrite/expander.cc index c4a581c..7ab69a7 100644 --- a/plugins/header_rewrite/expander.cc +++ b/plugins/header_rewrite/expander.cc @@ -29,34 +29,6 @@ #include "parser.h" #include "expander.h" -// Helper function to cleanly get the IP as a string. -std::string -getIP(sockaddr const* s_sockaddr) -{ - if (s_sockaddr == NULL) - return ""; - - char res[INET6_ADDRSTRLEN] = { '\0' }; - - switch (s_sockaddr->sa_family) { - case AF_INET: - { - const struct sockaddr_in *s_sockaddr_in = reinterpret_cast<const struct sockaddr_in *>(s_sockaddr); - inet_ntop(AF_INET, &s_sockaddr_in->sin_addr, res, INET_ADDRSTRLEN); - } - break; - case AF_INET6: - { - const struct sockaddr_in6 *s_sockaddr_in6 = reinterpret_cast<const struct sockaddr_in6 *>(s_sockaddr); - inet_ntop(AF_INET6, &s_sockaddr_in6->sin6_addr, res, INET6_ADDRSTRLEN); - } - break; - } - - return res; -} - - // Main expander method std::string VariableExpander::expand(const Resources& res) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/factory.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/factory.cc b/plugins/header_rewrite/factory.cc index c1b8a2e..b9e2fa8 100644 --- a/plugins/header_rewrite/factory.cc +++ b/plugins/header_rewrite/factory.cc @@ -109,7 +109,7 @@ condition_factory(const std::string& cond) c = new ConditionDBM(); } else if (c_name == "INTERNAL-TRANSACTION") { c = new ConditionInternalTransaction(); - } else if (c_name == "%<chi>") { + } else if (c_name == "CLIENT-IP") { c = new ConditionClientIp(); } else { TSError("%s: unknown condition: %s", PLUGIN_NAME, c_name.c_str()); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/lulu.cc ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/lulu.cc b/plugins/header_rewrite/lulu.cc new file mode 100644 index 0000000..9db1f56 --- /dev/null +++ b/plugins/header_rewrite/lulu.cc @@ -0,0 +1,63 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include <string> + +#include "ts/ts.h" +#include "lulu.h" + +// Helper function to cleanly get the IP as a string. +char* +getIP(sockaddr const* s_sockaddr, char res[INET6_ADDRSTRLEN]) +{ + res[0] = '\0'; + + if (s_sockaddr == NULL) { + return NULL; + } + + switch (s_sockaddr->sa_family) { + case AF_INET: + { + const struct sockaddr_in *s_sockaddr_in = reinterpret_cast<const struct sockaddr_in *>(s_sockaddr); + inet_ntop(AF_INET, &s_sockaddr_in->sin_addr, res, INET_ADDRSTRLEN); + } + break; + case AF_INET6: + { + const struct sockaddr_in6 *s_sockaddr_in6 = reinterpret_cast<const struct sockaddr_in6 *>(s_sockaddr); + inet_ntop(AF_INET6, &s_sockaddr_in6->sin6_addr, res, INET6_ADDRSTRLEN); + } + break; + } + + return res[0] ? res : NULL; +} + +// Return it as a std::string instead (more expensive, but sometimes convenient) +std::string +getIP(sockaddr const* s_sockaddr) +{ + char res[INET6_ADDRSTRLEN] = { '\0' }; + + if (getIP(s_sockaddr, res)) { + return res; + } + + return ""; +} http://git-wip-us.apache.org/repos/asf/trafficserver/blob/896ce4d3/plugins/header_rewrite/lulu.h ---------------------------------------------------------------------- diff --git a/plugins/header_rewrite/lulu.h b/plugins/header_rewrite/lulu.h index 2226848..df6226a 100644 --- a/plugins/header_rewrite/lulu.h +++ b/plugins/header_rewrite/lulu.h @@ -15,6 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ + ////////////////////////////////////////////////////////////////////////////////////////////// // // Implement the classes for the various types of hash keys we support. @@ -22,9 +23,15 @@ #ifndef __LULU_H__ #define __LULU_H__ 1 +#include <string> + #include "ink_defs.h" #include "ink_platform.h" +std::string getIP(sockaddr const* s_sockaddr); +char* getIP(sockaddr const* s_sockaddr, char res[INET6_ADDRSTRLEN]); + + // Memory barriers #if defined(__i386__) #define mb() __asm__ __volatile__ ( "lock; addl $0,0(%%esp)" : : : "memory" )
