Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions plugins/header_rewrite/conditions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@

static const sockaddr *getClientAddr(TSHttpTxn txnp, int txn_private_slot);

static const char *
get_hook_name(TSHttpHookID hook)
{
if (hook == TS_REMAP_PSEUDO_HOOK) {
return "REMAP_PSEUDO_HOOK";
}

if (hook == TS_HTTP_LAST_HOOK) {
return "UNKNOWN_HOOK";
}

if (const char *name = TSHttpHookNameLookup(hook); name != nullptr) {
return name;
}

return "UNKNOWN_HOOK";
}

static const char *
get_url_type_name(ConditionUrl::UrlType type)
{
switch (type) {
case ConditionUrl::CLIENT:
return "CLIENT-URL";
case ConditionUrl::SERVER:
return "SERVER-URL";
case ConditionUrl::FROM:
return "FROM-URL";
case ConditionUrl::TO:
return "TO-URL";
case ConditionUrl::URL:
default:
return "URL";
}
}

#if TS_HAS_CRIPTS
#include "cripts/Certs.hpp"
#endif
Expand Down Expand Up @@ -318,6 +354,18 @@ ConditionUrl::set_qualifier(const std::string &q)
}
}

void
ConditionUrl::log_error(const Resources &res, const char *message) const
{
if (has_config_location()) {
TSError("[%s] %s at hook=%s: %%{%s%s%s} in %s:%d", PLUGIN_NAME, message, get_hook_name(res.hook), get_url_type_name(_type),
_qualifier.empty() ? "" : ":", _qualifier.c_str(), get_config_filename().c_str(), get_config_lineno());
} else {
TSError("[%s] %s at hook=%s: %%{%s%s%s}", PLUGIN_NAME, message, get_hook_name(res.hook), get_url_type_name(_type),
_qualifier.empty() ? "" : ":", _qualifier.c_str());
}
}

void
ConditionUrl::append_value(std::string &s, const Resources &res)
{
Expand All @@ -328,15 +376,15 @@ ConditionUrl::append_value(std::string &s, const Resources &res)
// CLIENT always uses the pristine URL
Dbg(pi_dbg_ctl, " Using the pristine url");
if (TSHttpTxnPristineUrlGet(res.state.txnp, &bufp, &url) != TS_SUCCESS) {
TSError("[%s] Error getting the pristine URL", PLUGIN_NAME);
log_error(res, "Error getting the pristine URL");
return;
}
} else if (_type == SERVER) {
Dbg(pi_dbg_ctl, " Using the server request url");
bufp = res.server_bufp;
if (bufp && res.server_hdr_loc) {
if (TSHttpHdrUrlGet(bufp, res.server_hdr_loc, &url) != TS_SUCCESS) {
TSError("[%s] Error getting the server request URL", PLUGIN_NAME);
log_error(res, "Error getting the server request URL");
return;
}
} else {
Expand All @@ -356,19 +404,19 @@ ConditionUrl::append_value(std::string &s, const Resources &res)
Dbg(pi_dbg_ctl, " Using the to url");
url = res._rri->mapToUrl;
} else {
TSError("[%s] Invalid option value", PLUGIN_NAME);
log_error(res, "Invalid URL option value");
return;
}
} else {
if (_type == URL) {
bufp = res.bufp;
TSMLoc hdr_loc = res.hdr_loc;
if (TSHttpHdrUrlGet(bufp, hdr_loc, &url) != TS_SUCCESS) {
TSError("[%s] Error getting the URL", PLUGIN_NAME);
log_error(res, "Error getting the URL");
return;
}
} else {
TSError("[%s] Rule not supported at this hook", PLUGIN_NAME);
log_error(res, "Rule not supported");
return;
}
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/header_rewrite/conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ class ConditionUrl : public Condition
bool eval(const Resources &res) override;

private:
void log_error(const Resources &res, const char *message) const;

UrlQualifiers _url_qual = URL_QUAL_NONE;
UrlType _type;
std::string _query_param; // Optional: specific query parameter name for QUERY sub-key
Expand Down
2 changes: 2 additions & 0 deletions plugins/header_rewrite/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,7 @@ OperatorIf::add_operator(Parser &p, const char *filename, int lineno)
}

