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;
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;
85 std::cout <<
"Hier 2a ************" << std::endl;
86 std::cout <<
"Params: " << req->params[
"id"] << std::endl;
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;
97 router.get(
"/test/:variable(\\d)/:uri", []
APPLICATION(req, res) {
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;
108 VLOG(1) <<
"Move on to the next route to query database";
112 VLOG(1) <<
"UserId: " << req->params[
"userId"];
113 std::string userId = req->params[
"userId"];
115 req->setAttribute<std::string,
"html-table">(std::string());
117 req->getAttribute<std::string,
"html-table">([&userId](std::string& table) {
121 "Response from snode.c for " +
132 " <table border = \"1\">\n";
137 "SELECT * FROM snodec where username = '" + userId +
"'",
138 [next, req, i](
const MYSQL_ROW row)
mutable {
139 if (row !=
nullptr) {
141 req->getAttribute<std::string,
"html-table">([row, &i](std::string& table) {
142 table.append(
" <tr>\n"
148 std::string(row[0]) +
158 req->getAttribute<std::string,
"html-table">([](std::string& table) {
159 table.append(std::string(
" </table>\n"
163 VLOG(1) <<
"Move on to the next route to send result";
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));
173 VLOG(1) <<
"And again 1: Move on to the next route to send result";
177 VLOG(1) <<
"And again 2: Move on to the next route to send result";
181 VLOG(1) <<
"And again 3: Move on to the next route to send result";
185 VLOG(1) <<
"SendResult";
187 req->getAttribute<std::string,
"html-table">(
188 [res](std::string& table) {
191 [res](
const std::string&) {
195 router.get(
"/account/:userId(\\d*)/:userName", [&db]
APPLICATION(req, res) {
196 VLOG(1) <<
"Show account of";
197 VLOG(1) <<
"UserId: " << req->params[
"userId"];
198 VLOG(1) <<
"UserName: " << req->params[
"userName"];
200 const std::string response =
"<html>"
202 " <title>Response from snode.c</title>"
205 " <h1>Regex return</h1>"
208 req->params[
"userId"] +
211 req->params[
"userName"] +
217 const std::string userId = req->params[
"userId"];
218 const std::string userName = req->params[
"userName"];
221 "INSERT INTO `snodec`(`username`, `password`) VALUES ('" + userId +
"','" + userName +
"')",
222 [userId, userName]() {
223 VLOG(1) <<
"Inserted: -> " << userId <<
" - " << userName;
225 [](
const std::string& errorString,
unsigned int errorNumber) {
226 VLOG(1) <<
"Error: " << errorString <<
" : " << errorNumber;
231 router.get(
"/asdf/:testRegex1(d\\d{3}e)/jklö/:testRegex2", []
APPLICATION(req, res) {
232 VLOG(1) <<
"Testing Regex";
233 VLOG(1) <<
"Regex1: " << req->params[
"testRegex1"];
234 VLOG(1) <<
"Regex2: " << req->params[
"testRegex2"];
236 const std::string response =
"<html>"
238 " <title>Response from snode.c</title>"
241 " <h1>Regex return</h1>"
244 req->params[
"testRegex1"] +
247 req->params[
"testRegex2"] +
255 router.get(
"/search/:search", []
APPLICATION(req, res) {
256 VLOG(1) <<
"Show Search of";
257 VLOG(1) <<
"Search: " << req->params[
"search"];
258 VLOG(1) <<
"Queries: " << req->query(
"test");
260 res->send(req->params[
"search"]);
263 res->status(404).send(
"Not found: " + req->url);
269int main(
int argc,
char* argv[]) {
279 .
socket =
"/run/mysqld/mysqld.sock",
293 VLOG(0) <<
"MySQL connected";
295 VLOG(0) <<
"MySQL disconnected";
300 legacy::in::WebApp legacyApp(
"legacy-testregex");
302 legacyApp.use(router(db));
304 legacyApp.listen(8080, [](
const legacy::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
306 case core::socket::State::OK:
307 VLOG(1) <<
"legacy-testregex: listening on '" << socketAddress.toString() <<
"'";
309 case core::socket::State::DISABLED:
310 VLOG(1) <<
"legacy-testregex: disabled";
312 case core::socket::State::ERROR:
313 VLOG(1) <<
"legacy-testregex: error occurred";
315 case core::socket::State::FATAL:
316 VLOG(1) <<
"legacy-testregex: fatal error occurred";
321 legacyApp.setOnConnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
322 VLOG(1) <<
"OnConnect:";
324 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
325 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
328 legacyApp.setOnDisconnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
329 VLOG(1) <<
"OnDisconnect:";
331 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
332 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
335 tls::in::WebApp tlsApp(
"tls-testregex");
337 tlsApp.use(legacyApp);
339 tlsApp.listen(8088, [](
const tls::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
341 case core::socket::State::OK:
342 VLOG(1) <<
"tls-testregex: listening on '" << socketAddress.toString() <<
"'";
344 case core::socket::State::DISABLED:
345 VLOG(1) <<
"tls-testregex: disabled";
347 case core::socket::State::ERROR:
348 VLOG(1) <<
"tls-testregex: error occurred";
350 case core::socket::State::FATAL:
351 VLOG(1) <<
"tls-testregex: fatal error occurred";
356 tlsApp.setOnConnect([](tls::in::WebApp::SocketConnection* socketConnection) {
357 VLOG(1) <<
"OnConnect:";
359 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
360 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
363 tlsApp.setOnConnected([](tls::in::WebApp::SocketConnection* socketConnection) {
364 VLOG(1) <<
"OnConnected:";
366 X509* client_cert = SSL_get_peer_certificate(socketConnection->getSSL());
368 if (client_cert !=
nullptr) {
369 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
371 VLOG(1) <<
"\tClient certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
373 char* str = X509_NAME_oneline(X509_get_subject_name(client_cert),
nullptr, 0);
374 VLOG(1) <<
"\t Subject: " + std::string(str);
377 str = X509_NAME_oneline(X509_get_issuer_name(client_cert),
nullptr, 0);
378 VLOG(1) <<
"\t Issuer: " + std::string(str);
383 GENERAL_NAMES* subjectAltNames =
384 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(client_cert, NID_subject_alt_name,
nullptr,
nullptr));
386 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
388 VLOG(1) <<
"\t Subject alternative name count: " << altNameCount;
389 for (int32_t i = 0; i < altNameCount; ++i) {
390 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
391 if (generalName->type == GEN_URI) {
392 const std::string subjectAltName =
393 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
394 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
395 VLOG(1) <<
"\t SAN (URI): '" + subjectAltName;
396 }
else if (generalName->type == GEN_DNS) {
397 const std::string subjectAltName =
398 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
399 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
400 VLOG(1) <<
"\t SAN (DNS): '" + subjectAltName;
402 VLOG(1) <<
"\t SAN (Type): '" + std::to_string(generalName->type);
406 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
408 X509_free(client_cert);
410 VLOG(1) <<
"\tClient certificate: no certificate";
414 tlsApp.setOnDisconnect([](tls::in::WebApp::SocketConnection* socketConnection) {
415 VLOG(1) <<
"OnDisconnect:";
417 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
418 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();