This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new f885a1d  Add support to X-Remap header to xdebug plugin.
f885a1d is described below

commit f885a1dda31eccb8636b595874a740bf44f620ec
Author: Walt Karas <wka...@yahoo-inc.com>
AuthorDate: Sat Feb 17 01:12:31 2018 +0000

    Add support to X-Remap header to xdebug plugin.
---
 plugins/xdebug/xdebug.cc                           | 66 ++++++++++++++++++-
 tests/gold_tests/pluginTest/xdebug/x_remap/none.in |  4 ++
 tests/gold_tests/pluginTest/xdebug/x_remap/one.in  |  4 ++
 .../gold_tests/pluginTest/xdebug/x_remap/out.gold  | 61 ++++++++++++++++++
 .../gold_tests/pluginTest/xdebug/x_remap/three.in  |  4 ++
 tests/gold_tests/pluginTest/xdebug/x_remap/two.in  |  4 ++
 .../pluginTest/xdebug/x_remap/x_remap.test.py      | 75 ++++++++++++++++++++++
 7 files changed, 216 insertions(+), 2 deletions(-)

diff --git a/plugins/xdebug/xdebug.cc b/plugins/xdebug/xdebug.cc
index 82ddef9..f3e7859 100644
--- a/plugins/xdebug/xdebug.cc
+++ b/plugins/xdebug/xdebug.cc
@@ -41,6 +41,7 @@ enum {
   XHEADER_X_GENERATION     = 0x0020u,
   XHEADER_X_TRANSACTION_ID = 0x0040u,
   XHEADER_X_DUMP_HEADERS   = 0x0080u,
+  XHEADER_X_REMAP          = 0x0100u,
 };
 
 static int XArgIndex             = 0;
@@ -242,6 +243,63 @@ done:
   }
 }
 
+static const char NotFound[] = "Not-Found";
+
+// The returned string must be freed with TSfree() unless it is equal to the 
constant NotFound.
+//
+static const char *
+getRemapUrlStr(TSHttpTxn txnp, TSReturnCode (*remapUrlGetFunc)(TSHttpTxn, 
TSMLoc *), int &urlStrLen)
+{
+  TSMLoc urlLoc;
+
+  TSReturnCode rc = remapUrlGetFunc(txnp, &urlLoc);
+  if (rc != TS_SUCCESS) {
+    urlStrLen = sizeof(NotFound) - 1;
+    return NotFound;
+  }
+
+  char *urlStr = TSUrlStringGet(nullptr, urlLoc, &urlStrLen);
+
+  // Be defensive.
+  if ((urlStrLen == 0) and urlStr) {
+    TSError("[xdebug] non-null remap URL string with zero length");
+    TSfree(urlStr);
+    urlStr = nullptr;
+  }
+
+  if (!urlStr) {
+    urlStrLen = sizeof(NotFound) - 1;
+    return NotFound;
+  }
+
+  return urlStr;
+}
+
+static void
+InjectRemapHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
+{
+  TSMLoc dst = FindOrMakeHdrField(buffer, hdr, "X-Remap", lengthof("X-Remap"));
+
+  if (TS_NULL_MLOC != dst) {
+    int fromUrlStrLen, toUrlStrLen;
+    const char *fromUrlStr = getRemapUrlStr(txn, TSRemapFromUrlGet, 
fromUrlStrLen);
+    const char *toUrlStr   = getRemapUrlStr(txn, TSRemapToUrlGet, toUrlStrLen);
+
+    char buf[2048];
+    int len = snprintf(buf, sizeof(buf) - 1, "from=%*s, to=%*s", 
fromUrlStrLen, fromUrlStr, toUrlStrLen, toUrlStr);
+
+    if (fromUrlStr != NotFound) {
+      TSfree(const_cast<char *>(fromUrlStr));
+    }
+    if (toUrlStr != NotFound) {
+      TSfree(const_cast<char *>(toUrlStr));
+    }
+
+    TSReleaseAssert(TSMimeHdrFieldValueStringInsert(buffer, hdr, dst, 0 /* idx 
*/, buf, len) == TS_SUCCESS);
+    TSHandleMLocRelease(buffer, hdr, dst);
+  }
+}
+
 static void
 InjectTxnUuidHeader(TSHttpTxn txn, TSMBuffer buffer, TSMLoc hdr)
 {
@@ -338,6 +396,10 @@ XInjectResponseHeaders(TSCont /* contp */, TSEvent event, 
void *edata)
     log_headers(txn, buffer, hdr, "ClientResponse");
   }
 
