Refactoring: Move Variables and Variable to independent files

This commit is contained in:
Felipe Zimmerle 2015-10-28 14:29:32 -03:00
parent 776502e021
commit 6f617e6ca8
4 changed files with 182 additions and 99 deletions

View File

@ -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<const unsigned char*>(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<std::string, std::string> {
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<Variable *>
resolveVariable(const std::string& key,
std::list<Variable *> *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<Variable *>
resolveVariable(const std::string& key) {
std::list<Variable *> 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<std::string, transaction::Variables *> collections;
#ifndef NO_LOGS
void debug(int, std::string);

View File

@ -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 <string>
#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_

View File

@ -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 <string>
#include <iostream>
#include <unordered_map>
#include <list>
#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<std::string, std::string> {
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<Variable *>
resolveVariable(const std::string& key,
std::list<Variable *> *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<Variable *>
resolveVariable(const std::string& key) {
std::list<Variable *> l;
return resolveVariable(key, &l);
}
};
} // namespace transaction
} // namespace ModSecurity
#endif
#endif // HEADERS_MODSECURITY_TRANSACTION_VARIABLES_H_

View File

@ -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<transaction::Variable *> *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;