Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction pull style API to retrieve variable value #2620

Open
wants to merge 3 commits into
base: v3/master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions headers/modsecurity/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ int msc_process_logging(Transaction *transaction);
/** @ingroup ModSecurity_C_API */
int msc_update_status_code(Transaction *transaction, int status);

/** @ingroup ModSecurity_C_API */
const char *msc_get_transaction_variable(Transaction *transaction, const char *var_name);

#ifdef __cplusplus
}
} // namespace modsecurity
Expand Down
60 changes: 60 additions & 0 deletions src/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2356,6 +2356,66 @@ extern "C" int msc_update_status_code(Transaction *transaction, int status) {
return transaction->updateStatusCode(status);
}

/**
* @name msc_get_transaction_variable
* @brief Retrieve variable value by name.
*
* This function returns tx_collection variable value by name.
*
* @param transaction ModSecurity transaction.
* @param var_name variable name
* @return variable value.
*
*/
extern "C" const char *msc_get_transaction_variable(Transaction *transaction, const char *var_name){
std::vector<const VariableValue *> l;
modsecurity::collection::Collection *c = NULL;
std::string cn;
const char *p = NULL;

if (var_name == NULL ){
return NULL;
}

p = strchr(var_name,':');

if ( p == NULL ){
return NULL;
}

cn = std::string(var_name,p-var_name);
transform(cn.begin(),cn.end(),cn.begin(),::tolower);

if ( cn.compare("global") == 0){
c = transaction->m_collections.m_global_collection;
}else if ( cn.compare("ip") == 0){
c = transaction->m_collections.m_ip_collection;
}else if ( cn.compare("session") == 0){
c = transaction->m_collections.m_session_collection;
}else if ( cn.compare("user") == 0){
c = transaction->m_collections.m_user_collection;
}else if ( cn.compare("resource") == 0){
c = transaction->m_collections.m_resource_collection;
}else if( cn.compare("tx") == 0){
c = transaction->m_collections.m_tx_collection;
}

if(c == NULL){
return NULL;
}

++p;
cn = std::string(p);
transform(cn.begin(),cn.end(),cn.begin(),::tolower);

c->resolveSingleMatch(cn.c_str(),&l);

if ( l.size() < 1){
return NULL;
}

return l[0]->getValue().c_str();
}

} // namespace modsecurity