13
13
*
14
14
*/
15
15
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
+
16
19
#include < string>
17
20
#include < memory>
18
21
#include < thread>
22
+ #include < array>
19
23
#include < chrono>
20
- #include < pthread.h>
24
+
25
+ #include " modsecurity/rule_message.h"
21
26
22
27
23
- # define NUM_THREADS 100
28
+ constexpr auto NUM_THREADS = 100 ;
24
29
25
30
26
31
char request_header[] = " " \
@@ -62,59 +67,33 @@ char response_body[] = "" \
62
67
63
68
char ip[] = " 200.249.12.31" ;
64
69
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
-
70
70
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 );
75
74
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 );
91
75
modsecTransaction->processConnection (ip, 12345 , " 127.0.0.1" , 80 );
92
76
modsecTransaction->processURI (request_uri, " GET" , " 1.1" );
93
77
94
78
std::this_thread::sleep_for (std::chrono::microseconds (10 ));
79
+
95
80
modsecTransaction->addRequestHeader (" Host" ,
96
81
" net.tutsplus.com" );
97
82
modsecTransaction->processRequestHeaders ();
98
83
modsecTransaction->processRequestBody ();
84
+
99
85
modsecTransaction->addResponseHeader (" HTTP/1.1" ,
100
86
" 200 OK" );
101
87
modsecTransaction->processResponseHeaders (200 , " HTTP 1.2" );
102
88
modsecTransaction->appendResponseBody (
103
89
(const unsigned char *)response_body,
104
90
strlen ((const char *)response_body));
105
91
modsecTransaction->processResponseBody ();
106
- modsecTransaction->processLogging ();
107
92
108
- delete modsecTransaction;
93
+ modsecTransaction-> processLogging () ;
109
94
}
110
-
111
- pthread_exit (nullptr );
112
95
}
113
96
114
- #if defined _MSC_VER
115
- #pragma warning(pop)
116
- #endif
117
-
118
97
class ReadingLogsViaRuleMessage {
119
98
public:
120
99
ReadingLogsViaRuleMessage (char *request_header,
@@ -134,11 +113,6 @@ class ReadingLogsViaRuleMessage {
134
113
{ }
135
114
136
115
int process () const {
137
- pthread_t threads[NUM_THREADS];
138
- int i;
139
- struct data_ms dms;
140
- void *status;
141
-
142
116
auto modsec = std::make_unique<modsecurity::ModSecurity>();
143
117
modsec->setConnectorInformation (" ModSecurity-test v0.0.1-alpha" \
144
118
" (ModSecurity test)" );
@@ -152,18 +126,19 @@ class ReadingLogsViaRuleMessage {
152
126
return -1 ;
153
127
}
154
128
155
- dms.modsec = modsec.get ();
156
- dms.rules = rules.get ();
129
+ std::array<std::thread, NUM_THREADS> threads;
157
130
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
+ });
161
136
}
162
137
163
138
std::this_thread::sleep_for (std::chrono::microseconds (10000 ));
164
139
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 ( );
167
142
std::cout << " Main: completed thread id :" << i << std::endl;
168
143
}
169
144
0 commit comments