+  if (xheaders & XHEADER_X_REMAP) {
+    InjectRemapHeader(txn, buffer, hdr);
+  }
+
 done:
   TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
   return TS_EVENT_NONE;
@@ -388,6 +450,8 @@ XScanRequestHeaders(TSCont /* contp */, TSEvent event, void 
*edata)
         xheaders |= XHEADER_X_GENERATION;
       } else if (header_field_eq("x-transaction-id", value, vsize)) {
         xheaders |= XHEADER_X_TRANSACTION_ID;
+      } else if (header_field_eq("x-remap", value, vsize)) {
+        xheaders |= XHEADER_X_REMAP;
       } else if (header_field_eq("via", value, vsize)) {
         // If the client requests the Via header, enable verbose Via debugging 
for this transaction.
         TSHttpTxnConfigIntSet(txn, TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR, 3);
@@ -504,5 +568,3 @@ TSPluginInit(int argc, const char *argv[])
   TSReleaseAssert(XInjectHeadersCont = TSContCreate(XInjectResponseHeaders, 
nullptr));
   TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, 
TSContCreate(XScanRequestHeaders, nullptr));
 }
-
-// vim: set ts=2 sw=2 et :
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/none.in 
b/tests/gold_tests/pluginTest/xdebug/x_remap/none.in
new file mode 100644
index 0000000..6e5b719
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/none.in
@@ -0,0 +1,4 @@
+GET /argh HTTP/1.1
+Host: none
+X-Debug: X-Remap
+
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/one.in 
b/tests/gold_tests/pluginTest/xdebug/x_remap/one.in
new file mode 100644
index 0000000..48306be
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/one.in
@@ -0,0 +1,4 @@
+GET /argh HTTP/1.1
+Host: one
+X-Debug: X-Remap
+
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold 
b/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
new file mode 100644
index 0000000..cb8fd49
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/out.gold
@@ -0,0 +1,61 @@
+HTTP/1.1 502 Cannot find server.
+Date:``
+Connection: keep-alive
+Server:``
+Cache-Control: no-store
+Content-Type: text/html
+Content-Language: en
+X-Remap: from=Not-Found, to=Not-Found
+Content-Length: 391
+
+<HTML>
+<HEAD>
+<TITLE>Unknown Host</TITLE>
+</HEAD>
+
+<BODY BGCOLOR="white" FGCOLOR="black">
+<H1>Unknown Host</H1>
+<HR>
+
+<FONT FACE="Helvetica,Arial"><B>
+Description: Unable to locate the server requested ---
+the server does not have a DNS entry.  Perhaps there is a misspelling
+in the server name, or the server no longer exists.  Double-check the
+name and try again.
+</B></FONT>
+<HR>
+</BODY>
+======
+HTTP/1.1 200 OK
+Date:``
+Age:``
+Transfer-Encoding: chunked
+Connection: keep-alive
+Server:``
+X-Remap: from=http://one/, to=http://127.0.0.1:SERVER_PORT/
+
+0
+
+======
+HTTP/1.1 200 OK
+Date:``
+Age:``
+Transfer-Encoding: chunked
+Connection: keep-alive
+Server:``
+X-Remap: from=http://two/, to=http://127.0.0.1:SERVER_PORT/
+
+0
+
+======
+HTTP/1.1 200 OK
+Date:``
+Age:``
+Transfer-Encoding: chunked
+Connection: keep-alive
+Server:``
+X-Remap: from=http://three[0-9]+/, to=http://127.0.0.1:SERVER_PORT/
+
+0
+
+======
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/three.in 
b/tests/gold_tests/pluginTest/xdebug/x_remap/three.in
new file mode 100644
index 0000000..b525bf8
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/three.in
@@ -0,0 +1,4 @@
+GET /argh HTTP/1.1
+Host: three123
+X-Debug: X-Remap
+
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/two.in 
b/tests/gold_tests/pluginTest/xdebug/x_remap/two.in
new file mode 100644
index 0000000..c971a91
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/two.in
@@ -0,0 +1,4 @@
+GET /argh HTTP/1.1
+Host: two
+X-Debug: X-Remap
+
diff --git a/tests/gold_tests/pluginTest/xdebug/x_remap/x_remap.test.py 
b/tests/gold_tests/pluginTest/xdebug/x_remap/x_remap.test.py
new file mode 100644
index 0000000..a0de674
--- /dev/null
+++ b/tests/gold_tests/pluginTest/xdebug/x_remap/x_remap.test.py
@@ -0,0 +1,75 @@
+#  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.
+
+Test.Summary = '''
+Test xdebug plugin X-Remap header
+'''
+
+server = Test.MakeOriginServer("server")
+
+request_header = {
+    "headers": "GET /argh HTTP/1.1\r\nHost: doesnotmatter\r\n\r\n", 
"timestamp": "1469733493.993", "body": "" }
+response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", 
"timestamp": "1469733493.993", "body": "" }
+server.addResponse("sessionlog.json", request_header, response_header)
+
+ts = Test.MakeATSProcess("ts")
+
+ts.Disk.records_config.update({
+    'proxy.config.url_remap.remap_required': 0,
+    'proxy.config.diags.debug.enabled': 0,
+    'proxy.config.diags.debug.tags': 'http'
+})
+
+ts.Disk.plugin_config.AddLine('xdebug.so')
+
+ts.Disk.remap_config.AddLine(
+    "map http://one http://127.0.0.1:{0}".format(server.Variables.Port)
+)
+ts.Disk.remap_config.AddLine(
+    "map http://two http://127.0.0.1:{0}".format(server.Variables.Port)
+)
+ts.Disk.remap_config.AddLine(
+    "regex_map http://three[0-9]+ 
http://127.0.0.1:{0}".format(server.Variables.Port)
+)
+
+tr = Test.AddTestRun()
+tr.Processes.Default.StartBefore(Test.Processes.ts)
+tr.Processes.Default.StartBefore(Test.Processes.server)
+tr.Processes.Default.Command = "cp {}/tcp_client.py {}/tcp_client.py".format(
+    Test.Variables.AtsTestToolsDir, Test.RunDirectory)
+tr.Processes.Default.ReturnCode = 0
+
+def sendMsg(msgFile):
+
+    tr = Test.AddTestRun()
+    tr.Processes.Default.Command = (
+        "( python {}/tcp_client.py 127.0.0.1 {} {}/{}.in".format(
+            Test.RunDirectory, ts.Variables.port, Test.TestDirectory, msgFile) 
+
+        " ; echo '======' ) | sed 's/:{}/:SERVER_PORT/' >>  {}/out.log 2>&1 
".format(
+            server.Variables.Port, Test.RunDirectory)
+    )
+    tr.Processes.Default.ReturnCode = 0
+
+sendMsg('none')
+sendMsg('one')
+sendMsg('two')
+sendMsg('three')
+
+tr = Test.AddTestRun()
+tr.Processes.Default.Command = "echo test gold"
+tr.Processes.Default.ReturnCode = 0
+f = tr.Disk.File("out.log")
+f.Content = "out.gold"

-- 
To stop receiving notification emails like this one, please contact
a...@apache.org.

Reply via email to