2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "database/mariadb/MariaDBClient.h"
43#include "express/legacy/in/WebApp.h"
44#include "express/tls/in/WebApp.h"
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
48#include "log/Logger.h"
53#include <openssl/ssl.h>
54#include <openssl/x509v3.h>
66using namespace express;
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[]) {
270 WebApp::init(argc, argv);
278 .
socket =
"/run/mysqld/mysqld.sock",
291 legacy::in::WebApp legacyApp(
"legacy-testregex");
293 legacyApp.use(router(db));
295 legacyApp.listen(8080, [](
const legacy::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
297 case core::socket::State::OK:
298 VLOG(1) <<
"legacy-testregex: listening on '" << socketAddress.toString() <<
"'";
300 case core::socket::State::DISABLED:
301 VLOG(1) <<
"legacy-testregex: disabled";
303 case core::socket::State::ERROR:
304 VLOG(1) <<
"legacy-testregex: error occurred";
306 case core::socket::State::FATAL:
307 VLOG(1) <<
"legacy-testregex: fatal error occurred";
312 legacyApp.setOnConnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
313 VLOG(1) <<
"OnConnect:";
315 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
316 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
319 legacyApp.setOnDisconnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
320 VLOG(1) <<
"OnDisconnect:";
322 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
323 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
326 tls::in::WebApp tlsApp(
"tls-testregex");
328 tlsApp.use(legacyApp);
330 tlsApp.listen(8088, [](
const tls::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
332 case core::socket::State::OK:
333 VLOG(1) <<
"tls-testregex: listening on '" << socketAddress.toString() <<
"'";
335 case core::socket::State::DISABLED:
336 VLOG(1) <<
"tls-testregex: disabled";
338 case core::socket::State::ERROR:
339 VLOG(1) <<
"tls-testregex: error occurred";
341 case core::socket::State::FATAL:
342 VLOG(1) <<
"tls-testregex: fatal error occurred";
347 tlsApp.setOnConnect([](tls::in::WebApp::SocketConnection* socketConnection) {
348 VLOG(1) <<
"OnConnect:";
350 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
351 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
354 tlsApp.setOnConnected([](tls::in::WebApp::SocketConnection* socketConnection) {
355 VLOG(1) <<
"OnConnected:";
357 X509* client_cert = SSL_get_peer_certificate(socketConnection->getSSL());
359 if (client_cert !=
nullptr) {
360 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
362 VLOG(1) <<
"\tClient certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
364 char* str = X509_NAME_oneline(X509_get_subject_name(client_cert),
nullptr, 0);
365 VLOG(1) <<
"\t Subject: " + std::string(str);
368 str = X509_NAME_oneline(X509_get_issuer_name(client_cert),
nullptr, 0);
369 VLOG(1) <<
"\t Issuer: " + std::string(str);
374 GENERAL_NAMES* subjectAltNames =
375 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(client_cert, NID_subject_alt_name,
nullptr,
nullptr));
377#pragma GCC diagnostic push
379#if __has_warning
("-Wused-but-marked-unused")
380#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
384 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
386#pragma GCC diagnostic pop
388 VLOG(1) <<
"\t Subject alternative name count: " << altNameCount;
389 for (int32_t i = 0; i < altNameCount; ++i) {
391#pragma GCC diagnostic push
393#if __has_warning
("-Wused-but-marked-unused")
394#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
398 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
400#pragma GCC diagnostic pop
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;
413 VLOG(1) <<
"\t SAN (Type): '" + std::to_string(generalName->type);
417#pragma GCC diagnostic push
419#if __has_warning
("-Wused-but-marked-unused")
420#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
424 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
426#pragma GCC diagnostic pop
428 X509_free(client_cert);
430 VLOG(1) <<
"\tClient certificate: no certificate";
434 tlsApp.setOnDisconnect([](tls::in::WebApp::SocketConnection* socketConnection) {
435 VLOG(1) <<
"OnDisconnect:";
437 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
438 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
442 return WebApp::start();
#define APPLICATION(req, res)
#define MIDDLEWARE(req, res, next)
MariaDBCommandSequence & exec(const std::string &sql, const std::function< void(void)> &onExec, const std::function< void(const std::string &, unsigned int)> &onError)
MariaDBClient(const MariaDBConnectionDetails &details)
int main(int argc, char *argv[])
Router router(database::mariadb::MariaDBClient &db)