mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-30 03:34:29 +03:00
Collection class was changed to be a simple interface
InMomoryPerProcess class was added to be used where the old Collection was used.
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "modsecurity/collection/collection.h"
|
||||
#include "src/collection/backend/in_memory-per_process.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <string>
|
||||
@@ -28,22 +28,23 @@
|
||||
|
||||
namespace modsecurity {
|
||||
namespace collection {
|
||||
namespace backend {
|
||||
|
||||
|
||||
Collection::Collection() {
|
||||
InMemoryPerProcess::InMemoryPerProcess() {
|
||||
this->reserve(1000);
|
||||
}
|
||||
|
||||
Collection::~Collection() {
|
||||
InMemoryPerProcess::~InMemoryPerProcess() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void Collection::store(std::string key, std::string value) {
|
||||
void InMemoryPerProcess::store(std::string key, std::string value) {
|
||||
this->emplace(key, value);
|
||||
}
|
||||
|
||||
|
||||
bool Collection::storeOrUpdateFirst(const std::string &key,
|
||||
bool InMemoryPerProcess::storeOrUpdateFirst(const std::string &key,
|
||||
const std::string &value) {
|
||||
if (updateFirst(key, value) == false) {
|
||||
store(key, value);
|
||||
@@ -52,7 +53,7 @@ bool Collection::storeOrUpdateFirst(const std::string &key,
|
||||
}
|
||||
|
||||
|
||||
bool Collection::updateFirst(const std::string &key, const std::string &value) {
|
||||
bool InMemoryPerProcess::updateFirst(const std::string &key, const std::string &value) {
|
||||
auto range = this->equal_range(key);
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
@@ -63,12 +64,12 @@ bool Collection::updateFirst(const std::string &key, const std::string &value) {
|
||||
}
|
||||
|
||||
|
||||
void Collection::del(const std::string& key) {
|
||||
void InMemoryPerProcess::del(const std::string& key) {
|
||||
this->erase(key);
|
||||
}
|
||||
|
||||
|
||||
void Collection::resolveSingleMatch(const std::string& var,
|
||||
void InMemoryPerProcess::resolveSingleMatch(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
auto range = this->equal_range(var);
|
||||
|
||||
@@ -78,7 +79,7 @@ void Collection::resolveSingleMatch(const std::string& var,
|
||||
}
|
||||
|
||||
|
||||
void Collection::resolveMultiMatches(const std::string& var,
|
||||
void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
size_t keySize = var.size();
|
||||
l->reserve(15);
|
||||
@@ -104,13 +105,13 @@ void Collection::resolveMultiMatches(const std::string& var,
|
||||
}
|
||||
|
||||
|
||||
void Collection::resolveRegularExpression(const std::string& var,
|
||||
void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
/* Not ready */
|
||||
}
|
||||
|
||||
|
||||
std::string* Collection::resolveFirst(const std::string& var) {
|
||||
std::string* InMemoryPerProcess::resolveFirst(const std::string& var) {
|
||||
auto range = equal_range(var);
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
@@ -120,6 +121,6 @@ std::string* Collection::resolveFirst(const std::string& var) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
} // namespace backend
|
||||
} // namespace collection
|
||||
} // namespace modsecurity
|
103
src/collection/backend/in_memory-per_process.h
Normal file
103
src/collection/backend/in_memory-per_process.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#endif
|
||||
|
||||
|
||||
#include "modsecurity/collection/variable.h"
|
||||
#include "modsecurity/collection/collection.h"
|
||||
|
||||
#ifndef HEADERS_MODSECURITY_COLLECTION_BACKEND_IN_MEMORY_PER_PROCESS_H_
|
||||
#define HEADERS_MODSECURITY_COLLECTION_BACKEND_IN_MEMORY_PER_PROCESS_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
namespace modsecurity {
|
||||
namespace collection {
|
||||
namespace backend {
|
||||
|
||||
/*
|
||||
* FIXME:
|
||||
*
|
||||
* This was an example grabbed from:
|
||||
* http://stackoverflow.com/questions/8627698/case-insensitive-stl-containers-e-g-stdunordered-set
|
||||
*
|
||||
* We have to have a better hash function, maybe based on the std::hash.
|
||||
*
|
||||
*/
|
||||
struct MyEqual {
|
||||
bool operator()(const std::string& Left, const std::string& Right) const {
|
||||
/*
|
||||
return Left.size() == Right.size()
|
||||
&& std::equal(Left.begin(), Left.end(), Right.begin(),
|
||||
[](char a, char b) {
|
||||
return tolower(a) == tolower(b);
|
||||
});
|
||||
*/
|
||||
return Left == Right;
|
||||
}
|
||||
};
|
||||
|
||||
struct MyHash{
|
||||
size_t operator()(const std::string& Keyval) const {
|
||||
// You might need a better hash function than this
|
||||
size_t h = 0;
|
||||
std::for_each(Keyval.begin(), Keyval.end(), [&](char c) {
|
||||
h += c;
|
||||
});
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
class InMemoryPerProcess :
|
||||
public std::unordered_multimap<std::string, std::string,
|
||||
/*std::hash<std::string>*/MyHash, MyEqual>,
|
||||
public Collection {
|
||||
public:
|
||||
InMemoryPerProcess();
|
||||
~InMemoryPerProcess();
|
||||
void store(std::string key, std::string value);
|
||||
|
||||
bool storeOrUpdateFirst(const std::string &key,
|
||||
const std::string &value);
|
||||
|
||||
bool updateFirst(const std::string &key, const std::string &value);
|
||||
|
||||
void del(const std::string& key);
|
||||
|
||||
std::string* resolveFirst(const std::string& var);
|
||||
|
||||
void resolveSingleMatch(const std::string& var,
|
||||
std::vector<const Variable *> *l);
|
||||
void resolveMultiMatches(const std::string& var,
|
||||
std::vector<const Variable *> *l);
|
||||
void resolveRegularExpression(const std::string& var,
|
||||
std::vector<const Variable *> *l);
|
||||
};
|
||||
|
||||
} // namespace backend
|
||||
} // namespace collection
|
||||
} // namespace modsecurity
|
||||
#endif
|
||||
|
||||
|
||||
#endif // HEADERS_MODSECURITY_COLLECTION_BACKEND_IN_MEMORY_PER_PROCESS_H_
|
@@ -26,6 +26,7 @@
|
||||
|
||||
#include "modsecurity/collection/variable.h"
|
||||
#include "modsecurity/collection/collection.h"
|
||||
#include "src/collection/backend/in_memory-per_process.h"
|
||||
#include "src/utils.h"
|
||||
|
||||
namespace modsecurity {
|
||||
@@ -37,9 +38,10 @@ Collections::Collections(GlobalCollection *global,
|
||||
: m_global_collection_key(""),
|
||||
m_ip_collection_key(""),
|
||||
m_global_collection(global),
|
||||
m_ip_collection(ip) {
|
||||
m_ip_collection(ip),
|
||||
m_transient(new backend::InMemoryPerProcess()) {
|
||||
/* Create collection TX */
|
||||
this->emplace("TX", new Collection());
|
||||
this->emplace("TX", new backend::InMemoryPerProcess());
|
||||
}
|
||||
|
||||
|
||||
@@ -82,38 +84,38 @@ void Collections::storeOrUpdateFirst(const std::string& collectionName,
|
||||
|
||||
|
||||
void Collections::store(std::string key, std::string value) {
|
||||
m_transient.store(key, value);
|
||||
m_transient->store(key, value);
|
||||
}
|
||||
|
||||
|
||||
bool Collections::storeOrUpdateFirst(const std::string &key,
|
||||
const std::string &value) {
|
||||
return m_transient.storeOrUpdateFirst(key, value);
|
||||
return m_transient->storeOrUpdateFirst(key, value);
|
||||
}
|
||||
|
||||
|
||||
bool Collections::updateFirst(const std::string &key,
|
||||
const std::string &value) {
|
||||
return m_transient.updateFirst(key, value);
|
||||
return m_transient->updateFirst(key, value);
|
||||
}
|
||||
|
||||
|
||||
void Collections::del(const std::string& key) {
|
||||
return m_transient.del(key);
|
||||
return m_transient->del(key);
|
||||
}
|
||||
|
||||
|
||||
std::string* Collections::resolveFirst(const std::string& var) {
|
||||
std::string *transientVar = m_transient.resolveFirst(var);
|
||||
std::string *transientVar = m_transient->resolveFirst(var);
|
||||
|
||||
if (transientVar != NULL) {
|
||||
return transientVar;
|
||||
}
|
||||
|
||||
for (auto &a : *this) {
|
||||
auto range = a.second->equal_range(var);
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
return & it->second;
|
||||
std::string *res = a.second->resolveFirst(toupper(a.first) + ":" + var);
|
||||
if (res != NULL) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,11 +139,9 @@ std::string* Collections::resolveFirst(const std::string& collectionName,
|
||||
|
||||
for (auto &a : *this) {
|
||||
if (tolower(a.first) == tolower(collectionName)) {
|
||||
Collection *t = a.second;
|
||||
auto range = t->equal_range(toupper(collectionName)
|
||||
+ ":" + var);
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
return &it->second;
|
||||
std::string *res = a.second->resolveFirst(toupper(a.first) + ":" + var);
|
||||
if (res != NULL) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,7 +153,7 @@ std::string* Collections::resolveFirst(const std::string& collectionName,
|
||||
void Collections::resolveSingleMatch(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
|
||||
m_transient.resolveSingleMatch(var, l);
|
||||
m_transient->resolveSingleMatch(var, l);
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ void Collections::resolveSingleMatch(const std::string& var,
|
||||
void Collections::resolveMultiMatches(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
|
||||
m_transient.resolveMultiMatches(var, l);
|
||||
m_transient->resolveMultiMatches(var, l);
|
||||
}
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ void Collections::resolveMultiMatches(const std::string& var,
|
||||
|
||||
void Collections::resolveRegularExpression(const std::string& var,
|
||||
std::vector<const Variable *> *l) {
|
||||
m_transient.resolveRegularExpression(var, l);
|
||||
m_transient->resolveRegularExpression(var, l);
|
||||
}
|
||||
|
||||
|
||||
|
128
src/collection/global_collections.cc
Normal file
128
src/collection/global_collections.cc
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "modsecurity/collection/global_collection.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#endif
|
||||
|
||||
#include "src/utils.h"
|
||||
|
||||
namespace modsecurity {
|
||||
namespace collection {
|
||||
|
||||
|
||||
GlobalCollection::GlobalCollection() {
|
||||
this->reserve(1000);
|
||||
}
|
||||
|
||||
GlobalCollection::~GlobalCollection() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void GlobalCollection::store(std::string key, std::string compartment,
|
||||
std::string value) {
|
||||
this->emplace(new CollectionKey(key, compartment), value);
|
||||
}
|
||||
|
||||
|
||||
bool GlobalCollection::storeOrUpdateFirst(const std::string &key,
|
||||
std::string compartment, const std::string &value) {
|
||||
if (updateFirst(key, compartment, value) == false) {
|
||||
store(key, compartment, value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool GlobalCollection::updateFirst(const std::string &key,
|
||||
std::string compartment, const std::string &value) {
|
||||
auto range = this->equal_range(new CollectionKey(key, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
it->second = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void GlobalCollection::del(const std::string& key, std::string compartment) {
|
||||
this->erase(new CollectionKey(key, compartment));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GlobalCollection::resolveSingleMatch(const std::string& var,
|
||||
std::string compartment, std::vector<const Variable *> *l) {
|
||||
auto range = this->equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
l->push_back(new Variable(var, it->second));
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalCollection::resolveMultiMatches(const std::string& var,
|
||||
std::string compartment, std::vector<const Variable *> *l) {
|
||||
size_t keySize = var.size();
|
||||
l->reserve(15);
|
||||
|
||||
auto range = this->equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
l->insert(l->begin(), new Variable(var, it->second));
|
||||
}
|
||||
|
||||
for (const auto& x : *this) {
|
||||
if (x.first->m_name.size() <= keySize + 1) {
|
||||
continue;
|
||||
}
|
||||
if (x.first->m_name.at(keySize) != ':') {
|
||||
continue;
|
||||
}
|
||||
if (x.first->m_name.compare(0, keySize, var) != 0) {
|
||||
continue;
|
||||
}
|
||||
l->insert(l->begin(),
|
||||
new Variable(x.first->m_name, x.second));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GlobalCollection::resolveRegularExpression(const std::string& var,
|
||||
std::string compartment, std::vector<const Variable *> *l) {
|
||||
/* Not ready */
|
||||
}
|
||||
|
||||
|
||||
std::string* GlobalCollection::resolveFirst(const std::string& var,
|
||||
std::string compartment) {
|
||||
auto range = equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
} // namespace collection
|
||||
} // namespace modsecurity
|
Reference in New Issue
Block a user