1+ #include < iostream>
2+ #include < thread>
3+ #include < array>
4+
5+ #include < modsecurity/modsecurity.h>
6+ #include < modsecurity/transaction.h>
7+ #include < modsecurity/rules_set.h>
8+
9+ static void process_request (modsecurity::ModSecurity *modsec, modsecurity::RulesSet *rules, int tid) {
10+ std::cout << " Hello World! It's me, thread #" << tid << std::endl;
11+
12+ for (int i = 0 ; i != 1'000 ; i++) {
13+ auto modsecTransaction = std::make_unique<modsecurity::Transaction>(modsec, rules, nullptr );
14+
15+ modsecTransaction->processConnection (" 127.0.0.1" , 12345 , " 127.0.0.1" , 80 );
16+ modsecTransaction->processURI (
17+ " https://www.modsecurity.org/test?foo=herewego" ,
18+ " GET" , " 1.1" );
19+
20+ modsecTransaction->addRequestHeader (" User-Agent" ,
21+ " Basic ModSecurity example" );
22+ modsecTransaction->processRequestHeaders ();
23+ modsecTransaction->processRequestBody ();
24+
25+ modsecTransaction->addResponseHeader (" HTTP/1.1" ,
26+ " 200 OK" );
27+ modsecTransaction->processResponseHeaders (200 , " HTTP 1.2" );
28+ modsecTransaction->processResponseBody ();
29+
30+ modsecTransaction->processLogging ();
31+
32+ std::this_thread::sleep_for (std::chrono::microseconds (100 ));
33+ }
34+
35+ std::cout << " Thread #" << tid << " exits" << std::endl;
36+ }
37+
38+ int main (int argc, char *argv[]) {
39+ auto modsec = std::make_unique<modsecurity::ModSecurity>();
40+ modsec->setConnectorInformation (" ModSecurity-test v0.0.1-alpha (Simple " \
41+ " example on how to use ModSecurity API" );
42+
43+ char main_rule_uri[] = " basic_rules.conf" ;
44+ auto rules = std::make_unique<modsecurity::RulesSet>();
45+ if (rules->loadFromUri (main_rule_uri) < 0 ) {
46+ std::cerr << " Problems loading the rules..." << std::endl;
47+ std::cerr << rules->m_parserError .str () << std::endl;
48+ return -1 ;
49+ }
50+
51+ constexpr auto NUM_THREADS = 100 ;
52+ std::array<std::thread, NUM_THREADS> threads;
53+
54+ for (auto i = 0 ; i != threads.size (); ++i) {
55+ threads[i] = std::thread (
56+ [&modsec, &rules, i]() {
57+ process_request (modsec.get (), rules.get (), i);
58+ });
59+ }
60+
61+ std::this_thread::sleep_for (std::chrono::microseconds (10000 ));
62+
63+ for (auto i = 0 ; i != threads.size (); ++i) {
64+ threads[i].join ();
65+ }
66+
67+ return 0 ;
68+ }
0 commit comments