mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-30 11:44:32 +03:00
Testing performance enhancements by enabling transformations cache
Also reduce the utilization of dynamic cast.
This commit is contained in:
@@ -36,6 +36,7 @@ class Action {
|
||||
: action_kind(2),
|
||||
action(_action),
|
||||
name(_action),
|
||||
m_isNone(false),
|
||||
temporaryAction(false) {
|
||||
name.erase(0, 2);
|
||||
}
|
||||
@@ -43,6 +44,7 @@ class Action {
|
||||
: action_kind(kind),
|
||||
action(_action),
|
||||
name(_action),
|
||||
m_isNone(false),
|
||||
temporaryAction(false) {
|
||||
name.erase(0, 2);
|
||||
}
|
||||
@@ -94,6 +96,7 @@ class Action {
|
||||
|
||||
virtual void fill_intervention(ModSecurityIntervention *intervention);
|
||||
bool temporaryAction;
|
||||
bool m_isNone;
|
||||
};
|
||||
|
||||
|
||||
|
@@ -36,12 +36,19 @@ namespace transformations {
|
||||
|
||||
std::string HtmlEntityDecode::evaluate(std::string value,
|
||||
Assay *assay) {
|
||||
|
||||
if (HtmlEntityDecodeInstantCache::getInstance().count(value) > 0) {
|
||||
return HtmlEntityDecodeInstantCache::getInstance().at(value);
|
||||
}
|
||||
|
||||
char *tmp = strdup(value.c_str());
|
||||
html_entities_decode_inplace((unsigned char *)tmp, value.size());
|
||||
std::string ret("");
|
||||
ret.assign(tmp);
|
||||
free(tmp);
|
||||
|
||||
HtmlEntityDecodeInstantCache::getInstance().emplace(value, ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "actions/action.h"
|
||||
#include "actions/transformations/transformation.h"
|
||||
@@ -29,6 +30,18 @@ namespace actions {
|
||||
namespace transformations {
|
||||
|
||||
|
||||
class HtmlEntityDecodeInstantCache : public std::unordered_map<std::string, std::string> {
|
||||
public:
|
||||
static HtmlEntityDecodeInstantCache& getInstance() {
|
||||
static HtmlEntityDecodeInstantCache instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
HtmlEntityDecodeInstantCache() {};
|
||||
};
|
||||
|
||||
|
||||
class HtmlEntityDecode : public Transformation {
|
||||
public:
|
||||
explicit HtmlEntityDecode(std::string action)
|
||||
|
@@ -35,10 +35,18 @@ std::string LowerCase::evaluate(std::string value,
|
||||
Assay *assay) {
|
||||
std::locale loc;
|
||||
|
||||
if (LowerCaseInstantCache::getInstance().count(value) > 0) {
|
||||
return LowerCaseInstantCache::getInstance().at(value);
|
||||
}
|
||||
|
||||
std::string orig_value = value;
|
||||
|
||||
for (std::string::size_type i=0; i < value.length(); ++i) {
|
||||
value[i] = std::tolower(value[i], loc);
|
||||
}
|
||||
|
||||
LowerCaseInstantCache::getInstance().emplace(orig_value, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "actions/action.h"
|
||||
#include "actions/transformations/transformation.h"
|
||||
@@ -28,6 +29,17 @@ class Assay;
|
||||
namespace actions {
|
||||
namespace transformations {
|
||||
|
||||
class LowerCaseInstantCache : public std::unordered_map<std::string, std::string> {
|
||||
public:
|
||||
static LowerCaseInstantCache& getInstance() {
|
||||
static LowerCaseInstantCache instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
LowerCaseInstantCache() {};
|
||||
};
|
||||
|
||||
|
||||
class LowerCase : public Transformation {
|
||||
public:
|
||||
|
@@ -31,7 +31,8 @@ namespace transformations {
|
||||
class None : public Transformation {
|
||||
public:
|
||||
explicit None(std::string action)
|
||||
: Transformation(action) { }
|
||||
: Transformation(action)
|
||||
{ m_isNone = true; }
|
||||
|
||||
std::string evaluate(std::string exp,
|
||||
Assay *assay) override;
|
||||
|
@@ -100,6 +100,10 @@ std::string UrlDecode::evaluate(std::string value,
|
||||
int invalid_count;
|
||||
int changed;
|
||||
|
||||
if (UrlDecodeInstantCache::getInstance().count(value) > 0) {
|
||||
return UrlDecodeInstantCache::getInstance().at(value);
|
||||
}
|
||||
|
||||
val = (unsigned char *) malloc(sizeof(char) * value.size() + 1);
|
||||
memcpy(val, value.c_str(), value.size() + 1);
|
||||
val[value.size()] = '\0';
|
||||
@@ -112,6 +116,8 @@ std::string UrlDecode::evaluate(std::string value,
|
||||
|
||||
free(val);
|
||||
|
||||
UrlDecodeInstantCache::getInstance().emplace(value, out);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "actions/action.h"
|
||||
#include "actions/transformations/transformation.h"
|
||||
@@ -28,6 +29,18 @@ class Assay;
|
||||
namespace actions {
|
||||
namespace transformations {
|
||||
|
||||
class UrlDecodeInstantCache : public std::unordered_map<std::string, std::string> {
|
||||
public:
|
||||
static UrlDecodeInstantCache& getInstance() {
|
||||
static UrlDecodeInstantCache instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
UrlDecodeInstantCache() {};
|
||||
};
|
||||
|
||||
|
||||
class UrlDecode : public Transformation {
|
||||
public:
|
||||
explicit UrlDecode(std::string action);
|
||||
|
Reference in New Issue
Block a user