Dbg(pi_dbg_ctl, " Adding operator: %s(%s)=\"%s\"", p.get_op().c_str(), p.get_arg().c_str(), p.get_value().c_str());
op->set_config_location(filename, lineno);

try {
op->initialize(p);
Expand Down Expand Up @@ -1745,6 +1746,7 @@ OperatorIf::make_condition(Parser &p, const char *filename, int lineno)
}

Dbg(pi_dbg_ctl, " Creating condition: %%{%s} with arg: %s", p.get_op().c_str(), p.get_arg().c_str());
cond->set_config_location(filename, lineno);

try {
cond->initialize(p);
Expand Down
1 change: 1 addition & 0 deletions plugins/header_rewrite/resources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
void
Resources::gather(const ResourceIDs ids, TSHttpHookID hook)
{
this->hook = hook;
Dbg(pi_dbg_ctl, "Building resources, hook=%s", TSHttpHookNameLookup(hook));
Dbg(pi_dbg_ctl, "Gathering resources for hook %s with IDs %d", TSHttpHookNameLookup(hook), ids);

Expand Down
1 change: 1 addition & 0 deletions plugins/header_rewrite/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Resources
#endif
TSHttpStatus resp_status = TS_HTTP_STATUS_NONE;
void *geo_handle = nullptr;
TSHttpHookID hook = TS_HTTP_LAST_HOOK;

struct LifetimeExtension {
std::string subject_storage;
Expand Down
2 changes: 2 additions & 0 deletions plugins/header_rewrite/ruleset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RuleSet::make_condition(Parser &p, const char *filename, int lineno)
}

Dbg(pi_dbg_ctl, " Creating condition: %%{%s} with arg: %s", p.get_op().c_str(), p.get_arg().c_str());
c->set_config_location(filename, lineno);
c->initialize(p);
if (!c->set_hook(_hook)) {
delete c;
Expand Down Expand Up @@ -92,6 +93,7 @@ RuleSet::add_operator(Parser &p, const char *filename, int lineno)

if (nullptr != op) {
Dbg(pi_dbg_ctl, " Adding operator: %s(%s)=\"%s\"", p.get_op().c_str(), p.get_arg().c_str(), p.get_value().c_str());
op->set_config_location(filename, lineno);
op->initialize(p);
if (!op->set_hook(_hook)) {
delete op;
Expand Down
29 changes: 28 additions & 1 deletion plugins/header_rewrite/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,31 @@ class Statement

ResourceIDs get_resource_ids() const;

void
set_config_location(const char *filename, int lineno)
{
_config_filename = filename ? filename : "";
_config_lineno = lineno;
}

bool
has_config_location() const
{
return !_config_filename.empty();
}

const std::string &
get_config_filename() const
{
return _config_filename;
}

int
get_config_lineno() const
{
return _config_lineno;
}

virtual void
initialize(Parser &)
{
Expand Down Expand Up @@ -269,7 +294,9 @@ class Statement
ResourceIDs _rsrc = RSRC_NONE;
TSHttpHookID _hook = TS_HTTP_READ_RESPONSE_HDR_HOOK;
std::vector<TSHttpHookID> _allowed_hooks;
bool _initialized = false;
std::string _config_filename;
int _config_lineno = 0;
bool _initialized = false;
};

union PrivateSlotData {
Expand Down
6 changes: 6 additions & 0 deletions plugins/header_rewrite/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void
Value::set_value(const std::string &val, Statement *owner)
{
_value = val;
if (owner && owner->has_config_location()) {
set_config_location(owner->get_config_filename().c_str(), owner->get_config_lineno());
}

if (_value.find("%{") != std::string::npos) {
HRWSimpleTokenizer tokenizer(_value);
Expand All @@ -54,6 +57,9 @@ Value::set_value(const std::string &val, Statement *owner)
Parser parser;

if (parser.parse_line(cond_token)) {
if (has_config_location()) {
tcond_val->set_config_location(get_config_filename().c_str(), get_config_lineno());
}
tcond_val->initialize(parser);
require_resources(tcond_val->get_resource_ids());
} else {
Expand Down