Skip to content

Commit 49f5205

Browse files
author
Walter Karas
committed
Allow for regex_remap of pristine URL.
Adds a new optional pparam "pristine" to the regex_remap core plugin. When present, this pparam causes regex_remap to match on the pristine URL, and to use the pristine URL for $ substitutions.
1 parent b21ecc8 commit 49f5205

3 files changed

Lines changed: 62 additions & 12 deletions

File tree

doc/admin-guide/plugins/regex_remap.en.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ profile dump, you can do ::
5454
$ sudo touch remap.config
5555
$ sudo traffic_ctl config reload
5656

57+
By default, this plugin operates on the post-remap URL (including any
58+
remappings done by preceding plugins in the remap rule). This behavior
59+
can be modified with the optional parameter ::
60+
61+
@pparam=[no-]pristine [default: off]
62+
63+
With ``@pparam=pristine``, the plugin will operate on the pre-remap, or
64+
pristine, URL. (But, if no regular expression in the config file is
65+
matched, the resulting URL will still be the post-remap URL.)
66+
5767
By default, only the path and query string of the URL are provided for
5868
the regular expressions to match. The following optional parameters can
5969
be used to modify the plugin instance behavior ::

plugins/regex_remap/regex_remap.cc

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ struct UrlComponents {
7878
UrlComponents() = default;
7979

8080
void
81-
populate(TSRemapRequestInfo *rri)
81+
populate(TSMBuffer bufp, TSMLoc url)
8282
{
83-
scheme = TSUrlSchemeGet(rri->requestBufp, rri->requestUrl, &scheme_len);
84-
host = TSUrlHostGet(rri->requestBufp, rri->requestUrl, &host_len);
85-
path = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &path_len);
86-
query = TSUrlHttpQueryGet(rri->requestBufp, rri->requestUrl, &query_len);
87-
matrix = TSUrlHttpParamsGet(rri->requestBufp, rri->requestUrl, &matrix_len);
88-
port = TSUrlPortGet(rri->requestBufp, rri->requestUrl);
83+
scheme = TSUrlSchemeGet(bufp, url, &scheme_len);
84+
host = TSUrlHostGet(bufp, url, &host_len);
85+
path = TSUrlPathGet(bufp, url, &path_len);
86+
query = TSUrlHttpQueryGet(bufp, url, &query_len);
87+
matrix = TSUrlHttpParamsGet(bufp, url, &matrix_len);
88+
port = TSUrlPortGet(bufp, url);
8989

9090
url_len = scheme_len + host_len + path_len + query_len + matrix_len + 32;
9191
}
@@ -626,6 +626,7 @@ struct RemapInstance {
626626

627627
RemapRegex *first = nullptr;
628628
RemapRegex *last = nullptr;
629+
bool pristine_url = false;
629630
bool profile = false;
630631
bool method = false;
631632
bool query_string = true;
@@ -726,6 +727,10 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSE
726727
ri->host = true;
727728
} else if (strncmp(argv[i], "no-host", 7) == 0) {
728729
ri->host = false;
730+
} else if (strcmp(argv[i], "pristine") == 0) {
731+
ri->pristine_url = true;
732+
} else if (strcmp(argv[i], "no-pristine") == 0) {
733+
ri->pristine_url = false;
729734
} else {
730735
TSError("[%s] invalid option '%s'", PLUGIN_NAME, argv[i]);
731736
}
@@ -913,12 +918,36 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
913918
TSDebug(PLUGIN_NAME, "Falling back to default URL on regex remap without rules");
914919
return TSREMAP_NO_REMAP;
915920
}
921+
RemapInstance *ri = static_cast<RemapInstance *>(ih);
922+
923+
struct SrcUrl {
924+
TSMBuffer bufp;
925+
TSMLoc loc;
926+
bool bad;
927+
};
928+
929+
const SrcUrl src_url([=]() -> SrcUrl {
930+
SrcUrl u;
931+
932+
if (ri->pristine_url) {
933+
u.bufp = rri->requestBufp;
934+
u.loc = rri->requestUrl;
935+
u.bad = false;
936+
937+
} else {
938+
u.bad = TSHttpTxnPristineUrlGet(txnp, &u.bufp, &u.loc) != TS_SUCCESS;
939+
}
940+
return u;
941+
}());
942+
943+
if (src_url.bad) {
944+
return TSREMAP_NO_REMAP;
945+
}
916946

917947
// Populate the request url
918948
UrlComponents req_url;
919-
req_url.populate(rri);
949+
req_url.populate(src_url.bufp, src_url.loc);
920950

921-
RemapInstance *ri = static_cast<RemapInstance *>(ih);
922951
int ovector[OVECCOUNT];
923952
int lengths[OVECCOUNT / 2 + 1];
924953
int dest_len;
@@ -1061,7 +1090,7 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
10611090
const char *start = dest;
10621091

10631092
// Setup the new URL
1064-
if (TS_PARSE_ERROR == TSUrlParse(rri->requestBufp, rri->requestUrl, &start, start + dest_len)) {
1093+
if (TS_PARSE_ERROR == TSUrlParse(src_url.bufp, src_url.loc, &start, start + dest_len)) {
10651094
TSHttpTxnStatusSet(txnp, TS_HTTP_STATUS_INTERNAL_SERVER_ERROR);
10661095
TSError("[%s] can't parse substituted URL string", PLUGIN_NAME);
10671096
}

tests/gold_tests/pluginTest/regex_remap/regex_remap.test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@
5353
regex_remap_conf_path = os.path.join(ts.Variables.CONFIGDIR, 'regex_remap.conf')
5454
curl_and_args = 'curl -s -D - -v --proxy localhost:{} '.format(ts.Variables.port)
5555

56-
path1_rule = 'path1 {}\n'.format(int(time.time()) + 600)
57-
5856
ts.Disk.File(regex_remap_conf_path, typename="ats:config").AddLines([
5957
"# regex_remap configuration\n"
6058
"^/alpha/bravo/[?]((?!action=(newsfeed|calendar|contacts|notepad)).)*$ http://example.one @status=301\n"
59+
"^/charlie http://example.one @status=301\n"
6160
])
6261

6362
ts.Disk.remap_config.AddLine(
6463
"map http://example.one/ http://localhost:{}/ @plugin=regex_remap.so @pparam=regex_remap.conf\n".format(server.Variables.Port)
6564
)
65+
ts.Disk.remap_config.AddLine(
66+
"map http://example.two/charlie http://localhost:{}/delta ".format(server.Variables.Port) +
67+
"@plugin=regex_remap.so @pparam=regex_remap.conf @pparam=pristine\n"
68+
)
6669

6770
# minimal configuration
6871
ts.Disk.records_config.update({
@@ -80,6 +83,14 @@
8083
tr.Processes.Default.Streams.stdout = "gold/regex_remap_smoke.gold"
8184
tr.StillRunningAfter = ts
8285

86+
tr = Test.AddTestRun("pristine test")
87+
tr.Processes.Default.Command = (
88+
curl_and_args + '--header "uuid: {}" http://example.two/charlie'.format(creq["headers"]["fields"][1][1])
89+
)
90+
tr.Processes.Default.ReturnCode = 0
91+
tr.Processes.Default.Streams.stdout = "gold/regex_remap_smoke.gold"
92+
tr.StillRunningAfter = ts
93+
8394
# Crash test.
8495
tr = Test.AddTestRun("crash test")
8596
creq = replay_txns[1]['client-request']

0 commit comments

Comments
 (0)