SNode.C
Loading...
Searching...
No Matches
testregex.cpp File Reference
Include dependency graph for testregex.cpp:

Go to the source code of this file.

Functions

Router router (database::mariadb::MariaDBClient &db)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 269 of file testregex.cpp.

269 {
270 WebApp::init(argc, argv);
271
273 .hostname = "localhost",
274 .username = "snodec",
275 .password = "pentium5",
276 .database = "snodec",
277 .port = 3306,
278 .socket = "/run/mysqld/mysqld.sock",
279 .flags = 0,
280 };
281
282 // CREATE USER 'snodec'@localhost IDENTIFIED BY 'pentium5'
283 // GRANT ALL PRIVILEGES ON *.* TO 'snodec'@localhost
284 // GRANT ALL PRIVILEGES ON 'snodec'.'snodec' TO 'snodec'@localhost
285 // CREATE DATABASE 'snodec';
286 // CREATE TABLE 'snodec' ('username' text NOT NULL, 'password' text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
287
289
290 {
291 legacy::in::WebApp legacyApp("legacy-testregex");
292
293 legacyApp.use(router(db));
294
295 legacyApp.listen(8080, [](const legacy::in::WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
296 switch (state) {
298 VLOG(1) << "legacy-testregex: listening on '" << socketAddress.toString() << "'";
299 break;
301 VLOG(1) << "legacy-testregex: disabled";
302 break;
304 VLOG(1) << "legacy-testregex: error occurred";
305 break;
307 VLOG(1) << "legacy-testregex: fatal error occurred";
308 break;
309 }
310 });
311
312 legacyApp.setOnConnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
313 VLOG(1) << "OnConnect:";
314
315 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
316 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
317 });
318
319 legacyApp.setOnDisconnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
320 VLOG(1) << "OnDisconnect:";
321
322 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
323 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
324 });
325
326 tls::in::WebApp tlsApp("tls-testregex");
327
328 tlsApp.use(legacyApp);
329
330 tlsApp.listen(8088, [](const tls::in::WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
331 switch (state) {
333 VLOG(1) << "tls-testregex: listening on '" << socketAddress.toString() << "'";
334 break;
336 VLOG(1) << "tls-testregex: disabled";
337 break;
339 VLOG(1) << "tls-testregex: error occurred";
340 break;
342 VLOG(1) << "tls-testregex: fatal error occurred";
343 break;
344 }
345 });
346
347 tlsApp.setOnConnect([](tls::in::WebApp::SocketConnection* socketConnection) {
348 VLOG(1) << "OnConnect:";
349
350 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
351 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
352 });
353
354 tlsApp.setOnConnected([](tls::in::WebApp::SocketConnection* socketConnection) {
355 VLOG(1) << "OnConnected:";
356
357 X509* client_cert = SSL_get_peer_certificate(socketConnection->getSSL());
358
359 if (client_cert != nullptr) {
360 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
361
362 VLOG(1) << "\tClient certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
363
364 char* str = X509_NAME_oneline(X509_get_subject_name(client_cert), nullptr, 0);
365 VLOG(1) << "\t Subject: " + std::string(str);
366 OPENSSL_free(str);
367
368 str = X509_NAME_oneline(X509_get_issuer_name(client_cert), nullptr, 0);
369 VLOG(1) << "\t Issuer: " + std::string(str);
370 OPENSSL_free(str);
371
372 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
373
374 GENERAL_NAMES* subjectAltNames =
375 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(client_cert, NID_subject_alt_name, nullptr, nullptr));
376#ifdef __GNUC__
377#pragma GCC diagnostic push
378#ifdef __has_warning
379#if __has_warning("-Wused-but-marked-unused")
380#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
381#endif
382#endif
383#endif
384 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
385#ifdef __GNUC_
386#pragma GCC diagnostic pop
387#endif
388 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
389 for (int32_t i = 0; i < altNameCount; ++i) {
390#ifdef __GNUC__
391#pragma GCC diagnostic push
392#ifdef __has_warning
393#if __has_warning("-Wused-but-marked-unused")
394#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
395#endif
396#endif
397#endif
398 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
399#ifdef __GNUC_
400#pragma GCC diagnostic pop
401#endif
402 if (generalName->type == GEN_URI) {
403 const std::string subjectAltName =
404 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
405 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
406 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
407 } else if (generalName->type == GEN_DNS) {
408 const std::string subjectAltName =
409 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
410 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
411 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
412 } else {
413 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
414 }
415 }
416#ifdef __GNUC__
417#pragma GCC diagnostic push
418#ifdef __has_warning
419#if __has_warning("-Wused-but-marked-unused")
420#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
421#endif
422#endif
423#endif
424 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
425#ifdef __GNUC_
426#pragma GCC diagnostic pop
427#endif
428 X509_free(client_cert);
429 } else {
430 VLOG(1) << "\tClient certificate: no certificate";
431 }
432 });
433
434 tlsApp.setOnDisconnect([](tls::in::WebApp::SocketConnection* socketConnection) {
435 VLOG(1) << "OnDisconnect:";
436
437 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
438 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
439 });
440 }
441
442 return WebApp::start();
443}
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
typename Server::SocketConnection SocketConnection
Definition WebAppT.h:69
typename Server::SocketAddress SocketAddress
Definition WebAppT.h:70
static void init(int argc, char *argv[])
Definition WebApp.cpp:56
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:60
Router router(database::mariadb::MariaDBClient &db)
Definition testregex.cpp:68

