1313 *
1414 */
1515
16+ #ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
17+ #define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
18+
1619#include < string>
1720#include < memory>
1821#include < thread>
22+ #include < array>
1923#include < chrono>
20- #include < pthread.h>
24+
25+ #include " modsecurity/rule_message.h"
2126
2227
23- # define NUM_THREADS 100
28+ constexpr auto NUM_THREADS = 100 ;
2429
2530
2631char request_header[] = " " \
@@ -62,59 +67,33 @@ char response_body[] = "" \
6267
6368char ip[] = " 200.249.12.31" ;
6469
65- #include " modsecurity/rule_message.h"
66-
67- #ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
68- #define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
69-
7070
71- struct data_ms {
72- modsecurity::ModSecurity *modsec;
73- modsecurity::RulesSet *rules;
74- };
71+ static void process_request (modsecurity::ModSecurity *modsec, modsecurity::RulesSet *rules) {
72+ for (auto z = 0 ; z < 10000 ; z++) {
73+ auto modsecTransaction = std::make_unique<modsecurity::Transaction>(modsec, rules, nullptr );
7574
76- #if defined _MSC_VER
77- #pragma warning(push)
78- #pragma warning(disable:4716) // avoid error C4716: 'process_request': must return a value, as MSVC C++ compiler doesn't support [[noreturn]]
79- #pragma warning(disable:4715) // avoid warning c4715: 'process_request' : not all control paths return a value
80- #endif
81-
82- [[noreturn]] static void *process_request (void *data) {
83- struct data_ms *a = (struct data_ms *)data;
84- modsecurity::ModSecurity *modsec = a->modsec ;
85- modsecurity::RulesSet *rules = a->rules ;
86- int z = 0 ;
87-
88- for (z = 0 ; z < 10000 ; z++) {
89- modsecurity::Transaction *modsecTransaction = \
90- new modsecurity::Transaction (modsec, rules, NULL );
9175 modsecTransaction->processConnection (ip, 12345 , " 127.0.0.1" , 80 );
9276 modsecTransaction->processURI (request_uri, " GET" , " 1.1" );
9377
9478 std::this_thread::sleep_for (std::chrono::microseconds (10 ));
79+
9580 modsecTransaction->addRequestHeader (" Host" ,
9681 " net.tutsplus.com" );
9782 modsecTransaction->processRequestHeaders ();
9883 modsecTransaction->processRequestBody ();
84+
9985 modsecTransaction->addResponseHeader (" HTTP/1.1" ,
10086 " 200 OK" );
10187 modsecTransaction->processResponseHeaders (200 , " HTTP 1.2" );
10288 modsecTransaction->appendResponseBody (
10389 (const unsigned char *)response_body,
10490 strlen ((const char *)response_body));
10591 modsecTransaction->processResponseBody ();
106- modsecTransaction->processLogging ();
10792
108- delete modsecTransaction;
93+ modsecTransaction-> processLogging () ;
10994 }
110-
111- pthread_exit (nullptr );
11295}
11396
114- #if defined _MSC_VER
115- #pragma warning(pop)
116- #endif
117-
11897class ReadingLogsViaRuleMessage {
11998 public:
12099 ReadingLogsViaRuleMessage (char *request_header,
@@ -134,11 +113,6 @@ class ReadingLogsViaRuleMessage {
134113 { }
135114
136115 int process () const {
137- pthread_t threads[NUM_THREADS];
138- int i;
139- struct data_ms dms;
140- void *status;
141-
142116 auto modsec = std::make_unique<modsecurity::ModSecurity>();
143117 modsec->setConnectorInformation (" ModSecurity-test v0.0.1-alpha" \
144118 " (ModSecurity test)" );
@@ -152,18 +126,19 @@ class ReadingLogsViaRuleMessage {
152126 return -1 ;
153127 }
154128
155- dms.modsec = modsec.get ();
156- dms.rules = rules.get ();
129+ std::array<std::thread, NUM_THREADS> threads;
157130
158- for (i = 0 ; i < NUM_THREADS; i++) {
159- pthread_create (&threads[i], NULL , process_request,
160- reinterpret_cast <void *>(&dms));
131+ for (auto i = 0 ; i != threads.size (); ++i) {
132+ threads[i] = std::thread (
133+ [&modsec, &rules]() {
134+ process_request (modsec.get (), rules.get ());
135+ });
161136 }
162137
163138 std::this_thread::sleep_for (std::chrono::microseconds (10000 ));
164139
165- for (i= 0 ; i < NUM_THREADS; i++ ) {
166- pthread_join ( threads[i], &status );
140+ for (auto i = 0 ; i != threads. size (); ++i ) {
141+ threads[i]. join ( );
167142 std::cout << " Main: completed thread id :" << i << std::endl;
168143 }
169144
0 commit comments