70 router.get(R"(/r1/:id([^/]+))", []
APPLICATION(req, res) {
71 std::cout <<
"Hier 1a ************" << std::endl;
72 std::cout <<
"Params: " << req->params[
"id"] << std::endl;
76 router.get(R"(/r1/:id/:subcollection)", []
APPLICATION(req, res) {
77 std::cout <<
"Hier 1b ************" << std::endl;
78 std::cout <<
"Params: " << req->params[
"id"] << std::endl;
79 std::cout <<
"Params: " << req->params[
"subcollection"] << std::endl;
84 std::cout <<
"Hier 2a ************" << std::endl;
85 std::cout <<
"Params: " << req->params[
"id"] << std::endl;
89 router.get(R"(/r2/:id/:subcollection)", []
APPLICATION(req, res) {
90 std::cout <<
"Hier 2b ************" << std::endl;
91 std::cout <<
"Params: " << req->params[
"id"] << std::endl;
92 std::cout <<
"Params: " << req->params[
"subcollection"] << std::endl;
96 router.get(
"/test/:variable(\\d)/:uri", []
APPLICATION(req, res) {
97 std::cout <<
"Hier 3 ************" << std::endl;
98 std::cout <<
"Params: " << req->params[
"variable"] << std::endl;
99 std::cout <<
"Params: " << req->params[
"uri"] << std::endl;
107 VLOG(1) <<
"Move on to the next route to query database";
111 VLOG(1) <<
"UserId: " << req->params[
"userId"];
112 std::string userId = req->params[
"userId"];
114 req->setAttribute<std::string,
"html-table">(std::string());
116 req->getAttribute<std::string,
"html-table">([&userId](std::string& table) {
120 "Response from snode.c for " +
131 " <table border = \"1\">\n";
136 "SELECT * FROM snodec where username = '" + userId +
"'",
137 [next, req, i](
const MYSQL_ROW row)
mutable {
138 if (row !=
nullptr) {
140 req->getAttribute<std::string,
"html-table">([row, &i](std::string& table) {
141 table.append(
" <tr>\n"
147 std::string(row[0]) +
157 req->getAttribute<std::string,
"html-table">([](std::string& table) {
158 table.append(std::string(
" </table>\n"
162 VLOG(1) <<
"Move on to the next route to send result";
166 [res, userId](
const std::string& errorString,
unsigned int errorNumber) {
167 VLOG(1) <<
"Error: " << errorString <<
" : " << errorNumber;
168 res->status(404).send(userId +
": " + errorString +
" - " + std::to_string(errorNumber));
172 VLOG(1) <<
"And again 1: Move on to the next route to send result";
176 VLOG(1) <<
"And again 2: Move on to the next route to send result";
180 VLOG(1) <<
"And again 3: Move on to the next route to send result";
184 VLOG(1) <<
"SendResult";
186 req->getAttribute<std::string,
"html-table">(
187 [res](std::string& table) {
190 [res](
const std::string&) {
194 router.get(
"/account/:userId(\\d*)/:userName", [&db]
APPLICATION(req, res) {
195 VLOG(1) <<
"Show account of";
196 VLOG(1) <<
"UserId: " << req->params[
"userId"];
197 VLOG(1) <<
"UserName: " << req->params[
"userName"];
199 const std::string response =
"<html>"
201 " <title>Response from snode.c</title>"
204 " <h1>Regex return</h1>"
207 req->params[
"userId"] +
210 req->params[
"userName"] +
216 const std::string userId = req->params[
"userId"];
217 const std::string userName = req->params[
"userName"];
220 "INSERT INTO `snodec`(`username`, `password`) VALUES ('" + userId +
"','" + userName +
"')",
221 [userId, userName]() {
222 VLOG(1) <<
"Inserted: -> " << userId <<
" - " << userName;
224 [](
const std::string& errorString,
unsigned int errorNumber) {
225 VLOG(1) <<
"Error: " << errorString <<
" : " << errorNumber;
230 router.get(
"/asdf/:testRegex1(d\\d{3}e)/jklö/:testRegex2", []
APPLICATION(req, res) {
231 VLOG(1) <<
"Testing Regex";
232 VLOG(1) <<
"Regex1: " << req->params[
"testRegex1"];
233 VLOG(1) <<
"Regex2: " << req->params[
"testRegex2"];
235 const std::string response =
"<html>"
237 " <title>Response from snode.c</title>"
240 " <h1>Regex return</h1>"
243 req->params[
"testRegex1"] +
246 req->params[
"testRegex2"] +
254 router.get(
"/search/:search", []
APPLICATION(req, res) {
255 VLOG(1) <<
"Show Search of";
256 VLOG(1) <<
"Search: " << req->params[
"search"];
257 VLOG(1) <<
"Queries: " << req->query(
"test");
259 res->send(req->params[
"search"]);
262 res->status(404).send(
"Not found: " + req->url);
268int main(
int argc,
char* argv[]) {
278 .
socket =
"/run/mysqld/mysqld.sock",
292 VLOG(0) <<
"MySQL connected";
294 VLOG(0) <<
"MySQL disconnected";
299 legacy::in::WebApp legacyApp(
"legacy-testregex");
301 legacyApp.use(router(db));
303 legacyApp.listen(8080, [](
const legacy::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
305 case core::socket::State::OK:
306 VLOG(1) <<
"legacy-testregex: listening on '" << socketAddress.toString() <<
"'";
308 case core::socket::State::DISABLED:
309 VLOG(1) <<
"legacy-testregex: disabled";
311 case core::socket::State::ERROR:
312 VLOG(1) <<
"legacy-testregex: error occurred";
314 case core::socket::State::FATAL:
315 VLOG(1) <<
"legacy-testregex: fatal error occurred";
320 legacyApp.setOnConnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
321 VLOG(1) <<
"OnConnect:";
323 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
324 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
327 legacyApp.setOnDisconnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
328 VLOG(1) <<
"OnDisconnect:";
330 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
331 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
334 tls::in::WebApp tlsApp(
"tls-testregex");
336 tlsApp.use(legacyApp);
338 tlsApp.listen(8088, [](
const tls::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
340 case core::socket::State::OK:
341 VLOG(1) <<
"tls-testregex: listening on '" << socketAddress.toString() <<
"'";
343 case core::socket::State::DISABLED:
344 VLOG(1) <<
"tls-testregex: disabled";
346 case core::socket::State::ERROR:
347 VLOG(1) <<
"tls-testregex: error occurred";
349 case core::socket::State::FATAL:
350 VLOG(1) <<
"tls-testregex: fatal error occurred";
355 tlsApp.setOnConnect([](tls::in::WebApp::SocketConnection* socketConnection) {
356 VLOG(1) <<
"OnConnect:";
358 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
359 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
362 tlsApp.setOnConnected([](tls::in::WebApp::SocketConnection* socketConnection) {
363 VLOG(1) <<
"OnConnected:";
365 X509* client_cert = SSL_get_peer_certificate(socketConnection->getSSL());
367 if (client_cert !=
nullptr) {
368 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
370 VLOG(1) <<
"\tClient certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
372 char* str = X509_NAME_oneline(X509_get_subject_name(client_cert),
nullptr, 0);
373 VLOG(1) <<
"\t Subject: " + std::string(str);
376 str = X509_NAME_oneline(X509_get_issuer_name(client_cert),
nullptr, 0);
377 VLOG(1) <<
"\t Issuer: " + std::string(str);
382 GENERAL_NAMES* subjectAltNames =
383 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(client_cert, NID_subject_alt_name,
nullptr,
nullptr));
385 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
387 VLOG(1) <<
"\t Subject alternative name count: " << altNameCount;
388 for (int32_t i = 0; i < altNameCount; ++i) {
389 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
390 if (generalName->type == GEN_URI) {
391 const std::string subjectAltName =
392 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
393 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
394 VLOG(1) <<
"\t SAN (URI): '" + subjectAltName;
395 }
else if (generalName->type == GEN_DNS) {
396 const std::string subjectAltName =
397 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
398 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
399 VLOG(1) <<
"\t SAN (DNS): '" + subjectAltName;
401 VLOG(1) <<
"\t SAN (Type): '" + std::to_string(generalName->type);
405 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
407 X509_free(client_cert);
409 VLOG(1) <<
"\tClient certificate: no certificate";
413 tlsApp.setOnDisconnect([](tls::in::WebApp::SocketConnection* socketConnection) {
414 VLOG(1) <<
"OnDisconnect:";
416 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
417 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();