SNode.C
Loading...
Searching...
No Matches
apps::http::tls Namespace Reference

Typedefs

using Client = web::http::tls::NET::Client
 
using Request = Client::Request
 
using Response = Client::Response
 
using SocketConnection = Client::SocketConnection
 
using WebApp = express::tls::NET::WebApp
 

Functions

static Client getClient ()
 
static WebApp getWebApp (const std::string &name)
 

Typedef Documentation

◆ Client

using apps::http::tls::Client = web::http::tls::NET::Client

Definition at line 333 of file clients.h.

◆ Request

using apps::http::tls::Request = Client::Request

Definition at line 334 of file clients.h.

◆ Response

using apps::http::tls::Response = Client::Response

Definition at line 335 of file clients.h.

◆ SocketConnection

typedef WebApp::SocketConnection apps::http::tls::SocketConnection = Client::SocketConnection

Definition at line 336 of file clients.h.

◆ WebApp

using apps::http::tls::WebApp = express::tls::NET::WebApp

Definition at line 71 of file servers.h.

Function Documentation

◆ getClient()

static Client apps::http::tls::getClient ( )
static

Definition at line 338 of file clients.h.

338 {
339 Client client(
340 "httpclient",
341 [](const std::shared_ptr<Request>& req) {
342 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestStart";
343
344 req->url = "/";
345 req->set("Connection", "keep-alive");
346 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
347 logResponse(req, res);
348 });
349 req->url = "/";
350 req->set("Connection", "keep-alive");
351 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
352 logResponse(req, res);
353 });
354 req->url = "/index.html";
355 req->set("Connection", "keep-alive");
356 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
357 logResponse(req, res);
358 });
359 req->url = "/";
360 req->set("Connection", "keep-alive");
361 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
362 logResponse(req, res);
363 });
364 req->url = "/index.html";
365 req->set("Connection", "keep-alive");
366 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
367 logResponse(req, res);
368 });
369 req->url = "/";
370 req->set("Connection", "keep-alive");
371 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
372 logResponse(req, res);
373 });
374 req->url = "/index.html";
375 req->set("Connection", "keep-alive");
376 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
377 logResponse(req, res);
378 });
379 req->url = "/";
380 req->set("Connection", "keep-alive");
381 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
382 logResponse(req, res);
383 });
384 req->url = "/index.html";
385 req->set("Connection", "keep-alive");
386 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
387 logResponse(req, res);
388 });
389 req->url = "/";
390 req->set("Connection", "keep-alive");
391 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
392 logResponse(req, res);
393 });
394 req->url = "/index.html";
395 req->set("Connection", "keep-alive");
396 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
397 logResponse(req, res);
398 });
399 req->url = "/";
400 req->set("Connection", "keep-alive");
401 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
402 logResponse(req, res);
403 });
404 req->url = "/index.html";
405 req->set("Connection", "keep-alive");
406 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
407 logResponse(req, res);
408 });
409 req->url = "/";
410 req->set("Connection", "keep-alive");
411 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
412 logResponse(req, res);
413 });
414 req->url = "/index.html";
415 req->set("Connection", "keep-alive");
416 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
417 logResponse(req, res);
418 });
419 req->url = "/";
420 req->set("Connection", "keep-alive");
421 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
422 logResponse(req, res);
423 });
424 req->url = "/index.html";
425 req->set("Connection", "keep-alive");
426 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
427 logResponse(req, res);
428 });
429 req->url = "/";
430 req->set("Connection", "close");
431 req->end([](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
432 logResponse(req, res);
433 });
434 },
435 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
436 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestEnd";
437 });
438
439 client.setOnConnect([](SocketConnection* socketConnection) { // onConnect
440 VLOG(1) << "OnConnect " << socketConnection->getConnectionName();
441
442 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
443 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
444
445 /* Enable automatic hostname checks */
446 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
447
448 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
449 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
450 // // handle error
451 // socketConnection->close();
452 // }
453 });
454
455 client.setOnConnected([](SocketConnection* socketConnection) { // onConnected
456 VLOG(1) << socketConnection->getConnectionName() << ": OnConnected";
457 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
458 if (server_cert != nullptr) {
459 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
460
461 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
462 std::string(X509_verify_cert_error_string(verifyErr));
463
464 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
465 VLOG(1) << "\t Subject: " + std::string(str);
466 OPENSSL_free(str);
467
468 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
469 VLOG(1) << "\t Issuer: " + std::string(str);
470 OPENSSL_free(str);
471
472 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
473
474 GENERAL_NAMES* subjectAltNames =
475 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
476#ifdef __GNUC__
477#pragma GCC diagnostic push
478#ifdef __has_warning
479#if __has_warning("-Wused-but-marked-unused")
480#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
481#endif
482#endif
483#endif
484 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
485#ifdef __GNUC_
486#pragma GCC diagnostic pop
487#endif
488 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
489 for (int32_t i = 0; i < altNameCount; ++i) {
490#ifdef __GNUC__
491#pragma GCC diagnostic push
492#ifdef __has_warning
493#if __has_warning("-Wused-but-marked-unused")
494#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
495#endif
496#endif
497#endif
498 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
499#ifdef __GNUC_
500#pragma GCC diagnostic pop
501#endif
502 if (generalName->type == GEN_URI) {
503 std::string subjectAltName =
504 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
505 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
506 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
507 } else if (generalName->type == GEN_DNS) {
508 std::string subjectAltName =
509 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
510 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
511 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
512 } else {
513 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
514 }
515 }
516#ifdef __GNUC__
517#pragma GCC diagnostic push
518#ifdef __has_warning
519#if __has_warning("-Wused-but-marked-unused")
520#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
521#endif
522#endif
523#endif
524 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
525#ifdef __GNUC_
526#pragma GCC diagnostic pop
527#endif
528 X509_free(server_cert);
529 } else {
530 VLOG(1) << "\tPeer certificate: no certificate";
531 }
532 });
533
534 client.setOnDisconnect([](SocketConnection* socketConnection) { // onDisconnect
535 VLOG(1) << socketConnection->getConnectionName() << ": OnDisconnect";
536
537 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
538 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
539 });
540
541 return client;
542 }
static void logResponse(const std::shared_ptr< web::http::client::Request > &req, const std::shared_ptr< web::http::client::Response > &res)
Definition clients.h:45
web::http::tls::NET::Client Client
Definition clients.h:333