References database::mariadb::MariaDBConnectionDetails::database, database::mariadb::MariaDBConnectionDetails::flags, database::mariadb::MariaDBConnectionDetails::hostname, database::mariadb::MariaDBClient::MariaDBClient(), database::mariadb::MariaDBConnectionDetails::password, database::mariadb::MariaDBConnectionDetails::port, database::mariadb::MariaDBConnectionDetails::socket, and database::mariadb::MariaDBConnectionDetails::username.

Here is the call graph for this function:

◆ router()

Definition at line 68 of file testregex.cpp.

68 {
69 const Router router;
70
71 router.get(R"(/r1/:id([^/]+))", [] APPLICATION(req, res) {
72 std::cout << "Hier 1a ************" << std::endl;
73 std::cout << "Params: " << req->params["id"] << std::endl;
74 res->send("Done\n");
75 });
76
77 router.get(R"(/r1/:id/:subcollection)", [] APPLICATION(req, res) {
78 std::cout << "Hier 1b ************" << std::endl;
79 std::cout << "Params: " << req->params["id"] << std::endl;
80 std::cout << "Params: " << req->params["subcollection"] << std::endl;
81 res->send("Done\n");
82 });
83
84 router.get(R"(/r2/:id)", [] APPLICATION(req, res) {
85 std::cout << "Hier 2a ************" << std::endl;
86 std::cout << "Params: " << req->params["id"] << std::endl;
87 res->send("Done\n");
88 });
89
90 router.get(R"(/r2/:id/:subcollection)", [] APPLICATION(req, res) {
91 std::cout << "Hier 2b ************" << std::endl;
92 std::cout << "Params: " << req->params["id"] << std::endl;
93 std::cout << "Params: " << req->params["subcollection"] << std::endl;
94 res->send("Done\n");
95 });
96
97 router.get("/test/:variable(\\d)/:uri", [] APPLICATION(req, res) { // http://localhost:8080/test/1/urlstring
98 std::cout << "Hier 3 ************" << std::endl;
99 std::cout << "Params: " << req->params["variable"] << std::endl;
100 std::cout << "Params: " << req->params["uri"] << std::endl;
101 res->send("Done\n");
102 });
103
104 router
105 .get(
106 "/query/:userId",
107 [] MIDDLEWARE(req, res, next) {
108 VLOG(1) << "Move on to the next route to query database";
109 next();
110 },
111 [&db] MIDDLEWARE(req, res, next) { // http://localhost:8080/query/123
112 VLOG(1) << "UserId: " << req->params["userId"];
113 std::string userId = req->params["userId"];
114
115 req->setAttribute<std::string, "html-table">(std::string());
116
117 req->getAttribute<std::string, "html-table">([&userId](std::string& table) {
118 table = "<html>\n"
119 " <head>\n"
120 " <title>"
121 "Response from snode.c for " +
122 userId +
123 "\n"
124 " </title>\n"
125 " </head>\n"
126 " <body>\n"
127 " <h1>Return for " +
128 userId +
129 "\n"
130 " </h1>\n"
131 " <body>\n"
132 " <table border = \"1\">\n";
133 });
134
135 int i = 0;
136 db.query(
137 "SELECT * FROM snodec where username = '" + userId + "'",
138 [next, req, i](const MYSQL_ROW row) mutable {
139 if (row != nullptr) {
140 i++;
141 req->getAttribute<std::string, "html-table">([row, &i](std::string& table) {
142 table.append(" <tr>\n"
143 " <td>\n" +
144 std::to_string(i) +
145 "\n"
146 " </td>\n"
147 " <td>\n" +
148 std::string(row[0]) +
149 "\n"
150 " </td>\n"
151 " <td>\n" +
152 row[1] +
153 "\n"
154 " </td>\n"
155 " </tr>\n");
156 });
157 } else {
158 req->getAttribute<std::string, "html-table">([](std::string& table) {
159 table.append(std::string(" </table>\n"
160 " </body>\n"
161 "</html>\n"));
162 });
163 VLOG(1) << "Move on to the next route to send result";
164 next();
165 }
166 },
167 [res, userId](const std::string& errorString, unsigned int errorNumber) {
168 VLOG(1) << "Error: " << errorString << " : " << errorNumber;
169 res->status(404).send(userId + ": " + errorString + " - " + std::to_string(errorNumber));
170 });
171 },
172 [] MIDDLEWARE(req, res, next) {
173 VLOG(1) << "And again 1: Move on to the next route to send result";
174 next();
175 },
176 [] MIDDLEWARE(req, res, next) {
177 VLOG(1) << "And again 2: Move on to the next route to send result";
178 next();
179 })
180 .get([] MIDDLEWARE(req, res, next) {
181 VLOG(1) << "And again 3: Move on to the next route to send result";
182 next();
183 })
184 .get([] APPLICATION(req, res) {
185 VLOG(1) << "SendResult";
186
187 req->getAttribute<std::string, "html-table">(
188 [res](std::string& table) {
189 res->send(table);
190 },
191 [res](const std::string&) {
192 res->end();
193 });
194 });
195 router.get("/account/:userId(\\d*)/:userName", [&db] APPLICATION(req, res) { // http://localhost:8080/account/123/perfectNDSgroup
196 VLOG(1) << "Show account of";
197 VLOG(1) << "UserId: " << req->params["userId"];
198 VLOG(1) << "UserName: " << req->params["userName"];
199
200 const std::string response = "<html>"
201 " <head>"
202 " <title>Response from snode.c</title>"
203 " </head>"
204 " <body>"
205 " <h1>Regex return</h1>"
206 " <ul>"
207 " <li>UserId: " +
208 req->params["userId"] +
209 " </li>"
210 " <li>UserName: " +
211 req->params["userName"] +
212 " </li>"
213 " </ul>"
214 " </body>"
215 "</html>";
216
217 const std::string userId = req->params["userId"];
218 const std::string userName = req->params["userName"];
219
220 db.exec(
221 "INSERT INTO `snodec`(`username`, `password`) VALUES ('" + userId + "','" + userName + "')",
222 [userId, userName]() {
223 VLOG(1) << "Inserted: -> " << userId << " - " << userName;
224 },
225 [](const std::string& errorString, unsigned int errorNumber) {
226 VLOG(1) << "Error: " << errorString << " : " << errorNumber;
227 });
228
229 res->send(response);
230 });
231 router.get("/asdf/:testRegex1(d\\d{3}e)/jklö/:testRegex2", [] APPLICATION(req, res) { // http://localhost:8080/asdf/d123e/jklö/hallo
232 VLOG(1) << "Testing Regex";
233 VLOG(1) << "Regex1: " << req->params["testRegex1"];
234 VLOG(1) << "Regex2: " << req->params["testRegex2"];
235
236 const std::string response = "<html>"
237 " <head>"
238 " <title>Response from snode.c</title>"
239 " </head>"
240 " <body>"
241 " <h1>Regex return</h1>"
242 " <ul>"
243 " <li>Regex 1: " +
244 req->params["testRegex1"] +
245 " </li>"
246 " <li>Regex 2: " +
247 req->params["testRegex2"] +
248 " </li>"
249 " </ul>"
250 " </body>"
251 "</html>";
252
253 res->send(response);
254 });
255 router.get("/search/:search", [] APPLICATION(req, res) { // http://localhost:8080/search/buxtehude123
256 VLOG(1) << "Show Search of";
257 VLOG(1) << "Search: " << req->params["search"];
258 VLOG(1) << "Queries: " << req->query("test");
259
260 res->send(req->params["search"]);
261 });
262 router.use([] APPLICATION(req, res) {
263 res->status(404).send("Not found: " + req->url);
264 });
265
266 return router;
267}
#define APPLICATION(req, res)
Definition Router.h:68
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
MariaDBCommandSequence & exec(const std::string &sql, const std::function< void(void)> &onExec, const std::function< void(const std::string &, unsigned int)> &onError)
MariaDBCommandSequence & query(const std::string &sql, const std::function< void(const MYSQL_ROW)> &onQuery, const std::function< void(const std::string &, unsigned int)> &onError)
Route & use(const Router &router) const
Definition Router.cpp:88
Route & get(const Router &router) const
Definition Router.cpp:90

References database::mariadb::MariaDBClientASyncAPI::exec().

Here is the call graph for this function: