Skip to content

Commit 534475f

Browse files
authored
header_rewrite: Fix a bug that parameters on remap rules are overwritten by subsequent rules (#12530)
1 parent 1754d38 commit 534475f

1 file changed

Lines changed: 43 additions & 28 deletions

File tree

plugins/header_rewrite/header_rewrite.cc

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ namespace header_rewrite_ns
4242
{
4343
std::once_flag initHRWLibs;
4444
PluginFactory plugin_factory;
45-
46-
int timezone = 0;
47-
int inboundIpSource = 0;
48-
4945
} // namespace header_rewrite_ns
5046

5147
static void
@@ -74,7 +70,7 @@ static int cont_rewrite_headers(TSCont, TSEvent, void *);
7470
class RulesConfig
7571
{
7672
public:
77-
RulesConfig()
73+
RulesConfig(int timezone, int inboundIpSource) : _timezone(timezone), _inboundIpSource(inboundIpSource)
7874
{
7975
Dbg(dbg_ctl, "RulesConfig CTOR");
8076
_cont = TSContCreate(cont_rewrite_headers, nullptr);
@@ -105,6 +101,18 @@ class RulesConfig
105101
return _rules[hook].get();
106102
}
107103

104+
[[nodiscard]] int
105+
timezone() const
106+
{
107+
return _timezone;
108+
}
109+
110+
[[nodiscard]] int
111+
inboundIpSource() const
112+
{
113+
return _inboundIpSource;
114+
}
115+
108116
bool parse_config(const std::string &fname, TSHttpHookID default_hook, char *from_url = nullptr, char *to_url = nullptr);
109117

110118
private:
@@ -113,6 +121,9 @@ class RulesConfig
113121
TSCont _cont;
114122
std::array<std::unique_ptr<RuleSet>, TS_HTTP_LAST_HOOK + 1> _rules{};
115123
std::array<ResourceIDs, TS_HTTP_LAST_HOOK + 1> _resids{};
124+
125+
int _timezone = 0;
126+
int _inboundIpSource = 0;
116127
};
117128

118129
void
@@ -331,16 +342,16 @@ RulesConfig::parse_config(const std::string &fname, TSHttpHookID default_hook, c
331342
}
332343

333344
static void
334-
setPluginControlValues(TSHttpTxn txnp)
345+
setPluginControlValues(TSHttpTxn txnp, RulesConfig *conf)
335346
{
336-
if (header_rewrite_ns::timezone != 0 || header_rewrite_ns::inboundIpSource != 0) {
347+
if (conf->timezone() != 0 || conf->inboundIpSource() != 0) {
337348
ConditionNow temporal_statement; // This could be any statement that use the private slot.
338349
int slot = temporal_statement.get_txn_private_slot();
339350

340351
PrivateSlotData private_data;
341352
private_data.raw = reinterpret_cast<uint64_t>(TSUserArgGet(txnp, slot));
342-
private_data.timezone = header_rewrite_ns::timezone;
343-
private_data.ip_source = header_rewrite_ns::inboundIpSource;
353+
private_data.timezone = conf->timezone();
354+
private_data.ip_source = conf->inboundIpSource();
344355
TSUserArgSet(txnp, slot, reinterpret_cast<void *>(private_data.raw));
345356
}
346357
}
@@ -373,7 +384,7 @@ cont_rewrite_headers(TSCont contp, TSEvent event, void *edata)
373384
break;
374385
case TS_EVENT_HTTP_TXN_START:
375386
hook = TS_HTTP_TXN_START_HOOK;
376-
setPluginControlValues(txnp);
387+
setPluginControlValues(txnp, conf);
377388
break;
378389
case TS_EVENT_HTTP_TXN_CLOSE:
379390
hook = TS_HTTP_TXN_CLOSE_HOOK;
@@ -441,6 +452,8 @@ TSPluginInit(int argc, const char *argv[])
441452
}
442453

443454
std::string geoDBpath;
455+
int inboundIpSource = 0;
456+
int timezone = 0;
444457
while (true) {
445458
int opt = getopt_long(argc, const_cast<char *const *>(argv), "m:t:i:", longopt, nullptr);
446459

@@ -449,23 +462,23 @@ TSPluginInit(int argc, const char *argv[])
449462
geoDBpath = optarg;
450463
break;
451464
case 't':
452-
Dbg(pi_dbg_ctl, "Global timezone %s", optarg);
465+
Dbg(pi_dbg_ctl, "Default timezone %s", optarg);
453466
if (strcmp(optarg, "LOCAL") == 0) {
454-
header_rewrite_ns::timezone = TIMEZONE_LOCAL;
467+
timezone = TIMEZONE_LOCAL;
455468
} else if (strcmp(optarg, "GMT") == 0) {
456-
header_rewrite_ns::timezone = TIMEZONE_GMT;
469+
timezone = TIMEZONE_GMT;
457470
} else {
458471
TSError("[%s] Unknown value for timezone parameter: %s", PLUGIN_NAME, optarg);
459472
}
460473
break;
461474
case 'i':
462-
Dbg(pi_dbg_ctl, "Global inbound IP source %s", optarg);
475+
Dbg(pi_dbg_ctl, "Default inbound IP source %s", optarg);
463476
if (strcmp(optarg, "PEER") == 0) {
464-
header_rewrite_ns::inboundIpSource = IP_SRC_PEER;
477+
inboundIpSource = IP_SRC_PEER;
465478
} else if (strcmp(optarg, "PROXY") == 0) {
466-
header_rewrite_ns::inboundIpSource = IP_SRC_PROXY;
479+
inboundIpSource = IP_SRC_PROXY;
467480
} else if (strcmp(optarg, "PLUGIN") == 0) {
468-
header_rewrite_ns::inboundIpSource = IP_SRC_PLUGIN;
481+
inboundIpSource = IP_SRC_PLUGIN;
469482
} else {
470483
TSError("[%s] Unknown value for inbound-ip-source parameter: %s", PLUGIN_NAME, optarg);
471484
}
@@ -487,7 +500,7 @@ TSPluginInit(int argc, const char *argv[])
487500

488501
// Parse the global config file(s). All rules are just appended
489502
// to the "global" Rules configuration.
490-
auto *conf = new RulesConfig;
503+
auto *conf = new RulesConfig(timezone, inboundIpSource);
491504
bool got_config = false;
492505

493506
for (int i = optind; i < argc; ++i) {
@@ -553,6 +566,8 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSE
553566
++argv;
554567

555568
std::string geoDBpath;
569+
int timezone = 0;
570+
int inboundIpSource = 0;
556571
while (true) {
557572
int opt = getopt_long(argc, (char *const *)argv, "m:t:i:", longopt, nullptr);
558573

@@ -561,23 +576,23 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSE
561576
geoDBpath = optarg;
562577
break;
563578
case 't':
564-
Dbg(pi_dbg_ctl, "Global timezone %s", optarg);
579+
Dbg(pi_dbg_ctl, "Default timezone %s", optarg);
565580
if (strcmp(optarg, "LOCAL") == 0) {
566-
header_rewrite_ns::timezone = TIMEZONE_LOCAL;
581+
timezone = TIMEZONE_LOCAL;
567582
} else if (strcmp(optarg, "GMT") == 0) {
568-
header_rewrite_ns::timezone = TIMEZONE_GMT;
583+
timezone = TIMEZONE_GMT;
569584
} else {
570585
TSError("[%s] Unknown value for timezone parameter: %s", PLUGIN_NAME, optarg);
571586
}
572587
break;
573588
case 'i':
574-
Dbg(pi_dbg_ctl, "Global inbound IP source %s", optarg);
589+
Dbg(pi_dbg_ctl, "Default inbound IP source %s", optarg);
575590
if (strcmp(optarg, "PEER") == 0) {
576-
header_rewrite_ns::inboundIpSource = IP_SRC_PEER;
591+
inboundIpSource = IP_SRC_PEER;
577592
} else if (strcmp(optarg, "PROXY") == 0) {
578-
header_rewrite_ns::inboundIpSource = IP_SRC_PROXY;
593+
inboundIpSource = IP_SRC_PROXY;
579594
} else if (strcmp(optarg, "PLUGIN") == 0) {
580-
header_rewrite_ns::inboundIpSource = IP_SRC_PLUGIN;
595+
inboundIpSource = IP_SRC_PLUGIN;
581596
} else {
582597
TSError("[%s] Unknown value for inbound-ip-source parameter: %s", PLUGIN_NAME, optarg);
583598
}
@@ -598,7 +613,7 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSE
598613
std::call_once(initHRWLibs, [&geoDBpath]() { initHRWLibraries(geoDBpath); });
599614
}
600615

601-
auto *conf = new RulesConfig;
616+
auto *conf = new RulesConfig(timezone, inboundIpSource);
602617

603618
for (int i = optind; i < argc; ++i) {
604619
Dbg(pi_dbg_ctl, "Loading remap configuration file %s", argv[i]);
@@ -635,11 +650,11 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
635650
return TSREMAP_NO_REMAP;
636651
}
637652

638-
setPluginControlValues(rh);
639-
640653
TSRemapStatus rval = TSREMAP_NO_REMAP;
641654
auto *conf = static_cast<RulesConfig *>(ih);
642655

656+
setPluginControlValues(rh, conf);
657+
643658
// Go through all hooks we support, and setup the txn hook(s) as necessary
644659
for (int i = TS_HTTP_READ_REQUEST_HDR_HOOK; i < TS_HTTP_LAST_HOOK; ++i) {
645660
if (conf->rule(i)) {

0 commit comments

Comments
 (0)