◆ getWebApp()

static WebApp apps::http::tls::getWebApp ( const std::string & name)
static

Definition at line 74 of file servers.h.

74 {
75 WebApp webApp(name, getRouter());
76
77 webApp.setOnConnect([webApp](SocketConnection* socketConnection) { // onConnect
78 VLOG(1) << "OnConnect " << webApp.getConfig().getInstanceName();
79
80 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
81 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
82
83 /* Enable automatic hostname checks */
84 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
85
86 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
87 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
88 // // handle error
89 // socketConnection->close();
90 // }
91 });
92
93 webApp.setOnConnected([webApp](SocketConnection* socketConnection) { // onConnected
94 VLOG(1) << "OnConnected " << webApp.getConfig().getInstanceName();
95
96 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
97 if (server_cert != nullptr) {
98 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
99
100 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
101 std::string(X509_verify_cert_error_string(verifyErr));
102
103 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
104 VLOG(1) << "\t Subject: " + std::string(str);
105 OPENSSL_free(str);
106
107 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
108 VLOG(1) << "\t Issuer: " + std::string(str);
109 OPENSSL_free(str);
110
111 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
112
113 GENERAL_NAMES* subjectAltNames =
114 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
115#ifdef __GNUC__
116#pragma GCC diagnostic push
117#ifdef __has_warning
118#if __has_warning("-Wused-but-marked-unused")
119#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
120#endif
121#endif
122#endif
123 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
124#ifdef __GNUC_
125#pragma GCC diagnostic pop
126#endif
127 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
128 for (int32_t i = 0; i < altNameCount; ++i) {
129#ifdef __GNUC__
130#pragma GCC diagnostic push
131#ifdef __has_warning
132#if __has_warning("-Wused-but-marked-unused")
133#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
134#endif
135#endif
136#endif
137 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
138#ifdef __GNUC_
139#pragma GCC diagnostic pop
140#endif
141 if (generalName->type == GEN_URI) {
142 std::string subjectAltName =
143 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
144 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
145 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
146 } else if (generalName->type == GEN_DNS) {
147 std::string subjectAltName =
148 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
149 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
150 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
151 } else {
152 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
153 }
154 }
155#ifdef __GNUC__
156#pragma GCC diagnostic push
157#ifdef __has_warning
158#if __has_warning("-Wused-but-marked-unused")
159#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
160#endif
161#endif
162#endif
163 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
164#ifdef __GNUC_
165#pragma GCC diagnostic pop
166#endif
167 X509_free(server_cert);
168 } else {
169 LOG(WARNING) << "\tPeer certificate: no certificate";
170 }
171 });
172
173 webApp.setOnDisconnect([webApp](SocketConnection* socketConnection) { // onDisconnect
174 VLOG(1) << "OnDisconnect " << webApp.getConfig().getInstanceName();
175
176 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
177 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
178 });
179
180 return webApp;
181 }
static express::Router getRouter()
Definition servers.h:46
Client::SocketConnection SocketConnection
Definition clients.h:336
express::tls::NET::WebApp WebApp
Definition servers.h:71