49 router.get(
"/test/:variable(\\d)/:uri", []
APPLICATION(req, res) {
50 std::cout <<
"TEST" << std::endl;
56 VLOG(1) <<
"Move on to the next route to query database";
60 VLOG(1) <<
"UserId: " << req->params[
"userId"];
61 std::string userId = req->params[
"userId"];
63 req->setAttribute<std::string,
"html-table">(std::string());
65 req->getAttribute<std::string,
"html-table">([&userId](std::string& table) {
69 "Response from snode.c for " +
80 " <table border = \"1\">\n";
85 "SELECT * FROM snodec where username = '" + userId +
"'",
86 [next, req, i](
const MYSQL_ROW row)
mutable {
89 req->getAttribute<std::string,
"html-table">([row, &i](std::string& table) {
90 table.append(
" <tr>\n"
106 req->getAttribute<std::string,
"html-table">([](std::string& table) {
107 table.append(std::string(
" </table>\n"
111 VLOG(1) <<
"Move on to the next route to send result";
115 [res, userId](
const std::string& errorString,
unsigned int errorNumber) {
116 VLOG(1) <<
"Error: " << errorString <<
" : " << errorNumber;
117 res->status(404).send(userId +
": " + errorString +
" - " + std::to_string(errorNumber));
121 VLOG(1) <<
"And again 1: Move on to the next route to send result";
125 VLOG(1) <<
"And again 2: Move on to the next route to send result";
129 VLOG(1) <<
"And again 3: Move on to the next route to send result";
133 VLOG(1) <<
"SendResult";
135 req->getAttribute<std::string,
"html-table">(
136 [res](std::string& table) {
139 [res](
const std::string&) {
143 router.get(
"/account/:userId(\\d*)/:userName", [&db]
APPLICATION(req, res) {
144 VLOG(1) <<
"Show account of";
145 VLOG(1) <<
"UserId: " << req->params[
"userId"];
146 VLOG(1) <<
"UserName: " << req->params[
"userName"];
148 const std::string response =
"<html>"
150 " <title>Response from snode.c</title>"
153 " <h1>Regex return</h1>"
156 req->params[
"userId"] +
159 req->params[
"userName"] +
165 const std::string userId = req->params[
"userId"];
166 const std::string userName = req->params[
"userName"];
169 "INSERT INTO `snodec`(`username`, `password`) VALUES ('" + userId +
"','" + userName +
"')",
170 [userId, userName]() {
171 VLOG(1) <<
"Inserted: -> " << userId <<
" - " << userName;
173 [](
const std::string& errorString,
unsigned int errorNumber) {
174 VLOG(1) <<
"Error: " << errorString <<
" : " << errorNumber;
179 router.get(
"/asdf/:testRegex1(d\\d{3}e)/jklö/:testRegex2", []
APPLICATION(req, res) {
180 VLOG(1) <<
"Testing Regex";
181 VLOG(1) <<
"Regex1: " << req->params[
"testRegex1"];
182 VLOG(1) <<
"Regex2: " << req->params[
"testRegex2"];
184 const std::string response =
"<html>"
186 " <title>Response from snode.c</title>"
189 " <h1>Regex return</h1>"
192 req->params[
"testRegex1"] +
195 req->params[
"testRegex2"] +
203 router.get(
"/search/:search", []
APPLICATION(req, res) {
204 VLOG(1) <<
"Show Search of";
205 VLOG(1) <<
"Search: " << req->params[
"search"];
206 VLOG(1) <<
"Queries: " << req->query(
"test");
208 res->send(req->params[
"search"]);
211 res->status(404).send(
"Not found: " + req->url);
217int main(
int argc,
char* argv[]) {
226 .
socket =
"/run/mysqld/mysqld.sock",
239 legacy::in::WebApp legacyApp(
"legacy-testregex");
241 legacyApp.use(router(db));
243 legacyApp.listen(8080, [](
const legacy::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
245 case core::socket::State::OK:
246 VLOG(1) <<
"legacy-testregex: listening on '" << socketAddress.toString() <<
"'";
248 case core::socket::State::DISABLED:
249 VLOG(1) <<
"legacy-testregex: disabled";
251 case core::socket::State::ERROR:
252 VLOG(1) <<
"legacy-testregex: error occurred";
254 case core::socket::State::FATAL:
255 VLOG(1) <<
"legacy-testregex: fatal error occurred";
260 legacyApp.setOnConnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
261 VLOG(1) <<
"OnConnect:";
263 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
264 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
267 legacyApp.setOnDisconnect([](legacy::in::WebApp::SocketConnection* socketConnection) {
268 VLOG(1) <<
"OnDisconnect:";
270 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
271 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
274 tls::in::WebApp tlsApp(
"tls-testregex");
276 tlsApp.use(legacyApp);
278 tlsApp.listen(8088, [](
const tls::in::WebApp::SocketAddress& socketAddress,
const core::socket::State& state) {
280 case core::socket::State::OK:
281 VLOG(1) <<
"tls-testregex: listening on '" << socketAddress.toString() <<
"'";
283 case core::socket::State::DISABLED:
284 VLOG(1) <<
"tls-testregex: disabled";
286 case core::socket::State::ERROR:
287 VLOG(1) <<
"tls-testregex: error occurred";
289 case core::socket::State::FATAL:
290 VLOG(1) <<
"tls-testregex: fatal error occurred";
295 tlsApp.setOnConnect([](tls::in::WebApp::SocketConnection* socketConnection) {
296 VLOG(1) <<
"OnConnect:";
298 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
299 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();
302 tlsApp.setOnConnected([](tls::in::WebApp::SocketConnection* socketConnection) {
303 VLOG(1) <<
"OnConnected:";
305 X509* client_cert = SSL_get_peer_certificate(socketConnection->getSSL());
307 if (client_cert !=
nullptr) {
308 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
310 VLOG(1) <<
"\tClient certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
312 char* str = X509_NAME_oneline(X509_get_subject_name(client_cert),
nullptr, 0);
313 VLOG(1) <<
"\t Subject: " + std::string(str);
316 str = X509_NAME_oneline(X509_get_issuer_name(client_cert),
nullptr, 0);
317 VLOG(1) <<
"\t Issuer: " + std::string(str);
322 GENERAL_NAMES* subjectAltNames =
323 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(client_cert, NID_subject_alt_name,
nullptr,
nullptr));
325#pragma GCC diagnostic push
327#if __has_warning
("-Wused-but-marked-unused")
328#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
332 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
334#pragma GCC diagnostic pop
336 VLOG(1) <<
"\t Subject alternative name count: " << altNameCount;
337 for (int32_t i = 0; i < altNameCount; ++i) {
339#pragma GCC diagnostic push
341#if __has_warning
("-Wused-but-marked-unused")
342#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
346 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
348#pragma GCC diagnostic pop
350 if (generalName->type == GEN_URI) {
351 const std::string subjectAltName =
352 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
353 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
354 VLOG(1) <<
"\t SAN (URI): '" + subjectAltName;
355 }
else if (generalName->type == GEN_DNS) {
356 const std::string subjectAltName =
357 std::string(
reinterpret_cast<
const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
358 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
359 VLOG(1) <<
"\t SAN (DNS): '" + subjectAltName;
361 VLOG(1) <<
"\t SAN (Type): '" + std::to_string(generalName->type);
365#pragma GCC diagnostic push
367#if __has_warning
("-Wused-but-marked-unused")
368#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
372 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
374#pragma GCC diagnostic pop
376 X509_free(client_cert);
378 VLOG(1) <<
"\tClient certificate: no certificate";
382 tlsApp.setOnDisconnect([](tls::in::WebApp::SocketConnection* socketConnection) {
383 VLOG(1) <<
"OnDisconnect:";
385 VLOG(1) <<
"\tServer: " + socketConnection->getRemoteAddress().toString();
386 VLOG(1) <<
"\tClient: " + socketConnection->getLocalAddress().toString();