diff --git a/headers/modsecurity/assay.h b/headers/modsecurity/assay.h index fbe22176..38e621cf 100644 --- a/headers/modsecurity/assay.h +++ b/headers/modsecurity/assay.h @@ -39,6 +39,8 @@ typedef struct Rules_t Rules; #endif #include "modsecurity/intervention.h" +#include "modsecurity/transaction/variable.h" +#include "modsecurity/transaction/variables.h" #define LOGFY_ADD(a, b) \ yajl_gen_string(g, reinterpret_cast(a), strlen(a)); \ @@ -73,95 +75,6 @@ namespace operators { class Operator; } -namespace transaction { - -class Variable { - public: - Variable(const std::string& key, const std::string& value) : - m_key(key), - m_value(value) { } - std::string m_key; - std::string m_value; -}; - - -class Variables : - public std::unordered_multimap { - public: - Variables() { - this->reserve(1000); - } - - void storeVariable(std::string key, std::string value) { - this->emplace(key, value); - } - - bool storeOrUpdateVariable(const std::string &key, - const std::string &value) { - if (updateFirstVariable(key, value) == false) { - storeVariable(key, value); - } - return true; - } - - - bool updateFirstVariable(const std::string &key, const std::string &value) { - auto range = this->equal_range(key); - - for (auto it = range.first; it != range.second; ++it) { - it->second = value; - return true; - } - return false; - } - - - void deleteVariable(const std::string& key) { - this->erase(key); - } - - std::list - resolveVariable(const std::string& key, - std::list *l) { - auto range = this->equal_range(key); - - for (auto it = range.first; it != range.second; ++it) { - l->push_back(new transaction::Variable(key, it->second)); - } - - if (key.find(":") == std::string::npos && l->size() == 0) { - size_t keySize = key.size() + 1; - for (auto& x : *this) { - if (x.first.size() <= keySize) { - continue; - } - if (x.first.at(keySize - 1) != ':') { - continue; - } - if (x.first.compare(0, keySize, key + ":") != 0) { - continue; - } - // auto range = this->equal_range(x.first); - - // for (auto it = range.first; it != range.second; ++it) { - l->push_back(new transaction::Variable(x.first, x.second)); - // } - } - } - - return *l; - } - - std::list - resolveVariable(const std::string& key) { - std::list l; - - return resolveVariable(key, &l); - } -}; - - -} // name space Transaction /** @ingroup ModSecurity_CPP_API */ class Assay { @@ -248,7 +161,7 @@ class Assay { bool update_variable_first(std::string var, const std::string &value); void delete_variable(std::string key); - transaction::Variables m_variables_strings; + transaction::Variables m_variables; std::unordered_map collections; #ifndef NO_LOGS void debug(int, std::string); diff --git a/headers/modsecurity/transaction/variable.h b/headers/modsecurity/transaction/variable.h new file mode 100644 index 00000000..7e55860f --- /dev/null +++ b/headers/modsecurity/transaction/variable.h @@ -0,0 +1,47 @@ +/* + * ModSecurity, http://www.modsecurity.org/ + * Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/) + * + * 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 + * + * If any of the files related to licensing are missing or if you have any + * other questions related to licensing please contact Trustwave Holdings, Inc. + * directly using the email address security@modsecurity.org. + * + */ + + +#ifdef __cplusplus +#include +#endif + + +#ifndef HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_ +#define HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_ + + +#ifndef __cplusplus +typedef struct Variable_t Variable; +#endif + +#ifdef __cplusplus +namespace ModSecurity { +namespace transaction { + +class Variable { + public: + Variable(const std::string& key, const std::string& value) : + m_key(key), + m_value(value) { } + std::string m_key; + std::string m_value; +}; + +} // namespace transaction +} // namespace ModSecurity +#endif + +#endif // HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_ diff --git a/headers/modsecurity/transaction/variables.h b/headers/modsecurity/transaction/variables.h new file mode 100644 index 00000000..9e95594b --- /dev/null +++ b/headers/modsecurity/transaction/variables.h @@ -0,0 +1,123 @@ +/* + * ModSecurity, http://www.modsecurity.org/ + * Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/) + * + * 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 + * + * If any of the files related to licensing are missing or if you have any + * other questions related to licensing please contact Trustwave Holdings, Inc. + * directly using the email address security@modsecurity.org. + * + */ + + +#ifdef __cplusplus +#include +#include +#include +#include +#endif + + +#include "modsecurity/transaction/variable.h" + + +#ifndef HEADERS_MODSECURITY_TRANSACTION_VARIABLES_H_ +#define HEADERS_MODSECURITY_TRANSACTION_VARIABLES_H_ + +#ifndef __cplusplus +typedef struct Variable_t Variables; +#endif + +#ifdef __cplusplus +namespace ModSecurity { +namespace transaction { + +class Variables : + public std::unordered_multimap { + public: + Variables() { + this->reserve(1000); + } + + + void storeVariable(std::string key, std::string value) { + this->emplace(key, value); + } + + + bool storeOrUpdateVariable(const std::string &key, + const std::string &value) { + if (updateFirstVariable(key, value) == false) { + storeVariable(key, value); + } + return true; + } + + + bool updateFirstVariable(const std::string &key, const std::string &value) { + auto range = this->equal_range(key); + + for (auto it = range.first; it != range.second; ++it) { + it->second = value; + return true; + } + return false; + } + + + void deleteVariable(const std::string& key) { + this->erase(key); + } + + + std::list + resolveVariable(const std::string& key, + std::list *l) { + auto range = this->equal_range(key); + + for (auto it = range.first; it != range.second; ++it) { + l->push_back(new transaction::Variable(key, it->second)); + } + + if (key.find(":") == std::string::npos && l->size() == 0) { + size_t keySize = key.size() + 1; + for (auto& x : *this) { + if (x.first.size() <= keySize) { + continue; + } + if (x.first.at(keySize - 1) != ':') { + continue; + } + if (x.first.compare(0, keySize, key + ":") != 0) { + continue; + } + // auto range = this->equal_range(x.first); + + // for (auto it = range.first; it != range.second; ++it) { + l->push_back(new transaction::Variable(x.first, x.second)); + // } + } + } + + return *l; + } + + + std::list + resolveVariable(const std::string& key) { + std::list l; + + return resolveVariable(key, &l); + } +}; + +} // namespace transaction +} // namespace ModSecurity +#endif + + +#endif // HEADERS_MODSECURITY_TRANSACTION_VARIABLES_H_ diff --git a/src/assay.cc b/src/assay.cc index c54b3789..360497b1 100644 --- a/src/assay.cc +++ b/src/assay.cc @@ -1243,7 +1243,7 @@ std::string Assay::toOldAuditLogFormat(int parts, const std::string &trailer) { audit_log << this->m_protocol << " " << this->m_uri << " " << "HTTP/"; audit_log << this->m_httpVersion << std::endl; - for (auto h : this->m_variables_strings) { + for (auto h : m_variables) { std::string filter = "REQUEST_HEADERS:"; std::string a = h.first; std::string b = h.second; @@ -1270,7 +1270,7 @@ std::string Assay::toOldAuditLogFormat(int parts, const std::string &trailer) { } if (parts & AuditLog::FAuditLogPart) { audit_log << "--" << trailer << "-" << "F--" << std::endl; - for (auto h : this->m_variables_strings) { + for (auto h : m_variables) { std::string filter = "RESPONSE_HEADERS:"; std::string a = h.first; std::string b = h.second; @@ -1359,7 +1359,7 @@ std::string Assay::to_json(int parts) { strlen("headers")); yajl_gen_map_open(g); - for (auto h : this->m_variables_strings) { + for (auto h : m_variables) { std::string filter = "REQUEST_HEADERS:"; std::string a = h.first; std::string b = h.second; @@ -1394,7 +1394,7 @@ std::string Assay::to_json(int parts) { strlen("headers")); yajl_gen_map_open(g); - for (auto h : this->m_variables_strings) { + for (auto h : m_variables) { std::string filter = "RESPONSE_HEADERS:"; std::string a = h.first; std::string b = h.second; @@ -1460,11 +1460,11 @@ std::string Assay::to_json(int parts) { void Assay::store_variable(std::string key, std::string value) { - this->m_variables_strings.emplace(key, value); + m_variables.emplace(key, value); } bool Assay::update_variable_first(std::string var, const std::string &value) { - auto range = m_variables_strings.equal_range(var); + auto range = m_variables.equal_range(var); for (auto it = range.first; it != range.second; ++it) { it->second = value; @@ -1475,14 +1475,14 @@ bool Assay::update_variable_first(std::string var, const std::string &value) { } void Assay::delete_variable(std::string key) { - this->m_variables_strings.erase(key); + m_variables.erase(key); } void Assay::resolve_variable(const std::string& var, std::list *l) { - m_variables_strings.resolveVariable(var, l); + m_variables.resolveVariable(var, l); /* It may be a collection */ for (auto &a : collections) { @@ -1507,7 +1507,7 @@ void Assay::serverLog(const std::string& msg) { std::string* Assay::resolve_variable_first(const std::string& var) { - auto range = m_variables_strings.equal_range(var); + auto range = m_variables.equal_range(var); for (auto it = range.first; it != range.second; ++it) { return &it->second;