Skip to content

Commit 64c4f23

Browse files
committed
Collection class was changed to be a simple interface
InMomoryPerProcess class was added to be used where the old Collection was used.
1 parent bc887cd commit 64c4f23

File tree

12 files changed

+984
-72
lines changed

12 files changed

+984
-72
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
17+
#ifdef __cplusplus
18+
#include <string>
19+
#include <iostream>
20+
#include <unordered_map>
21+
#include <list>
22+
#include <vector>
23+
#include <algorithm>
24+
#endif
25+
26+
27+
#include "modsecurity/collection/variable.h"
28+
29+
#ifndef HEADERS_MODSECURITY_TRANSACTION_COLLECTION_H_
30+
#define HEADERS_MODSECURITY_TRANSACTION_COLLECTION_H_
31+
32+
#ifndef __cplusplus
33+
typedef struct Variable_t Variables;
34+
#endif
35+
36+
#ifdef __cplusplus
37+
namespace modsecurity {
38+
namespace collection {
39+
40+
class Collection {
41+
public:
42+
virtual void store(std::string key, std::string value) = 0;
43+
44+
virtual bool storeOrUpdateFirst(const std::string &key,
45+
const std::string &value) = 0;
46+
47+
virtual bool updateFirst(const std::string &key, const std::string &value) = 0;
48+
49+
virtual void del(const std::string& key) = 0;
50+
51+
virtual std::string* resolveFirst(const std::string& var) = 0;
52+
53+
virtual void resolveSingleMatch(const std::string& var,
54+
std::vector<const Variable *> *l) = 0;
55+
virtual void resolveMultiMatches(const std::string& var,
56+
std::vector<const Variable *> *l) = 0;
57+
virtual void resolveRegularExpression(const std::string& var,
58+
std::vector<const Variable *> *l) = 0;
59+
};
60+
61+
} // namespace collection
62+
} // namespace modsecurity
63+
#endif
64+
65+
66+
#endif // HEADERS_MODSECURITY_TRANSACTION_COLLECTION_H_
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
17+
#ifdef __cplusplus
18+
#include <ctime>
19+
#include <iostream>
20+
#include <unordered_map>
21+
#include <fstream>
22+
#include <vector>
23+
#include <iomanip>
24+
#include <set>
25+
#include <cstdio>
26+
#include <string>
27+
#include <list>
28+
#endif
29+
30+
#include "modsecurity/collection/global_collection.h"
31+
#include "modsecurity/collection/collection.h"
32+
#include "modsecurity/collection/variable.h"
33+
34+
#ifndef HEADERS_MODSECURITY_TRANSACTION_COLLECTIONS_H_
35+
#define HEADERS_MODSECURITY_TRANSACTION_COLLECTIONS_H_
36+
37+
#ifndef __cplusplus
38+
typedef struct Collections_t Collections;
39+
#endif
40+
41+
#ifdef __cplusplus
42+
43+
namespace modsecurity {
44+
namespace collection {
45+
46+
class Collections :
47+
public std::unordered_map<std::string, Collection *> {
48+
public:
49+
Collections(GlobalCollection *global, GlobalCollection *ip);
50+
~Collections();
51+
52+
void store(std::string key, std::string value);
53+
void storeOrUpdateFirst(const std::string& collectionName,
54+
const std::string& variableName,
55+
const std::string& targetValue);
56+
bool storeOrUpdateFirst(const std::string &key, const std::string &value);
57+
bool updateFirst(const std::string &key, const std::string &value);
58+
void del(const std::string& key);
59+
std::string* resolveFirst(const std::string& var);
60+
std::string* resolveFirst(const std::string& collectionName,
61+
const std::string& var);
62+
63+
void resolveSingleMatch(const std::string& var,
64+
std::vector<const Variable *> *l);
65+
void resolveSingleMatch(const std::string& var,
66+
const std::string& collection,
67+
std::vector<const Variable *> *l);
68+
void resolveMultiMatches(const std::string& var,
69+
std::vector<const Variable *> *l);
70+
void resolveMultiMatches(const std::string& var,
71+
const std::string& collection,
72+
std::vector<const Variable *> *l);
73+
void resolveRegularExpression(const std::string& var,
74+
std::vector<const Variable *> *l);
75+
void resolveRegularExpression(const std::string& var,
76+
const std::string& collection,
77+
std::vector<const Variable *> *l);
78+
79+
/**
80+
* This is a special collection to host the transaction variables.
81+
*
82+
* It exists independent of initialization and it is only valid during a transaction.
83+
*
84+
* Notice that it is not the TX collection.
85+
*/
86+
Collection *m_transient;
87+
88+
std::string m_global_collection_key;
89+
std::string m_ip_collection_key;
90+
91+
GlobalCollection *m_global_collection;
92+
GlobalCollection *m_ip_collection;
93+
};
94+
95+
} // namespace collection
96+
} // namespace modsecurity
97+
#endif
98+
99+
100+
#endif // HEADERS_MODSECURITY_TRANSACTION_COLLECTIONS_H_
101+
102+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
17+
#ifdef __cplusplus
18+
#include <algorithm>
19+
#include <iostream>
20+
#include <list>
21+
#include <string>
22+
#include <unordered_map>
23+
#include <vector>
24+
#endif
25+
26+
#include "modsecurity/collection/collection.h"
27+
#include "modsecurity/collection/variable.h"
28+
29+
30+
#ifndef HEADERS_MODSECURITY_TRANSACTION_GLOBAL_COLLECTION_H_
31+
#define HEADERS_MODSECURITY_TRANSACTION_GLOBAL_COLLECTION_H_
32+
33+
#ifndef __cplusplus
34+
typedef struct GlobalCollection_t GlobalCollection;
35+
#endif
36+
37+
#ifdef __cplusplus
38+
namespace modsecurity {
39+
namespace collection {
40+
41+
class CollectionKey {
42+
public:
43+
CollectionKey()
44+
: m_compartiment(""),
45+
m_name("") { }
46+
explicit CollectionKey(std::string name)
47+
: m_compartiment(""),
48+
m_name(name) { }
49+
CollectionKey(std::string name, std::string compartiment)
50+
: m_compartiment(compartiment),
51+
m_name(name) { }
52+
53+
std::string m_name;
54+
std::string m_compartiment;
55+
};
56+
57+
58+
class collection_hash {
59+
public:
60+
size_t operator()(const CollectionKey *v) const {
61+
size_t h = 0;
62+
std::for_each(v->m_name.begin(), v->m_name.end(), [&](char c) {
63+
h += tolower(c);
64+
});
65+
std::for_each(v->m_compartiment.begin(),
66+
v->m_compartiment.end(), [&](char c) {
67+
h += tolower(c);
68+
});
69+
70+
return h;
71+
}
72+
};
73+
74+
75+
class collection_equal {
76+
public:
77+
bool operator()(const CollectionKey *u, const CollectionKey *v) const {
78+
return u->m_name == v->m_name
79+
&& u->m_compartiment == v->m_compartiment;
80+
}
81+
};
82+
83+
84+
class GlobalCollection :
85+
public std::unordered_multimap<CollectionKey *, std::string,
86+
collection_hash, collection_equal> {
87+
public:
88+
GlobalCollection();
89+
~GlobalCollection();
90+
void store(std::string key, std::string compartment, std::string value);
91+
92+
bool storeOrUpdateFirst(const std::string &key, std::string compartment,
93+
const std::string &value);
94+
95+
bool updateFirst(const std::string &key, std::string compartment,
96+
const std::string &value);
97+
98+
void del(const std::string& key, std::string compartment);
99+
100+
std::string* resolveFirst(const std::string& var, std::string compartment);
101+
void resolveSingleMatch(const std::string& var, std::string compartment,
102+
std::vector<const Variable *> *l);
103+
void resolveMultiMatches(const std::string& var, std::string compartment,
104+
std::vector<const Variable *> *l);
105+
106+
void resolveRegularExpression(const std::string& var,
107+
std::string compartment,
108+
std::vector<const Variable *> *l);
109+
};
110+
111+
} // namespace collection
112+
} // namespace modsecurity
113+
114+
#endif
115+
116+
117+
#endif // HEADERS_MODSECURITY_TRANSACTION_GLOBAL_COLLECTION_H_
118+
119+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
17+
#ifdef __cplusplus
18+
#include <string>
19+
#endif
20+
21+
22+
#ifndef HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_
23+
#define HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_
24+
25+
26+
#ifndef __cplusplus
27+
typedef struct Variable_t Variable;
28+
#endif
29+
30+
#ifdef __cplusplus
31+
namespace modsecurity {
32+
namespace collection {
33+
34+
class Variable {
35+
public:
36+
Variable(const std::string& key, const std::string& value) :
37+
m_key(key),
38+
m_value(value) { }
39+
std::string m_key;
40+
std::string m_value;
41+
};
42+
43+
} // namespace collection
44+
} // namespace modsecurity
45+
#endif
46+
47+
#endif // HEADERS_MODSECURITY_TRANSACTION_VARIABLE_H_

src/Makefile.am

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ UTILS = \
173173
utils/regex.cc \
174174
utils/sha1.cc
175175

176-
177-
libmodsecurity_la_SOURCES = \
178-
collection/collection.cc \
176+
COLLECTION = \
179177
collection/collections.cc \
180178
collection/global_collections.cc \
179+
collection/backend/in_memory-per_process.cc
180+
181+
182+
libmodsecurity_la_SOURCES = \
181183
parser/seclang-parser.yy \
182184
parser/seclang-scanner.ll \
183185
parser/driver.cc \
@@ -199,6 +201,7 @@ libmodsecurity_la_SOURCES = \
199201
rule.cc \
200202
unique_id.cc \
201203
${ACTIONS} \
204+
${COLLECTION} \
202205
${OPERATORS} \
203206
${UTILS} \
204207
${VARIABLES}

0 commit comments

Comments
 (0)