From 99edcf0c37413021ee5b7f335903e9ac5ee5ea74 Mon Sep 17 00:00:00 2001 From: Bryan Call Date: Mon, 27 Jul 2020 16:35:50 -0700 Subject: [PATCH 1/3] Fixed CLIENT-URL to use the pristine client URL --- plugins/header_rewrite/conditions.cc | 30 +++++----- .../gold/header_rewrite-client.gold | 12 ++++ .../header_rewrite_client.test.py | 60 +++++++++++++++++++ .../header_rewrite/rules/rule_client.conf | 22 +++++++ 4 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold create mode 100644 tests/gold_tests/pluginTest/header_rewrite/header_rewrite_client.test.py create mode 100644 tests/gold_tests/pluginTest/header_rewrite/rules/rule_client.conf diff --git a/plugins/header_rewrite/conditions.cc b/plugins/header_rewrite/conditions.cc index e785e266e9d..b9df7b89e28 100644 --- a/plugins/header_rewrite/conditions.cc +++ b/plugins/header_rewrite/conditions.cc @@ -274,11 +274,17 @@ ConditionUrl::append_value(std::string &s, const Resources &res) TSMLoc url = nullptr; TSMBuffer bufp = nullptr; - if (res._rri != nullptr) { + if (_type == CLIENT) { + // CLIENT always uses the pristine URL + TSDebug(PLUGIN_NAME, " Using the pristine url"); + if (TSHttpTxnPristineUrlGet(res.txnp, &bufp, &url) != TS_SUCCESS) { + TSError("[header_rewrite] Error getting the pristine URL"); + return; + } + } else if (res._rri != nullptr) { // called at the remap hook bufp = res._rri->requestBufp; - if (_type == URL || _type == CLIENT) { - // res._rri->requestBufp and res.client_bufp are the same if it is at the remap hook + if (_type == URL) { TSDebug(PLUGIN_NAME, " Using the request url"); url = res._rri->requestUrl; } else if (_type == FROM) { @@ -292,21 +298,17 @@ ConditionUrl::append_value(std::string &s, const Resources &res) return; } } else { - TSMLoc hdr_loc = nullptr; - if (_type == CLIENT) { - bufp = res.client_bufp; - hdr_loc = res.client_hdr_loc; - } else if (_type == URL) { - bufp = res.bufp; - hdr_loc = res.hdr_loc; + if (_type == URL) { + bufp = res.bufp; + TSMLoc hdr_loc = res.hdr_loc; + if (TSHttpHdrUrlGet(bufp, hdr_loc, &url) != TS_SUCCESS) { + TSError("[header_rewrite] Error getting the URL"); + return; + } } else { TSError("[header_rewrite] Rule not supported at this hook"); return; } - if (TSHttpHdrUrlGet(bufp, hdr_loc, &url) != TS_SUCCESS) { - TSError("[header_rewrite] Error getting the URL"); - return; - } } int i; diff --git a/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold new file mode 100644 index 00000000000..f7de90ac8f5 --- /dev/null +++ b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold @@ -0,0 +1,12 @@ +`` +> GET http://www.example.com`` +> Host: www.example.com`` +> User-Agent: curl/`` +> Accept: */* +`` +< HTTP/1.1 304 Not Modified +< Date: `` +< Proxy-Connection: keep-alive +< Server: ATS/`` +< +`` diff --git a/tests/gold_tests/pluginTest/header_rewrite/header_rewrite_client.test.py b/tests/gold_tests/pluginTest/header_rewrite/header_rewrite_client.test.py new file mode 100644 index 00000000000..53aa08541de --- /dev/null +++ b/tests/gold_tests/pluginTest/header_rewrite/header_rewrite_client.test.py @@ -0,0 +1,60 @@ +''' +''' +# 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 header_rewrite and CLIENT-URL +''' + +Test.ContinueOnFail = True +# Define default ATS +ts = Test.MakeATSProcess("ts") +server = Test.MakeOriginServer("server") + +Test.testName = "" +request_header = {"headers": "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n", "timestamp": "1469733493.993", "body": ""} +# expected response from the origin server +response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": ""} + +# add response to the server dictionary +server.addResponse("sessionfile.log", request_header, response_header) +ts.Disk.records_config.update({ + 'proxy.config.diags.debug.enabled': 1, + 'proxy.config.diags.debug.tags': 'header.*', +}) +# The following rule changes the status code returned from origin server to 303 +ts.Setup.CopyAs('rules/rule_client.conf', Test.RunDirectory) + +ts.Disk.remap_config.AddLine( + 'map http://www.example.com/from_path/ https://127.0.0.1:{0}/to_path/ @plugin=header_rewrite.so @pparam={1}/rule_client.conf'.format(server.Variables.Port, Test.RunDirectory) +) +ts.Disk.remap_config.AddLine( + 'map http://www.example.com:8080/from_path/ https://127.0.0.1:{0}/to_path/ @plugin=header_rewrite.so @pparam={1}/rule_client.conf'.format(server.Variables.Port, Test.RunDirectory) +) + +# call localhost straight +tr = Test.AddTestRun() +tr.Processes.Default.Command = 'curl --proxy 127.0.0.1:{0} "http://www.example.com/from_path/hello?=foo=bar" -H "Proxy-Connection: keep-alive" --verbose'.format( + ts.Variables.port) +tr.Processes.Default.ReturnCode = 0 +# time delay as proxy.config.http.wait_for_cache could be broken +tr.Processes.Default.StartBefore(server, ready=When.PortOpen(server.Variables.Port)) +tr.Processes.Default.StartBefore(Test.Processes.ts) +tr.Processes.Default.Streams.stderr = "gold/header_rewrite-client.gold" +tr.StillRunningAfter = server + +ts.Streams.All = "gold/header_rewrite-tag.gold" diff --git a/tests/gold_tests/pluginTest/header_rewrite/rules/rule_client.conf b/tests/gold_tests/pluginTest/header_rewrite/rules/rule_client.conf new file mode 100644 index 00000000000..a481775573e --- /dev/null +++ b/tests/gold_tests/pluginTest/header_rewrite/rules/rule_client.conf @@ -0,0 +1,22 @@ +# +# 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. + +cond %{CLIENT-URL:PATH} /^from_path/ +cond %{CLIENT-URL:SCHEME} =http +cond %{CLIENT-URL:HOST} =www.example.com +cond %{CLIENT-URL:QUERY} /foo=bar/ +set-status 304 \ No newline at end of file From 5b6b4427ffe31e6693050ff7b554e22f651c10dd Mon Sep 17 00:00:00 2001 From: Bryan Call Date: Tue, 28 Jul 2020 08:18:33 -0700 Subject: [PATCH 2/3] Added line for autest, worked on Fedora 32 before pushing --- .../pluginTest/header_rewrite/gold/header_rewrite-client.gold | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold index f7de90ac8f5..44a6a7a6e12 100644 --- a/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold +++ b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold @@ -8,5 +8,6 @@ < Date: `` < Proxy-Connection: keep-alive < Server: ATS/`` +< Cache-Control: no-store < `` From f3583cc7780890a649f2bb239d1687b56a40609c Mon Sep 17 00:00:00 2001 From: Bryan Call Date: Tue, 28 Jul 2020 12:17:19 -0700 Subject: [PATCH 3/3] Added a space in the gold file --- .../pluginTest/header_rewrite/gold/header_rewrite-client.gold | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold index 44a6a7a6e12..418608bcb57 100644 --- a/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold +++ b/tests/gold_tests/pluginTest/header_rewrite/gold/header_rewrite-client.gold @@ -9,5 +9,5 @@ < Proxy-Connection: keep-alive < Server: ATS/`` < Cache-Control: no-store -< +< ``